001: /*
002: * Parser.java
003: *
004: *
005: * Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
006: * All rights reserved.
007: *
008: * This package is written by Lars Andersen <lars@rimfaxe.com>
009: * and licensed by Rimfaxe ApS.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by Rimfaxe ApS
026: (www.rimfaxe.com)"
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
031: * "Rimfaxe WebServer" must not be used to endorse or promote products
032: * derived from this software without prior written permission. For written
033: * permission, please contact info@rimfaxe.com
034: *
035: * 5. Products derived from this software may not be called "Rimfaxe"
036: * nor may "Rimfaxe" appear in their names without prior written
037: * permission of the Rimfaxe ApS.
038: *
039: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
040: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
043: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
044: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
045: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
046: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
047: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
048: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: *
051: */
052:
053: package com.rimfaxe.xml;
054:
055: import java.io.OutputStreamWriter;
056: import java.io.PrintWriter;
057: import java.io.UnsupportedEncodingException;
058:
059: import org.w3c.dom.Attr;
060: import org.w3c.dom.Document;
061: import org.w3c.dom.NamedNodeMap;
062: import org.w3c.dom.Node;
063: import org.w3c.dom.NodeList;
064:
065: /**
066: *
067: * @author Lars Andersen
068: */
069: public class XercesParser implements ParserInterface {
070:
071: String XMLcontent = "";
072: Document document = null;
073:
074: StringBuffer errorlog = new StringBuffer();
075: StringBuffer error = new StringBuffer();
076:
077: /** Creates new Parser */
078: public XercesParser() {
079: }
080:
081: public void parse(String in) {
082: java.io.ByteArrayOutputStream outstream = new java.io.ByteArrayOutputStream();
083: System.setErr(new java.io.PrintStream(outstream));
084: //errorlog.append("com.rimfaxe.xml.Parser");
085:
086: XMLcontent = in;
087: try {
088: com.rimfaxe.xml.dom.DOMParserWrapper parser = new com.rimfaxe.xml.xerceswrappers.DOMParser();
089: document = parser.parse(in);
090: //errorlog.append("Seems to work fine! signal -> [ok]");
091: } catch (org.xml.sax.SAXParseException spe) {
092: //errorlog.append( "org.xml.sax.SAXParseException : "+spe );
093: //errorlog.append("signal -> [exception]");
094: } catch (org.xml.sax.SAXException se) {
095: if (se.getException() != null) {
096: //errorlog.append( "org.xml.sax.SAXException : "+se.getException() );
097: //errorlog.append("signal -> [exception]");
098:
099: } else {
100: //errorlog.append( "org.xml.sax.SAXException : "+se );
101: //errorlog.append("signal -> [exception]");
102:
103: }
104: } catch (Exception e) {
105: //errorlog.append( "Exception : "+e );
106: //errorlog.append("signal -> [exception]");
107: }
108: //error.append(outstream.toString());
109: }
110:
111: public String getStacktrace(Exception e) {
112: java.lang.StringBuffer sb = new java.lang.StringBuffer();
113: sb.append(e.toString() + "\n\n");
114:
115: java.io.ByteArrayOutputStream outstream = new java.io.ByteArrayOutputStream();
116: e.printStackTrace(new java.io.PrintStream(outstream));
117: sb.append(outstream.toString());
118: return "" + sb;
119: }
120:
121: public String getErrorLog() {
122: return "" + errorlog;
123: }
124:
125: public String getError() {
126: return "" + error;
127: }
128:
129: public void parse(String in, boolean validating) {
130: java.io.ByteArrayOutputStream outstream = new java.io.ByteArrayOutputStream();
131: //System.setErr( new java.io.PrintStream(outstream) );
132: errorlog
133: .append("com.rimfaxe.xml.Parser(String in, boolean validating)\n");
134: XMLcontent = in;
135: try {
136:
137: if (validating) {
138: com.rimfaxe.xml.dom.DOMParserWrapper parser = new com.rimfaxe.xml.xerceswrappers.DOMParser();
139: document = parser.parse(in);
140: } else {
141: //System.out.println("Parsing with non validating parser");
142: com.rimfaxe.xml.dom.DOMParserWrapper parser = new com.rimfaxe.xml.xerceswrappers.NonValidatingDOMParser();
143: document = parser.parse(in);
144: }
145:
146: //errorlog.append("signal -> [ok]\n");
147: } catch (org.xml.sax.SAXParseException spe) {
148: //errorlog.append( "org.xml.sax.SAXParseException : \n"+getStacktrace(spe)+"\n" );
149: errorlog.append("signal -> [exception]\n");
150: } catch (org.xml.sax.SAXException se) {
151: if (se.getException() != null) {
152: errorlog.append("org.xml.sax.SAXException : "
153: + getStacktrace(se.getException()) + "\n");
154: errorlog.append("signal -> [exception]\n");
155:
156: } else {
157: errorlog.append("org.xml.sax.SAXException : "
158: + getStacktrace(se) + "\n");
159: errorlog.append("signal -> [exception]\n");
160:
161: }
162: } catch (Exception e) {
163:
164: errorlog.append("Exception : " + getStacktrace(e) + "\n");
165: errorlog.append("signal -> [exception]\n");
166: }
167:
168: //error.append(outstream.toString());
169: }
170:
171: public Document getDocument() {
172: //System.out.println(errorlog);
173: return document;
174: }
175: }
|