1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.geronimo.ews.ws4j2ee.utils;
18
19 import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.w3c.dom.Document;
23 import org.xml.sax.EntityResolver;
24 import org.xml.sax.InputSource;
25 import org.xml.sax.SAXException;
26
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import java.io.IOException;
30 import java.io.InputStream;
31
32 /***
33 * @author hemapani@opensource.lk
34 */
35 public class EWSUtils {
36 protected static Log log =
37 LogFactory.getLog(EWSUtils.class.getName());
38 public static Document createDocument(InputStream in) throws GenerationFault {
39 try {
40 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
41 dbf.setNamespaceAware(true);
42 dbf.setValidating(false);
43 dbf.setExpandEntityReferences(false);
44 DocumentBuilder db = dbf.newDocumentBuilder();
45 EntityResolver er = new EntityResolver() {
46 public InputSource resolveEntity(String publicId,
47 String systemId)
48 throws SAXException, IOException {
49 InputStream is = null;
50 if ("http://java.sun.com/dtd/ejb-jar_2_0.dtd".equalsIgnoreCase(systemId)) {
51 return getInputSource(EWSUtils.class.getClassLoader().getResourceAsStream("ejb-jar_2_0.dtd"));
52 } else if ("http://java.sun.com/dtd/web-app_2_3.dtd".equalsIgnoreCase(systemId))
53 return getInputSource(EWSUtils.class.getClassLoader().getResourceAsStream("web-app_2_3.dtd"));
54 return null;
55 }
56
57 private InputSource getInputSource(InputStream is) throws IOException {
58 if (is == null)
59 throw new IOException("error at the project set up can not find entity");
60 return new InputSource(is);
61 }
62 };
63 db.setEntityResolver(er);
64 return db.parse(in);
65 } catch (Exception e) {
66 log.error(e);
67 throw GenerationFault.createGenerationFault(e);
68 }
69 }
70 }