KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > remote > RemoteSearchParser


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.base.remote;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Stack JavaDoc;
19
20 import javax.xml.parsers.ParserConfigurationException JavaDoc;
21 import javax.xml.parsers.SAXParser JavaDoc;
22 import javax.xml.parsers.SAXParserFactory JavaDoc;
23
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.help.internal.search.SearchHit;
26 import org.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.helpers.DefaultHandler JavaDoc;
30
31 /*
32  * Converts search results serialized by the SearchServlet on remote help server
33  * back into model objects.
34  */

35 public class RemoteSearchParser extends DefaultHandler JavaDoc {
36
37     private SAXParser JavaDoc parser;
38     private Stack JavaDoc stack;
39     private List JavaDoc hits;
40     private StringBuffer JavaDoc summary;
41
42     /*
43      * Parses the given serialized search hits and returns generated model
44      * objects (SearchHit objects).
45      */

46     public List JavaDoc parse(InputStream JavaDoc in, IProgressMonitor monitor) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
47         monitor.beginTask("", 1); //$NON-NLS-1$
48
init();
49         parser.parse(in, this);
50         monitor.worked(1);
51         monitor.done();
52         return hits;
53     }
54     
55     /*
56      * Initializes the parser's state for a new search. Must be called
57      * before each parse.
58      */

59     private void init() throws ParserConfigurationException JavaDoc, SAXException JavaDoc {
60         if (hits == null) {
61             hits = new ArrayList JavaDoc();
62         }
63         else if (!hits.isEmpty()) {
64             hits.clear();
65         }
66         if (stack == null) {
67             stack = new Stack JavaDoc();
68         }
69         else if (!stack.isEmpty()) {
70             stack.clear();
71         }
72         summary = null;
73         if (parser == null) {
74             parser = SAXParserFactory.newInstance().newSAXParser();
75         }
76     }
77     
78     /* (non-Javadoc)
79      * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
80      */

81     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
82         if (qName.equals("hit")) { //$NON-NLS-1$
83
handleHit(attributes);
84         }
85         else if (qName.equals("summary")) { //$NON-NLS-1$
86
handleSummary(attributes);
87         }
88     }
89     
90     /* (non-Javadoc)
91      * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
92      */

93     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
94         if (qName.equals("hit")) { //$NON-NLS-1$
95
stack.pop();
96         }
97         else if (qName.equals("summary") && summary != null) { //$NON-NLS-1$
98
// collect the summary from the buffer
99
SearchHit hit = (SearchHit)stack.peek();
100             hit.setSummary(summary.toString());
101             summary = null;
102         }
103     }
104
105     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
106         // are we in <summary></summary> elements?
107
if (summary != null) {
108             // if so, add to the buffer
109
summary.append(ch, start, length);
110         }
111     }
112
113     private void handleHit(Attributes JavaDoc attr) {
114         String JavaDoc href = attr.getValue("href"); //$NON-NLS-1$
115
String JavaDoc label = attr.getValue("label"); //$NON-NLS-1$
116
boolean isPotentialHit = (String.valueOf(true).equalsIgnoreCase(attr.getValue("isPotentialHit"))); //$NON-NLS-1$
117
float score;
118         try {
119             score = Float.parseFloat(attr.getValue("score")); //$NON-NLS-1$
120
}
121         catch (NumberFormatException JavaDoc e) {
122             // score was probably missing; default to 0
123
score = 0;
124         }
125         SearchHit hit = new SearchHit(href, label, null, score, null, null, null, isPotentialHit);
126         hits.add(hit);
127         stack.push(hit);
128     }
129     
130     private void handleSummary(Attributes JavaDoc attr) {
131         // prepare the buffer to receive text summary
132
summary = new StringBuffer JavaDoc();
133     }
134     
135     /*
136      * Note: throws clause does not declare IOException due to a bug in
137      * sun jdk: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6327149
138      *
139      * @see org.xml.sax.helpers.DefaultHandler#resolveEntity(java.lang.String, java.lang.String)
140      */

141     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
142         return new InputSource JavaDoc(new StringReader JavaDoc("")); //$NON-NLS-1$
143
}
144 }
145
Popular Tags