View Javadoc
1   /**
2    *    Copyright 2011 meltmedia
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  package org.xchain.namespaces.core;
17  
18  import java.net.URL;
19  import java.io.InputStream;
20  import javax.xml.parsers.DocumentBuilderFactory;
21  import javax.xml.parsers.DocumentBuilder;
22  import org.w3c.dom.Document;
23  import org.xchain.framework.net.UrlFactory;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * @author Christian Trimble
29   * @author Jason Rose
30   * @author Josh Kennedy
31   */
32  public class DocumentFunctions
33  {
34    public static Logger log = LoggerFactory.getLogger( DocumentFunctions.class );
35      public static Document document( String systemId )
36      {
37        URL url = null;
38  
39        try {
40          url = UrlFactory.getInstance().newUrl(systemId);
41        }
42        catch( Exception e ) {
43          if( log.isDebugEnabled() ) {
44            log.debug("Could not create document for system id '"+systemId+".", e);
45          }
46          throw new RuntimeException("Could not create document.", e);
47        }
48  
49        return document(url);
50      }
51  
52      public static Document document( URL url )
53      {
54        InputStream documentIn = null;
55        try {
56        // get the document builder factory.
57        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
58  
59        // configure the factory to be namespace aware.
60        factory.setNamespaceAware( true );
61  
62        // get the document builder.
63        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
64  
65        // create an input stream for the document.
66        documentIn = url.openStream();
67  
68        // build the document.
69        Document document = documentBuilder.parse(documentIn);
70  
71        // return the document.
72        return document;
73        }
74        catch( Exception e ) {
75          if( log.isDebugEnabled() ) {
76            log.debug("Could not create document for system id '"+url.toExternalForm()+".", e);
77          }
78          throw new RuntimeException("Could not create document.", e);
79  
80        }
81        finally {
82          if( documentIn != null ) {
83            try {
84              documentIn.close();
85            }
86            catch( Exception e ) {
87              log.warn("Failed to close input stream.", e);
88            }
89          }
90        }
91      }
92  }