Coverage Report - ca.uhn.hl7v2.util.XMLUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLUtils
78%
40/51
66%
4/6
1.909
XMLUtils$ClasspathResourceResolver
100%
9/9
100%
2/2
1.909
 
 1  
 /**
 2  
 The contents of this file are subject to the Mozilla Public License Version 1.1 
 3  
 (the "License"); you may not use this file except in compliance with the License. 
 4  
 You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
 5  
 Software distributed under the License is distributed on an "AS IS" basis, 
 6  
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
 7  
 specific language governing rights and limitations under the License. 
 8  
 
 9  
 The Original Code is "XMLUtils.java".  Description: 
 10  
 "XML utilities" 
 11  
 
 12  
 The Initial Developer of the Original Code is University Health Network. Copyright (C) 
 13  
 2001.  All Rights Reserved. 
 14  
 
 15  
 Contributor(s): ______________________________________. 
 16  
 
 17  
 Alternatively, the contents of this file may be used under the terms of the 
 18  
 GNU General Public License (the  "GPL"), in which case the provisions of the GPL are 
 19  
 applicable instead of those above.  If you wish to allow use of your version of this 
 20  
 file only under the terms of the GPL and not to allow others to use your version 
 21  
 of this file under the MPL, indicate your decision by deleting  the provisions above 
 22  
 and replace  them with the notice and other provisions required by the GPL License.  
 23  
 If you do not delete the provisions above, a recipient may use your version of 
 24  
 this file under either the MPL or the GPL. 
 25  
  */
 26  
 package ca.uhn.hl7v2.util;
 27  
 
 28  
 import java.io.InputStream;
 29  
 import java.io.StringWriter;
 30  
 import java.io.Writer;
 31  
 
 32  
 import org.w3c.dom.DOMConfiguration;
 33  
 import org.w3c.dom.DOMErrorHandler;
 34  
 import org.w3c.dom.DOMImplementation;
 35  
 import org.w3c.dom.Document;
 36  
 import org.w3c.dom.bootstrap.DOMImplementationRegistry;
 37  
 import org.w3c.dom.ls.DOMImplementationLS;
 38  
 import org.w3c.dom.ls.LSInput;
 39  
 import org.w3c.dom.ls.LSOutput;
 40  
 import org.w3c.dom.ls.LSParser;
 41  
 import org.w3c.dom.ls.LSResourceResolver;
 42  
 import org.w3c.dom.ls.LSSerializer;
 43  
 
 44  0
 public class XMLUtils {
 45  
 
 46  
     private static DOMImplementation IMPL;
 47  
 
 48  
     @SuppressWarnings("unchecked")
 49  
     public synchronized static <T> T getDOMImpl() {
 50  710
         if (IMPL == null) {
 51  
             try {
 52  5
                 DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
 53  5
                 IMPL = registry.getDOMImplementation("LS 3.0");
 54  0
             } catch (Exception e) {
 55  0
                 throw new RuntimeException(e);
 56  5
             }
 57  
         }
 58  710
         return (T) IMPL;
 59  
     }
 60  
 
 61  
     @SuppressWarnings("unchecked")
 62  
     public static <T> T getDOMImplUncached() {
 63  
         try {
 64  0
             DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
 65  0
             return (T) registry.getDOMImplementation("LS 3.0");
 66  0
         } catch (Exception e) {
 67  0
             throw new RuntimeException(e);
 68  
         }
 69  
     }
 70  
 
 71  
     public static Document parse(String s) {
 72  125
         return parse(s, false);
 73  
     }
 74  
 
 75  
     public static Document parse(String s, boolean validateIfSchema) {
 76  200
         DOMImplementationLS impl = getDOMImpl();
 77  200
         LSInput input = impl.createLSInput();
 78  200
         input.setStringData(s);
 79  200
         return parse(input, validateIfSchema);
 80  
     }
 81  
 
 82  
     public static Document parse(InputStream s, boolean validateIfSchema) {
 83  0
         DOMImplementationLS impl = getDOMImpl();
 84  0
         LSInput input = impl.createLSInput();
 85  0
         input.setByteStream(s);
 86  0
         return parse(input, validateIfSchema);
 87  
     }
 88  
 
 89  
     private static Document parse(LSInput input, boolean validateIfSchema) {
 90  200
         DOMImplementationLS impl = getDOMImpl();
 91  200
         LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
 92  200
         DOMConfiguration config = parser.getDomConfig();
 93  200
         config.setParameter("element-content-whitespace", false);
 94  200
         config.setParameter("namespaces", true);
 95  200
         config.setParameter("validate-if-schema", validateIfSchema);
 96  200
         return parser.parse(input);
 97  
     }
 98  
 
 99  
     public static void validate(Document d, String schema, DOMErrorHandler handler) {
 100  30
         DOMConfiguration config = d.getDomConfig();
 101  30
         config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema");
 102  30
         config.setParameter("validate", true);
 103  30
         config.setParameter("schema-location", schema);
 104  30
         config.setParameter("resource-resolver", new ClasspathResourceResolver());
 105  30
         config.setParameter("error-handler", handler);
 106  30
         d.normalizeDocument();
 107  30
     }
 108  
 
 109  
     public static String serialize(Document document, boolean prettyPrint) {
 110  150
         DOMImplementationLS impl = getDOMImpl();
 111  150
         LSSerializer serializer = impl.createLSSerializer();
 112  
         // document.normalizeDocument();
 113  150
         DOMConfiguration config = serializer.getDomConfig();
 114  150
         if (prettyPrint && config.canSetParameter("format-pretty-print", Boolean.TRUE)) {
 115  150
             config.setParameter("format-pretty-print", true);
 116  
         }
 117  150
         config.setParameter("xml-declaration", true);        
 118  150
         LSOutput output = impl.createLSOutput();
 119  150
         output.setEncoding("UTF-8");
 120  150
         Writer writer = new StringWriter();
 121  150
         output.setCharacterStream(writer);
 122  150
         serializer.write(document, output);
 123  150
         return writer.toString();
 124  
     }
 125  
 
 126  
     public static Document emptyDocument(String title) {
 127  125
         DOMImplementation impl = getDOMImpl();
 128  125
         Document doc = impl.createDocument("urn:hl7-org:v2xml", title, null);
 129  125
         return doc;
 130  
     }
 131  
 
 132  
     /**
 133  
      * This is an implementation of LSResourceResolver that can resolve XML schemas from the
 134  
      * classpath
 135  
      */
 136  
     private static class ClasspathResourceResolver implements LSResourceResolver {
 137  
         private DOMImplementationLS impl;
 138  
 
 139  30
         ClasspathResourceResolver() {
 140  30
             impl = getDOMImpl();
 141  30
         }
 142  
 
 143  
         public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId,
 144  
                 String baseURI) {
 145  120
             LSInput lsInput = impl.createLSInput();
 146  120
             InputStream is = getClass().getResourceAsStream("/" + systemId);
 147  120
             if (is == null)
 148  110
                 return null;
 149  10
             lsInput.setByteStream(is);
 150  10
             return lsInput;
 151  
         }
 152  
     }
 153  
 
 154  
 }