1 11 package org.eclipse.help.internal.base.remote; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.StringReader ; 16 import java.util.ArrayList ; 17 import java.util.List ; 18 import java.util.Stack ; 19 20 import javax.xml.parsers.ParserConfigurationException ; 21 import javax.xml.parsers.SAXParser ; 22 import javax.xml.parsers.SAXParserFactory ; 23 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.help.internal.search.SearchHit; 26 import org.xml.sax.Attributes ; 27 import org.xml.sax.InputSource ; 28 import org.xml.sax.SAXException ; 29 import org.xml.sax.helpers.DefaultHandler ; 30 31 35 public class RemoteSearchParser extends DefaultHandler { 36 37 private SAXParser parser; 38 private Stack stack; 39 private List hits; 40 private StringBuffer summary; 41 42 46 public List parse(InputStream in, IProgressMonitor monitor) throws ParserConfigurationException , SAXException , IOException { 47 monitor.beginTask("", 1); init(); 49 parser.parse(in, this); 50 monitor.worked(1); 51 monitor.done(); 52 return hits; 53 } 54 55 59 private void init() throws ParserConfigurationException , SAXException { 60 if (hits == null) { 61 hits = new ArrayList (); 62 } 63 else if (!hits.isEmpty()) { 64 hits.clear(); 65 } 66 if (stack == null) { 67 stack = new Stack (); 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 81 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 82 if (qName.equals("hit")) { handleHit(attributes); 84 } 85 else if (qName.equals("summary")) { handleSummary(attributes); 87 } 88 } 89 90 93 public void endElement(String uri, String localName, String qName) throws SAXException { 94 if (qName.equals("hit")) { stack.pop(); 96 } 97 else if (qName.equals("summary") && summary != null) { 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 { 106 if (summary != null) { 108 summary.append(ch, start, length); 110 } 111 } 112 113 private void handleHit(Attributes attr) { 114 String href = attr.getValue("href"); String label = attr.getValue("label"); boolean isPotentialHit = (String.valueOf(true).equalsIgnoreCase(attr.getValue("isPotentialHit"))); float score; 118 try { 119 score = Float.parseFloat(attr.getValue("score")); } 121 catch (NumberFormatException e) { 122 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 attr) { 131 summary = new StringBuffer (); 133 } 134 135 141 public InputSource resolveEntity(String publicId, String systemId) throws SAXException { 142 return new InputSource (new StringReader ("")); } 144 } 145 | Popular Tags |