KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > display > filters > dtd > LookupImpl


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

208     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
209         entityResolver = resolver;
210     }
211
212     /**
213      * <p>
214      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
215      * </p>
216      *
217      * @param handler the entity resolver to use.
218      */

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