KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > display > filters > dtd > RemoveImpl


1 /**
2  * This class was generated from a set of XML constraints
3  * by the Enhydra Zeus XML Data Binding Framework. All
4  * source code in this file is constructed specifically
5  * to work with other Zeus-generated classes. If you
6  * modify this file by hand, you run the risk of breaking
7  * this interoperation, as well as introducing errors in
8  * source code compilation.
9  *
10  * * * * * MODIFY THIS FILE AT YOUR OWN RISK * * * * *
11  *
12  * To find out more about the Enhydra Zeus framework, you
13  * can point your browser at <http://zeus.enhydra.org>
14  * where you can download releases, join and discuss Zeus
15  * on user and developer mailing lists, and access source
16  * code. Please report any bugs through that website.
17  */

18 package org.enhydra.barracuda.contrib.dbroggisch.display.filters.dtd;
19
20 // Global Implementation Import Statements
21
import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.io.Writer JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 import org.xml.sax.EntityResolver JavaDoc;
35 import org.xml.sax.ErrorHandler JavaDoc;
36 import org.xml.sax.InputSource JavaDoc;
37 import org.xml.sax.Locator JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39 import org.xml.sax.SAXParseException JavaDoc;
40 import org.xml.sax.XMLReader JavaDoc;
41 import org.xml.sax.ext.LexicalHandler JavaDoc;
42 import org.xml.sax.helpers.DefaultHandler JavaDoc;
43 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
44
45 public class RemoveImpl extends DefaultHandler JavaDoc implements Unmarshallable, LexicalHandler JavaDoc, Remove {
46
47     private String JavaDoc name;
48     private boolean zeus_NameSet;
49
50     /** Any DOCTYPE reference/statements. */
51     private String JavaDoc docTypeString;
52
53     /** The encoding for the output document */
54     private String JavaDoc outputEncoding;
55
56     /** The current node in unmarshalling */
57     private Unmarshallable zeus_currentUNode;
58
59     /** The parent node in unmarshalling */
60     private Unmarshallable zeus_parentUNode;
61
62     /** Whether this node has been handled */
63     private boolean zeus_thisNodeHandled = false;
64
65     /** Whether a DTD exists for an unmarshal call */
66     private boolean hasDTD;
67
68     /** Whether validation is occurring */
69     private boolean validate;
70
71     /** The namespace mappings on this element */
72     private Map JavaDoc namespaceMappings;
73
74     /** The EntityResolver for SAX parsing to use */
75     private static EntityResolver JavaDoc entityResolver;
76
77     /** The ErrorHandler for SAX parsing to use */
78     private static ErrorHandler JavaDoc errorHandler;
79
80     public RemoveImpl() {
81         zeus_NameSet = false;
82         docTypeString = "";
83         hasDTD = false;
84         validate = false;
85         namespaceMappings = new HashMap JavaDoc();
86     }
87
88     public String JavaDoc getName() {
89         return name;
90     }
91
92     public void setName(String JavaDoc name) {
93         this.name = name;
94         zeus_NameSet = true;
95     }
96
97     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
98         try {
99             startDTD(name, publicID, systemID);
100         } catch (SAXException JavaDoc neverHappens) { }
101     }
102
103     public void setOutputEncoding(String JavaDoc outputEncoding) {
104         this.outputEncoding = outputEncoding;
105     }
106
107     public void marshal(File JavaDoc file) throws IOException JavaDoc {
108         // Delegate to the marshal(Writer) method
109
marshal(new FileWriter JavaDoc(file));
110     }
111
112     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
113         // Delegate to the marshal(Writer) method
114
marshal(new OutputStreamWriter JavaDoc(outputStream));
115     }
116
117     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
118         // Write out the XML declaration
119
writer.write("<?xml version=\"1.0\" ");
120         if (outputEncoding != null) {
121             writer.write("encoding=\"");
122             writer.write(outputEncoding);
123             writer.write("\"?>\n\n");
124
125         } else {
126             writer.write("encoding=\"UTF-8\"?>\n\n");
127
128         }
129         // Handle DOCTYPE declaration
130
writer.write(docTypeString);
131         writer.write("\n");
132         // Now start the recursive writing
133
writeXMLRepresentation(writer, "");
134
135         // Close up
136
writer.flush();
137         writer.close();
138     }
139
140     protected void writeXMLRepresentation(Writer JavaDoc writer,
141                                           String JavaDoc indent)
142         throws IOException JavaDoc {
143
144         writer.write(indent);
145         writer.write("<remove");
146
147         // Handle namespace mappings (if needed)
148
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
149             String JavaDoc prefix = (String JavaDoc)i.next();
150             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
151             writer.write(" xmlns");
152             if (!prefix.trim().equals("")) {
153                 writer.write(":");
154                 writer.write(prefix);
155             }
156             writer.write("=\"");
157             writer.write(uri);
158             writer.write("\"\n ");
159         }
160
161         // Handle attributes (if needed)
162
if (zeus_NameSet) {
163             writer.write(" name=\"");
164             writer.write(escapeAttributeValue(name));
165             writer.write("\"");
166         }
167         writer.write("/>\n");
168     }
169
170     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
171         String JavaDoc returnValue = attributeValue;
172         for (int i = 0; i < returnValue.length(); i++) {
173             char ch = returnValue.charAt(i);
174             if (ch == '"') {
175                 returnValue = new StringBuffer JavaDoc()
176                     .append(returnValue.substring(0, i))
177                     .append("&quot;")
178                     .append(returnValue.substring(i+1))
179                     .toString();
180             }
181         }
182         return returnValue;
183     }
184
185     /**
186      * <p>
187      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
188      * </p>
189      *
190      * @param resolver the entity resolver to use.
191      */

