1 13 package com.tonbeller.wcf.utils; 14 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.io.Writer ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.util.Hashtable ; 21 import java.util.Properties ; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.http.HttpSession ; 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.FactoryConfigurationError ; 28 import javax.xml.parsers.ParserConfigurationException ; 29 import javax.xml.transform.Result ; 30 import javax.xml.transform.Source ; 31 import javax.xml.transform.Templates ; 32 import javax.xml.transform.Transformer ; 33 import javax.xml.transform.TransformerConfigurationException ; 34 import javax.xml.transform.TransformerException ; 35 import javax.xml.transform.TransformerFactory ; 36 import javax.xml.transform.TransformerFactoryConfigurationError ; 37 import javax.xml.transform.dom.DOMSource ; 38 import javax.xml.transform.stream.StreamResult ; 39 import javax.xml.transform.stream.StreamSource ; 40 41 import org.apache.log4j.Logger; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Node ; 44 import org.xml.sax.InputSource ; 45 import org.xml.sax.SAXException ; 46 47 51 public class XmlUtils { 52 53 private static final String WEBKEY = XmlUtils.class.getName(); 54 private static Logger logger = Logger.getLogger(XmlUtils.class); 55 56 private XmlUtils() { 57 } 58 59 64 private Hashtable templatesCache = new Hashtable (); 65 66 71 public Transformer getTransformer(ServletContext ctx, String xslUri, boolean xslCache) { 72 synchronized (templatesCache) { 73 try { 74 Templates templates = null; 75 if (xslCache) 76 templates = (Templates ) templatesCache.get(xslUri); 77 if (templates == null) { 78 TransformerFactory tf = TransformerFactory.newInstance(); 79 URL url = ctx.getResource(xslUri); 80 if (url == null) 81 throw new IllegalArgumentException ("stylesheet \"" + xslUri + "\" not found"); 82 83 StreamSource ss = new StreamSource (url.toExternalForm()); 84 if ("file".equals(url.getProtocol())) { 86 File f = new File (url.getFile()); 87 ss.setSystemId(f); 88 } 89 templates = tf.newTemplates(ss); 90 if (xslCache) 91 templatesCache.put(xslUri, templates); 92 } 93 return templates.newTransformer(); 94 } catch (TransformerConfigurationException e) { 95 throw new SoftException(e); 96 } catch (MalformedURLException e) { 97 throw new SoftException(e); 98 } 99 } 100 } 101 102 108 public static Transformer getTransformer(HttpSession session, String xslUri, boolean xslCache) { 109 return instance(session).getTransformer(session.getServletContext(), xslUri, xslCache); 110 } 111 112 public static DocumentBuilder getParser() { 113 try { 114 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 115 dbf.setValidating(false); 116 dbf.setExpandEntityReferences(true); 117 return dbf.newDocumentBuilder(); 118 } catch (FactoryConfigurationError e) { 119 throw new SoftException(e); 120 } catch (ParserConfigurationException e) { 121 throw new SoftException(e); 122 } 123 } 124 125 public static Document createDocument() { 126 try { 127 return getParser().newDocument(); 128 } catch (FactoryConfigurationError e) { 129 throw new SoftException(e); 130 } 131 } 132 133 public static void print(Node node, Writer out, Properties p) { 134 try { 135 TransformerFactory tf = TransformerFactory.newInstance(); 136 Source src = new DOMSource (node); 137 Result dest = new StreamResult (out); 138 Transformer t = tf.newTransformer(); 139 if (p != null) 140 t.setOutputProperties(p); 141 t.transform(src, dest); 142 } catch (TransformerConfigurationException e) { 143 throw new SoftException(e); 144 } catch (TransformerFactoryConfigurationError e) { 145 throw new SoftException(e); 146 } catch (TransformerException e) { 147 throw new SoftException(e); 148 } 149 } 150 151 public static void print(Node node, Writer out) { 152 print(node, out, null); 153 } 154 155 public static synchronized XmlUtils instance(HttpSession session) { 156 XmlUtils service = (XmlUtils) session.getAttribute(WEBKEY); 157 if (service == null) { 158 service = new XmlUtils(); 159 session.setAttribute(WEBKEY, service); 160 } 161 return service; 162 } 163 164 168 public static Document getDocument(Node node) { 169 if (node.getNodeType() == Node.DOCUMENT_NODE) 170 return (Document ) node; 171 return node.getOwnerDocument(); 172 } 173 174 public static Document parse(URL url) { 175 try { 176 InputSource src = new InputSource (url.toExternalForm()); 177 return getParser().parse(src); 178 } catch (IOException e) { 179 throw new SoftException(e); 180 } catch (SAXException e) { 181 throw new SoftException(e); 182 } 183 } 184 185 189 public static String escapeXml(String s) { 190 if (s == null) 191 return null; 192 char[] arr = s.toCharArray(); 193 StringBuffer sb = new StringBuffer (); 194 for (int i = 0; i < arr.length; i++) { 195 switch(arr[i]) { 196 case '>': 197 sb.append(">"); 198 break; 199 case '<': 200 sb.append("<"); 201 break; 202 case '&': 203 sb.append("&"); 204 break; 205 case '"': 206 sb.append("""); 207 break; 208 default: 209 sb.append(arr[i]); 210 } 211 } 212 return sb.toString(); 213 } 214 215 216 } 217 | Popular Tags |