View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }