1 34 package org.dspace.app.webui.util; 35 36 import java.io.File ; 37 import java.io.IOException ; 38 import java.io.StringWriter ; 39 import java.util.Enumeration ; 40 import java.util.Hashtable ; 41 42 import javax.xml.parsers.DocumentBuilder ; 43 import javax.xml.parsers.DocumentBuilderFactory ; 44 import javax.xml.parsers.ParserConfigurationException ; 45 import javax.xml.transform.Transformer ; 46 import javax.xml.transform.TransformerFactory ; 47 import javax.xml.transform.dom.DOMResult ; 48 import javax.xml.transform.dom.DOMSource ; 49 import javax.xml.transform.stream.StreamResult ; 50 import javax.xml.transform.stream.StreamSource ; 51 52 import org.w3c.dom.Document ; 53 import org.xml.sax.SAXException ; 54 55 62 public class XMLUtil 63 { 64 65 75 public static Document loadXML(String filename) throws IOException , 76 ParserConfigurationException , SAXException 77 { 78 DocumentBuilder builder = DocumentBuilderFactory.newInstance() 79 .newDocumentBuilder(); 80 return builder.parse(new File (filename)); 81 } 82 83 93 public static Document transformDocument(Document xmlDocument, 94 String xsltFilename) throws Exception 95 { 96 return transformDocument(xmlDocument, new Hashtable (), xsltFilename); 97 } 98 99 112 public static Document transformDocument(Document xmlDocument, 113 Hashtable parameters, String xsltFilename) throws Exception 114 { 115 116 Transformer transformer = TransformerFactory.newInstance() 118 .newTransformer(new StreamSource (xsltFilename)); 119 120 if (parameters != null) 122 { 123 Enumeration keys = parameters.keys(); 124 while (keys.hasMoreElements()) 125 { 126 String key = (String ) keys.nextElement(); 127 String value = (String ) parameters.get(key); 128 transformer.setParameter(key, value); 129 } 130 131 } 132 133 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); 135 dFactory.setNamespaceAware(true); 136 DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); 137 Document dstDocument = dBuilder.newDocument(); 138 139 DOMResult domResult = new DOMResult (dstDocument); 140 141 transformer.transform(new DOMSource (xmlDocument), domResult); 143 return dstDocument; 145 } 146 147 161 public static String transformDocumentAsString(Document xmlDocument, 162 Hashtable parameters, String xsltFilename) throws Exception 163 { 164 165 Transformer transformer = TransformerFactory.newInstance() 167 .newTransformer(new StreamSource (xsltFilename)); 168 169 if (parameters != null) 171 { 172 Enumeration keys = parameters.keys(); 173 while (keys.hasMoreElements()) 174 { 175 String key = (String ) keys.nextElement(); 176 String value = (String ) parameters.get(key); 177 transformer.setParameter(key, value); 178 } 179 180 } 181 182 StringWriter stringWriter = new StringWriter (); 183 StreamResult streamResult = new StreamResult (stringWriter); 184 185 transformer.transform(new DOMSource (xmlDocument), streamResult); 187 return stringWriter.toString(); 189 } 190 191 201 public static String transformDocumentAsString(Document xmlDocument, 202 String xsltFilename) throws Exception 203 { 204 Transformer transformer = TransformerFactory.newInstance() 206 .newTransformer(new StreamSource (xsltFilename)); 207 208 StringWriter stringWriter = new StringWriter (); 209 StreamResult streamResult = new StreamResult (stringWriter); 210 211 transformer.transform(new DOMSource (xmlDocument), streamResult); 213 return stringWriter.toString(); 215 } 216 217 } 218 | Popular Tags |