KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > convert > xml > WelcomeFileListImpl


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.convert.xml;
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 // Local Implementation Import Statements
46
import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.LinkedList JavaDoc;
49
50 public class WelcomeFileListImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, WelcomeFileList {
51
52     private List JavaDoc welcomeFileList;
53     private String JavaDoc id;
54     private boolean zeus_IdSet;
55
56     /** Any DOCTYPE reference/statements. */
57     private String JavaDoc docTypeString;
58
59     /** The encoding for the output document */
60     private String JavaDoc outputEncoding;
61
62     /** The current node in unmarshalling */
63     private Unmarshallable zeus_currentUNode;
64
65     /** The parent node in unmarshalling */
66     private Unmarshallable zeus_parentUNode;
67
68     /** Whether this node has been handled */
69     private boolean zeus_thisNodeHandled = false;
70
71     /** Whether a DTD exists for an unmarshal call */
72     private boolean hasDTD;
73
74     /** Whether validation is occurring */
75     private boolean validate;
76
77     /** The namespace mappings on this element */
78     private Map JavaDoc namespaceMappings;
79
80     /** The EntityResolver for SAX parsing to use */
81     private static EntityResolver JavaDoc entityResolver;
82
83     /** The ErrorHandler for SAX parsing to use */
84     private static ErrorHandler JavaDoc errorHandler;
85
86     private static WelcomeFileListImpl prototype = null;
87
88     public static void setPrototype(WelcomeFileListImpl prototype) {
89         WelcomeFileListImpl.prototype = prototype;
90     }
91     public static WelcomeFileListImpl newInstance() {
92         try {
93             return (prototype!=null)?(WelcomeFileListImpl)prototype.clone(): new WelcomeFileListImpl();
94         } catch (CloneNotSupportedException JavaDoc e) {
95             return null; // never
96
}
97     }
98     public WelcomeFileListImpl() {
99         welcomeFileList = new LinkedList JavaDoc();
100         zeus_IdSet = false;
101         docTypeString = "";
102         hasDTD = false;
103         validate = false;
104         namespaceMappings = new HashMap JavaDoc();
105     }
106
107     public List JavaDoc getWelcomeFileList() {
108         return welcomeFileList;
109     }
110
111     public void setWelcomeFileList(List JavaDoc welcomeFileList) {
112         this.welcomeFileList = welcomeFileList;
113     }
114
115     public void addWelcomeFile(WelcomeFile welcomeFile) {
116         welcomeFileList.add(welcomeFile);
117     }
118
119     public void removeWelcomeFile(WelcomeFile welcomeFile) {
120         welcomeFileList.remove(welcomeFile);
121     }
122
123     public String JavaDoc getId() {
124         return id;
125     }
126
127     public void setId(String JavaDoc id) {
128         this.id = id;
129         zeus_IdSet = true;
130     }
131
132     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
133         try {
134             startDTD(name, publicID, systemID);
135         } catch (SAXException JavaDoc neverHappens) { }
136     }
137
138     public void setOutputEncoding(String JavaDoc outputEncoding) {
139         this.outputEncoding = outputEncoding;
140     }
141
142     public void marshal(File JavaDoc file) throws IOException JavaDoc {
143         // Delegate to the marshal(Writer) method
144
marshal(new FileWriter JavaDoc(file));
145     }
146
147     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
148         // Delegate to the marshal(Writer) method
149
marshal(new OutputStreamWriter JavaDoc(outputStream));
150     }
151
152     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
153         // Write out the XML declaration
154
writer.write("<?xml version=\"1.0\" ");
155         if (outputEncoding != null) {
156             writer.write("encoding=\"");
157             writer.write(outputEncoding);
158             writer.write("\"?>\n\n");
159
160         } else {
161             writer.write("encoding=\"UTF-8\"?>\n\n");
162
163         }
164         // Handle DOCTYPE declaration
165
writer.write(docTypeString);
166         writer.write("\n");
167         // Now start the recursive writing
168
writeXMLRepresentation(writer, "");
169
170         // Close up
171
writer.flush();
172         writer.close();
173     }
174
175     protected void writeXMLRepresentation(Writer JavaDoc writer,
176                                           String JavaDoc indent)
177         throws IOException JavaDoc {
178
179         writer.write(indent);
180         writer.write("<welcome-file-list");
181
182         // Handle namespace mappings (if needed)
183
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
184             String JavaDoc prefix = (String JavaDoc)i.next();
185             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
186             writer.write(" xmlns");
187             if (!prefix.trim().equals("")) {
188                 writer.write(":");
189                 writer.write(prefix);
190             }
191             writer.write("=\"");
192             writer.write(uri);
193             writer.write("\"\n ");
194         }
195
196         // Handle attributes (if needed)
197
if (zeus_IdSet) {
198             writer.write(" id=\"");
199             writer.write(escapeAttributeValue(id));
200             writer.write("\"");
201         }
202         writer.write(">");
203         writer.write("\n");
204
205         // Handle child elements
206
for (Iterator JavaDoc i=welcomeFileList.iterator(); i.hasNext(); ) {
207             WelcomeFileImpl welcomeFile = (WelcomeFileImpl)i.next();
208             welcomeFile.writeXMLRepresentation(writer,
209                 new StringBuffer JavaDoc(indent).append(" ").toString());
210         }
211         writer.write(indent);
212         writer.write("</welcome-file-list>\n");
213     }
214
215     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
216         String JavaDoc returnValue = attributeValue;
217         for (int i = 0; i < returnValue.length(); i++) {
218             char ch = returnValue.charAt(i);
219             if (ch == '"') {
220                 returnValue = new StringBuffer JavaDoc()
221                     .append(returnValue.substring(0, i))
222                     .append("&quot;")
223                     .append(returnValue.substring(i+1))
224                     .toString();
225             }
226         }
227         return returnValue;
228     }
229
230     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
231         String JavaDoc returnValue = textValue;
232         for (int i = 0; i < returnValue.length(); i++) {
233             char ch = returnValue.charAt(i);
234             if (ch == '<') {
235                 returnValue = new StringBuffer JavaDoc()
236                     .append(returnValue.substring(0, i))
237                     .append("&lt;")
238                     .append(returnValue.substring(i+1))
239                     .toString();
240             } else if (ch == '>') {
241                 returnValue = new StringBuffer JavaDoc()
242                     .append(returnValue.substring(0, i))
243                     .append("&gt;")
244                     .append(returnValue.substring(i+1))
245                     .toString();
246             }
247         }
248         return returnValue;
249     }
250
251     /**
252      * <p>
253      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
254      * </p>
255      *
256      * @param resolver the entity resolver to use.
257      */

