1 20 package org.apache.cactus.internal.server.runner; 21 22 import java.io.InputStream ; 23 import java.io.Reader ; 24 import java.io.Writer ; 25 26 import javax.xml.transform.Source ; 27 import javax.xml.transform.Templates ; 28 import javax.xml.transform.Transformer ; 29 import javax.xml.transform.TransformerConfigurationException ; 30 import javax.xml.transform.TransformerException ; 31 import javax.xml.transform.TransformerFactory ; 32 import javax.xml.transform.stream.StreamResult ; 33 import javax.xml.transform.stream.StreamSource ; 34 35 43 public class XMLTransformer 44 { 45 47 50 private static final String HTML_MIME_TYPE = "text/html"; 51 52 55 private static final String HTML_OUTPUT_METHOD = "html"; 56 57 60 private static final String TEXT_MIME_TYPE = "text/plain"; 61 62 65 private static final String TEXT_OUTPUT_METHOD = "text"; 66 67 70 private static final String XML_MIME_TYPE = "text/xml"; 71 72 75 private static final String XSL_OUTPUT_PROPERTY_METHOD = "method"; 76 77 79 82 private Templates templates = null; 83 84 88 private String contentType = XML_MIME_TYPE; 89 90 92 100 public XMLTransformer(InputStream theStylesheet) 101 throws TransformerConfigurationException 102 { 103 TransformerFactory transformerFactory = 109 TransformerFactory.newInstance(); 110 Source source = new StreamSource (theStylesheet); 111 this.templates = transformerFactory.newTemplates(source); 112 113 String outputMethod = this.templates.getOutputProperties().getProperty( 117 XSL_OUTPUT_PROPERTY_METHOD); 118 119 this.contentType = getContentType(outputMethod); 120 } 121 122 124 130 public String getContentType() 131 { 132 return this.contentType; 133 } 134 135 144 public void transform(Reader theXml, Writer theWriter) 145 throws TransformerException 146 { 147 Transformer transformer = this.templates.newTransformer(); 148 transformer.transform(new StreamSource (theXml), 149 new StreamResult (theWriter)); 150 } 151 152 154 159 private String getContentType(String theOutputMethod) 160 { 161 String contentType; 162 163 if (HTML_OUTPUT_METHOD.equals(theOutputMethod)) 164 { 165 contentType = HTML_MIME_TYPE; 166 } 167 else if (TEXT_OUTPUT_METHOD.equals(theOutputMethod)) 168 { 169 contentType = TEXT_MIME_TYPE; 170 } 171 else 172 { 173 contentType = XML_MIME_TYPE; 174 } 175 return contentType; 176 } 177 178 } 179 | Popular Tags |