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: *******************************************************************************/package org.ofbiz.base.util.template;
019:
020: import java.util.Map;
021: import java.util.Set;
022: import java.util.Iterator;
023: import java.io.StringReader;
024: import java.net.URL;
025: import java.net.URLConnection;
026: import java.io.InputStream;
027:
028: import org.ofbiz.base.util.UtilValidate;
029: import org.ofbiz.base.util.UtilXml;
030: import org.ofbiz.base.util.URLConnector;
031: import org.ofbiz.base.util.cache.UtilCache;
032: import org.ofbiz.base.location.FlexibleLocation;
033: import javax.xml.transform.Transformer;
034: import javax.xml.transform.TransformerFactory;
035: import javax.xml.transform.Templates;
036: import javax.xml.transform.Source;
037: import javax.xml.transform.dom.DOMSource;
038: import javax.xml.transform.dom.DOMResult;
039: import javax.xml.transform.TransformerConfigurationException;
040: import javax.xml.transform.TransformerException;
041: import org.w3c.dom.Document;
042: import org.w3c.dom.Node;
043: import org.ofbiz.base.util.GeneralException;
044: import java.io.IOException;
045:
046: import javax.xml.transform.stream.StreamSource;
047:
048: /**
049: * XslTransform
050: *
051: * This utility takes an input document and a XSL stylesheet and performs the
052: * transform, returning the output document.
053: * The input for both the input document and stylesheet can be in one of three forms
054: * - a URL to the doc, the doc in string form and the doc in DOM Document form.
055: * It keeps its own cache for storing the compiled transforms.
056: *
057: */
058: public final class XslTransform {
059:
060: public static final String module = XslTransform.class.getName();
061: public static UtilCache xslTemplatesCache = new UtilCache(
062: "XsltTemplates", 0, 0);
063:
064: public static Document transform(Map context, Map params)
065: throws GeneralException, IOException,
066: TransformerConfigurationException, TransformerException {
067: Document outputDocument = null;
068: TransformerFactory tFactory = TransformerFactory.newInstance();
069: Templates translet = null;
070: String templateName = (String) context.get("templateName");
071: if (UtilValidate.isNotEmpty(templateName)) {
072: translet = (Templates) xslTemplatesCache.get(templateName);
073: }
074:
075: if (translet == null) {
076: String templateUrl = (String) context.get("templateUrl");
077: String templateString = (String) context
078: .get("templateString");
079: Document templateDocument = (Document) context
080: .get("templateDocument");
081: Source templateSource = getSource(templateDocument,
082: templateUrl, templateString);
083: translet = tFactory.newTemplates(templateSource);
084: if (UtilValidate.isNotEmpty(templateName)) {
085: xslTemplatesCache.put(templateName, translet);
086: }
087: }
088: if (translet != null) {
089: Transformer transformer = translet.newTransformer();
090: if (params != null) {
091: Set entrySet = params.entrySet();
092: Iterator iter = entrySet.iterator();
093: while (iter.hasNext()) {
094: Map.Entry entry = (Map.Entry) iter.next();
095: String key = (String) entry.getKey();
096: Object val = entry.getValue();
097: transformer.setParameter(key, val);
098: }
099: }
100:
101: DOMResult outputResult = new DOMResult(UtilXml
102: .makeEmptyXmlDocument());
103:
104: String inputUrl = (String) context.get("inputUrl");
105: String inputString = (String) context.get("inputString");
106: Document inputDocument = (Document) context
107: .get("inputDocument");
108: Source inputSource = getSource(inputDocument, inputUrl,
109: inputString);
110:
111: transformer.transform(inputSource, outputResult);
112: Node nd = outputResult.getNode();
113: outputDocument = (Document) nd;
114: }
115:
116: return outputDocument;
117: }
118:
119: private static Source getSource(Document inputDocument,
120: String inputUrl, String inputString)
121: throws GeneralException, IOException {
122: Source source = null;
123: if (inputDocument != null) {
124: source = new DOMSource(inputDocument);
125: } else if (UtilValidate.isNotEmpty(inputString)) {
126: source = new StreamSource(new StringReader(inputString));
127: } else if (UtilValidate.isNotEmpty(inputUrl)) {
128: URL url = FlexibleLocation.resolveLocation(inputUrl);
129: URLConnection conn = URLConnector.openConnection(url);
130: InputStream in = conn.getInputStream();
131: source = new StreamSource(in);
132: }
133: return source;
134: }
135: }
|