List of usage examples for javax.xml.transform TransformerFactory getAssociatedStylesheet
public abstract Source getAssociatedStylesheet(Source source, String media, String title, String charset) throws TransformerConfigurationException;
From source file:UseStylesheetPI.java
public static void main(String[] args) throws TransformerException, TransformerConfigurationException { String media = null, title = null, charset = null; try {/*from w w w .j a v a 2 s. c o m*/ TransformerFactory tFactory = TransformerFactory.newInstance(); Source stylesheet = tFactory.getAssociatedStylesheet(new StreamSource("fooX.xml"), media, title, charset); Transformer transformer = tFactory.newTransformer(stylesheet); transformer.transform(new StreamSource("fooX.xml"), new StreamResult(new java.io.FileOutputStream("foo.out"))); System.out.println("************* The result is in foo.out *************"); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.n52.wps.ags.algorithmpackage.AlgorithmPackage.java
/** * Generates an XML file. Uses a ProcessDescription file and a XSLT file to generate an * AlgorithmDescription.// ww w .j a va 2 s . co m * * @param xmlFile - the ProcessDescription in XML * @param xsltFile - the transformation rules in XSLT * @throws Exception - the exceptions */ private static StringReader generateAlgorithmDescription(File xmlFile) { Source xmlSource = new StreamSource(xmlFile); TransformerFactory transFact = TransformerFactory.newInstance(); StringWriter sw = new StringWriter(); StreamResult transformResult = new StreamResult(sw); try { Source xsltSource = transFact.getAssociatedStylesheet(xmlSource, null, null, null); Transformer trans = transFact.newTransformer(xsltSource); trans.transform(xmlSource, transformResult); StringReader sr = new StringReader(sw.toString()); return sr; } catch (TransformerException e) { LOGGER.error("Error evaluating ProcessDescription XML."); e.printStackTrace(); } return null; }
From source file:ORG.oclc.os.SRW.SRUServerTester.java
public String sruRead(String initialURL) { out('\n');/*from w w w.j a va2s. c om*/ out(" trying: "); out(initialURL); out('\n'); numTests++; URL url; try { url = new URL(initialURL); } catch (java.net.MalformedURLException e) { out("</pre><pre class='red'>"); out("test failed: using URL: "); out(e.getMessage()); out('\n'); out("</pre><pre>"); return null; } HttpURLConnection huc; try { huc = (HttpURLConnection) url.openConnection(); } catch (IOException e) { out("</pre><pre class='red'>"); out("test failed: using URL: "); out(e.getMessage()); out('\n'); out("</pre><pre>"); return null; } String contentType = huc.getContentType(); if (contentType == null || (!contentType.contains("text/xml") && !contentType.contains("application/xml"))) { out(" ** Warning: Content-Type not set to text/xml or application/xml"); out('\n'); out(" Content-type: "); out(contentType); out('\n'); numWarns++; } InputStream urlStream; try { urlStream = huc.getInputStream(); } catch (java.io.IOException e) { out("</pre><pre class='red'>"); out("test failed: opening URL: "); out(e.getMessage()); out('\n'); out("</pre><pre>"); return null; } BufferedReader in = new BufferedReader(new InputStreamReader(urlStream)); boolean xml = true; String href = null, inputLine; StringBuilder content = new StringBuilder(); Transformer transformer = null; try { inputLine = in.readLine(); } catch (java.io.IOException e) { out("</pre><pre class='red'>"); out("test failed: reading first line of response: "); out(e.getMessage()); out('\n'); out("</pre><pre>"); return null; } if (inputLine == null) { out("</pre><pre class='red'>"); out("test failed: No input read from URL"); out('\n'); out("</pre><pre>"); return null; } if (!inputLine.startsWith("<?xml ")) { xml = false; content.append(inputLine); } if (xml) { // normally, you'd expect to read the next line of input here // but some servers don't put a newline after the initial <?xml ?> int offset = inputLine.indexOf('>'); if (offset + 2 < inputLine.length()) { inputLine = inputLine.substring(offset + 1); offset = inputLine.indexOf('<'); if (offset > 0) inputLine = inputLine.substring(offset); } else try { inputLine = in.readLine(); // I ran into a server that put an empty line before the stylesheet reference while (inputLine.length() == 0) inputLine = in.readLine(); } catch (java.io.IOException e) { out("</pre><pre class='red'>"); out("test failed: reading response: "); out(e.getMessage()); out('\n'); out("</pre><pre>"); return null; } if (inputLine.startsWith("<?xml-stylesheet ")) { href = (inputLine.substring(inputLine.indexOf("href=") + 6)); href = href.substring(0, href.indexOf('"')); transformer = transformers.get(href); if (stylesheets.get(href) == null) try { // never seen this stylesheet before out(" reading stylesheet: "); out(href); out('\n'); out(" from source: "); out(url.toString()); out('\n'); StreamSource source = new StreamSource(url.toString()); TransformerFactory tFactory = TransformerFactory.newInstance(); Source stylesht = tFactory.getAssociatedStylesheet(source, null, null, null); transformer = tFactory.newTransformer(stylesht); transformers.put(href, transformer); } catch (TransformerConfigurationException e) { log.error(e, e); out("</pre><pre class='red'>"); out("unable to load stylesheet: "); out(e.getMessage()); out('\n'); out("</pre><pre>"); } stylesheets.put(href, href); } else content.append(inputLine); } try { while ((inputLine = in.readLine()) != null) content.append(inputLine); } catch (java.io.IOException e) { out("</pre><pre class='red'>"); out("test failed: reading response: "); out(e.getMessage()); out('\n'); out("</pre><pre>"); return null; } // if(!xml) { // out("test failed: response was not an XML record");out('\n'); // out(content.toString());out('\n'); // return null; // } String contentStr = content.toString(); if (transformer != null) { StreamSource streamXMLRecord = new StreamSource(new StringReader(contentStr)); StringWriter xmlRecordWriter = new StringWriter(); try { transformer.transform(streamXMLRecord, new StreamResult(xmlRecordWriter)); out(" successfully applied stylesheet '"); out(href); out("'"); out('\n'); } catch (javax.xml.transform.TransformerException e) { out("</pre><pre class='red'>"); out("unable to apply stylesheet '"); out(href); out("'to response: "); out(e.getMessage()); out('\n'); out("</pre><pre>"); log.error(e, e); transformer.reset(); } transformer.reset(); } return contentStr; }