001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.dbschema.migration.archiver.deserializer;
043:
044: import java.lang.*;
045: import javax.xml.parsers.*;
046:
047: import org.xml.sax.helpers.*;
048: import org.xml.sax.*;
049:
050: public class BaseXMLDeserializer extends java.lang.Object implements
051: XMLDeserializer, org.xml.sax.DocumentHandler,
052: org.xml.sax.DTDHandler, org.xml.sax.ErrorHandler {
053:
054: // Fields
055: protected java.lang.Object InitialObject;
056: protected org.xml.sax.Parser Parser;
057: public org.xml.sax.Locator TheLocator;
058: protected org.xml.sax.InputSource TheSource;
059: public java.lang.StringBuffer TheCharacters;
060:
061: // Constructors
062: public BaseXMLDeserializer() {
063: this .TheCharacters = new StringBuffer();
064: } /*Constructor-End*/
065:
066: // Methods
067: public void notationDecl(String name, String publicId,
068: String systemId) throws org.xml.sax.SAXException {
069:
070: } /*Method-END*/
071:
072: public void unparsedEntityDecl(String name, String publicId,
073: String systemId, String notationName)
074: throws org.xml.sax.SAXException {
075:
076: } /*Method-End*/
077:
078: public void processingInstruction(String target, String data)
079: throws org.xml.sax.SAXException {
080:
081: } /*Method-Enc*/
082:
083: public void setDocumentLocator(org.xml.sax.Locator locator) {
084: this .TheLocator = locator;
085: } /*Method-End*/
086:
087: public void ignorableWhitespace(char[] ch, int start, int length)
088: throws org.xml.sax.SAXException {
089:
090: } /*Method-End*/
091:
092: public void endElement(java.lang.String name)
093: throws org.xml.sax.SAXException {
094: this .TheCharacters.delete(0, this .TheCharacters.length());
095: } /*Method-End*/
096:
097: public void endDocument() throws org.xml.sax.SAXException {
098: this .freeResources();
099: } /*Method-End*/
100:
101: public void characters(char[] ch, int start, int length)
102: throws org.xml.sax.SAXException {
103: this .TheCharacters.append(ch, start, length);
104: } /*Method-End*/
105:
106: public void startElement(java.lang.String name,
107: org.xml.sax.AttributeList atts)
108: throws org.xml.sax.SAXException {
109: this .TheCharacters.delete(0, this .TheCharacters.length());
110: } /*Method-End*/
111:
112: public void startDocument() throws org.xml.sax.SAXException {
113: this .freeResources();
114: } /*Method-End*/
115:
116: public void fatalError(org.xml.sax.SAXParseException exception)
117: throws org.xml.sax.SAXException {
118: this .commonErrorProcessor(exception);
119: } /*Method-End*/
120:
121: public void warning(org.xml.sax.SAXParseException exception)
122: throws org.xml.sax.SAXException {
123:
124: } /*Method-End*/
125:
126: public void error(org.xml.sax.SAXParseException exception)
127: throws org.xml.sax.SAXException {
128: this .commonErrorProcessor(exception);
129: } /*Method-End*/
130:
131: public void setInitialObject(java.lang.Object obj) {
132: this .InitialObject = obj;
133: } /*Method-End*/
134:
135: public void freeResources() {
136: this .TheCharacters.delete(0, this .TheCharacters.length());
137: } /*Method-End*/
138:
139: public java.lang.String getCharacters() {
140: // trim escaped newline character and newline characters
141:
142: int lOffset = 0;
143:
144: while (lOffset < this .TheCharacters.length()) {
145: if (lOffset + 2 < this .TheCharacters.length()
146: && this .TheCharacters.substring(lOffset,
147: lOffset + 2).equals("\\n")) {
148: this .TheCharacters.delete(lOffset, lOffset + 2);
149: } else if (this .TheCharacters.charAt(lOffset) == '\n') {
150: this .TheCharacters.deleteCharAt(lOffset);
151: }
152: lOffset++;
153: }
154:
155: return this .TheCharacters.toString();
156: } /*Method-End*/
157:
158: public int Begin() throws org.xml.sax.SAXException {
159: try {
160: if (this .Parser == null) {
161: org.xml.sax.Parser parser;
162: SAXParserFactory factory;
163:
164: factory = SAXParserFactory.newInstance();
165: factory.setValidating(false); // asi validate=false
166: factory.setNamespaceAware(false);
167:
168: this .Parser = factory.newSAXParser().getParser();
169:
170: this .Parser.setDocumentHandler(this );
171: this .Parser.setDTDHandler(this );
172: this .Parser.setErrorHandler(this );
173: }
174: } catch (ParserConfigurationException e1) {
175: SAXException classError = new SAXException(e1.getMessage());
176: throw classError;
177: }
178:
179: return 1;
180: } /*Method-End*/
181:
182: public void commonErrorProcessor(org.xml.sax.SAXParseException error)
183: throws org.xml.sax.SAXException {
184: throw (error);
185: } /*Method-End*/
186:
187: public java.lang.Object XlateObject()
188: throws org.xml.sax.SAXException, java.io.IOException {
189: this .Begin();
190:
191: this .Parser.parse(this .TheSource);
192:
193: return InitialObject;
194: } /*Method-End*/
195:
196: public void setSource(org.xml.sax.InputSource source) {
197: this .TheSource = source;
198: } /*Method-End*/
199:
200: public java.lang.Object XlateObject(java.io.InputStream stream)
201: throws org.xml.sax.SAXException, java.io.IOException {
202: this .Begin();
203:
204: InputSource is = new InputSource(stream);
205: is.setSystemId("archiverNoID");
206: this .setSource(is);
207:
208: this .Parser.parse(this .TheSource);
209:
210: return InitialObject;
211: } /*Method-End*/
212:
213: public void DumpStatus() {
214: // This method is a debug method to dump status information about this object
215:
216: System.out.println("Dump Status from class BaseXMLSerializer");
217: System.out.println("The initial object is an instance of "
218: + this .InitialObject.getClass().getName());
219: System.out.println("The initial object state "
220: + this .InitialObject.toString());
221: System.out.println("The current stream dump is "
222: + this .TheCharacters);
223: System.out
224: .println("Dump Status from class BaseXMLSerializer - END");
225:
226: }
227:
228: } // end of class
|