summaryrefslogtreecommitdiff
path: root/officecfg/org
diff options
context:
space:
mode:
authorDirk Grobler <dg@openoffice.org>2000-09-20 15:14:58 +0000
committerDirk Grobler <dg@openoffice.org>2000-09-20 15:14:58 +0000
commit145b6c19e9decbc2233a8eae5a08e250000c14a6 (patch)
tree90c5a27b643658af6a08bdc976df261525fa99de /officecfg/org
parentdda1568f69b8b6b64927f0cbc92b4bb1c6176016 (diff)
new java helper for validation add pretty printing of xml
Diffstat (limited to 'officecfg/org')
-rw-r--r--officecfg/org/openoffice/helper/PrettyPrinter.java232
-rw-r--r--officecfg/org/openoffice/helper/Validator.java163
-rw-r--r--officecfg/org/openoffice/helper/makefile.mk44
3 files changed, 439 insertions, 0 deletions
diff --git a/officecfg/org/openoffice/helper/PrettyPrinter.java b/officecfg/org/openoffice/helper/PrettyPrinter.java
new file mode 100644
index 000000000000..474ad7e0d3cb
--- /dev/null
+++ b/officecfg/org/openoffice/helper/PrettyPrinter.java
@@ -0,0 +1,232 @@
+/**
+ * Title: pretty printing for xml files
+ * Description: Validates an xml document against a dtd<p>
+ * Copyright: null<p>
+ * Company: null<p>
+ * @author Dirk Grobler
+ * @version 1.0
+ */
+package org.openoffice.helper;
+
+import java.io.*;
+import org.xml.sax.*;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+
+public class PrettyPrinter extends HandlerBase
+{
+ public static void main (String argv [])
+ {
+ if (argv.length != 2) {
+ System.err.println ("Usage: cmd filename outfile");
+ System.exit (1);
+ }
+
+ // Use the validating parser
+ SAXParserFactory factory = SAXParserFactory.newInstance();
+ factory.setValidating(false);
+ try {
+
+ // Set the output file
+ out = new java.io.BufferedWriter(new OutputStreamWriter(new FileOutputStream(argv[1]), "UTF8"));
+
+ // Parse the input
+ SAXParser saxParser = factory.newSAXParser();
+ saxParser.parse( new File(argv [0]), new PrettyPrinter() );
+
+ if (out != null)
+ out.close();
+
+ } catch (SAXParseException spe) {
+ // Error generated by the parser
+ System.out.println ("\n** Parsing error"
+ + ", line " + spe.getLineNumber ()
+ + ", uri " + spe.getSystemId ());
+ System.out.println(" " + spe.getMessage() );
+
+ // Use the contained exception, if any
+ Exception x = spe;
+ if (spe.getException() != null)
+ x = spe.getException();
+ x.printStackTrace();
+
+ } catch (SAXException sxe) {
+ // Error generated by this application
+ // (or a parser-initialization error)
+ Exception x = sxe;
+ if (sxe.getException() != null)
+ x = sxe.getException();
+ x.printStackTrace();
+
+ } catch (ParserConfigurationException pce) {
+ // Parser with specified options can't be built
+ pce.printStackTrace();
+
+ } catch (IOException ioe) {
+ // I/O error
+ ioe.printStackTrace();
+ }
+
+ System.exit (0);
+ }
+
+ static private java.io.BufferedWriter out = null;
+ private String indentString = "\t"; // Amount to indent
+ private boolean bHasContentOrSubElements = true;
+ private int indentLevel = 0;
+
+ private static int NONE = 0;
+ private static int START_ELEMENT = 1;
+ private static int CONTENT = 2;
+ private static int END_ELEMENT = 3;
+
+ private int nStatus = NONE;
+
+
+ //===========================================================
+ // SAX DocumentHandler methods
+ //===========================================================
+
+ public void setDocumentLocator (Locator l)
+ {
+ }
+
+ public void startDocument ()
+ throws SAXException
+ {
+ emit ("<?xml version='1.0' encoding='UTF-8'?>");
+ nl();
+ }
+
+ public void endDocument ()
+ throws SAXException
+ {
+ nl();
+ try {
+ nl();
+ out.flush ();
+ } catch (IOException e) {
+ throw new SAXException ("I/O error", e);
+ }
+ }
+
+ public void startElement (String name, AttributeList attrs)
+ throws SAXException
+ {
+ if (indentLevel != 0 && (nStatus == START_ELEMENT))
+ emit (">");
+
+ nStatus = START_ELEMENT;
+
+ nl(); emit ("<"+ name);
+ if (attrs != null) {
+ for (int i = 0; i < attrs.getLength (); i++) {
+ emit (" ");
+ emit (attrs.getName (i));
+ emit ("=");
+ emit ("\"");
+ emit (attrs.getValue (i));
+ emit ("\"");
+ }
+ }
+ indentLevel++;
+ }
+
+ public void endElement (String name)
+ throws SAXException
+ {
+ indentLevel--;
+ if (nStatus == START_ELEMENT)
+ emit ("/>");
+ else
+ {
+ // treet the value special
+ if (name != "value" && name != "defaultvalue")
+ nl();
+ emit ("</"+name+">");
+ }
+ nStatus = END_ELEMENT;
+ }
+
+ public void characters (char buf [], int offset, int len)
+ throws SAXException
+ {
+ String s = new String(buf, offset, len);
+ if (!s.trim().equals(""))
+ {
+ if (nStatus == START_ELEMENT)
+ emit (">");
+ emit(s);
+ nStatus = CONTENT;
+ }
+
+ // otherwise ignore it
+ }
+
+ public void ignorableWhitespace (char buf [], int offset, int len)
+ throws SAXException
+ {
+ String s = new String(buf, offset, len);
+ if (nStatus == START_ELEMENT)
+ emit (">");
+ emit(s);
+ nStatus = CONTENT;
+ }
+
+ public void processingInstruction (String target, String data)
+ throws SAXException
+ {
+ }
+
+ //===========================================================
+ // SAX ErrorHandler methods
+ //===========================================================
+
+ // treat validation errors as fatal
+ public void error (SAXParseException e)
+ throws SAXParseException
+ {
+ throw e;
+ }
+
+ // dump warnings too
+ public void warning (SAXParseException err)
+ throws SAXParseException
+ {
+ System.out.println ("** Warning"
+ + ", line " + err.getLineNumber ()
+ + ", uri " + err.getSystemId ());
+ System.out.println(" " + err.getMessage ());
+ }
+
+ //===========================================================
+ // Helpers ...
+ //===========================================================
+
+ // Wrap I/O exceptions in SAX exceptions, to
+ // suit handler signature requirements
+ private void emit (String s)
+ throws SAXException
+ {
+ try {
+ out.write (s);
+ } catch (IOException e) {
+ throw new SAXException ("I/O error", e);
+ }
+ }
+
+ // Start a new line
+ // and indent the next line appropriately
+ private void nl ()
+ throws SAXException
+ {
+ String lineEnd = System.getProperty("line.separator");
+ try {
+ out.write (lineEnd);
+ for (int i=0; i < indentLevel; i++) out.write(indentString);
+ } catch (IOException e) {
+ throw new SAXException ("I/O error", e);
+ }
+ }
+}
diff --git a/officecfg/org/openoffice/helper/Validator.java b/officecfg/org/openoffice/helper/Validator.java
new file mode 100644
index 000000000000..6802f0bcbe60
--- /dev/null
+++ b/officecfg/org/openoffice/helper/Validator.java
@@ -0,0 +1,163 @@
+/**
+ * Title: Validation of xml files
+ * Description: Validates an xml document against a dtd<p>
+ * Copyright: null<p>
+ * Company: null<p>
+ * @author Dirk Grobler
+ * @version 1.0
+ */
+package org.openoffice.helper;
+
+import java.io.*;
+import org.xml.sax.*;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+
+public class Validator extends HandlerBase
+{
+ public static void main (String argv [])
+ {
+ if (argv.length != 1) {
+ System.err.println ("Usage: cmd filename");
+ System.exit (1);
+ }
+ // Use the validating parser
+ SAXParserFactory factory = SAXParserFactory.newInstance();
+ factory.setValidating(true);
+ try
+ {
+ // Set up output stream
+ out = new OutputStreamWriter (System.out, "UTF8");
+
+ // Parse the input
+ SAXParser saxParser = factory.newSAXParser();
+ saxParser.parse( new File(argv [0]), new Validator() );
+ }
+ catch (SAXParseException spe) {
+ // Error generated by the parser
+ System.out.println ("\n** Parsing error"
+ + ", line " + spe.getLineNumber ()
+ + ", uri " + spe.getSystemId ());
+ System.out.println(" " + spe.getMessage() );
+ System.exit (1);
+ }
+ catch (SAXException sxe) {
+ // Error generated by this application
+ // (or a parser-initialization error)
+ Exception x = sxe;
+ if (sxe.getException() != null)
+ x = sxe.getException();
+ x.printStackTrace();
+ System.exit (1);
+ }
+ catch (ParserConfigurationException pce) {
+ // Parser with specified options can't be built
+ pce.printStackTrace();
+ System.exit (1);
+
+ }
+ catch (IOException ioe) {
+ // I/O error
+ ioe.printStackTrace();
+ System.exit (1);
+ }
+
+ System.exit (0);
+ }
+
+ //===========================================================
+ // SAX DocumentHandler methods
+ //===========================================================
+ public InputSource resolveEntity(java.lang.String publicId,
+ java.lang.String systemId)
+ throws SAXException
+ {
+ return new InputSource(systemId);
+ }
+
+ static private Writer out;
+ //===========================================================
+ // SAX DocumentHandler methods
+ //===========================================================
+
+ public void setDocumentLocator (Locator l)
+ {
+ // Save this to resolve relative URIs or to give diagnostics.
+ try {
+ out.write ("** Start validating: ");
+ out.write (l.getSystemId());
+ out.flush ();
+ } catch (IOException e) {
+ // Ignore errors
+ }
+ }
+
+ public void startDocument ()
+ throws SAXException
+ {
+ }
+
+ public void endDocument ()
+ throws SAXException
+ {
+ nl(); emit ("** Document is valid!");
+ try {
+ nl();
+ out.flush ();
+ } catch (IOException e) {
+ throw new SAXException ("I/O error", e);
+ }
+ }
+
+ //===========================================================
+ // SAX ErrorHandler methods
+ //===========================================================
+
+ // treat validation errors as fatal
+ public void error (SAXParseException e)
+ throws SAXParseException
+ {
+ throw e;
+ }
+
+ // dump warnings too
+ public void warning (SAXParseException err)
+ throws SAXParseException
+ {
+ System.out.println ("** Warning"
+ + ", line " + err.getLineNumber ()
+ + ", uri " + err.getSystemId ());
+ System.out.println(" " + err.getMessage ());
+ }
+
+ //===========================================================
+ // Helpers ...
+ //===========================================================
+
+ // Wrap I/O exceptions in SAX exceptions, to
+ // suit handler signature requirements
+ private void emit (String s)
+ throws SAXException
+ {
+ try {
+ out.write (s);
+ out.flush ();
+ } catch (IOException e) {
+ throw new SAXException ("I/O error", e);
+ }
+ }
+
+ // Start a new line
+ // and indent the next line appropriately
+ private void nl ()
+ throws SAXException
+ {
+ String lineEnd = System.getProperty("line.separator");
+ try {
+ out.write (lineEnd);
+ } catch (IOException e) {
+ throw new SAXException ("I/O error", e);
+ }
+ }
+}
diff --git a/officecfg/org/openoffice/helper/makefile.mk b/officecfg/org/openoffice/helper/makefile.mk
new file mode 100644
index 000000000000..f7c5eef5e04e
--- /dev/null
+++ b/officecfg/org/openoffice/helper/makefile.mk
@@ -0,0 +1,44 @@
+#*************************************************************************
+#
+# $RCSfile: makefile.mk,v $
+#
+# $Revision: 1.1 $
+#
+# last change: $Author: dg $ $Date: 2000-09-20 16:14:58 $
+#
+# Copyright according the GNU Public License.
+#
+#*************************************************************************
+
+PRJ=..$/..$/..
+
+PRJNAME=officecfg
+TARGET =schema
+PACKAGE=org$/openoffice$/helper
+
+# --- Settings -----------------------------------------------------
+
+.INCLUDE : svpre.mk
+.INCLUDE : settings.mk
+.INCLUDE : sv.mk
+
+EXTRAJARFILES = jaxp.jar parser.jar
+
+JAVACLASSFILES= \
+ $(CLASSDIR)$/$(PACKAGE)$/Validator.class \
+ $(CLASSDIR)$/$(PACKAGE)$/PrettyPrinter.class
+
+JAVAFILES= $(subst,$(CLASSDIR)$/$(PACKAGE)$/, $(subst,.class,.java $(JAVACLASSFILES)))
+
+RC_SUBDIRSDEPS=$(JAVATARGET)
+
+JARCLASSDIRS = $(PACKAGE)
+JARTARGET = $(TARGET).jar
+JARCOMPRESS = TRUE
+
+
+# --- Targets ------------------------------------------------------
+
+
+.INCLUDE : target.mk
+