KickJava   Java API By Example, From Geeks To Geeks.

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


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

244     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
245         entityResolver = resolver;
246     }
247
248     /**
249      * <p>
250      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
251      * </p>
252      *
253      * @param handler the entity resolver to use.
254      */

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