new admin templates! with many thanks to init...
[mir.git] / source / mir / util / XMLReader.java
index b8bc9e3..23e0823 100755 (executable)
-/*\r
- * Copyright (C) 2001, 2002  The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
- *\r
- * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with the com.oreilly.servlet library, any library\r
- * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
- * the above that use the same license as the above), and distribute linked\r
- * combinations including the two.  You must obey the GNU General Public\r
- * License in all respects for all of the code used other than the above\r
- * mentioned libraries.  If you modify this file, you may extend this exception\r
- * to your version of the file, but you are not obligated to do so.  If you do\r
- * not wish to do so, delete this exception statement from your version.\r
- */\r
-\r
-package mir.util;\r
-\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.StringReader;\r
-import java.io.FileNotFoundException;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Stack;\r
-\r
-import javax.xml.parsers.ParserConfigurationException;\r
-import javax.xml.parsers.SAXParser;\r
-import javax.xml.parsers.SAXParserFactory;\r
-\r
-import multex.Exc;\r
-import multex.Failure;\r
-\r
-import org.xml.sax.Attributes;\r
-import org.xml.sax.InputSource;\r
-import org.xml.sax.Locator;\r
-import org.xml.sax.SAXException;\r
-import org.xml.sax.SAXParseException;\r
-import org.xml.sax.helpers.DefaultHandler;\r
-\r
-public class XMLReader {\r
-  private Locator locator;\r
-  private String filename;\r
-  private boolean namespaceAware;\r
-\r
-  public XMLReader() {\r
-    this(false);\r
-  }\r
-\r
-  public XMLReader(boolean aNameSpaceAware) {\r
-    namespaceAware = aNameSpaceAware;\r
-    filename="";\r
-  }\r
-\r
-  public void parseFile(String aFileName, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc {\r
-    filename= aFileName;\r
-    try {\r
-      parseInputStream(new FileInputStream(aFileName), aRootHandler);\r
-    }\r
-    catch (Throwable t) {\r
-      throw new XMLReaderFailure(t);\r
-    }\r
-  }\r
-\r
-  public void parseInputStream(InputStream anInputStream, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc {\r
-    try {\r
-      SAXParserFactory parserFactory = SAXParserFactory.newInstance();\r
-\r
-      parserFactory.setNamespaceAware(namespaceAware);\r
-      parserFactory.setValidating(true);\r
-\r
-      XMLReaderHandler handler = new XMLReaderHandler(parserFactory, aRootHandler);\r
-\r
-      handler.processInputStream(anInputStream);\r
-    }\r
-    catch (Throwable e) {\r
-      Throwable t = ExceptionFunctions.traceCauseException(e);\r
-\r
-      if (t instanceof XMLReaderExc) {\r
-        if (locator!=null && filename!=null)\r
-          ((XMLReaderExc) t).setLocation(filename, locator.getLineNumber(), locator.getColumnNumber());\r
-        throw (XMLReaderExc) t;\r
-      }\r
-\r
-      if (t instanceof XMLReaderFailure) {\r
-        throw (XMLReaderFailure) t;\r
-      }\r
-\r
-      throw new XMLReaderFailure(t);\r
-    }\r
-  }\r
-\r
-  private class XMLReaderHandler extends DefaultHandler {\r
-    private SAXParserFactory parserFactory;\r
-    private SectionsManager manager;\r
-    private InputSource inputSource;\r
-\r
-    public XMLReaderHandler(SAXParserFactory aParserFactory, SectionHandler aRootHandler) {\r
-      super();\r
-\r
-      parserFactory=aParserFactory;\r
-      manager = new SectionsManager();\r
-      manager.pushHandler(aRootHandler);\r
-   }\r
-\r
-    public void setDocumentLocator(Locator aLocator) {\r
-      locator=aLocator;\r
-    }\r
-\r
-    private void processInputStream(InputStream anInputStream) throws XMLReaderExc, XMLReaderFailure {\r
-      try {\r
-        SAXParser parser=parserFactory.newSAXParser();\r
-\r
-        inputSource = new InputSource(anInputStream);\r
-        parser.parse(inputSource, this);\r
-      }\r
-      catch (ParserConfigurationException e) {\r
-        throw new XMLReaderExc("Internal exception: "+e.getMessage());\r
-      }\r
-      catch (Throwable e) {\r
-        throw new XMLReaderFailure(e);\r
-      }\r
-    }\r
-\r
-    public void startElement(String aUri, String aTag, String aQualifiedName, Attributes anAttributes) throws SAXException {\r
-      Map attributesMap;\r
-      int i;\r
-\r
-      try {\r
-        attributesMap = new HashMap();\r
-        for (i=0; i<anAttributes.getLength(); i++)\r
-          attributesMap.put(anAttributes.getQName(i), anAttributes.getValue(i));\r
-\r
-        SectionHandler handler = manager.currentHandler().startElement(aQualifiedName, attributesMap);\r
-\r
-        manager.pushHandler( handler );\r
-      }\r
-      catch (XMLReaderExc e) {\r
-        throw new SAXParseException(e.getMessage(), null, e);\r
-      }\r
-      catch (Exception e) {\r
-        throw new SAXException(e);\r
-      }\r
-    }\r
-\r
-    public void endElement(String aUri, String aTag, String aQualifiedName) throws SAXException {\r
-      try\r
-      {\r
-        if (!aQualifiedName.equals("include")) {\r
-          SectionHandler handler = manager.popHandler();\r
-\r
-          handler.finishSection();\r
-\r
-          if (!manager.isEmpty()) {\r
-            manager.currentHandler().endElement(handler);\r
-          }\r
-        }\r
-      }\r
-      catch (XMLReaderExc e) {\r
-        throw new SAXParseException(e.getMessage(), null, e);\r
-      }\r
-      catch (Exception e) {\r
-        throw new SAXException(e);\r
-      }\r
-    }\r
-\r
-    public void characters(char[] aBuffer, int aStart, int anEnd) throws SAXException {\r
-      try {\r
-        String text = new String(aBuffer, aStart, anEnd);\r
-\r
-        manager.currentHandler().characters(text);\r
-      }\r
-      catch (XMLReaderExc e) {\r
-        throw new SAXParseException(e.getMessage(), null, e);\r
-      }\r
-      catch (Exception e) {\r
-        throw new SAXException(e);\r
-      }\r
-    }\r
-  }\r
-\r
-  private class SectionsManager {\r
-    Stack handlerStack;\r
-\r
-    public SectionsManager() {\r
-      handlerStack = new Stack();\r
-    }\r
-\r
-    public void pushHandler(SectionHandler aSectionHandler) {\r
-      handlerStack.push(aSectionHandler);\r
-    }\r
-\r
-    public SectionHandler popHandler() {\r
-      return (SectionHandler) handlerStack.pop();\r
-    }\r
-\r
-    public SectionHandler currentHandler() {\r
-      return (SectionHandler) handlerStack.peek();\r
-    }\r
-\r
-    public boolean isEmpty() {\r
-      return handlerStack.isEmpty();\r
-    }\r
-  }\r
-\r
-  public static interface SectionHandler {\r
-    public abstract SectionHandler startElement(String aTag, Map anAttributes) throws XMLReaderExc;\r
-\r
-    public abstract void endElement(SectionHandler aHandler) throws XMLReaderExc;\r
-\r
-    public void characters(String aCharacters) throws XMLReaderExc;\r
-\r
-    public void finishSection() throws XMLReaderExc;\r
-  }\r
-\r
-  public static abstract class AbstractSectionHandler implements SectionHandler {\r
-    public SectionHandler startElement(String aTag, Map anAttributes) throws XMLReaderExc {\r
-      return null;\r
-    };\r
-\r
-    public void endElement(SectionHandler aHandler) throws XMLReaderExc {\r
-    };\r
-\r
-    public void finishSection() throws XMLReaderExc {\r
-    }\r
-\r
-    public void characters(String aCharacters) throws XMLReaderExc {\r
-      if ( aCharacters.trim().length() > 0) {\r
-        throw new XMLReaderExc("Text not allowed");\r
-      }\r
-    }\r
-  }\r
-\r
-  public static class XMLReaderExc extends Exc {\r
-    private boolean hasLocation;\r
-    private String filename;\r
-    private int lineNr;\r
-    private int columnNr;\r
-\r
-    public XMLReaderExc(String aMessage) {\r
-      super(aMessage);\r
-      hasLocation = false;\r
-    }\r
-\r
-    protected void setLocation(String aFilename, int aLineNr, int aColumnNr) {\r
-      filename = aFilename;\r
-      lineNr = aLineNr;\r
-      columnNr = aColumnNr;\r
-      hasLocation = true;\r
-    }\r
-\r
-    public boolean getHasLocation() {\r
-      return hasLocation;\r
-    }\r
-\r
-    public int getLineNr() {\r
-      return lineNr;\r
-    }\r
-\r
-    public int getColumnNr() {\r
-      return columnNr;\r
-    }\r
-\r
-    public String getFilename() {\r
-      return filename;\r
-    }\r
-  }\r
-\r
-  public static class XMLReaderFailure extends Failure {\r
-    public XMLReaderFailure(String aMessage, Throwable aCause) {\r
-      super(aMessage, aCause);\r
-    }\r
-\r
-    public XMLReaderFailure(Throwable aCause) {\r
-      super(aCause.getMessage(), aCause);\r
-    }\r
-  }\r
-\r
+/*
+ * Copyright (C) 2001, 2002  The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with the com.oreilly.servlet library, any library
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of
+ * the above that use the same license as the above), and distribute linked
+ * combinations including the two.  You must obey the GNU General Public
+ * License in all respects for all of the code used other than the above
+ * mentioned libraries.  If you modify this file, you may extend this exception
+ * to your version of the file, but you are not obligated to do so.  If you do
+ * not wish to do so, delete this exception statement from your version.
+ */
+
+package mir.util;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.StringReader;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import multex.Exc;
+import multex.Failure;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.helpers.DefaultHandler;
+
+public class XMLReader {
+  private Locator locator;
+  private String filename;
+  private boolean namespaceAware;
+
+  public XMLReader() {
+    this(false);
+  }
+
+  public XMLReader(boolean aNameSpaceAware) {
+    namespaceAware = aNameSpaceAware;
+    filename="";
+  }
+
+  public void parseFile(String aFileName, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc {
+    filename= aFileName;
+    try {
+      parseInputStream(new FileInputStream(aFileName), aRootHandler);
+    }
+    catch (Throwable t) {
+      throw new XMLReaderFailure(t);
+    }
+  }
+
+  public void parseInputStream(InputStream anInputStream, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc {
+    try {
+      SAXParserFactory parserFactory = SAXParserFactory.newInstance();
+
+      parserFactory.setNamespaceAware(namespaceAware);
+      parserFactory.setValidating(true);
+
+      XMLReaderHandler handler = new XMLReaderHandler(parserFactory, aRootHandler);
+
+      handler.processInputStream(anInputStream);
+    }
+    catch (Throwable e) {
+      Throwable t = ExceptionFunctions.traceCauseException(e);
+
+      if (t instanceof XMLReaderExc) {
+        if (locator!=null && filename!=null)
+          ((XMLReaderExc) t).setLocation(filename, locator.getLineNumber(), locator.getColumnNumber());
+        throw (XMLReaderExc) t;
+      }
+
+      if (t instanceof XMLReaderFailure) {
+        throw (XMLReaderFailure) t;
+      }
+
+      throw new XMLReaderFailure(t);
+    }
+  }
+
+  private class XMLReaderHandler extends DefaultHandler {
+    private SAXParserFactory parserFactory;
+    private SectionsManager manager;
+    private InputSource inputSource;
+
+    public XMLReaderHandler(SAXParserFactory aParserFactory, SectionHandler aRootHandler) {
+      super();
+
+      parserFactory=aParserFactory;
+      manager = new SectionsManager();
+      manager.pushHandler(aRootHandler);
+   }
+
+    public void setDocumentLocator(Locator aLocator) {
+      locator=aLocator;
+    }
+
+    private void processInputStream(InputStream anInputStream) throws XMLReaderExc, XMLReaderFailure {
+      try {
+        SAXParser parser=parserFactory.newSAXParser();
+
+        inputSource = new InputSource(anInputStream);
+        parser.parse(inputSource, this);
+      }
+      catch (ParserConfigurationException e) {
+        throw new XMLReaderExc("Internal exception: "+e.getMessage());
+      }
+      catch (Throwable e) {
+        throw new XMLReaderFailure(e);
+      }
+    }
+
+    public void startElement(String aUri, String aTag, String aQualifiedName, Attributes anAttributes) throws SAXException {
+      Map attributesMap;
+      int i;
+
+      try {
+        attributesMap = new HashMap();
+        for (i=0; i<anAttributes.getLength(); i++)
+          attributesMap.put(anAttributes.getQName(i), anAttributes.getValue(i));
+
+        SectionHandler handler = manager.currentHandler().startElement(aQualifiedName, attributesMap);
+
+        manager.pushHandler( handler );
+      }
+      catch (XMLReaderExc e) {
+        throw new SAXParseException(e.getMessage(), null, e);
+      }
+      catch (Exception e) {
+        throw new SAXException(e);
+      }
+    }
+
+    public void endElement(String aUri, String aTag, String aQualifiedName) throws SAXException {
+      try
+      {
+        if (!aQualifiedName.equals("include")) {
+          SectionHandler handler = manager.popHandler();
+
+          handler.finishSection();
+
+          if (!manager.isEmpty()) {
+            manager.currentHandler().endElement(handler);
+          }
+        }
+      }
+      catch (XMLReaderExc e) {
+        throw new SAXParseException(e.getMessage(), null, e);
+      }
+      catch (Exception e) {
+        throw new SAXException(e);
+      }
+    }
+
+    public void characters(char[] aBuffer, int aStart, int anEnd) throws SAXException {
+      try {
+        String text = new String(aBuffer, aStart, anEnd);
+
+        manager.currentHandler().characters(text);
+      }
+      catch (XMLReaderExc e) {
+        throw new SAXParseException(e.getMessage(), null, e);
+      }
+      catch (Exception e) {
+        throw new SAXException(e);
+      }
+    }
+  }
+
+  private class SectionsManager {
+    Stack handlerStack;
+
+    public SectionsManager() {
+      handlerStack = new Stack();
+    }
+
+    public void pushHandler(SectionHandler aSectionHandler) {
+      handlerStack.push(aSectionHandler);
+    }
+
+    public SectionHandler popHandler() {
+      return (SectionHandler) handlerStack.pop();
+    }
+
+    public SectionHandler currentHandler() {
+      return (SectionHandler) handlerStack.peek();
+    }
+
+    public boolean isEmpty() {
+      return handlerStack.isEmpty();
+    }
+  }
+
+  public static interface SectionHandler {
+    public abstract SectionHandler startElement(String aTag, Map anAttributes) throws XMLReaderExc;
+
+    public abstract void endElement(SectionHandler aHandler) throws XMLReaderExc;
+
+    public void characters(String aCharacters) throws XMLReaderExc;
+
+    public void finishSection() throws XMLReaderExc;
+  }
+
+  public static abstract class AbstractSectionHandler implements SectionHandler {
+    public SectionHandler startElement(String aTag, Map anAttributes) throws XMLReaderExc {
+      return null;
+    };
+
+    public void endElement(SectionHandler aHandler) throws XMLReaderExc {
+    };
+
+    public void finishSection() throws XMLReaderExc {
+    }
+
+    public void characters(String aCharacters) throws XMLReaderExc {
+      if ( aCharacters.trim().length() > 0) {
+        throw new XMLReaderExc("Text not allowed");
+      }
+    }
+  }
+
+  public static class XMLReaderExc extends Exc {
+    private boolean hasLocation;
+    private String filename;
+    private int lineNr;
+    private int columnNr;
+
+    public XMLReaderExc(String aMessage) {
+      super(aMessage);
+      hasLocation = false;
+    }
+
+    protected void setLocation(String aFilename, int aLineNr, int aColumnNr) {
+      filename = aFilename;
+      lineNr = aLineNr;
+      columnNr = aColumnNr;
+      hasLocation = true;
+    }
+
+    public boolean getHasLocation() {
+      return hasLocation;
+    }
+
+    public int getLineNr() {
+      return lineNr;
+    }
+
+    public int getColumnNr() {
+      return columnNr;
+    }
+
+    public String getFilename() {
+      return filename;
+    }
+  }
+
+  public static class XMLReaderFailure extends Failure {
+    public XMLReaderFailure(String aMessage, Throwable aCause) {
+      super(aMessage, aCause);
+    }
+
+    public XMLReaderFailure(Throwable aCause) {
+      super(aCause.getMessage(), aCause);
+    }
+  }
+
 }
\ No newline at end of file