X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Futil%2FXMLReader.java;h=3e2f0cb3ae1a056a53f6298515fd56849247143f;hb=8ef7ced59b6fb15832febbaeb5b59826bdd3d1c7;hp=a1be97c268411ea5a9803cfc9968609f1dfadbd4;hpb=a841b85307a342bbaaf328ab2a79d9dbc4619b1f;p=mir.git diff --git a/source/mir/util/XMLReader.java b/source/mir/util/XMLReader.java index a1be97c2..3e2f0cb3 100755 --- a/source/mir/util/XMLReader.java +++ b/source/mir/util/XMLReader.java @@ -1,394 +1,432 @@ -/* - * 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 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.*; -import java.io.InputStream; -import java.util.HashMap; -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 parseString(String aString, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc { - try { - parseInputSource(new InputSource(new StringReader(aString)), aRootHandler); - } - catch (Throwable t) { - throw new XMLReaderFailure(t); - } - } - - 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 { - parseInputSource(new InputSource(anInputStream), aRootHandler); - } - - public void parseInputSource(InputSource anInputSource, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc { - try { - SAXParserFactory parserFactory = SAXParserFactory.newInstance(); - - parserFactory.setNamespaceAware(true); - parserFactory.setValidating(true); - - XMLReaderHandler handler = new XMLReaderHandler(parserFactory, aRootHandler); - - handler.processInputSource(anInputSource); - } - catch (Throwable e) { - Throwable t = ExceptionFunctions.traceCauseException(e); - - if (t instanceof XMLReaderExc) { - 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; - - public XMLReaderHandler(SAXParserFactory aParserFactory, SectionHandler aRootHandler) { - super(); - - parserFactory=aParserFactory; - manager = new SectionsManager(); - manager.pushHandler(aRootHandler); - } - - public void setDocumentLocator(Locator aLocator) { - locator=aLocator; - } - - private void processInputSource(InputSource anInputSource) throws XMLReaderExc, XMLReaderFailure { - try { - SAXParser parser=parserFactory.newSAXParser(); - - parser.parse(anInputSource, this); - } - 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 SAXParseException) { - XMLReaderExc r = new XMLReaderExc(t.getMessage()); - - if (locator!=null && filename!=null) - r.setLocation(filename, locator.getLineNumber(), locator.getColumnNumber()); - - throw r; - } - - if (t instanceof XMLReaderFailure) { - throw (XMLReaderFailure) t; - } - - if (t instanceof ParserConfigurationException) { - throw new XMLReaderFailure("Internal exception: "+t.toString(), t); - } - - throw new XMLReaderFailure(t); - } - } - - public void startElement(String aUri, String aLocalName, String aQualifiedName, Attributes anAttributes) throws SAXException { - Map attributesMap; - int i; - - try { - attributesMap = new HashMap(); - -// if (namespaceAware) - for (i=0; i 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); - } - } - - public static class XMLName { - private String namespaceURI; - private String localName; - private String prefix; - - public XMLName(String aLocalName) { - this(null, null, aLocalName); - } - - public XMLName(String aNamespaceURI, String aPrefix, String aLocalName) { - localName=""; - prefix=""; - namespaceURI=""; - - if (aLocalName!=null) - localName = aLocalName; - if (aPrefix!=null) - prefix = aPrefix; - if (aNamespaceURI!=null) - namespaceURI = aNamespaceURI; - } - - public XMLName(String aNamespaceURI, String aLocalName) { - this (aNamespaceURI, null, aLocalName); - } - - public String getNamespaceURI() { - return namespaceURI; - } - - public String getLocalName() { - return localName; - } - - public String getPrefix() { - return prefix; - } - - public int hashCode() { - if (namespaceURI == null) - return localName.hashCode() + 3*prefix.hashCode(); - else - return localName.hashCode() + 3*namespaceURI.hashCode(); - } - - public String toString() { - return ((namespaceURI.length()>0)? "["+namespaceURI+"]":"")+((prefix.length()>0)?prefix+":":"")+localName; - } - - public boolean equals(Object anObject) { - if (anObject instanceof XMLName) { - if (namespaceURI==null) - return (((XMLName) anObject).namespaceURI == null) && - prefix.equals(((XMLName) anObject).prefix) && - localName.equals(((XMLName) anObject).localName); - else - return namespaceURI.equals(((XMLName) anObject).namespaceURI) && - localName.equals(((XMLName) anObject).localName); - } - else - return false; - } - } - +/* + * 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 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.FileInputStream; +import java.io.InputStream; +import java.io.StringReader; +import java.util.HashMap; +import java.util.Map; +import java.util.Stack; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.ccil.cowan.tagsoup.Parser; +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; +import multex.Exc; +import multex.Failure; + +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 parseString(String aString, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc { + try { + parseInputSource(new InputSource(new StringReader(aString)), aRootHandler); + } + catch (Throwable t) { + throw new XMLReaderFailure(t); + } + } + + public void parseString(boolean aTagSoup, String aString, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc { + try { + parseInputSource(aTagSoup, new InputSource(new StringReader(aString)), aRootHandler); + } + catch (Throwable t) { + throw new XMLReaderFailure(t); + } + } + + 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 { + parseInputSource(new InputSource(anInputStream), aRootHandler); + } + + public void parseInputStream(boolean aTagSoup, InputStream anInputStream, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc { + parseInputSource(aTagSoup, new InputSource(anInputStream), aRootHandler); + } + + public void parseInputSource(InputSource anInputSource, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc { + try { + parseInputSource(false, anInputSource, aRootHandler); + } + catch (Throwable e) { + Throwable t = ExceptionFunctions.traceCauseException(e); + + if (t instanceof XMLReaderExc) { + throw (XMLReaderExc) t; + } + + if (t instanceof XMLReaderFailure) { + throw (XMLReaderFailure) t; + } + + throw new XMLReaderFailure(t); + } + } + + public void parseInputSource(boolean aTagSoup, InputSource anInputSource, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc { + try { + XMLReaderHandler handler = new XMLReaderHandler(aRootHandler); + + if (aTagSoup) { + org.ccil.cowan.tagsoup.Parser parser = new Parser(); + parser.setContentHandler(handler); + parser.setDTDHandler(handler); + parser.parse(anInputSource); + } + else { + SAXParserFactory parserFactory = SAXParserFactory.newInstance(); + + parserFactory.setNamespaceAware(true); + parserFactory.setValidating(true); + SAXParser parser = parserFactory.newSAXParser(); + + parser.parse(anInputSource, handler); + } + } + catch (Throwable e) { + Throwable t = ExceptionFunctions.traceCauseException(e); + + if (t instanceof XMLReaderExc) { + throw (XMLReaderExc) t; + } + + if (t instanceof XMLReaderFailure) { + throw (XMLReaderFailure) t; + } + + if (t instanceof XMLReaderExc) { + if (locator!=null && filename!=null) + ((XMLReaderExc) t).setLocation(filename, locator.getLineNumber(), locator.getColumnNumber()); + throw (XMLReaderExc) t; + } + + if (t instanceof SAXParseException) { + XMLReaderExc r = new XMLReaderExc(t.getMessage()); + + if (locator!=null && filename!=null) + r.setLocation(filename, locator.getLineNumber(), locator.getColumnNumber()); + + throw r; + } + + if (t instanceof XMLReaderFailure) { + throw (XMLReaderFailure) t; + } + + if (t instanceof ParserConfigurationException) { + throw new XMLReaderFailure("Internal exception: "+t.toString(), t); + } + + throw new XMLReaderFailure(t); + } + } + private class XMLReaderHandler extends DefaultHandler { + private SectionsManager manager; + + public XMLReaderHandler(SectionHandler aRootHandler) { + super(); + + manager = new SectionsManager(); + manager.pushHandler(aRootHandler); + } + + public void setDocumentLocator(Locator aLocator) { + locator=aLocator; + } + + public void startElement(String aUri, String aLocalName, String aQualifiedName, Attributes anAttributes) throws SAXException { + Map attributesMap; + int i; + + try { + attributesMap = new HashMap(); + + if (namespaceAware) + for (i=0; i 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); + } + } + + public static class XMLName { + private String namespaceURI; + private String localName; + private String prefix; + + public XMLName(String aLocalName) { + this(null, null, aLocalName); + } + + public XMLName(String aNamespaceURI, String aPrefix, String aLocalName) { + localName=""; + prefix=""; + namespaceURI=""; + + if (aLocalName!=null) + localName = aLocalName; + if (aPrefix!=null) + prefix = aPrefix; + if (aNamespaceURI!=null) + namespaceURI = aNamespaceURI; + } + + public XMLName(String aNamespaceURI, String aLocalName) { + this (aNamespaceURI, null, aLocalName); + } + + public String getNamespaceURI() { + return namespaceURI; + } + + public String getLocalName() { + return localName; + } + + public String getPrefix() { + return prefix; + } + + public int hashCode() { + if (namespaceURI == null) + return localName.hashCode() + 3*prefix.hashCode(); + else + return localName.hashCode() + 3*namespaceURI.hashCode(); + } + + public String toString() { + return ((namespaceURI.length()>0)? "["+namespaceURI+"]":"")+((prefix.length()>0)?prefix+":":"")+localName; + } + + public boolean equals(Object anObject) { + if (anObject instanceof XMLName) { + if (namespaceURI==null) + return (((XMLName) anObject).namespaceURI == null) && + prefix.equals(((XMLName) anObject).prefix) && + localName.equals(((XMLName) anObject).localName); + else + return namespaceURI.equals(((XMLName) anObject).namespaceURI) && + localName.equals(((XMLName) anObject).localName); + } + else + return false; + } + } + } \ No newline at end of file