KickJava   Java API By Example, From Geeks To Geeks.

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


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 SearchImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, Search {
46
47     private SearchSummary searchSummary;
48     private SearchResult searchResult;
49
50     /** Any DOCTYPE reference/statements. */
51     private String JavaDoc docTypeString;
52
53     /** The encoding for the output document */
54     private String JavaDoc outputEncoding;
55
56     /** The current node in unmarshalling */
57     private Unmarshallable zeus_currentUNode;
58
59     /** The parent node in unmarshalling */
60     private Unmarshallable zeus_parentUNode;
61
62     /** Whether this node has been handled */
63     private boolean zeus_thisNodeHandled = false;
64
65     /** Whether a DTD exists for an unmarshal call */
66     private boolean hasDTD;
67
68     /** Whether validation is occurring */
69     private boolean validate;
70
71     /** The namespace mappings on this element */
72     private Map JavaDoc namespaceMappings;
73
74     /** The EntityResolver for SAX parsing to use */
75     private static EntityResolver JavaDoc entityResolver;
76
77     /** The ErrorHandler for SAX parsing to use */
78     private static ErrorHandler JavaDoc errorHandler;
79
80     private static SearchImpl prototype = null;
81
82     public static void setPrototype(SearchImpl prototype) {
83         SearchImpl.prototype = prototype;
84     }
85     public static SearchImpl newInstance() {
86         try {
87             return (prototype!=null)?(SearchImpl)prototype.clone(): new SearchImpl();
88         } catch (CloneNotSupportedException JavaDoc e) {
89             return null; // never
90
}
91     }
92     public SearchImpl() {
93         docTypeString = "";
94         hasDTD = false;
95         validate = false;
96         namespaceMappings = new HashMap JavaDoc();
97     }
98
99     public SearchSummary getSearchSummary() {
100         return searchSummary;
101     }
102
103     public void setSearchSummary(SearchSummary searchSummary) {
104         this.searchSummary = searchSummary;
105     }
106
107     public SearchResult getSearchResult() {
108         return searchResult;
109     }
110
111     public void setSearchResult(SearchResult searchResult) {
112         this.searchResult = searchResult;
113     }
114
115     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
116         try {
117             startDTD(name, publicID, systemID);
118         } catch (SAXException JavaDoc neverHappens) { }
119     }
120
121     public void setOutputEncoding(String JavaDoc outputEncoding) {
122         this.outputEncoding = outputEncoding;
123     }
124
125     public void marshal(File JavaDoc file) throws IOException JavaDoc {
126         // Delegate to the marshal(Writer) method
127
marshal(new FileWriter JavaDoc(file));
128     }
129
130     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
131         // Delegate to the marshal(Writer) method
132
marshal(new OutputStreamWriter JavaDoc(outputStream));
133     }
134
135     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
136         // Write out the XML declaration
137
writer.write("<?xml version=\"1.0\" ");
138         if (outputEncoding != null) {
139             writer.write("encoding=\"");
140             writer.write(outputEncoding);
141             writer.write("\"?>\n\n");
142
143         } else {
144             writer.write("encoding=\"UTF-8\"?>\n\n");
145
146         }
147         // Handle DOCTYPE declaration
148
writer.write(docTypeString);
149         writer.write("\n");
150         // Now start the recursive writing
151
writeXMLRepresentation(writer, "");
152
153         // Close up
154
writer.flush();
155         writer.close();
156     }
157
158     protected void writeXMLRepresentation(Writer JavaDoc writer,
159                                           String JavaDoc indent)
160         throws IOException JavaDoc {
161
162         writer.write(indent);
163         writer.write("<Search");
164
165         // Handle namespace mappings (if needed)
166
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
167             String JavaDoc prefix = (String JavaDoc)i.next();
168             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
169             writer.write(" xmlns");
170             if (!prefix.trim().equals("")) {
171                 writer.write(":");
172                 writer.write(prefix);
173             }
174             writer.write("=\"");
175             writer.write(uri);
176             writer.write("\"\n ");
177         }
178
179         // Handle attributes (if needed)
180
writer.write(">");
181         writer.write("\n");
182
183         // Handle child elements
184
if (searchSummary != null) {
185             ((SearchSummaryImpl)searchSummary).writeXMLRepresentation(writer,
186                 new StringBuffer JavaDoc(indent).append(" ").toString());
187         }
188
189         if (searchResult != null) {
190             ((SearchResultImpl)searchResult).writeXMLRepresentation(writer,
191                 new StringBuffer JavaDoc(indent).append(" ").toString());
192         }
193
194         writer.write(indent);
195         writer.write("</Search>\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 Search 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 Search 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 Search 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 Search 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 Search unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
277         // Delegate with default validation value
278
return unmarshal(reader, false);
279     }
280
281     public static Search unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
282         SearchImpl search = SearchImpl.newInstance();
283         search.setValidating(validate);
284         search.setCurrentUNode(search);
285         search.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(search);
300
301             // Register lexical handler
302
parser.setProperty("http://xml.org/sax/properties/lexical-handler", search);
303
304             // Register content handler
305
parser.setContentHandler(search);
306         } catch (SAXException JavaDoc e) {
307             throw new IOException JavaDoc("Could not load XML parser: " +
308                 e.getMessage());
309         }
310         CustomResolver temp = new CustomResolver();
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 search;
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("Search")) && (!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             }
378             zeus_thisNodeHandled = true;
379             return;
380         } else {
381             // Delegate handling
382
if (localName.equals("SearchSummary") && (searchSummary==null)) {
383                 SearchSummaryImpl searchSummary = SearchSummaryImpl.newInstance();
384                 current = getCurrentUNode();
385                 searchSummary.setParentUNode(current);
386                 searchSummary.setCurrentUNode(searchSummary);
387                 this.setCurrentUNode(searchSummary);
388                 searchSummary.startElement(namespaceURI, localName, qName, atts);
389                 // Add this value in
390
this.searchSummary = searchSummary;
391                 return;
392             }
393             if (localName.equals("SearchResult") && (searchResult==null)) {
394                 SearchResultImpl searchResult = SearchResultImpl.newInstance();
395                 current = getCurrentUNode();
396                 searchResult.setParentUNode(current);
397                 searchResult.setCurrentUNode(searchResult);
398                 this.setCurrentUNode(searchResult);
399                 searchResult.startElement(namespaceURI, localName, qName, atts);
400                 // Add this value in
401
this.searchResult = searchResult;
402                 return;
403             }
404         }
405     }
406
407     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
408                            String JavaDoc qName)
409         throws SAXException JavaDoc {
410
411         Unmarshallable current = getCurrentUNode();
412         if (current != this) {
413             current.endElement(namespaceURI, localName, qName);
414             return;
415         }
416
417         Unmarshallable parent = getCurrentUNode().getParentUNode();
418         if (parent != null) {
419             parent.setCurrentUNode(parent);
420         }
421     }
422
423     public void characters(char[] ch, int start, int len)
424         throws SAXException JavaDoc {
425
426         // Feed this to the correct ContentHandler
427
Unmarshallable current = getCurrentUNode();
428         if (current != this) {
429             current.characters(ch, start, len);
430             return;
431         }
432
433         String JavaDoc text = new String JavaDoc(ch, start, len);
434     }
435
436     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
437         // Currently no-op
438
}
439
440     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
441         if (errorHandler != null) {
442             errorHandler.warning(e);
443         }
444     }
445
446     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
447         if ((validate) && (!hasDTD)) {
448             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.");
449         }
450         if (errorHandler != null) {
451             errorHandler.error(e);
452         }
453     }
454
455     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
456         if ((validate) && (!hasDTD)) {
457             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.");
458         }
459         if (errorHandler != null) {
460             errorHandler.fatalError(e);
461         }
462     }
463
464     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
465         throws SAXException JavaDoc {
466
467         if ((name == null) || (name.equals(""))) {
468             docTypeString = "";
469             return;
470         }
471
472         hasDTD = true;
473         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
474         boolean hasPublic = false;
475
476         docTypeSB.append("<!DOCTYPE ")
477                  .append(name);
478
479         if ((publicID != null) && (!publicID.equals(""))) {
480             docTypeSB.append(" PUBLIC \"")
481                      .append(publicID)
482                      .append("\"");
483             hasPublic = true;
484         }
485
486         if ((systemID != null) && (!systemID.equals(""))) {
487             if (!hasPublic) {
488                 docTypeSB.append(" SYSTEM");
489             }
490             docTypeSB.append(" \"")
491                      .append(systemID)
492                      .append("\"");
493
494         }
495
496         docTypeSB.append(">");
497
498         docTypeString = docTypeSB.toString();
499     }
500
501     public void endDTD() throws SAXException JavaDoc {
502         // Currently no-op
503
}
504
505     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
506         // Currently no-op
507
}
508
509     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
510         // Currently no-op
511
}
512
513     public void startCDATA() throws SAXException JavaDoc {
514         // Currently no-op
515
}
516
517     public void endCDATA() throws SAXException JavaDoc {
518         // Currently no-op
519
}
520    
521    
522
523 }
524
525
Popular Tags