KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > 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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jstl.el;
31
32 import com.caucho.el.Expr;
33 import com.caucho.jsp.PageContextImpl;
34 import com.caucho.jstl.NameValueTag;
35 import com.caucho.log.Log;
36 import com.caucho.util.L10N;
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.el.ELContext;
44 import javax.el.ELException;
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import javax.servlet.jsp.JspException JavaDoc;
47 import javax.servlet.jsp.JspWriter JavaDoc;
48 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
49 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
50 import javax.xml.transform.Result JavaDoc;
51 import javax.xml.transform.Source JavaDoc;
52 import javax.xml.transform.Transformer JavaDoc;
53 import javax.xml.transform.TransformerFactory JavaDoc;
54 import javax.xml.transform.dom.DOMResult JavaDoc;
55 import javax.xml.transform.dom.DOMSource JavaDoc;
56 import javax.xml.transform.stream.StreamResult JavaDoc;
57 import javax.xml.transform.stream.StreamSource JavaDoc;
58 import java.io.InputStream JavaDoc;
59 import java.io.Reader 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 Expr _xml;
68   private Expr _xslt;
69
70   private Expr _xmlSystemId;
71   private Expr _xsltSystemId;
72   
73   private String JavaDoc _var;
74   private String JavaDoc _scope;
75
76   private Expr _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 setXml(Expr xml)
85   {
86     setDoc(xml);
87   }
88   
89   /**
90    * Sets the JSP-EL XML value.
91    */

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

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

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

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

124   public void setXsltSystemId(Expr 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(Expr 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 EVAL_BODY_BUFFERED;
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       ELContext env = pageContext.getELContext();
183       
184       JspWriter JavaDoc out = pageContext.getOut();
185
186       TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
187
188       Source JavaDoc source = getSource(_xslt, _xsltSystemId);
189
190       Transformer JavaDoc transformer = factory.newTransformer(source);
191
192       for (int i = 0; i < _paramNames.size(); i++) {
193         String JavaDoc name = _paramNames.get(i);
194         String JavaDoc value = _paramValues.get(i);
195
196         transformer.setParameter(name, value);
197       }
198
199       if (_xml != null)
200     source = getSource(_xml, _xmlSystemId);
201       else {
202     BodyContent JavaDoc bodyContent = getBodyContent();
203
204     source = new StreamSource JavaDoc(bodyContent.getReader());
205     source.setSystemId(((HttpServletRequest JavaDoc) pageContext.getRequest()).getRequestURI());
206       }
207
208       Result JavaDoc result;
209       Node JavaDoc top = null;
210
211       if (_result != null) {
212         result = (Result JavaDoc) _result.evalObject(env);
213       }
214       else if (_var != null) {
215         top = new com.caucho.xml.QDocument();
216         
217         result = new DOMResult JavaDoc(top);
218       }
219       else
220         result = new StreamResult JavaDoc(out);
221
222       transformer.transform(source, result);
223
224       if (_var != null)
225         CoreSetTag.setValue(pageContext, _var, _scope, top);
226     } catch (Exception JavaDoc e) {
227       throw new JspException JavaDoc(e);
228     }
229
230     return SKIP_BODY;
231   }
232
233   private Source JavaDoc getSource(Expr xmlExpr, Expr systemIdExpr)
234     throws JspException JavaDoc
235   {
236     try {
237       PageContextImpl pageContext = (PageContextImpl) this.pageContext;
238       
239       Object JavaDoc xmlObj = xmlExpr.evalObject(pageContext.getELContext());
240       String JavaDoc systemId = null;
241       Source JavaDoc source = null;
242
243       if (systemIdExpr != null)
244     systemId = systemIdExpr.evalString(pageContext.getELContext());
245       
246       source = convertToSource(xmlObj, systemId);
247
248       return source;
249     } catch (ELException e) {
250       throw new JspException JavaDoc(e);
251     }
252   }
253
254   public static Source JavaDoc convertToSource(Object JavaDoc xmlObj, String JavaDoc systemId)
255     throws JspException JavaDoc
256   {
257     if (xmlObj instanceof String JavaDoc) {
258       ReadStream is = Vfs.openString((String JavaDoc) xmlObj);
259
260       return new StreamSource JavaDoc(is, systemId);
261     }
262     else if (xmlObj instanceof InputStream JavaDoc) {
263       return new StreamSource JavaDoc((InputStream JavaDoc) xmlObj, systemId);
264     }
265     else if (xmlObj instanceof Reader JavaDoc) {
266       return new StreamSource JavaDoc((Reader JavaDoc) xmlObj, systemId);
267     }
268     else if (xmlObj instanceof Node JavaDoc) {
269       return new DOMSource JavaDoc((Node JavaDoc) xmlObj, systemId);
270     }
271     else if (xmlObj instanceof NodeList JavaDoc) {
272       return new DOMSource JavaDoc(((NodeList JavaDoc) xmlObj).item(0), systemId);
273     }
274     else if (xmlObj instanceof Source JavaDoc)
275       return (Source JavaDoc) xmlObj;
276     else if (xmlObj instanceof ArrayList JavaDoc) {
277       ArrayList JavaDoc list = (ArrayList JavaDoc) xmlObj;
278
279       if (list.size() > 0)
280     return convertToSource(list.get(0), systemId);
281     }
282     
283     throw new JspException JavaDoc(L.l("unknown xml object type '{0}' '{1}'",
284                    xmlObj, xmlObj.getClass()));
285   }
286 }
287
Popular Tags