001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.util;
021:
022: import org.w3c.dom.Document;
023:
024: import javax.xml.transform.Result;
025: import javax.xml.transform.Source;
026: import javax.xml.transform.Transformer;
027: import javax.xml.transform.TransformerException;
028: import javax.xml.transform.TransformerFactory;
029: import javax.xml.transform.TransformerFactoryConfigurationError;
030: import javax.xml.transform.URIResolver;
031: import javax.xml.transform.dom.DOMSource;
032: import javax.xml.transform.stream.StreamResult;
033: import javax.xml.transform.stream.StreamSource;
034: import java.io.InputStream;
035: import java.io.OutputStream;
036:
037: public class XSLTTemplateProcessor {
038:
039: /**
040: * Parses an XML stream with an XSL stream
041: *
042: * @param out Stream to write the output
043: * @param xmlStream Source XML stream
044: * @param xsltStream Source XSL stream
045: * @throws TransformerFactoryConfigurationError
046: *
047: * @throws TransformerException
048: */
049: public static void parse(OutputStream out, InputStream xmlStream,
050: InputStream xsltStream)
051: throws TransformerFactoryConfigurationError,
052: TransformerException {
053: Source xmlSource = new StreamSource(xmlStream);
054: Source xsltSource = new StreamSource(xsltStream);
055: Result result = new StreamResult(out);
056: Transformer transformer = TransformerFactory.newInstance()
057: .newTransformer(xsltSource);
058: transformer.transform(xmlSource, result);
059:
060: }
061:
062: /**
063: * Parses an XML stream with an XSL stream
064: *
065: * @param out Stream to write the output
066: * @param doc
067: * @param transformer
068: * @throws TransformerFactoryConfigurationError
069: *
070: * @throws TransformerException
071: */
072: public static void parse(OutputStream out, Document doc,
073: Transformer transformer)
074: throws TransformerFactoryConfigurationError,
075: TransformerException {
076: Source xmlSource = new DOMSource(doc);
077: Result result = new StreamResult(out);
078: transformer.transform(xmlSource, result);
079:
080: }
081:
082: /**
083: * @param out
084: * @param document
085: * @param xsltStream
086: * @throws TransformerFactoryConfigurationError
087: *
088: * @throws TransformerException
089: */
090: public static void parse(OutputStream out, Document document,
091: InputStream xsltStream)
092: throws TransformerFactoryConfigurationError,
093: TransformerException {
094: Source xsltSource = new StreamSource(xsltStream);
095: Transformer transformer = TransformerFactory.newInstance()
096: .newTransformer(xsltSource);
097: parse(out, document, transformer);
098:
099: }
100:
101: /**
102: * @param out
103: * @param document
104: * @param xsltStream
105: * @throws TransformerFactoryConfigurationError
106: *
107: * @throws TransformerException
108: */
109: public static void parse(OutputStream out, Document document,
110: InputStream xsltStream, URIResolver customResolver)
111: throws TransformerFactoryConfigurationError,
112: TransformerException {
113: parse(out, document, xsltStream, customResolver, false);
114: }
115:
116: /**
117: * @param out
118: * @param document
119: * @param xsltStream
120: * @throws TransformerFactoryConfigurationError
121: *
122: * @throws TransformerException
123: */
124: public static void parse(OutputStream out, Document document,
125: InputStream xsltStream, URIResolver customResolver,
126: boolean pretty)
127: throws TransformerFactoryConfigurationError,
128: TransformerException {
129: Source xsltSource = new StreamSource(xsltStream);
130: TransformerFactory transformerFactory = TransformerFactory
131: .newInstance();
132: if (pretty) {
133: try {
134: transformerFactory.setAttribute("indent-number",
135: new Integer(2));
136: } catch (Exception e) {
137: }
138: }
139: if (customResolver != null) {
140: transformerFactory.setURIResolver(customResolver);
141: }
142:
143: Transformer transformer = transformerFactory
144: .newTransformer(xsltSource);
145: if (pretty) {
146: try {
147: transformer.setOutputProperty(
148: "{http://xml.apache.org/xalan}indent-amount",
149: "2");
150: } catch (Exception e) {
151: }
152: }
153:
154: parse(out, document, transformer);
155:
156: }
157: }
|