1 8 package org.codehaus.aspectwerkz.definition; 9 10 import org.codehaus.aspectwerkz.exception.DefinitionException; 11 import org.codehaus.aspectwerkz.exception.WrappedRuntimeException; 12 import org.dom4j.Document; 13 import org.dom4j.DocumentException; 14 import org.dom4j.Element; 15 import org.dom4j.DocumentHelper; 16 import org.dom4j.io.SAXReader; 17 import org.xml.sax.EntityResolver ; 18 import org.xml.sax.InputSource ; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.FileInputStream ; 24 import java.io.BufferedInputStream ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 31 36 public class XmlParser { 37 40 private final static String DTD_PUBLIC_ID = "-//AspectWerkz//DTD 2.0//EN"; 41 42 45 private final static String DTD_PUBLIC_ID_ALIAS = "-//AspectWerkz//DTD//EN"; 46 47 50 private final static InputStream DTD_STREAM = XmlParser.class.getResourceAsStream("/aspectwerkz2.dtd"); 51 52 55 private static File s_timestamp = new File (".timestamp"); 56 57 60 private static Set s_definitions = null; 61 62 82 100 115 151 168 175 public static Set parseNoCache(final ClassLoader loader, final URL url) { 176 try { 177 Document document = createDocument(url); 178 s_definitions = DocumentParser.parse(loader, document); 179 return s_definitions; 180 } catch (Exception e) { 181 throw new WrappedRuntimeException(e); 182 } 183 } 184 185 192 public static Document mergeDocuments(final Document document1, final Document document2) { 193 if ((document2 == null) && (document1 != null)) { 194 return document1; 195 } 196 if ((document1 == null) && (document2 != null)) { 197 return document2; 198 } 199 if ((document1 == null) && (document2 == null)) { 200 return null; 201 } 202 try { 203 Element root1 = document1.getRootElement(); 204 Element root2 = document2.getRootElement(); 205 for (Iterator it1 = root2.elementIterator(); it1.hasNext();) { 206 Element element = (Element) it1.next(); 207 element.setParent(null); 208 root1.add(element); 209 } 210 } catch (Exception e) { 211 throw new WrappedRuntimeException(e); 212 } 213 return document1; 214 } 215 216 223 public static Document createDocument(final URL url) throws DocumentException { 224 SAXReader reader = new SAXReader(); 225 setEntityResolver(reader); 226 InputStream in = null; 227 try { 228 in = url.openStream(); 229 return reader.read(in); 230 } catch (IOException e) { 231 throw new DocumentException(e); 232 } finally { 233 try {in.close();} catch (Throwable t) {;} 234 } 235 } 236 237 250 257 public static Document createDocument(final String string) throws DocumentException { 258 return DocumentHelper.parseText(string); 259 } 260 261 266 private static void setEntityResolver(final SAXReader reader) { 267 EntityResolver resolver = new EntityResolver () { 268 public InputSource resolveEntity(String publicId, String systemId) { 269 if (publicId.equals(DTD_PUBLIC_ID) || publicId.equals(DTD_PUBLIC_ID_ALIAS)) { 270 InputStream in = DTD_STREAM; 271 if (in == null) { 272 System.err.println("AspectWerkz - WARN - could not open DTD"); 273 return new InputSource (); 274 } else { 275 return new InputSource (in); 276 } 277 } else { 278 System.err.println( 279 "AspectWerkz - WARN - deprecated DTD " 280 + publicId 281 + " - consider upgrading to " 282 + DTD_PUBLIC_ID 283 ); 284 return new InputSource (); } 286 } 287 }; 288 reader.setEntityResolver(resolver); 289 } 290 291 297 private static boolean isNotUpdated(final File definitionFile) { 298 return (definitionFile.lastModified() < getParsingTimestamp()) && (s_definitions != null); 299 } 300 301 304 private static void setParsingTimestamp() { 305 final long newModifiedTime = System.currentTimeMillis(); 306 s_timestamp.setLastModified(newModifiedTime); 307 } 308 309 314 private static long getParsingTimestamp() { 315 final long modifiedTime = s_timestamp.lastModified(); 316 if (modifiedTime == 0L) { 317 try { 319 s_timestamp.createNewFile(); 320 } catch (IOException e) { 321 throw new RuntimeException ("could not create timestamp file: " + s_timestamp.getAbsolutePath()); 322 } 323 } 324 return modifiedTime; 325 } 326 } | Popular Tags |