View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.ws.security.util;
21  
22  import org.w3c.dom.Document;
23  import org.w3c.dom.Element;
24  import org.w3c.dom.Node;
25  import org.xml.sax.InputSource;
26  
27  import javax.xml.transform.Source;
28  import javax.xml.transform.Transformer;
29  import javax.xml.transform.TransformerFactory;
30  import javax.xml.transform.dom.DOMSource;
31  import javax.xml.transform.sax.SAXSource;
32  import javax.xml.transform.stream.StreamResult;
33  import javax.xml.transform.stream.StreamSource;
34  import java.io.ByteArrayInputStream;
35  import java.io.ByteArrayOutputStream;
36  import java.io.OutputStream;
37  
38  public final class XMLUtils {
39      
40      private static final org.apache.commons.logging.Log LOG = 
41          org.apache.commons.logging.LogFactory.getLog(XMLUtils.class);
42      private static final boolean DO_DEBUG = LOG.isDebugEnabled();
43      
44      private XMLUtils() {
45          // Complete
46      }
47      
48      public static String PrettyDocumentToString(Document doc) {
49          ByteArrayOutputStream baos = new ByteArrayOutputStream();
50          ElementToStream(doc.getDocumentElement(), baos);
51          return new String(baos.toByteArray());
52      }
53  
54      public static void ElementToStream(Element element, OutputStream out) {
55          try {
56              DOMSource source = new DOMSource(element);
57              StreamResult result = new StreamResult(out);
58              TransformerFactory transFactory = TransformerFactory.newInstance();
59              Transformer transformer = transFactory.newTransformer();
60              transformer.transform(source, result);
61          } catch (Exception ex) {
62              if (DO_DEBUG) {
63                  LOG.debug(ex.getMessage(), ex);
64              }
65          }
66      }
67  
68      /**
69       * Utility to get the bytes uri
70       *
71       * @param source the resource to get
72       */
73      public static InputSource sourceToInputSource(Source source) {
74          if (source instanceof SAXSource) {
75              return ((SAXSource) source).getInputSource();
76          } else if (source instanceof DOMSource) {
77              ByteArrayOutputStream baos = new ByteArrayOutputStream();
78              Node node = ((DOMSource) source).getNode();
79              if (node instanceof Document) {
80                  node = ((Document) node).getDocumentElement();
81              }
82              Element domElement = (Element) node;
83              ElementToStream(domElement, baos);
84              InputSource isource = new InputSource(source.getSystemId());
85              isource.setByteStream(new ByteArrayInputStream(baos.toByteArray()));
86              return isource;
87          } else if (source instanceof StreamSource) {
88              StreamSource ss = (StreamSource) source;
89              InputSource isource = new InputSource(ss.getSystemId());
90              isource.setByteStream(ss.getInputStream());
91              isource.setCharacterStream(ss.getReader());
92              isource.setPublicId(ss.getPublicId());
93              return isource;
94          } else {
95              return getInputSourceFromURI(source.getSystemId());
96          }
97      }
98  
99      /**
100      * Utility to get the bytes uri.
101      * Does NOT handle authenticated URLs,
102      * use getInputSourceFromURI(uri, username, password)
103      *
104      * @param uri the resource to get
105      */
106     public static InputSource getInputSourceFromURI(String uri) {
107         return new InputSource(uri);
108     }
109     
110 }