KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > XmlTransformTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jstl.rt;
30
31 import com.caucho.jsp.PageContextImpl;
32 import com.caucho.jstl.NameValueTag;
33 import com.caucho.log.Log;
34 import com.caucho.server.webapp.WebApp;
35 import com.caucho.util.L10N;
36 import com.caucho.vfs.Path;
37 import com.caucho.vfs.ReadStream;
38 import com.caucho.vfs.Vfs;
39
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.jsp.JspException JavaDoc;
45 import javax.servlet.jsp.JspWriter JavaDoc;
46 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
47 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
48 import javax.xml.transform.Result JavaDoc;
49 import javax.xml.transform.Source JavaDoc;
50 import javax.xml.transform.Transformer JavaDoc;
51 import javax.xml.transform.TransformerFactory JavaDoc;
52 import javax.xml.transform.dom.DOMResult JavaDoc;
53 import javax.xml.transform.dom.DOMSource JavaDoc;
54 import javax.xml.transform.stream.StreamResult JavaDoc;
55 import javax.xml.transform.stream.StreamSource JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.io.InputStream JavaDoc;
58 import java.io.Reader JavaDoc;
59 import java.io.Writer JavaDoc;
60 import java.util.ArrayList JavaDoc;
61 import java.util.logging.Logger JavaDoc;
62
63 public class XmlTransformTag extends BodyTagSupport JavaDoc implements NameValueTag {
64   private static final Logger JavaDoc log = Log.open(XmlTransformTag.class);
65   private static final L10N L = new L10N(XmlTransformTag.class);
66   
67   private Object JavaDoc _xml;
68   private Object JavaDoc _xslt;
69
70   private String JavaDoc _xmlSystemId;
71   private String JavaDoc _xsltSystemId;
72   
73   private String JavaDoc _var;
74   private String JavaDoc _scope;
75
76   private Object JavaDoc _result;
77   
78   private ArrayList JavaDoc<String JavaDoc> _paramNames = new ArrayList JavaDoc<String JavaDoc>();
79   private ArrayList JavaDoc<String JavaDoc> _paramValues = new ArrayList JavaDoc<String JavaDoc>();
80   
81   /**
82    * Sets the JSP-EL XML value.
83    */

84   public void setDoc(Object JavaDoc xml)
85   {
86     _xml = xml;
87   }
88   
89   /**
90    * Sets the JSP-EL XML value.
91    */

92   public void setXml(Object JavaDoc xml)
93   {
94     setDoc(xml);
95   }
96
97   /**
98    * Sets the JSP-EL XML value.
99    */

100   public void setXslt(Object JavaDoc xslt)
101   {
102     _xslt = xslt;
103   }
104
105   /**
106    * Sets the JSP-EL XML system id expr.
107    */

108   public void setXmlSystemId(String JavaDoc xmlSystemId)
109   {
110     _xmlSystemId = xmlSystemId;
111   }
112
113   /**
114    * Sets the JSP-EL XML system id expr.
115    */

116   public void setDocSystemId(String JavaDoc docSystemId)
117   {
118     _xmlSystemId = docSystemId;
119   }
120
121   /**
122    * Sets the JSP-EL XSLT system id.
123    */

124   public void setXsltSystemId(String JavaDoc xsltSystemId)
125   {
126     _xsltSystemId = xsltSystemId;
127   }
128
129   /**
130    * Sets the variable
131    */

132   public void setVar(String JavaDoc var)
133   {
134     _var = var;
135   }
136
137   /**
138    * Sets the scope
139    */

140   public void setScope(String JavaDoc scope)
141   {
142     _scope = scope;
143   }
144
145   /**
146    * Sets the result
147    */

148   public void setResult(Object JavaDoc result)
149   {
150     _result = result;
151   }
152   
153   /**
154    * Adds a parameter.
155    */

156   public void addParam(String JavaDoc name, String JavaDoc value)
157   {
158     _paramNames.add(name);
159     _paramValues.add(value);
160   }
161
162   /**
163    * Process the tag.
164    */

165   public int doStartTag()
166     throws JspException JavaDoc
167   {
168     _paramNames.clear();
169     _paramValues.clear();
170
171     return super.doStartTag();
172   }
173
174   /**
175    * Process the tag.
176    */

177   public int doEndTag()
178     throws JspException JavaDoc
179   {
180     try {
181       PageContextImpl pageContext = (PageContextImpl) this.pageContext;
182       
183       JspWriter JavaDoc out = pageContext.getOut();
184
185       TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
186
187       String JavaDoc xsltSystemId = getCanonicalURL(pageContext, _xsltSystemId);
188       
189       Source JavaDoc source = getSource(_xslt, xsltSystemId);
190
191       Transformer JavaDoc transformer = factory.newTransformer(source);
192
193       for (int i = 0; i < _paramNames.size(); i++) {
194         String JavaDoc name = _paramNames.get(i);
195         String JavaDoc value = _paramValues.get(i);
196
197         transformer.setParameter(name, value);
198       }
199
200       if (_xml != null)
201     source = getSource(_xml, getCanonicalURL(pageContext, _xmlSystemId));
202       else {
203     BodyContent JavaDoc bodyContent = getBodyContent();
204
205     source = new StreamSource JavaDoc(bodyContent.getReader());
206
207     if (_xmlSystemId != null)
208       source.setSystemId(getCanonicalURL(pageContext, _xmlSystemId));
209     else
210       source.setSystemId(((HttpServletRequest JavaDoc) pageContext.getRequest()).getRequestURI());
211       }
212
213       Result JavaDoc result;
214       Node JavaDoc top = null;
215
216       if (_result != null) {
217         result = (Result JavaDoc) _result;
218       }
219       else if (_var != null) {
220         top = new com.caucho.xml.QDocument();
221         
222         result = new DOMResult JavaDoc(top);
223       }
224       else {
225         result = new StreamResult JavaDoc(new WriterWrapper(out));
226       }
227
228       transformer.transform(source, result);
229
230       if (_var != null)
231         CoreSetTag.setValue(pageContext, _var, _scope, top);
232     } catch (Exception JavaDoc e) {
233       throw new JspException JavaDoc(e);
234     }
235
236     return SKIP_BODY;
237   }
238
239   /**
240    * Returns the ML source.
241    */

242   private Source JavaDoc getSource(Object JavaDoc xml, String JavaDoc systemId)
243     throws JspException JavaDoc
244   {
245     PageContextImpl pageContext = (PageContextImpl) this.pageContext;
246       
247     Object JavaDoc xmlObj = xml;
248     Source JavaDoc source = null;
249
250     if (xmlObj instanceof String JavaDoc) {
251       ReadStream is = Vfs.openString((String JavaDoc) xmlObj);
252
253       source = new StreamSource JavaDoc(is, systemId);
254     }
255     else if (xmlObj instanceof InputStream JavaDoc) {
256       source = new StreamSource JavaDoc((InputStream JavaDoc) xmlObj, systemId);
257     }
258     else if (xmlObj instanceof Reader JavaDoc) {
259       source = new StreamSource JavaDoc((Reader JavaDoc) xmlObj, systemId);
260     }
261     else if (xmlObj instanceof Node JavaDoc) {
262       source = new DOMSource JavaDoc((Node JavaDoc) xmlObj, systemId);
263     }
264     else if (xmlObj instanceof NodeList JavaDoc) {
265       source = new DOMSource JavaDoc(((NodeList JavaDoc) xmlObj).item(0), systemId);
266     }
267     else if (xmlObj instanceof Source JavaDoc)
268       source = (Source JavaDoc) xmlObj;
269     else
270       throw new JspException JavaDoc(L.l("unknown xml object type `{0}'", xmlObj));
271
272     return source;
273   }
274
275   private String JavaDoc getCanonicalURL(PageContextImpl pageContext, String JavaDoc url)
276   {
277     WebApp app = pageContext.getApplication();
278     Path appDir = pageContext.getApplication().getAppDir();
279       
280     if (url != null) {
281       if (url.startsWith("/"))
282     url = app.getRealPath(url);
283       else if (url.indexOf(':') > 0 &&
284            url.indexOf(':') < url.indexOf('/')) {
285       }
286       else {
287     url = pageContext.getRequest().getRealPath(url);
288       }
289     
290       url = appDir.lookup(url).getURL();
291     }
292
293     return url;
294   }
295
296   static class WriterWrapper extends Writer JavaDoc {
297     private JspWriter JavaDoc _out;
298
299     WriterWrapper(JspWriter JavaDoc out)
300     {
301       _out = out;
302     }
303     
304     public void write(char []buf, int off, int len)
305       throws IOException JavaDoc
306     {
307       _out.write(buf, off, len);
308     }
309     
310     public void write(int ch)
311       throws IOException JavaDoc
312     {
313       _out.write(ch);
314     }
315     
316     public void write(String JavaDoc s)
317       throws IOException JavaDoc
318     {
319       _out.write(s);
320     }
321     
322     public void write(String JavaDoc s, int off, int len)
323       throws IOException JavaDoc
324     {
325       _out.write(s, off, len);
326     }
327
328     public void flush()
329     {
330     }
331
332     public void close()
333     {
334     }
335   }
336 }
337
Popular Tags