1 23 24 package org.objectweb.fractal.adl.xml; 25 26 import org.objectweb.fractal.adl.Node; 27 import org.objectweb.fractal.adl.Parser; 28 import org.objectweb.fractal.adl.ParserException; 29 import org.objectweb.fractal.adl.xml.XMLNodeClassLoader; 30 import org.objectweb.fractal.adl.xml.DTDHandler; 31 import org.objectweb.fractal.adl.xml.XMLNode; 32 33 import java.io.InputStream ; 34 import java.io.IOException ; 35 import java.util.List ; 36 import java.util.ArrayList ; 37 import java.util.Map ; 38 import java.util.HashMap ; 39 import java.net.URL ; 40 import java.net.MalformedURLException ; 41 42 import javax.xml.parsers.ParserConfigurationException ; 43 import javax.xml.parsers.SAXParser ; 44 import javax.xml.parsers.SAXParserFactory ; 45 46 import org.xml.sax.helpers.DefaultHandler ; 47 import org.xml.sax.SAXException ; 48 import org.xml.sax.InputSource ; 49 import org.xml.sax.Attributes ; 50 import org.xml.sax.SAXNotRecognizedException ; 51 import org.xml.sax.Locator ; 52 import org.xml.sax.SAXParseException ; 53 54 57 58 public class XMLParser extends DefaultHandler implements Parser { 59 60 63 64 SAXParserFactory spf; 65 66 70 71 Map loaders; 72 73 76 77 String file; 78 79 82 83 XMLNodeClassLoader loader; 84 85 88 89 List stack; 90 91 94 95 XMLNode result; 96 97 100 101 Locator locator; 102 103 int line; 104 105 109 public XMLParser () { 110 this(true); 111 } 112 113 public XMLParser (boolean validate) { 114 loaders = new HashMap (); 115 stack = new ArrayList (); 116 spf = SAXParserFactory.newInstance(); 117 spf.setValidating(validate); 118 } 119 120 124 public Node parse (final InputStream is, final String name) 125 throws ParserException 126 { 127 file = name; 128 try { 129 SAXParser sp = spf.newSAXParser(); 130 sp.parse(new InputSource (is), this); 131 return result; 132 } catch (IOException e) { 133 throw new ParserException("Parser error (" + file + ":" + line + ")", e); 134 } catch (ParserConfigurationException e) { 135 throw new ParserException("Parser error (" + file + ":" + line + ")", e); 136 } catch (SAXException e) { 137 throw new ParserException("Parser error (" + file + ":" + line + ")", e); 138 } 139 } 140 141 145 public void setDocumentLocator (final Locator locator) { 146 super.setDocumentLocator(locator); 147 this.locator = locator; 148 } 149 150 public InputSource resolveEntity ( 151 final String publicId, 152 final String systemId) throws SAXException 153 { 154 loader = (XMLNodeClassLoader)loaders.get(systemId); 156 if (loader == null) { 157 loader = new XMLNodeClassLoader(getClass().getClassLoader()); 158 loaders.put(systemId, loader); 159 160 try { 161 InputStream is; 162 if (systemId.startsWith("classpath://")) { 164 is = getClass().getClassLoader().getResourceAsStream( 165 systemId.substring("classpath://".length())); 166 } else if (systemId.startsWith("file:")) { 167 is = new URL (systemId).openStream(); 168 } else { 169 throw new SAXNotRecognizedException ( 170 "Unrecognized system identifier: " + systemId); 171 } 172 new DTDHandler().checkDTD(is, loader); 174 } catch (MalformedURLException e) { 175 throw new SAXException ("Cannot find the DTD", e); 176 } catch (IOException e) { 177 throw new SAXException ("Cannot read the DTD", e); 178 } catch (ClassNotFoundException e) { 179 throw new SAXException ("Cannot check the DTD", e); 180 } 181 } 182 183 if (systemId.startsWith("classpath://")) { 184 return new InputSource (getClass().getClassLoader().getResourceAsStream( 185 systemId.substring("classpath://".length()))); 186 } else { 187 return null; 188 } 189 } 190 191 public void startElement ( 192 final String uri, 193 final String localName, 194 final String qualifiedName, 195 final Attributes attributes) throws SAXException 196 { 197 String xmlNodeClassName = loader.getASTClassName(qualifiedName); 198 XMLNode o; 199 try { 200 o = (XMLNode)loader.loadClass(xmlNodeClassName).newInstance(); 201 } catch (Exception e) { 202 throw new SAXException ("Internal error", e); 203 } 204 o.astSetSource(file + ":" + locator.getLineNumber()); 205 o.xmlSetAttributes(attributes); 206 if (stack.size() == 0) { 207 result = o; 208 } else { 209 Object p = stack.get(stack.size() - 1); 210 ((XMLNode)p).xmlAddNode(qualifiedName, o); 211 } 212 stack.add(o); 213 } 214 215 public void endElement ( 216 final String uri, 217 final String localName, 218 final String qualifiedName) throws SAXException 219 { 220 stack.remove(stack.size() - 1); 221 } 222 223 public void error (final SAXParseException e) throws SAXException { 224 line = locator.getLineNumber(); 225 throw e; 226 } 227 228 public void fatalError (final SAXParseException e) throws SAXException { 229 line = locator.getLineNumber(); 230 throw e; 231 } 232 } 233 | Popular Tags |