KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > migration > archiver > deserializer > BaseXMLDeserializer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema.migration.archiver.deserializer;
21
22 import java.lang.*;
23 import javax.xml.parsers.*;
24
25 import org.xml.sax.helpers.*;
26 import org.xml.sax.*;
27
28 public class BaseXMLDeserializer extends java.lang.Object JavaDoc
29     implements XMLDeserializer, org.xml.sax.DocumentHandler JavaDoc, org.xml.sax.DTDHandler JavaDoc, org.xml.sax.ErrorHandler JavaDoc
30 {
31
32     // Fields
33
protected java.lang.Object JavaDoc InitialObject;
34     protected org.xml.sax.Parser JavaDoc Parser;
35     public org.xml.sax.Locator JavaDoc TheLocator;
36     protected org.xml.sax.InputSource JavaDoc TheSource;
37     public java.lang.StringBuffer JavaDoc TheCharacters;
38
39     // Constructors
40
public BaseXMLDeserializer()
41     {
42         this.TheCharacters = new StringBuffer JavaDoc();
43     } /*Constructor-End*/
44
45     // Methods
46
public void notationDecl(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws org.xml.sax.SAXException JavaDoc
47     {
48         
49     } /*Method-END*/
50     public void unparsedEntityDecl(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId, String JavaDoc notationName) throws org.xml.sax.SAXException JavaDoc
51     {
52         
53     } /*Method-End*/
54     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws org.xml.sax.SAXException JavaDoc
55     {
56         
57     } /*Method-Enc*/
58     public void setDocumentLocator(org.xml.sax.Locator JavaDoc locator)
59     {
60         this.TheLocator = locator;
61     } /*Method-End*/
62     public void ignorableWhitespace(char[] ch, int start, int length) throws org.xml.sax.SAXException JavaDoc
63     {
64         
65     } /*Method-End*/
66     public void endElement(java.lang.String JavaDoc name) throws org.xml.sax.SAXException JavaDoc
67     {
68         this.TheCharacters.delete(0,this.TheCharacters.length());
69     } /*Method-End*/
70
71     public void endDocument() throws org.xml.sax.SAXException JavaDoc
72     {
73         this.freeResources();
74     } /*Method-End*/
75     public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException JavaDoc
76     {
77         this.TheCharacters.append(ch,start,length);
78     } /*Method-End*/
79
80     public void startElement(java.lang.String JavaDoc name, org.xml.sax.AttributeList JavaDoc atts) throws org.xml.sax.SAXException JavaDoc
81     {
82         this.TheCharacters.delete(0,this.TheCharacters.length());
83     } /*Method-End*/
84
85     public void startDocument() throws org.xml.sax.SAXException JavaDoc
86     {
87         this.freeResources();
88     } /*Method-End*/
89
90     public void fatalError(org.xml.sax.SAXParseException JavaDoc exception) throws org.xml.sax.SAXException JavaDoc
91     {
92         this.commonErrorProcessor(exception);
93     } /*Method-End*/
94
95     public void warning(org.xml.sax.SAXParseException JavaDoc exception) throws org.xml.sax.SAXException JavaDoc
96     {
97         
98     } /*Method-End*/
99
100     public void error(org.xml.sax.SAXParseException JavaDoc exception) throws org.xml.sax.SAXException JavaDoc
101     {
102         this.commonErrorProcessor(exception);
103     } /*Method-End*/
104
105     public void setInitialObject(java.lang.Object JavaDoc obj)
106     {
107         this.InitialObject = obj;
108     } /*Method-End*/
109
110     public void freeResources()
111     {
112         this.TheCharacters.delete(0,this.TheCharacters.length());
113     } /*Method-End*/
114
115     public java.lang.String JavaDoc getCharacters()
116     {
117         // trim escaped newline character and newline characters
118

119         int lOffset = 0;
120                 
121         while (lOffset < this.TheCharacters.length())
122         {
123             if (lOffset + 2 < this.TheCharacters.length() &&
124                 this.TheCharacters.substring(lOffset, lOffset + 2).equals("\\n"))
125             {
126                this.TheCharacters.delete(lOffset, lOffset + 2);
127             }
128             else if (this.TheCharacters.charAt(lOffset) == '\n')
129             {
130                this.TheCharacters.deleteCharAt(lOffset);
131             }
132             lOffset++;
133         }
134              
135         return this.TheCharacters.toString();
136     } /*Method-End*/
137
138     public int Begin() throws org.xml.sax.SAXException JavaDoc {
139         try {
140             if (this.Parser == null) {
141                 org.xml.sax.Parser JavaDoc parser;
142                 SAXParserFactory factory;
143
144                 factory = SAXParserFactory.newInstance();
145                 factory.setValidating(false); // asi validate=false
146
factory.setNamespaceAware(false);
147
148                 this.Parser = factory.newSAXParser().getParser();
149                 
150                 this.Parser.setDocumentHandler(this);
151                 this.Parser.setDTDHandler(this);
152                 this.Parser.setErrorHandler(this);
153             }
154         } catch (ParserConfigurationException e1) {
155             SAXException classError = new SAXException(e1.getMessage());
156             throw classError;
157         }
158         
159         return 1;
160     } /*Method-End*/
161
162     public void commonErrorProcessor(org.xml.sax.SAXParseException JavaDoc error) throws org.xml.sax.SAXException JavaDoc
163     {
164         throw(error);
165     } /*Method-End*/
166
167     public java.lang.Object JavaDoc XlateObject() throws org.xml.sax.SAXException JavaDoc, java.io.IOException JavaDoc
168     {
169         this.Begin();
170         
171         this.Parser.parse(this.TheSource);
172         
173         return InitialObject;
174     } /*Method-End*/
175
176     public void setSource(org.xml.sax.InputSource JavaDoc source)
177     {
178         this.TheSource = source;
179     } /*Method-End*/
180
181     public java.lang.Object JavaDoc XlateObject(java.io.InputStream JavaDoc stream) throws org.xml.sax.SAXException JavaDoc, java.io.IOException JavaDoc
182     {
183         this.Begin();
184         
185         InputSource is = new InputSource(stream);
186         is.setSystemId("archiverNoID");
187         this.setSource(is);
188         
189         this.Parser.parse(this.TheSource);
190         
191         return InitialObject;
192     } /*Method-End*/
193     
194     public void DumpStatus()
195     {
196         // This method is a debug method to dump status information about this object
197

198         System.out.println("Dump Status from class BaseXMLSerializer");
199         System.out.println("The initial object is an instance of " + this.InitialObject.getClass().getName());
200         System.out.println("The initial object state " + this.InitialObject.toString());
201         System.out.println("The current stream dump is " + this.TheCharacters);
202         System.out.println("Dump Status from class BaseXMLSerializer - END");
203         
204     }
205     
206     
207 } // end of class
208
Popular Tags