1 4 package com.tc.aspectwerkz.definition; 5 6 import org.w3c.dom.Document ; 7 import org.xml.sax.EntityResolver ; 8 import org.xml.sax.InputSource ; 9 import org.xml.sax.SAXException ; 10 11 import com.tc.aspectwerkz.exception.WrappedRuntimeException; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.StringReader ; 16 import java.net.URL ; 17 import java.util.Set ; 18 19 import javax.xml.parsers.DocumentBuilder ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 import javax.xml.parsers.ParserConfigurationException ; 22 23 28 public class XmlParser { 29 32 private final static String DTD_PUBLIC_ID = "-//AspectWerkz//DTD 2.0//EN"; 33 34 37 private final static String DTD_PUBLIC_ID_ALIAS = "-//AspectWerkz//DTD//EN"; 38 39 42 private final static URL DTD_URL = XmlParser.class.getResource("/aspectwerkz2.dtd"); 43 44 47 private static Set s_definitions = null; 48 49 56 public static Set parseNoCache(final ClassLoader loader, final URL url) { 57 try { 58 62 s_definitions = DocumentParser.parse(loader, createDocument(url)); 63 return s_definitions; 64 } catch (Exception e) { 65 throw new WrappedRuntimeException(e); 66 } 67 } 68 69 76 100 110 public static Document createDocument(final URL url) throws IOException { 111 InputStream in = null; 112 try { 113 in = url.openStream(); 114 return createDocument(new InputSource (in)); 115 } finally { 116 try { 117 in.close(); 118 } catch (Throwable t) { 119 } 121 } 122 } 123 124 131 public static Document createDocument(final String string) throws IOException { 132 return createDocument(new InputSource (new StringReader (string))); 133 } 134 135 public static Document createDocument(InputSource in) throws IOException { 136 try { 137 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 138 DocumentBuilder builder = factory.newDocumentBuilder(); 139 builder.setEntityResolver(getEntityResolver()); 140 return builder.parse(in); 141 } catch (SAXException e) { 142 throw new IOException (e.toString()); 143 } catch (ParserConfigurationException e) { 144 throw new IOException (e.toString()); 145 } 146 } 147 148 149 154 private static EntityResolver getEntityResolver() { 155 return new EntityResolver () { 156 public InputSource resolveEntity(String publicId, String systemId) { 157 if (publicId.equals(DTD_PUBLIC_ID) || publicId.equals(DTD_PUBLIC_ID_ALIAS)) { 158 try { 159 InputStream in = DTD_URL.openStream(); 160 if (in != null) 161 return new InputSource (in); 162 } catch (IOException ioex) { 163 } 164 System.err.println("AspectWerkz - WARN - could not open DTD"); 165 return new InputSource (); } else { 167 System.err.println( 168 "AspectWerkz - WARN - deprecated DTD " + publicId + 169 " - consider upgrading to " + DTD_PUBLIC_ID); 170 return new InputSource (); } 172 } 173 }; 174 } 175 } | Popular Tags |