KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > business > xml > ParameterImpl


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.snapper.business.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 ParameterImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, Parameter {
46
47     private Key key;
48     private Value value;
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     private static ParameterImpl prototype = null;
81
82     public static void setPrototype(ParameterImpl prototype) {
83         ParameterImpl.prototype = prototype;
84     }
85     public static ParameterImpl newInstance() {
86         try {
87             return (prototype!=null)?(ParameterImpl)prototype.clone(): new ParameterImpl();
88         } catch (CloneNotSupportedException JavaDoc e) {
89             return null; // never
90
}
91     }
92     public ParameterImpl() {
93         docTypeString = "";
94         hasDTD = false;
95         validate = false;
96         namespaceMappings = new HashMap JavaDoc();
97     }
98
99     public Key getKey() {
100         return key;
101     }
102
103     public void setKey(Key key) {
104         this.key = key;
105     }
106
107     public Value getValue() {
108         return value;
109     }
110
111     public void setValue(Value value) {
112         this.value = value;
113     }
114
115     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
116         try {
117             startDTD(name, publicID, systemID);
118         } catch (SAXException JavaDoc neverHappens) { }
119     }
120
121     public void setOutputEncoding(String JavaDoc outputEncoding) {
122         this.outputEncoding = outputEncoding;
123     }
124
125     public void marshal(File JavaDoc file) throws IOException JavaDoc {
126         // Delegate to the marshal(Writer) method
127
marshal(new FileWriter JavaDoc(file));
128     }
129
130     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
131         // Delegate to the marshal(Writer) method
132
marshal(new OutputStreamWriter JavaDoc(outputStream));
133     }
134
135     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
136         // Write out the XML declaration
137
writer.write("<?xml version=\"1.0\" ");
138         if (outputEncoding != null) {
139             writer.write("encoding=\"");
140             writer.write(outputEncoding);
141             writer.write("\"?>\n\n");
142
143         } else {
144             writer.write("encoding=\"UTF-8\"?>\n\n");
145
146         }
147         // Handle DOCTYPE declaration
148
writer.write(docTypeString);
149         writer.write("\n");
150         // Now start the recursive writing
151
writeXMLRepresentation(writer, "");
152
153         // Close up
154
writer.flush();
155         writer.close();
156     }
157
158     protected void writeXMLRepresentation(Writer JavaDoc writer,
159                                           String JavaDoc indent)
160         throws IOException JavaDoc {
161
162         writer.write(indent);
163         writer.write("<Parameter");
164
165         // Handle namespace mappings (if needed)
166
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
167             String JavaDoc prefix = (String JavaDoc)i.next();
168             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
169             writer.write(" xmlns");
170             if (!prefix.trim().equals("")) {
171                 writer.write(":");
172                 writer.write(prefix);
173             }
174             writer.write("=\"");
175             writer.write(uri);
176             writer.write("\"\n ");
177         }
178
179         // Handle attributes (if needed)
180
writer.write(">");
181         writer.write("\n");
182
183         // Handle child elements
184
if (key != null) {
185             ((KeyImpl)key).writeXMLRepresentation(writer,
186                 new StringBuffer JavaDoc(indent).append(" ").toString());
187         }
188
189         if (value != null) {
190             ((ValueImpl)value).writeXMLRepresentation(writer,
191                 new StringBuffer JavaDoc(indent).append(" ").toString());
192         }
193
194         writer.write(indent);
195         writer.write("</Parameter>\n");
196     }
197
198     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
199         String JavaDoc returnValue = attributeValue;
200         for (int i = 0; i < returnValue.length(); i++) {
201             char ch = returnValue.charAt(i);
202             if (ch == '"') {
203                 returnValue = new StringBuffer JavaDoc()
204                     .append(returnValue.substring(0, i))
205                     .append("&quot;")
206                     .append(returnValue.substring(i+1))
207                     .toString();
208             }
209         }
210         return returnValue;
211     }
212
213     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
214         String JavaDoc returnValue = textValue;
215         for (int i = 0; i < returnValue.length(); i++) {
216             char ch = returnValue.charAt(i);
217             if (ch == '<') {
218                 returnValue = new StringBuffer JavaDoc()
219                     .append(returnValue.substring(0, i))
220                     .append("&lt;")
221                     .append(returnValue.substring(i+1))
222                     .toString();
223             } else if (ch == '>') {
224                 returnValue = new StringBuffer JavaDoc()
225                     .append(returnValue.substring(0, i))
226                     .append("&gt;")
227                     .append(returnValue.substring(i+1))
228                     .toString();
229             }
230         }
231         return returnValue;
232     }
233
234     /**
235      * <p>
236      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
237      * </p>
238      *
239      * @param resolver the entity resolver to use.
240      */

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

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