1 11 package org.eclipse.help.internal.context; 12 13 import java.io.*; 14 import com.ibm.icu.text.MessageFormat; 15 16 import javax.xml.parsers.*; 17 18 import org.eclipse.help.internal.*; 19 import org.eclipse.help.internal.util.*; 20 import org.xml.sax.*; 21 import org.xml.sax.helpers.*; 22 23 26 public class ContextsFileParser extends DefaultHandler { 27 protected FastStack stack = new FastStack(); 28 29 StringBuffer buffer = new StringBuffer (); 30 31 boolean seenDescription = false; 32 33 ContextsFile contextsFile; 34 35 private ContextsBuilder builder; 36 37 private final static SAXParserFactory factory = SAXParserFactory 38 .newInstance(); 39 40 public ContextsFileParser(ContextsBuilder builder) { 41 super(); 42 this.builder = builder; 43 } 44 45 48 public void characters(char ch[], int start, int length) 49 throws SAXException { 50 if (seenDescription) 51 buffer.append(ch, start, length); 52 if (HelpPlugin.DEBUG_CONTEXT) { 53 System.out 54 .println("ContextsFileParser.characters(): got char from parser= " + new StringBuffer ().append(ch, start, length) 56 .toString()); 57 } 58 } 59 60 63 public void endElement(String namespaceURI, String localName, String qName) 64 throws SAXException { 65 if (qName.equals(ContextsNode.DESC_ELEM)) { 67 seenDescription = false; 68 ((Context) stack.peek()).setStyledText(buffer.toString()); 69 buffer.setLength(0); 70 } else if (qName.equals(ContextsNode.DESC_TXT_BOLD)) { 71 stack.pop(); 73 if (!(stack.peek()).equals(ContextsNode.BOLD_TAG)) 74 buffer.append(ContextsNode.BOLD_CLOSE_TAG); 75 } else if (!qName.equals("filter")) { ContextsNode node = (ContextsNode) stack.pop(); 77 node.build(builder); 78 } 79 } 80 81 84 public void error(SAXParseException ex) { 85 HelpPlugin.logError("Error parsing " + getErrorDetails(ex), null); } 87 88 91 public void fatalError(SAXParseException ex) throws SAXException { 92 HelpPlugin.logError("Failed to parse " + getErrorDetails(ex), ex); } 94 95 public String getErrorDetails(SAXParseException ex) { 96 String param0 = ex.getSystemId(); 97 Integer param1 = new Integer (ex.getLineNumber()); 98 Integer param2 = new Integer (ex.getColumnNumber()); 99 String param3 = ex.getMessage(); 100 String message = MessageFormat 101 .format( 102 "URL: {0} at line: {1,number,integer}, column: {2,number,integer}.\r\n{3}", new Object [] { param0, param1, param2, param3 }); 104 return message; 105 } 106 107 110 public void startElement(String namespaceURI, String localName, 111 String qName, Attributes atts) throws SAXException { 112 if (qName.equals(ContextsNode.DESC_ELEM)) 114 seenDescription = true; 115 else if (qName.equals(ContextsNode.DESC_TXT_BOLD)) { 116 if (!(stack.peek()).equals(ContextsNode.BOLD_TAG)) 124 buffer.append(ContextsNode.BOLD_TAG); 125 stack.push(ContextsNode.BOLD_TAG); 126 } else { 127 ContextsNode e = null; 128 if (qName.equals(ContextsNode.CONTEXTS_ELEM)) { 130 e = new Contexts(atts); 131 } else if (qName.equals(ContextsNode.CONTEXT_ELEM)) { 132 e = new Context(atts); 133 } else if (qName.equals(ContextsNode.RELATED_ELEM)) { 134 e = new RelatedTopic(atts); 135 } else if (qName.equals("filter")) { if (!stack.empty()) { 137 Object parent = stack.peek(); 138 if (parent instanceof FilterableUAElement && atts != null) { 139 FilterableUAElement filterableNode = (FilterableUAElement)parent; 140 String name = atts.getValue("name"); String value = atts.getValue("value"); if (name != null && value != null) { 143 filterableNode.addFilter(name, value); 144 } 145 } 146 } 147 return; 148 } else 149 return; 150 if (!stack.empty()) 151 ((ContextsNode) stack.peek()).addChild(e); 152 stack.push(e); 153 } 154 } 155 156 public void warning(SAXParseException ex) { 157 HelpPlugin.logWarning("Warning parsing " + getErrorDetails(ex)); } 159 160 public void parse(ContextsFile contextsFile) { 161 this.contextsFile = contextsFile; 162 InputStream is = contextsFile.getInputStream(); 163 if (is == null) 164 return; 165 InputSource inputSource = new InputSource(is); 166 String file = "/" + contextsFile.getDefiningPluginID() + "/" + contextsFile.getHref(); 168 inputSource.setSystemId(file); 169 try { 170 SAXParser parser = factory.newSAXParser(); 171 parser.parse(inputSource, this); 172 } catch (ParserConfigurationException pce) { 173 HelpPlugin.logError( 174 "SAXParser implementation could not be loaded.", pce); } catch (SAXException se) { 176 HelpPlugin.logError("", se); } catch (IOException ioe) { 178 HelpPlugin.logError("Error loading file " + file + ".", ioe); } finally { 180 if (is != null) { 181 try { 182 is.close(); 183 } catch (IOException e) { 184 } 185 } 186 } 187 } 188 189 194 public InputSource resolveEntity(String publicId, String systemId) { 195 InputSource source = new InputSource(new ByteArrayInputStream( 196 new byte[0])); 197 source.setPublicId(publicId); 198 source.setSystemId(systemId); 199 return source; 200 } 201 } 202 | Popular Tags |