192     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
193         entityResolver = resolver;
194     }
195
196     /**
197      * <p>
198      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
199      * </p>
200      *
201      * @param handler the entity resolver to use.
202      */

203     public static void setErrorHandler(ErrorHandler JavaDoc handler) {
204         errorHandler = handler;
205     }
206
207     public static Remove unmarshal(File JavaDoc file) throws IOException JavaDoc {
208         // Delegate to the unmarshal(Reader) method
209
return unmarshal(new FileReader JavaDoc(file));
210     }
211
212     public static Remove unmarshal(File JavaDoc file, boolean validate) throws IOException JavaDoc {
213         // Delegate to the unmarshal(Reader) method
214
return unmarshal(new FileReader JavaDoc(file), validate);
215     }
216
217     public static Remove unmarshal(InputStream JavaDoc inputStream) throws IOException JavaDoc {
218         // Delegate to the unmarshal(Reader) method
219
return unmarshal(new InputStreamReader JavaDoc(inputStream));
220     }
221
222     public static Remove unmarshal(InputStream JavaDoc inputStream, boolean validate) throws IOException JavaDoc {
223         // Delegate to the unmarshal(Reader) method
224
return unmarshal(new InputStreamReader JavaDoc(inputStream), validate);
225     }
226
227     public static Remove unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
228         // Delegate with default validation value
229
return unmarshal(reader, false);
230     }
231
232     public static Remove unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
233         RemoveImpl remove = new RemoveImpl();
234         remove.setValidating(validate);
235         remove.setCurrentUNode(remove);
236         remove.setParentUNode(null);
237         // Load the XML parser
238
XMLReader JavaDoc parser = null;
239         String JavaDoc parserClass = System.getProperty("org.xml.sax.driver",
240             "org.apache.xerces.parsers.SAXParser");
241         try {
242             parser = XMLReaderFactory.createXMLReader(parserClass);
243
244             // Set entity resolver, if needed
245
if (entityResolver != null) {
246                 parser.setEntityResolver(entityResolver);
247             }
248
249             // Set error handler
250
parser.setErrorHandler(remove);
251
252             // Register lexical handler
253
parser.setProperty("http://xml.org/sax/properties/lexical-handler", remove);
254
255             // Register content handler
256
parser.setContentHandler(remove);
257         } catch (SAXException JavaDoc e) {
258             throw new IOException JavaDoc("Could not load XML parser: " +
259                 e.getMessage());
260         }
261
262         InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
263         try {
264             parser.setFeature("http://xml.org/sax/features/validation", new Boolean JavaDoc(validate).booleanValue());
265             parser.setFeature("http://xml.org/sax/features/namespaces", true);
266             parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
267             parser.parse(inputSource);
268         } catch (SAXException JavaDoc e) {
269             throw new IOException JavaDoc("Error parsing XML document: " +
270                 e.getMessage());
271         }
272
273         // Return the resultant object
274
return remove;
275     }
276
277     public Unmarshallable getParentUNode() {
278         return zeus_parentUNode;
279     }
280
281     public void setParentUNode(Unmarshallable parentUNode) {
282         this.zeus_parentUNode = parentUNode;
283     }
284
285     public Unmarshallable getCurrentUNode() {
286         return zeus_currentUNode;
287     }
288
289     public void setCurrentUNode(Unmarshallable currentUNode) {
290         this.zeus_currentUNode = currentUNode;
291     }
292
293     public void setValidating(boolean validate) {
294         this.validate = validate;
295     }
296
297     public void startDocument() throws SAXException JavaDoc {
298         // no-op
299
}
300
301     public void setDocumentLocator(Locator JavaDoc locator) {
302         // no-op
303
}
304
305     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
306         throws SAXException JavaDoc {
307         namespaceMappings.put(prefix, uri);
308     }
309
310     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
311                              String JavaDoc qName, org.xml.sax.Attributes JavaDoc atts)
312         throws SAXException JavaDoc {
313
314         // Feed this to the correct ContentHandler
315
Unmarshallable current = getCurrentUNode();
316         if (current != this) {
317             current.startElement(namespaceURI, localName, qName, atts);
318             return;
319         }
320
321         // See if we handle, or we delegate
322
if ((localName.equals("remove")) && (!zeus_thisNodeHandled)) {
323             // Handle ourselves
324
for (int i=0, len=atts.getLength(); i<len; i++) {
325                 String JavaDoc attName= atts.getLocalName(i);
326                 String JavaDoc attValue = atts.getValue(i);
327                 if (attName.equals("name")) {
328                     setName(attValue);
329                 }
330             }
331             zeus_thisNodeHandled = true;
332             return;
333         } else {
334             // Delegate handling
335
}
336     }
337
338     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
339                            String JavaDoc qName)
340         throws SAXException JavaDoc {
341
342         Unmarshallable current = getCurrentUNode();
343         if (current != this) {
344             current.endElement(namespaceURI, localName, qName);
345             return;
346         }
347
348         Unmarshallable parent = getCurrentUNode().getParentUNode();
349         if (parent != null) {
350             parent.setCurrentUNode(parent);
351         }
352     }
353
354     public void characters(char[] ch, int start, int len)
355         throws SAXException JavaDoc {
356
357         // Feed this to the correct ContentHandler
358
Unmarshallable current = getCurrentUNode();
359         if (current != this) {
360             current.characters(ch, start, len);
361             return;
362         }
363
364         String JavaDoc text = new String JavaDoc(ch, start, len);
365         text = text.trim();
366     }
367
368     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
369         // Currently no-op
370
}
371
372     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
373         if (errorHandler != null) {
374             errorHandler.warning(e);
375         }
376     }
377
378     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
379         if ((validate) && (!hasDTD)) {
380             throw new SAXException JavaDoc("Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
381         }
382         if (errorHandler != null) {
383             errorHandler.error(e);
384         }
385     }
386
387     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
388         if ((validate) && (!hasDTD)) {
389             throw new SAXException JavaDoc("Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
390         }
391         if (errorHandler != null) {
392             errorHandler.fatalError(e);
393         }
394     }
395
396     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
397         throws SAXException JavaDoc {
398
399         if ((name == null) || (name.equals(""))) {
400             docTypeString = "";
401             return;
402         }
403
404         hasDTD = true;
405         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
406         boolean hasPublic = false;
407
408         docTypeSB.append("<!DOCTYPE ")
409                  .append(name);
410
411         if ((publicID != null) && (!publicID.equals(""))) {
412             docTypeSB.append(" PUBLIC \"")
413                      .append(publicID)
414                      .append("\"");
415             hasPublic = true;
416         }
417
418         if ((systemID != null) && (!systemID.equals(""))) {
419             if (!hasPublic) {
420                 docTypeSB.append(" SYSTEM");
421             }
422             docTypeSB.append(" \"")
423                      .append(systemID)
424                      .append("\"");
425
426         }
427
428         docTypeSB.append(">");
429
430         docTypeString = docTypeSB.toString();
431     }
432
433     public void endDTD() throws SAXException JavaDoc {
434         // Currently no-op
435
}
436
437     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
438         // Currently no-op
439
}
440
441     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
442         // Currently no-op
443
}
444
445     public void startCDATA() throws SAXException JavaDoc {
446         // Currently no-op
447
}
448
449     public void endCDATA() throws SAXException JavaDoc {
450         // Currently no-op
451
}
452
453 }
454
Popular Tags