KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > xmlform > dtd > HiddenImpl


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.sam.xmlform.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 HiddenImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, Hidden {
46
47
48     /** Any DOCTYPE reference/statements. */
49     private String JavaDoc docTypeString;
50
51     /** The encoding for the output document */
52     private String JavaDoc outputEncoding;
53
54     /** The current node in unmarshalling */
55     private Unmarshallable zeus_currentUNode;
56
57     /** The parent node in unmarshalling */
58     private Unmarshallable zeus_parentUNode;
59
60     /** Whether this node has been handled */
61     private boolean zeus_thisNodeHandled = false;
62
63     /** Whether a DTD exists for an unmarshal call */
64     private boolean hasDTD;
65
66     /** Whether validation is occurring */
67     private boolean validate;
68
69     /** The namespace mappings on this element */
70     private Map JavaDoc namespaceMappings;
71
72     /** The EntityResolver for SAX parsing to use */
73     private static EntityResolver JavaDoc entityResolver;
74
75     /** The ErrorHandler for SAX parsing to use */
76     private static ErrorHandler JavaDoc errorHandler;
77
78     private static HiddenImpl prototype = null;
79
80     public static void setPrototype(HiddenImpl prototype) {
81         HiddenImpl.prototype = prototype;
82     }
83     public static HiddenImpl newInstance() {
84         try {
85             return (prototype!=null)?(HiddenImpl)prototype.clone(): new HiddenImpl();
86         } catch (CloneNotSupportedException JavaDoc e) {
87             return null; // never
88
}
89     }
90     public HiddenImpl() {
91         docTypeString = "";
92         hasDTD = false;
93         validate = false;
94         namespaceMappings = new HashMap JavaDoc();
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("<hidden");
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
writer.write("/>\n");
163     }
164
165     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
166         String JavaDoc returnValue = attributeValue;
167         for (int i = 0; i < returnValue.length(); i++) {
168             char ch = returnValue.charAt(i);
169             if (ch == '"') {
170                 returnValue = new StringBuffer JavaDoc()
171                     .append(returnValue.substring(0, i))
172                     .append("&quot;")
173                     .append(returnValue.substring(i+1))
174                     .toString();
175             }
176         }
177         return returnValue;
178     }
179
180     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
181         String JavaDoc returnValue = textValue;
182         for (int i = 0; i < returnValue.length(); i++) {
183             char ch = returnValue.charAt(i);
184             if (ch == '<') {
185                 returnValue = new StringBuffer JavaDoc()
186                     .append(returnValue.substring(0, i))
187                     .append("&lt;")
188                     .append(returnValue.substring(i+1))
189                     .toString();
190             } else if (ch == '>') {
191                 returnValue = new StringBuffer JavaDoc()
192                     .append(returnValue.substring(0, i))
193                     .append("&gt;")
194                     .append(returnValue.substring(i+1))
195                     .toString();
196             }
197         }
198         return returnValue;
199     }
200
201     /**
202      * <p>
203      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
204      * </p>
205      *
206      * @param resolver the entity resolver to use.
207      */

208     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
209         entityResolver = resolver;
210     }
211
212     /**
213      * <p>
214      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
215      * </p>
216      *
217      * @param handler the entity resolver to use.
218      */

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