258     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
259         entityResolver = resolver;
260     }
261
262     /**
263      * <p>
264      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
265      * </p>
266      *
267      * @param handler the entity resolver to use.
268      */

269     public static void setErrorHandler(ErrorHandler JavaDoc handler) {
270         errorHandler = handler;
271     }
272
273     public static WelcomeFileList unmarshal(File JavaDoc file) throws IOException JavaDoc {
274         // Delegate to the unmarshal(Reader) method
275
return unmarshal(new FileReader JavaDoc(file));
276     }
277
278     public static WelcomeFileList unmarshal(File JavaDoc file, boolean validate) throws IOException JavaDoc {
279         // Delegate to the unmarshal(Reader) method
280
return unmarshal(new FileReader JavaDoc(file), validate);
281     }
282
283     public static WelcomeFileList unmarshal(InputStream JavaDoc inputStream) throws IOException JavaDoc {
284         // Delegate to the unmarshal(Reader) method
285
return unmarshal(new InputStreamReader JavaDoc(inputStream));
286     }
287
288     public static WelcomeFileList unmarshal(InputStream JavaDoc inputStream, boolean validate) throws IOException JavaDoc {
289         // Delegate to the unmarshal(Reader) method
290
return unmarshal(new InputStreamReader JavaDoc(inputStream), validate);
291     }
292
293     public static WelcomeFileList unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
294         // Delegate with default validation value
295
return unmarshal(reader, false);
296     }
297
298     public static WelcomeFileList unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
299         WelcomeFileListImpl welcomeFileList = WelcomeFileListImpl.newInstance();
300         welcomeFileList.setValidating(validate);
301         welcomeFileList.setCurrentUNode(welcomeFileList);
302         welcomeFileList.setParentUNode(null);
303         // Load the XML parser
304
XMLReader JavaDoc parser = null;
305         String JavaDoc parserClass = System.getProperty("org.xml.sax.driver",
306             "org.apache.xerces.parsers.SAXParser");
307         try {
308             parser = XMLReaderFactory.createXMLReader(parserClass);
309
310             // Set entity resolver, if needed
311
if (entityResolver != null) {
312                 parser.setEntityResolver(entityResolver);
313             }
314
315             // Set error handler
316
parser.setErrorHandler(welcomeFileList);
317
318             // Register lexical handler
319
parser.setProperty("http://xml.org/sax/properties/lexical-handler", welcomeFileList);
320
321             // Register content handler
322
parser.setContentHandler(welcomeFileList);
323         } catch (SAXException JavaDoc e) {
324             throw new IOException JavaDoc("Could not load XML parser: " +
325                 e.getMessage());
326         }
327
328         InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
329         try {
330             parser.setFeature("http://xml.org/sax/features/validation", new Boolean JavaDoc(validate).booleanValue());
331             parser.setFeature("http://xml.org/sax/features/namespaces", true);
332             parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
333             parser.parse(inputSource);
334         } catch (SAXException JavaDoc e) {
335             throw new IOException JavaDoc("Error parsing XML document: " +
336                 e.getMessage());
337         }
338
339         // Return the resultant object
340
return welcomeFileList;
341     }
342
343     public Unmarshallable getParentUNode() {
344         return zeus_parentUNode;
345     }
346
347     public void setParentUNode(Unmarshallable parentUNode) {
348         this.zeus_parentUNode = parentUNode;
349     }
350
351     public Unmarshallable getCurrentUNode() {
352         return zeus_currentUNode;
353     }
354
355     public void setCurrentUNode(Unmarshallable currentUNode) {
356         this.zeus_currentUNode = currentUNode;
357     }
358
359     public void setValidating(boolean validate) {
360         this.validate = validate;
361     }
362
363     public void startDocument() throws SAXException JavaDoc {
364         // no-op
365
}
366
367     public void setDocumentLocator(Locator JavaDoc locator) {
368         // no-op
369
}
370
371     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
372         throws SAXException JavaDoc {
373         namespaceMappings.put(prefix, uri);
374     }
375
376     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
377                              String JavaDoc qName, org.xml.sax.Attributes JavaDoc atts)
378         throws SAXException JavaDoc {
379
380         // Feed this to the correct ContentHandler
381
Unmarshallable current = getCurrentUNode();
382         if (current != this) {
383             current.startElement(namespaceURI, localName, qName, atts);
384             return;
385         }
386
387         // See if we handle, or we delegate
388
if ((localName.equals("welcome-file-list")) && (!zeus_thisNodeHandled)) {
389             // Handle ourselves
390
for (int i=0, len=atts.getLength(); i<len; i++) {
391                 String JavaDoc attName= atts.getLocalName(i);
392                 String JavaDoc attValue = atts.getValue(i);
393                 if (attName.equals("id")) {
394                     setId(attValue);
395                 }
396             }
397             zeus_thisNodeHandled = true;
398             return;
399         } else {
400             // Delegate handling
401
if (localName.equals("welcome-file")) {
402                 WelcomeFileImpl welcomeFile = WelcomeFileImpl.newInstance();
403                 current = getCurrentUNode();
404                 welcomeFile.setParentUNode(current);
405                 welcomeFile.setCurrentUNode(welcomeFile);
406                 this.setCurrentUNode(welcomeFile);
407                 welcomeFile.startElement(namespaceURI, localName, qName, atts);
408                 // Add this value in
409
welcomeFileList.add(welcomeFile);
410                 return;
411             }
412         }
413     }
414
415     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
416                            String JavaDoc qName)
417         throws SAXException JavaDoc {
418
419         Unmarshallable current = getCurrentUNode();
420         if (current != this) {
421             current.endElement(namespaceURI, localName, qName);
422             return;
423         }
424
425         Unmarshallable parent = getCurrentUNode().getParentUNode();
426         if (parent != null) {
427             parent.setCurrentUNode(parent);
428         }
429     }
430
431     public void characters(char[] ch, int start, int len)
432         throws SAXException JavaDoc {
433
434         // Feed this to the correct ContentHandler
435
Unmarshallable current = getCurrentUNode();
436         if (current != this) {
437             current.characters(ch, start, len);
438             return;
439         }
440
441         String JavaDoc text = new String JavaDoc(ch, start, len);
442     }
443
444     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
445         // Currently no-op
446
}
447
448     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
449         if (errorHandler != null) {
450             errorHandler.warning(e);
451         }
452     }
453
454     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
455         if ((validate) && (!hasDTD)) {
456             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.");
457         }
458         if (errorHandler != null) {
459             errorHandler.error(e);
460         }
461     }
462
463     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
464         if ((validate) && (!hasDTD)) {
465             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.");
466         }
467         if (errorHandler != null) {
468             errorHandler.fatalError(e);
469         }
470     }
471
472     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
473         throws SAXException JavaDoc {
474
475         if ((name == null) || (name.equals(""))) {
476             docTypeString = "";
477             return;
478         }
479
480         hasDTD = true;
481         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
482         boolean hasPublic = false;
483
484         docTypeSB.append("<!DOCTYPE ")
485                  .append(name);
486
487         if ((publicID != null) && (!publicID.equals(""))) {
488             docTypeSB.append(" PUBLIC \"")
489                      .append(publicID)
490                      .append("\"");
491             hasPublic = true;
492         }
493
494         if ((systemID != null) && (!systemID.equals(""))) {
495             if (!hasPublic) {
496                 docTypeSB.append(" SYSTEM");
497             }
498             docTypeSB.append(" \"")
499                      .append(systemID)
500                      .append("\"");
501
502         }
503
504         docTypeSB.append(">");
505
506         docTypeString = docTypeSB.toString();
507     }
508
509     public void endDTD() throws SAXException JavaDoc {
510         // Currently no-op
511
}
512
513     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
514         // Currently no-op
515
}
516
517     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
518         // Currently no-op
519
}
520
521     public void startCDATA() throws SAXException JavaDoc {
522         // Currently no-op
523
}
524
525     public void endCDATA() throws SAXException JavaDoc {
526         // Currently no-op
527
}
528
529 }
530
Popular Tags