KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > definition > XmlParser


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.definition;
5
6 import org.w3c.dom.Document JavaDoc;
7 import org.xml.sax.EntityResolver JavaDoc;
8 import org.xml.sax.InputSource JavaDoc;
9 import org.xml.sax.SAXException JavaDoc;
10
11 import com.tc.aspectwerkz.exception.WrappedRuntimeException;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
22
23 /**
24  * Parses the XML definition file using <tt>dom4j</tt>.
25  *
26  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
27  */

28 public class XmlParser {
29   /**
30    * The current DTD public id. The matching dtd will be searched as a resource.
31    */

32   private final static String JavaDoc DTD_PUBLIC_ID = "-//AspectWerkz//DTD 2.0//EN";
33
34   /**
35    * The DTD alias, for better user experience.
36    */

37   private final static String JavaDoc DTD_PUBLIC_ID_ALIAS = "-//AspectWerkz//DTD//EN";
38
39   /**
40    * A handler to the DTD stream so that we are only using one file descriptor
41    */

42   private final static URL JavaDoc DTD_URL = XmlParser.class.getResource("/aspectwerkz2.dtd");
43
44   /**
45    * The AspectWerkz definitions.
46    */

47   private static Set JavaDoc s_definitions = null;
48
49   /**
50    * Parses the XML definition file not using the cache.
51    *
52    * @param loader the current class loader
53    * @param url the URL to the definition file
54    * @return the definition object
55    */

56   public static Set JavaDoc parseNoCache(final ClassLoader JavaDoc loader, final URL JavaDoc url) {
57     try {
58       // XXX: AspectJ aop.xml parsing is done like this:
59
// Definition definition = DocumentParser.parse(url);
60
// System.out.println("------------------> " + definition);
61

62       s_definitions = DocumentParser.parse(loader, createDocument(url));
63       return s_definitions;
64     } catch (Exception JavaDoc e) {
65       throw new WrappedRuntimeException(e);
66     }
67   }
68
69   /**
70    * Merges two DOM documents.
71    *
72    * @param document1 the first document
73    * @param document2 the second document
74    * @return the definition merged document
75    */

76 // public static Document mergeDocuments(final Document document1, final Document document2) {
77
// if ((document2 == null) && (document1 != null)) {
78
// return document1;
79
// }
80
// if ((document1 == null) && (document2 != null)) {
81
// return document2;
82
// }
83
// if ((document1 == null) && (document2 == null)) {
84
// return null;
85
// }
86
// try {
87
// Element root1 = document1.getRootElement();
88
// Element root2 = document2.getRootElement();
89
// for (Iterator it1 = root2.elementIterator(); it1.hasNext();) {
90
// Element element = (Element) it1.next();
91
// element.setParent(null);
92
// root1.add(element);
93
// }
94
// } catch (Exception e) {
95
// throw new WrappedRuntimeException(e);
96
// }
97
// return document1;
98
// }
99

100   /**
101    * Creates a DOM document.
102    *
103    * @param url the URL to the file containing the XML
104    * @return the DOM document
105    *
106    * @throws IOException
107    * @throws SAXException
108    * @throws ParserConfigurationException
109    */

110   public static Document JavaDoc createDocument(final URL JavaDoc url) throws IOException JavaDoc {
111     InputStream JavaDoc in = null;
112     try {
113       in = url.openStream();
114       return createDocument(new InputSource JavaDoc(in));
115     } finally {
116       try {
117         in.close();
118       } catch (Throwable JavaDoc t) {
119         // ignore
120
}
121     }
122   }
123
124   /**
125    * Creates a DOM document.
126    *
127    * @param string the string containing the XML
128    * @return the DOM document
129    * @throws DocumentException
130    */

131   public static Document JavaDoc createDocument(final String JavaDoc string) throws IOException JavaDoc {
132     return createDocument(new InputSource JavaDoc(new StringReader JavaDoc(string)));
133   }
134
135   public static Document JavaDoc createDocument(InputSource JavaDoc in) throws IOException JavaDoc {
136     try {
137       DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
138       DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
139       builder.setEntityResolver(getEntityResolver());
140       return builder.parse(in);
141     } catch (SAXException JavaDoc e) {
142       throw new IOException JavaDoc(e.toString());
143     } catch (ParserConfigurationException JavaDoc e) {
144       throw new IOException JavaDoc(e.toString());
145     }
146   }
147   
148
149   /**
150    * Sets the entity resolver which is created based on the DTD from in the root dir of the AspectWerkz distribution.
151    *
152    * @param reader the reader to set the resolver in
153    */

154   private static EntityResolver JavaDoc getEntityResolver() {
155     return new EntityResolver JavaDoc() {
156         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) {
157           if (publicId.equals(DTD_PUBLIC_ID) || publicId.equals(DTD_PUBLIC_ID_ALIAS)) {
158             try {
159               InputStream JavaDoc in = DTD_URL.openStream();
160               if (in != null)
161                 return new InputSource JavaDoc(in);
162             } catch (IOException JavaDoc ioex) {
163             }
164             System.err.println("AspectWerkz - WARN - could not open DTD");
165             return new InputSource JavaDoc(); // avoid null pointer exception }
166
} else {
167             System.err.println(
168                     "AspectWerkz - WARN - deprecated DTD " + publicId +
169                             " - consider upgrading to " + DTD_PUBLIC_ID);
170             return new InputSource JavaDoc(); // avoid null pointer exception
171
}
172         }
173       };
174   }
175 }
Popular Tags