KickJava   Java API By Example, From Geeks To Geeks.

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


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 ServletMappingImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, ServletMapping {
46
47     private ServletName servletName;
48     private UrlPattern urlPattern;
49     private String JavaDoc id;
50     private boolean zeus_IdSet;
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 ServletMappingImpl prototype = null;
83
84     public static void setPrototype(ServletMappingImpl prototype) {
85         ServletMappingImpl.prototype = prototype;
86     }
87     public static ServletMappingImpl newInstance() {
88         try {
89             return (prototype!=null)?(ServletMappingImpl)prototype.clone(): new ServletMappingImpl();
90         } catch (CloneNotSupportedException JavaDoc e) {
91             return null; // never
92
}
93     }
94     public ServletMappingImpl() {
95         zeus_IdSet = false;
96         docTypeString = "";
97         hasDTD = false;
98         validate = false;
99         namespaceMappings = new HashMap JavaDoc();
100     }
101
102     public ServletName getServletName() {
103         return servletName;
104     }
105
106     public void setServletName(ServletName servletName) {
107         this.servletName = servletName;
108     }
109
110     public UrlPattern getUrlPattern() {
111         return urlPattern;
112     }
113
114     public void setUrlPattern(UrlPattern urlPattern) {
115         this.urlPattern = urlPattern;
116     }
117
118     public String JavaDoc getId() {
119         return id;
120     }
121
122     public void setId(String JavaDoc id) {
123         this.id = id;
124         zeus_IdSet = true;
125     }
126
127     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
128         try {
129             startDTD(name, publicID, systemID);
130         } catch (SAXException JavaDoc neverHappens) { }
131     }
132
133     public void setOutputEncoding(String JavaDoc outputEncoding) {
134         this.outputEncoding = outputEncoding;
135     }
136
137     public void marshal(File JavaDoc file) throws IOException JavaDoc {
138         // Delegate to the marshal(Writer) method
139
marshal(new FileWriter JavaDoc(file));
140     }
141
142     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
143         // Delegate to the marshal(Writer) method
144
marshal(new OutputStreamWriter JavaDoc(outputStream));
145     }
146
147     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
148         // Write out the XML declaration
149
writer.write("<?xml version=\"1.0\" ");
150         if (outputEncoding != null) {
151             writer.write("encoding=\"");
152             writer.write(outputEncoding);
153             writer.write("\"?>\n\n");
154
155         } else {
156             writer.write("encoding=\"UTF-8\"?>\n\n");
157
158         }
159         // Handle DOCTYPE declaration
160
writer.write(docTypeString);
161         writer.write("\n");
162         // Now start the recursive writing
163
writeXMLRepresentation(writer, "");
164
165         // Close up
166
writer.flush();
167         writer.close();
168     }
169
170     protected void writeXMLRepresentation(Writer JavaDoc writer,
171                                           String JavaDoc indent)
172         throws IOException JavaDoc {
173
174         writer.write(indent);
175         writer.write("<servlet-mapping");
176
177         // Handle namespace mappings (if needed)
178
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
179             String JavaDoc prefix = (String JavaDoc)i.next();
180             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
181             writer.write(" xmlns");
182             if (!prefix.trim().equals("")) {
183                 writer.write(":");
184                 writer.write(prefix);
185             }
186             writer.write("=\"");
187             writer.write(uri);
188             writer.write("\"\n ");
189         }
190
191         // Handle attributes (if needed)
192
if (zeus_IdSet) {
193             writer.write(" id=\"");
194             writer.write(escapeAttributeValue(id));
195             writer.write("\"");
196         }
197         writer.write(">");
198         writer.write("\n");
199
200         // Handle child elements
201
if (servletName != null) {
202             ((ServletNameImpl)servletName).writeXMLRepresentation(writer,
203                 new StringBuffer JavaDoc(indent).append(" ").toString());
204         }
205
206         if (urlPattern != null) {
207             ((UrlPatternImpl)urlPattern).writeXMLRepresentation(writer,
208                 new StringBuffer JavaDoc(indent).append(" ").toString());
209         }
210
211         writer.write(indent);
212         writer.write("</servlet-mapping>\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 ServletMapping 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 ServletMapping 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 ServletMapping 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 ServletMapping 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 ServletMapping unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
294         // Delegate with default validation value
295
return unmarshal(reader, false);
296     }
297
298     public static ServletMapping unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
299         ServletMappingImpl servletMapping = ServletMappingImpl.newInstance();
300         servletMapping.setValidating(validate);
301         servletMapping.setCurrentUNode(servletMapping);
302         servletMapping.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(servletMapping);
317
318             // Register lexical handler
319
parser.setProperty("http://xml.org/sax/properties/lexical-handler", servletMapping);
320
321             // Register content handler
322
parser.setContentHandler(servletMapping);
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 servletMapping;
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("servlet-mapping")) && (!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("servlet-name") && (servletName==null)) {
402                 ServletNameImpl servletName = ServletNameImpl.newInstance();
403                 current = getCurrentUNode();
404                 servletName.setParentUNode(current);
405                 servletName.setCurrentUNode(servletName);
406                 this.setCurrentUNode(servletName);
407                 servletName.startElement(namespaceURI, localName, qName, atts);
408                 // Add this value in
409
this.servletName = servletName;
410                 return;
411             }
412             if (localName.equals("url-pattern") && (urlPattern==null)) {
413                 UrlPatternImpl urlPattern = UrlPatternImpl.newInstance();
414                 current = getCurrentUNode();
415                 urlPattern.setParentUNode(current);
416                 urlPattern.setCurrentUNode(urlPattern);
417                 this.setCurrentUNode(urlPattern);
418                 urlPattern.startElement(namespaceURI, localName, qName, atts);
419                 // Add this value in
420
this.urlPattern = urlPattern;
421                 return;
422             }
423         }
424     }
425
426     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
427                            String JavaDoc qName)
428         throws SAXException JavaDoc {
429
430         Unmarshallable current = getCurrentUNode();
431         if (current != this) {
432             current.endElement(namespaceURI, localName, qName);
433             return;
434         }
435
436         Unmarshallable parent = getCurrentUNode().getParentUNode();
437         if (parent != null) {
438             parent.setCurrentUNode(parent);
439         }
440     }
441
442     public void characters(char[] ch, int start, int len)
443         throws SAXException JavaDoc {
444
445         // Feed this to the correct ContentHandler
446
Unmarshallable current = getCurrentUNode();
447         if (current != this) {
448             current.characters(ch, start, len);
449             return;
450         }
451
452         String JavaDoc text = new String JavaDoc(ch, start, len);
453     }
454
455     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
456         // Currently no-op
457
}
458
459     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
460         if (errorHandler != null) {
461             errorHandler.warning(e);
462         }
463     }
464
465     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
466         if ((validate) && (!hasDTD)) {
467             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.");
468         }
469         if (errorHandler != null) {
470             errorHandler.error(e);
471         }
472     }
473
474     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
475         if ((validate) && (!hasDTD)) {
476             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.");
477         }
478         if (errorHandler != null) {
479             errorHandler.fatalError(e);
480         }
481     }
482
483     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
484         throws SAXException JavaDoc {
485
486         if ((name == null) || (name.equals(""))) {
487             docTypeString = "";
488             return;
489         }
490
491         hasDTD = true;
492         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
493         boolean hasPublic = false;
494
495         docTypeSB.append("<!DOCTYPE ")
496                  .append(name);
497
498         if ((publicID != null) && (!publicID.equals(""))) {
499             docTypeSB.append(" PUBLIC \"")
500                      .append(publicID)
501                      .append("\"");
502             hasPublic = true;
503         }
504
505         if ((systemID != null) && (!systemID.equals(""))) {
506             if (!hasPublic) {
507                 docTypeSB.append(" SYSTEM");
508             }
509             docTypeSB.append(" \"")
510                      .append(systemID)
511                      .append("\"");
512
513         }
514
515         docTypeSB.append(">");
516
517         docTypeString = docTypeSB.toString();
518     }
519
520     public void endDTD() throws SAXException JavaDoc {
521         // Currently no-op
522
}
523
524     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
525         // Currently no-op
526
}
527
528     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
529         // Currently no-op
530
}
531
532     public void startCDATA() throws SAXException JavaDoc {
533         // Currently no-op
534
}
535
536     public void endCDATA() throws SAXException JavaDoc {
537         // Currently no-op
538
}
539
540 }
541
Popular Tags