KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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