001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.xslt;
024:
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.net.URL;
028:
029: import javax.servlet.FilterConfig;
030: import javax.servlet.ServletContext;
031: import javax.xml.transform.Result;
032: import javax.xml.transform.Source;
033: import javax.xml.transform.Templates;
034: import javax.xml.transform.Transformer;
035: import javax.xml.transform.TransformerException;
036: import javax.xml.transform.TransformerFactory;
037:
038: import org.apache.log4j.Logger;
039:
040: import biz.hammurapi.web.HammurapiWebException;
041:
042: /**
043: * @author Pavel Vlasov
044: * @version $Revision: 1.2 $
045: */
046: public abstract class AbstractXsltTransformer {
047: private ThreadLocal transformerTL = new ThreadLocal();
048: private Templates templates;
049: protected Logger logger = Logger
050: .getLogger(AbstractXsltTransformer.class);
051:
052: public interface SetParametersCallback {
053: void setParameters(Transformer transformer);
054: }
055:
056: public void setLogger(Logger logger) {
057: this .logger = logger;
058: }
059:
060: private ServletContext servletContext;
061:
062: public void setFilterConfig(FilterConfig filterConfig) {
063: if (filterConfig != null) {
064: this .servletContext = filterConfig.getServletContext();
065: }
066: }
067:
068: public void setServletContext(ServletContext servletContext) {
069: this .servletContext = servletContext;
070: }
071:
072: private String url;
073:
074: /**
075: * @param url Stylesheet URL (to download from another server)
076: */
077: public void setUrl(String url) {
078: this .url = url;
079: }
080:
081: private String uri;
082:
083: /**
084: * @param uri Stylesheet URI (to read from the same application)
085: */
086: public void setUri(String uri) {
087: this .uri = uri;
088: }
089:
090: private String resource;
091:
092: /**
093: * @param resource Stylesheet resource (to load from classpath)
094: */
095: public void setResource(String resource) {
096: this .resource = resource;
097: }
098:
099: public interface SetContentTypeCallBack {
100: void setContentType(String contentType);
101: }
102:
103: public void transform(Source source, Result result,
104: SetContentTypeCallBack setContentTypeCallBack,
105: SetParametersCallback setParametersCallback)
106: throws TransformerException, HammurapiWebException {
107: Transformer transformer = (Transformer) transformerTL.get();
108: try {
109: if (transformer == null) {
110: if (templates == null) {
111: TransformerFactory tf = createTransformerFactory();
112: Source styleSource = getSource();
113: if (styleSource == null) {
114: if (logger != null) {
115: logger.debug("XML, style not found)");
116: }
117:
118: if (setContentTypeCallBack != null) {
119: setContentTypeCallBack
120: .setContentType("text/xml");
121: }
122: transformer = tf.newTransformer();
123: } else {
124: if (logger != null) {
125: logger.debug("HTML");
126: }
127: templates = tf.newTemplates(styleSource);
128: transformer = templates.newTransformer();
129: if (setContentTypeCallBack != null) {
130: setContentTypeCallBack
131: .setContentType("text/html");
132: }
133: }
134: } else {
135: transformer = templates.newTransformer();
136: }
137: transformerTL.set(transformer);
138: }
139:
140: if (setParametersCallback != null) {
141: setParametersCallback.setParameters(transformer);
142: }
143: transformer.transform(source, result);
144: } finally {
145: if (transformer != null) {
146: transformer.clearParameters();
147: }
148: }
149: }
150:
151: /**
152: * @return
153: */
154: protected InputStream getStyleSheetAsStream()
155: throws HammurapiWebException {
156: try {
157: if (url != null) {
158: return new URL(url).openStream();
159: } else if (uri != null) {
160: if (servletContext == null) {
161: if (logger != null) {
162: logger.warn("servletContext is null");
163: }
164: return null;
165: }
166:
167: InputStream resourceAsStream = servletContext
168: .getResourceAsStream(uri);
169: if (resourceAsStream == null) {
170: throw new HammurapiWebException(
171: "Resource not found: " + uri);
172: }
173:
174: return resourceAsStream;
175: } else if (resource != null) {
176: InputStream resourceAsStream = getClass()
177: .getClassLoader().getResourceAsStream(resource);
178: if (resourceAsStream == null) {
179: throw new HammurapiWebException(
180: "Resource not found: " + uri);
181: }
182:
183: return resourceAsStream;
184: } else {
185: return null;
186: }
187: } catch (IOException e) {
188: throw new HammurapiWebException(e);
189: }
190: }
191:
192: protected abstract Source getSource() throws HammurapiWebException;
193:
194: protected abstract TransformerFactory createTransformerFactory();
195: }
|