KickJava   Java API By Example, From Geeks To Geeks.

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

62   public void setXml(Object JavaDoc xml)
63   {
64     _xml = xml;
65   }
66
67   /**
68    * Sets the xml
69    */

70   public void setDoc(Object JavaDoc xml)
71   {
72     setXml(xml);
73   }
74
75   /**
76    * Sets the system id
77    */

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

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

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

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

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

118   public void setScopeDom(String JavaDoc scope)
119   {
120     _scopeDom = scope;
121   }
122
123   public int doEndTag() throws JspException JavaDoc
124   {
125     try {
126       PageContextImpl pageContext = (PageContextImpl) this.pageContext;
127       BodyContentImpl body = (BodyContentImpl) getBodyContent();
128
129       Reader JavaDoc reader;
130
131       if (_xml != null) {
132         Object JavaDoc obj = _xml;
133
134         if (obj instanceof Reader JavaDoc)
135           reader = (Reader JavaDoc) obj;
136         else if (obj instanceof String JavaDoc)
137           reader = Vfs.openString((String JavaDoc) obj).getReader();
138         else
139           throw new JspException JavaDoc(L.l("xml must be Reader or String at `{0}'",
140                                      obj));
141       }
142       else if (body != null)
143         reader = body.getReader();
144       else
145     throw new JspException JavaDoc(L.l("doc attribute must be a Reader or String at `{0}'",
146                    null));
147
148       XmlParser parser = new Xml();
149
150       InputSource JavaDoc is = new InputSource JavaDoc(reader);
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