KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > XmlParseTag


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.BodyContentImpl;
34 import com.caucho.jsp.PageContextImpl;
35 import com.caucho.util.L10N;
36 import com.caucho.vfs.Vfs;
37 import com.caucho.xml.Xml;
38 import com.caucho.xml.XmlParser;
39
40 import org.w3c.dom.Document JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42
43 import javax.servlet.jsp.JspException JavaDoc;
44 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
45 import java.io.Reader JavaDoc;
46
47 public class XmlParseTag extends BodyTagSupport JavaDoc {
48   private static L10N L = new L10N(XmlParseTag.class);
49
50   private Expr _xml;
51   private Expr _systemId;
52   private Expr _filter;
53   
54   private String JavaDoc _var;
55   private String JavaDoc _scope;
56   
57   private String JavaDoc _varDom;
58   private String JavaDoc _scopeDom;
59
60   /**
61    * Sets the xml
62    */

63   public void setXml(Expr xml)
64   {
65     setDoc(xml);
66   }
67
68   /**
69    * Sets the doc
70    */

71   public void setDoc(Expr xml)
72   {
73     _xml = xml;
74   }
75
76   /**
77    * Sets the system id
78    */

79   public void setSystemId(Expr systemId)
80   {
81     _systemId = systemId;
82   }
83
84   /**
85    * Sets the filter
86    */

87   public void setFilter(Expr filter)
88   {
89     _filter = filter;
90   }
91
92   /**
93    * Sets the variable
94    */

95   public void setVar(String JavaDoc var)
96   {
97     _var = var;
98   }
99
100   /**
101    * Sets the scope
102    */

103   public void setScope(String JavaDoc scope)
104   {
105     _scope = scope;
106   }
107
108   /**
109    * Sets the variable
110    */

111   public void setVarDom(String JavaDoc var)
112   {
113     _varDom = var;
114   }
115
116   /**
117    * Sets the scope
118    */

119   public void setScopeDom(String JavaDoc scope)
120   {
121     _scopeDom = scope;
122   }
123
124   public int doEndTag() throws JspException JavaDoc
125   {
126     try {
127       PageContextImpl pageContext = (PageContextImpl) this.pageContext;
128       BodyContentImpl body = (BodyContentImpl) getBodyContent();
129
130       Reader JavaDoc reader;
131
132       if (_xml != null) {
133         Object JavaDoc obj = _xml.evalObject(pageContext.getELContext());
134
135         if (obj instanceof Reader JavaDoc)
136           reader = (Reader JavaDoc) obj;
137         else if (obj instanceof String JavaDoc)
138           reader = Vfs.openString((String JavaDoc) obj).getReader();
139         else
140           throw new JspException JavaDoc(L.l("'doc' attribute must be a Reader or String at `{0}'",
141                                      obj));
142       }
143       else if (body != null)
144         reader = body.getReader();
145       else
146     throw new JspException JavaDoc(L.l("x:parse requires a body"));
147
148       InputSource JavaDoc is = new InputSource JavaDoc(reader);
149
150       XmlParser parser = new Xml();
151
152       Document JavaDoc doc = parser.parseDocument(is);
153
154       reader.close();
155
156       if (_var != null)
157         CoreSetTag.setValue(pageContext, _var, _scope, doc);
158       else if (_varDom != null)
159         CoreSetTag.setValue(pageContext, _varDom, _scopeDom, doc);
160       else
161         throw new JspException JavaDoc(L.l("x:parse needs either var or varDom"));
162     } catch (JspException JavaDoc e) {
163       throw e;
164     } catch (Exception JavaDoc e) {
165       throw new JspException JavaDoc(e);
166     }
167
168     return EVAL_PAGE;
169   }
170 }
171
Popular Tags