KickJava   Java API By Example, From Geeks To Geeks.

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


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 // 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 SearchResultImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, SearchResult {
51
52     private List JavaDoc ResultList;
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     private static SearchResultImpl prototype = null;
85
86     public static void setPrototype(SearchResultImpl prototype) {
87         SearchResultImpl.prototype = prototype;
88     }
89     public static SearchResultImpl newInstance() {
90         try {
91             return (prototype!=null)?(SearchResultImpl)prototype.clone(): new SearchResultImpl();
92         } catch (CloneNotSupportedException JavaDoc e) {
93             return null; // never
94
}
95     }
96     public SearchResultImpl() {
97         ResultList = new LinkedList JavaDoc();
98         docTypeString = "";
99         hasDTD = false;
100         validate = false;
101         namespaceMappings = new HashMap JavaDoc();
102     }
103
104     public List JavaDoc getResultList() {
105         return ResultList;
106     }
107
108     public void setResultList(List JavaDoc ResultList) {
109         this.ResultList = ResultList;
110     }
111
112     public void addResult(Result Result) {
113         ResultList.add(Result);
114     }
115
116     public void removeResult(Result Result) {
117         ResultList.remove(Result);
118     }
119
120     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
121         try {
122             startDTD(name, publicID, systemID);
123         } catch (SAXException JavaDoc neverHappens) { }
124     }
125
126     public void setOutputEncoding(String JavaDoc outputEncoding) {
127         this.outputEncoding = outputEncoding;
128     }
129
130     public void marshal(File JavaDoc file) throws IOException JavaDoc {
131         // Delegate to the marshal(Writer) method
132
marshal(new FileWriter JavaDoc(file));
133     }
134
135     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
136         // Delegate to the marshal(Writer) method
137
marshal(new OutputStreamWriter JavaDoc(outputStream));
138     }
139
140     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
141         // Write out the XML declaration
142
writer.write("<?xml version=\"1.0\" ");
143         if (outputEncoding != null) {
144             writer.write("encoding=\"");
145             writer.write(outputEncoding);
146             writer.write("\"?>\n\n");
147
148         } else {
149             writer.write("encoding=\"UTF-8\"?>\n\n");
150
151         }
152         // Handle DOCTYPE declaration
153
writer.write(docTypeString);
154         writer.write("\n");
155         // Now start the recursive writing
156
writeXMLRepresentation(writer, "");
157
158         // Close up
159
writer.flush();
160         writer.close();
161     }
162
163     protected void writeXMLRepresentation(Writer JavaDoc writer,
164                                           String JavaDoc indent)
165         throws IOException JavaDoc {
166
167         writer.write(indent);
168         writer.write("<SearchResult");
169
170         // Handle namespace mappings (if needed)
171
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
172             String JavaDoc prefix = (String JavaDoc)i.next();
173             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
174             writer.write(" xmlns");
175             if (!prefix.trim().equals("")) {
176                 writer.write(":");
177                 writer.write(prefix);
178             }
179             writer.write("=\"");
180             writer.write(uri);
181             writer.write("\"\n ");
182         }
183
184         // Handle attributes (if needed)
185
writer.write(">");
186         writer.write("\n");
187
188         // Handle child elements
189
for (Iterator JavaDoc i=ResultList.iterator(); i.hasNext(); ) {
190             ResultImpl result = (ResultImpl)i.next();
191             result.writeXMLRepresentation(writer,
192                 new StringBuffer JavaDoc(indent).append(" ").toString());
193         }
194         writer.write(indent);
195         writer.write("</SearchResult>\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 SearchResult 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 SearchResult 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 SearchResult 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 SearchResult 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 SearchResult unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
277         // Delegate with default validation value
278
return unmarshal(reader, false);
279     }
280
281     public static SearchResult unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
282         SearchResultImpl searchResult = SearchResultImpl.newInstance();
283         searchResult.setValidating(validate);
284         searchResult.setCurrentUNode(searchResult);
285         searchResult.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(searchResult);
300
301             // Register lexical handler
302
parser.setProperty("http://xml.org/sax/properties/lexical-handler", searchResult);
303
304             // Register content handler
305
parser.setContentHandler(searchResult);
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 searchResult;
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("SearchResult")) && (!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("Result")) {
382                 ResultImpl result = ResultImpl.newInstance();
383                 current = getCurrentUNode();
384                 result.setParentUNode(current);
385                 result.setCurrentUNode(result);
386                 this.setCurrentUNode(result);
387                 result.startElement(namespaceURI, localName, qName, atts);
388                 // Add this value in
389
ResultList.add(result);
390                 return;
391             }
392         }
393     }
394
395     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
396                            String JavaDoc qName)
397         throws SAXException JavaDoc {
398
399         Unmarshallable current = getCurrentUNode();
400         if (current != this) {
401             current.endElement(namespaceURI, localName, qName);
402             return;
403         }
404
405         Unmarshallable parent = getCurrentUNode().getParentUNode();
406         if (parent != null) {
407             parent.setCurrentUNode(parent);
408         }
409     }
410
411     public void characters(char[] ch, int start, int len)
412         throws SAXException JavaDoc {
413
414         // Feed this to the correct ContentHandler
415
Unmarshallable current = getCurrentUNode();
416         if (current != this) {
417             current.characters(ch, start, len);
418             return;
419         }
420
421         String JavaDoc text = new String JavaDoc(ch, start, len);
422     }
423
424     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
425         // Currently no-op
426
}
427
428     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
429         if (errorHandler != null) {
430             errorHandler.warning(e);
431         }
432     }
433
434     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
435         if ((validate) && (!hasDTD)) {
436             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.");
437         }
438         if (errorHandler != null) {
439             errorHandler.error(e);
440         }
441     }
442
443     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
444         if ((validate) && (!hasDTD)) {
445             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.");
446         }
447         if (errorHandler != null) {
448             errorHandler.fatalError(e);
449         }
450     }
451
452     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
453         throws SAXException JavaDoc {
454
455         if ((name == null) || (name.equals(""))) {
456             docTypeString = "";
457             return;
458         }
459
460         hasDTD = true;
461         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
462         boolean hasPublic = false;
463
464         docTypeSB.append("<!DOCTYPE ")
465                  .append(name);
466
467         if ((publicID != null) && (!publicID.equals(""))) {
468             docTypeSB.append(" PUBLIC \"")
469                      .append(publicID)
470                      .append("\"");
471             hasPublic = true;
472         }
473
474         if ((systemID != null) && (!systemID.equals(""))) {
475             if (!hasPublic) {
476                 docTypeSB.append(" SYSTEM");
477             }
478             docTypeSB.append(" \"")
479                      .append(systemID)
480                      .append("\"");
481
482         }
483
484         docTypeSB.append(">");
485
486         docTypeString = docTypeSB.toString();
487     }
488
489     public void endDTD() throws SAXException JavaDoc {
490         // Currently no-op
491
}
492
493     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
494         // Currently no-op
495
}
496
497     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
498         // Currently no-op
499
}
500
501     public void startCDATA() throws SAXException JavaDoc {
502         // Currently no-op
503
}
504
505     public void endCDATA() throws SAXException JavaDoc {
506         // Currently no-op
507
}
508
509 }
510
Popular Tags