001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 package graphlab.ui.xml; 005 006 import org.xml.sax.*; 007 008 /** 009 * The class reads XML documents according to specified DTD and 010 * translates all related events into UIHandler events. 011 * <p>Usage sample: 012 * <pre> 013 * UIParser parser = new UIParser(...); 014 * parser.parse(new InputSource("...")); 015 * </pre> 016 * <p><b>Warning:</b> the class is machine generated. DO NOT MODIFY</p> 017 */ 018 public class UIParser implements ContentHandler { 019 020 private java.lang.StringBuffer buffer; 021 022 private UIHandler handler; 023 024 private java.util.Stack context; 025 026 private EntityResolver resolver; 027 028 /** 029 * Creates a parser instance. 030 * 031 * @param handler handler interface implementation (never <code>null</code> 032 * @param resolver SAX entity resolver implementation or <code>null</code>. 033 * It is recommended that it could be able to resolve at least the DTD. 034 */ 035 public UIParser(final UIHandler handler, final EntityResolver resolver) { 036 037 this.handler = handler; 038 this.resolver = resolver; 039 buffer = new StringBuffer(111); 040 context = new java.util.Stack(); 041 } 042 043 /** 044 * This SAX interface method is implemented by the parser. 045 */ 046 public final void setDocumentLocator(Locator locator) { 047 } 048 049 /** 050 * This SAX interface method is implemented by the parser. 051 */ 052 public final void startDocument() throws SAXException { 053 } 054 055 /** 056 * This SAX interface method is implemented by the parser. 057 */ 058 public final void endDocument() throws SAXException { 059 } 060 061 /** 062 * This SAX interface method is implemented by the parser. 063 */ 064 public final void startElement(java.lang.String ns, java.lang.String name, java.lang.String qname, Attributes attrs) throws SAXException { 065 066 dispatch(true); 067 context.push(new Object[]{qname, new org.xml.sax.helpers.AttributesImpl(attrs)}); 068 if ("sidebar".equals(qname)) { 069 handler.handle_sidebar(attrs); 070 } else if ("action".equals(qname)) { 071 handler.handle_action(attrs); 072 } else if ("submenu".equals(qname)) { 073 handler.start_submenu(attrs); 074 } else if ("bar".equals(qname)) { 075 handler.handle_bar(attrs); 076 } else if ("toolbar".equals(qname)) { 077 handler.start_toolbar(attrs); 078 } else if ("toolbars".equals(qname)) { 079 handler.start_toolbars(attrs); 080 } else if ("tool".equals(qname)) { 081 handler.handle_tool(attrs); 082 } else if ("menu".equals(qname)) { 083 handler.handle_menu(attrs); 084 } else if ("menues".equals(qname)) { 085 handler.start_menues(attrs); 086 } else if ("body".equals(qname)) { 087 handler.handle_body(attrs); 088 } 089 } 090 091 /** 092 * This SAX interface method is implemented by the parser. 093 */ 094 public final void endElement(java.lang.String ns, java.lang.String name, java.lang.String qname) throws SAXException { 095 096 dispatch(false); 097 context.pop(); 098 if ("submenu".equals(qname)) { 099 handler.end_submenu(); 100 } else if ("toolbar".equals(qname)) { 101 handler.end_toolbar(); 102 } else if ("toolbars".equals(qname)) { 103 handler.end_toolbars(); 104 } else if ("menues".equals(qname)) { 105 handler.end_menues(); 106 } 107 } 108 109 /** 110 * This SAX interface method is implemented by the parser. 111 */ 112 public final void characters(char[] chars, int start, int len) throws SAXException { 113 114 buffer.append(chars, start, len); 115 } 116 117 /** 118 * This SAX interface method is implemented by the parser. 119 */ 120 public final void ignorableWhitespace(char[] chars, int start, int len) throws SAXException { 121 } 122 123 /** 124 * This SAX interface method is implemented by the parser. 125 */ 126 public final void processingInstruction(java.lang.String target, java.lang.String data) throws SAXException { 127 } 128 129 /** 130 * This SAX interface method is implemented by the parser. 131 */ 132 public final void startPrefixMapping(final java.lang.String prefix, final java.lang.String uri) throws SAXException { 133 } 134 135 /** 136 * This SAX interface method is implemented by the parser. 137 */ 138 public final void endPrefixMapping(final java.lang.String prefix) throws SAXException { 139 } 140 141 /** 142 * This SAX interface method is implemented by the parser. 143 */ 144 public final void skippedEntity(java.lang.String name) throws SAXException { 145 } 146 147 private void dispatch(final boolean fireOnlyIfMixed) throws SAXException { 148 149 if (fireOnlyIfMixed && buffer.length() == 0) return; //skip it 150 151 Object[] ctx = (Object[]) context.peek(); 152 String here = (String) ctx[0]; 153 Attributes attrs = (Attributes) ctx[1]; 154 buffer.delete(0, buffer.length()); 155 } 156 157 /** 158 * The recognizer entry method taking an InputSource. 159 * 160 * @param input InputSource to be parsed. 161 * @throws java.io.IOException on I/O error. 162 * @throws SAXException propagated exception thrown by a DocumentHandler. 163 * @throws javax.xml.parsers.ParserConfigurationException 164 * a parser satisfining requested configuration can not be created. 165 * @throws javax.xml.parsers.FactoryConfigurationRrror 166 * if the implementation can not be instantiated. 167 */ 168 public void parse(final InputSource input) throws SAXException, javax.xml.parsers.ParserConfigurationException, java.io.IOException { 169 170 parse(input, this); 171 } 172 173 /** 174 * The recognizer entry method taking a URL. 175 * 176 * @param url URL source to be parsed. 177 * @throws java.io.IOException on I/O error. 178 * @throws SAXException propagated exception thrown by a DocumentHandler. 179 * @throws javax.xml.parsers.ParserConfigurationException 180 * a parser satisfining requested configuration can not be created. 181 * @throws javax.xml.parsers.FactoryConfigurationRrror 182 * if the implementation can not be instantiated. 183 */ 184 public void parse(final java.net.URL url) throws SAXException, javax.xml.parsers.ParserConfigurationException, java.io.IOException { 185 186 parse(new InputSource(url.toExternalForm()), this); 187 } 188 189 /** 190 * The recognizer entry method taking an Inputsource. 191 * 192 * @param input InputSource to be parsed. 193 * @throws java.io.IOException on I/O error. 194 * @throws SAXException propagated exception thrown by a DocumentHandler. 195 * @throws javax.xml.parsers.ParserConfigurationException 196 * a parser satisfining requested configuration can not be created. 197 * @throws javax.xml.parsers.FactoryConfigurationRrror 198 * if the implementation can not be instantiated. 199 */ 200 public static void parse(final InputSource input, final UIHandler handler) throws SAXException, javax.xml.parsers.ParserConfigurationException, java.io.IOException { 201 202 parse(input, new UIParser(handler, null)); 203 } 204 205 /** 206 * The recognizer entry method taking a URL. 207 * 208 * @param url URL source to be parsed. 209 * @throws java.io.IOException on I/O error. 210 * @throws SAXException propagated exception thrown by a DocumentHandler. 211 * @throws javax.xml.parsers.ParserConfigurationException 212 * a parser satisfining requested configuration can not be created. 213 * @throws javax.xml.parsers.FactoryConfigurationRrror 214 * if the implementation can not be instantiated. 215 */ 216 public static void parse(final java.net.URL url, final UIHandler handler) throws SAXException, javax.xml.parsers.ParserConfigurationException, java.io.IOException { 217 218 parse(new InputSource(url.toExternalForm()), handler); 219 } 220 221 private static void parse(final InputSource input, final UIParser recognizer) throws SAXException, javax.xml.parsers.ParserConfigurationException, java.io.IOException { 222 223 javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance(); 224 factory.setValidating(false); //the code was generated according DTD 225 factory.setNamespaceAware(false); //the code was generated according DTD 226 XMLReader parser = factory.newSAXParser().getXMLReader(); 227 parser.setContentHandler(recognizer); 228 parser.setErrorHandler(recognizer.getDefaultErrorHandler()); 229 if (recognizer.resolver != null) parser.setEntityResolver(recognizer.resolver); 230 parser.parse(input); 231 } 232 233 /** 234 * Creates default error handler used by this parser. 235 * 236 * @return org.xml.sax.ErrorHandler implementation 237 */ 238 protected ErrorHandler getDefaultErrorHandler() { 239 240 return new ErrorHandler() { 241 public void error(SAXParseException ex) throws SAXException { 242 if (context.isEmpty()) System.err.println("Missing DOCTYPE."); 243 throw ex; 244 } 245 246 public void fatalError(SAXParseException ex) throws SAXException { 247 throw ex; 248 } 249 250 public void warning(SAXParseException ex) throws SAXException { 251 // ignore 252 } 253 }; 254 255 } 256 }