1 28 package net.sf.jasperreports.engine.util; 29 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.net.URL ; 34 35 import javax.xml.parsers.DocumentBuilder ; 36 import javax.xml.parsers.DocumentBuilderFactory ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 39 import net.sf.jasperreports.engine.JRException; 40 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Node ; 45 import org.xml.sax.InputSource ; 46 import org.xml.sax.SAXException ; 47 48 54 public class JRXmlUtils 55 { 56 private static final Log log = LogFactory.getLog(JRXmlUtils.class); 57 58 59 66 public static Document parse(InputSource is) throws JRException 67 { 68 try 69 { 70 return createDocumentBuilder().parse(is); 71 } 72 catch (SAXException e) 73 { 74 throw new JRException("Failed to parse the xml document", e); 75 } 76 catch (IOException e) 77 { 78 throw new JRException("Failed to parse the xml document", e); 79 } 80 } 81 82 83 90 public static Document parse(String uri) throws JRException 91 { 92 return parse(new InputSource (uri)); 93 } 94 95 96 103 public static Document parse(File file) throws JRException 104 { 105 try 106 { 107 return createDocumentBuilder().parse(file); 108 } 109 catch (SAXException e) 110 { 111 throw new JRException("Failed to parse the xmlf document", e); 112 } 113 catch (IOException e) 114 { 115 throw new JRException("Failed to parse the xml document", e); 116 } 117 } 118 119 120 127 public static Document parse(InputStream is) throws JRException 128 { 129 return parse(new InputSource (is)); 130 } 131 132 133 140 public static Document parse(URL url) throws JRException 141 { 142 InputStream is = null; 143 try 144 { 145 is = url.openStream(); 146 return createDocumentBuilder().parse(is); 147 } 148 catch (SAXException e) 149 { 150 throw new JRException("Failed to parse the xmlf document", e); 151 } 152 catch (IOException e) 153 { 154 throw new JRException("Failed to parse the xml document", e); 155 } 156 finally 157 { 158 if (is != null) 159 { 160 try 161 { 162 is.close(); 163 } 164 catch (IOException e) 165 { 166 log.warn("Error closing stream of URL " + url, e); 167 } 168 } 169 } 170 } 171 172 173 179 public static DocumentBuilder createDocumentBuilder() throws JRException 180 { 181 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 182 dbf.setValidating(false); 183 dbf.setIgnoringComments(true); 184 185 try 186 { 187 return dbf.newDocumentBuilder(); 188 } 189 catch (ParserConfigurationException e) 190 { 191 throw new JRException("Failed to create a document builder factory", e); 192 } 193 } 194 195 196 203 public static Document createDocument(Node sourceNode) throws JRException 204 { 205 Document doc = JRXmlUtils.createDocumentBuilder().newDocument(); 206 Node source; 207 if (sourceNode.getNodeType() == Node.DOCUMENT_NODE) { 208 source = ((Document ) sourceNode).getDocumentElement(); 209 } else { 210 source = sourceNode; 211 } 212 213 Node node = doc.importNode(source, true); 214 doc.appendChild(node); 215 216 return doc; 217 } 218 } 219 | Popular Tags |