KickJava   Java API By Example, From Geeks To Geeks.

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


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

283     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
284         entityResolver = resolver;
285     }
286
287     /**
288      * <p>
289      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
290      * </p>
291      *
292      * @param handler the entity resolver to use.
293      */

294     public static void setErrorHandler(ErrorHandler JavaDoc handler) {
295         errorHandler = handler;
296     }
297
298     public static SearchSummary unmarshal(File JavaDoc file) throws IOException JavaDoc {
299         // Delegate to the unmarshal(Reader) method
300
return unmarshal(new FileReader JavaDoc(file));
301     }
302
303     public static SearchSummary unmarshal(File JavaDoc file, boolean validate) throws IOException JavaDoc {
304         // Delegate to the unmarshal(Reader) method
305
return unmarshal(new FileReader JavaDoc(file), validate);
306     }
307
308     public static SearchSummary unmarshal(InputStream JavaDoc inputStream) throws IOException JavaDoc {
309         // Delegate to the unmarshal(Reader) method
310
return unmarshal(new InputStreamReader JavaDoc(inputStream));
311     }
312
313     public static SearchSummary unmarshal(InputStream JavaDoc inputStream, boolean validate) throws IOException JavaDoc {
314         // Delegate to the unmarshal(Reader) method
315
return unmarshal(new InputStreamReader JavaDoc(inputStream), validate);
316     }
317
318     public static SearchSummary unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
319         // Delegate with default validation value
320
return unmarshal(reader, false);
321     }
322
323     public static SearchSummary unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
324         SearchSummaryImpl searchSummary = SearchSummaryImpl.newInstance();
325         searchSummary.setValidating(validate);
326         searchSummary.setCurrentUNode(searchSummary);
327         searchSummary.setParentUNode(null);
328         // Load the XML parser
329
XMLReader JavaDoc parser = null;
330         String JavaDoc parserClass = System.getProperty("org.xml.sax.driver",
331             "org.apache.xerces.parsers.SAXParser");
332         try {
333             parser = XMLReaderFactory.createXMLReader(parserClass);
334
335             // Set entity resolver, if needed
336
if (entityResolver != null) {
337                 parser.setEntityResolver(entityResolver);
338             }
339
340             // Set error handler
341
parser.setErrorHandler(searchSummary);
342
343             // Register lexical handler
344
parser.setProperty("http://xml.org/sax/properties/lexical-handler", searchSummary);
345
346             // Register content handler
347
parser.setContentHandler(searchSummary);
348         } catch (SAXException JavaDoc e) {
349             throw new IOException JavaDoc("Could not load XML parser: " +
350                 e.getMessage());
351         }
352
353         InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
354         try {
355             parser.setFeature("http://xml.org/sax/features/validation", new Boolean JavaDoc(validate).booleanValue());
356             parser.setFeature("http://xml.org/sax/features/namespaces", true);
357             parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
358             parser.parse(inputSource);
359         } catch (SAXException JavaDoc e) {
360             throw new IOException JavaDoc("Error parsing XML document: " +
361                 e.getMessage());
362         }
363
364         // Return the resultant object
365
return searchSummary;
366     }
367
368     public Unmarshallable getParentUNode() {
369         return zeus_parentUNode;
370     }
371
372     public void setParentUNode(Unmarshallable parentUNode) {
373         this.zeus_parentUNode = parentUNode;
374     }
375
376     public Unmarshallable getCurrentUNode() {
377         return zeus_currentUNode;
378     }
379
380     public void setCurrentUNode(Unmarshallable currentUNode) {
381         this.zeus_currentUNode = currentUNode;
382     }
383
384     public void setValidating(boolean validate) {
385         this.validate = validate;
386     }
387
388     public void startDocument() throws SAXException JavaDoc {
389         // no-op
390
}
391
392     public void setDocumentLocator(Locator JavaDoc locator) {
393         // no-op
394
}
395
396     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
397         throws SAXException JavaDoc {
398         namespaceMappings.put(prefix, uri);
399     }
400
401     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
402                              String JavaDoc qName, org.xml.sax.Attributes JavaDoc atts)
403         throws SAXException JavaDoc {
404
405         // Feed this to the correct ContentHandler
406
Unmarshallable current = getCurrentUNode();
407         if (current != this) {
408             current.startElement(namespaceURI, localName, qName, atts);
409             return;
410         }
411
412         // See if we handle, or we delegate
413
if ((localName.equals("SearchSummary")) && (!zeus_thisNodeHandled)) {
414             // Handle ourselves
415
for (int i=0, len=atts.getLength(); i<len; i++) {
416                 String JavaDoc attName= atts.getLocalName(i);
417                 String JavaDoc attValue = atts.getValue(i);
418             }
419             zeus_thisNodeHandled = true;
420             return;
421         } else {
422             // Delegate handling
423
if (localName.equals("SearchedTerm") && (searchedTerm==null)) {
424                 SearchedTermImpl searchedTerm = SearchedTermImpl.newInstance();
425                 current = getCurrentUNode();
426                 searchedTerm.setParentUNode(current);
427                 searchedTerm.setCurrentUNode(searchedTerm);
428                 this.setCurrentUNode(searchedTerm);
429                 searchedTerm.startElement(namespaceURI, localName, qName, atts);
430                 // Add this value in
431
this.searchedTerm = searchedTerm;
432                 return;
433             }
434             if (localName.equals("BeginIndex") && (beginIndex==null)) {
435                 BeginIndexImpl beginIndex = BeginIndexImpl.newInstance();
436                 current = getCurrentUNode();
437                 beginIndex.setParentUNode(current);
438                 beginIndex.setCurrentUNode(beginIndex);
439                 this.setCurrentUNode(beginIndex);
440                 beginIndex.startElement(namespaceURI, localName, qName, atts);
441                 // Add this value in
442
this.beginIndex = beginIndex;
443                 return;
444             }
445             if (localName.equals("EndIndex") && (endIndex==null)) {
446                 EndIndexImpl endIndex = EndIndexImpl.newInstance();
447                 current = getCurrentUNode();
448                 endIndex.setParentUNode(current);
449                 endIndex.setCurrentUNode(endIndex);
450                 this.setCurrentUNode(endIndex);
451                 endIndex.startElement(namespaceURI, localName, qName, atts);
452                 // Add this value in
453
this.endIndex = endIndex;
454                 return;
455             }
456             if (localName.equals("DocsNumber") && (docsNumber==null)) {
457                 DocsNumberImpl docsNumber = DocsNumberImpl.newInstance();
458                 current = getCurrentUNode();
459                 docsNumber.setParentUNode(current);
460                 docsNumber.setCurrentUNode(docsNumber);
461                 this.setCurrentUNode(docsNumber);
462                 docsNumber.startElement(namespaceURI, localName, qName, atts);
463                 // Add this value in
464
this.docsNumber = docsNumber;
465                 return;
466             }
467             if (localName.equals("SearchedTime") && (searchedTime==null)) {
468                 SearchedTimeImpl searchedTime = SearchedTimeImpl.newInstance();
469                 current = getCurrentUNode();
470                 searchedTime.setParentUNode(current);
471                 searchedTime.setCurrentUNode(searchedTime);
472                 this.setCurrentUNode(searchedTime);
473                 searchedTime.startElement(namespaceURI, localName, qName, atts);
474                 // Add this value in
475
this.searchedTime = searchedTime;
476                 return;
477             }
478         }
479     }
480
481     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
482                            String JavaDoc qName)
483         throws SAXException JavaDoc {
484
485         Unmarshallable current = getCurrentUNode();
486         if (current != this) {
487             current.endElement(namespaceURI, localName, qName);
488             return;
489         }
490
491         Unmarshallable parent = getCurrentUNode().getParentUNode();
492         if (parent != null) {
493             parent.setCurrentUNode(parent);
494         }
495     }
496
497     public void characters(char[] ch, int start, int len)
498         throws SAXException JavaDoc {
499
500         // Feed this to the correct ContentHandler
501
Unmarshallable current = getCurrentUNode();
502         if (current != this) {
503             current.characters(ch, start, len);
504             return;
505         }
506
507         String JavaDoc text = new String JavaDoc(ch, start, len);
508     }
509
510     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
511         // Currently no-op
512
}
513
514     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
515         if (errorHandler != null) {
516             errorHandler.warning(e);
517         }
518     }
519
520     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
521         if ((validate) && (!hasDTD)) {
522             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.");
523         }
524         if (errorHandler != null) {
525             errorHandler.error(e);
526         }
527     }
528
529     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
530         if ((validate) && (!hasDTD)) {
531             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.");
532         }
533         if (errorHandler != null) {
534             errorHandler.fatalError(e);
535         }
536     }
537
538     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
539         throws SAXException JavaDoc {
540
541         if ((name == null) || (name.equals(""))) {
542             docTypeString = "";
543             return;
544         }
545
546         hasDTD = true;
547         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
548         boolean hasPublic = false;
549
550         docTypeSB.append("<!DOCTYPE ")
551                  .append(name);
552
553         if ((publicID != null) && (!publicID.equals(""))) {
554             docTypeSB.append(" PUBLIC \"")
555                      .append(publicID)
556                      .append("\"");
557             hasPublic = true;
558         }
559
560         if ((systemID != null) && (!systemID.equals(""))) {
561             if (!hasPublic) {
562                 docTypeSB.append(" SYSTEM");
563             }
564             docTypeSB.append(" \"")
565                      .append(systemID)
566                      .append("\"");
567
568         }
569
570         docTypeSB.append(">");
571
572         docTypeString = docTypeSB.toString();
573     }
574
575     public void endDTD() throws SAXException JavaDoc {
576         // Currently no-op
577
}
578
579     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
580         // Currently no-op
581
}
582
583     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
584         // Currently no-op
585
}
586
587     public void startCDATA() throws SAXException JavaDoc {
588         // Currently no-op
589
}
590
591     public void endCDATA() throws SAXException JavaDoc {
592         // Currently no-op
593
}
594
595 }
596
Popular Tags