View Javadoc

1   package org.apache.dvsl.dom4j;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.StringWriter;
23  import java.io.Writer;
24  
25  import java.util.List;
26  import java.util.ArrayList;
27  import java.util.Map;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  
31  import org.dom4j.Node;
32  import org.dom4j.Element;
33  import org.dom4j.Document;
34  import org.dom4j.Text;
35  import org.dom4j.Attribute;
36  import org.dom4j.Comment;
37  import org.dom4j.CDATA;
38  import org.dom4j.Branch;
39  import org.dom4j.DocumentHelper;
40  import org.dom4j.XPath;
41  
42  import org.dom4j.io.XMLWriter;
43  
44  import org.apache.dvsl.DVSLNode;
45  
46  
47  /**
48   * wrapper class for dom4j nodes to implement the
49   *  DVSLNode interface for template use
50   *
51   *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
52   */
53  public class Dom4jNodeImpl implements DVSLNode
54  {
55      protected Node element = null;
56  
57      protected Map attributes = null;
58  
59      /**
60       *  this is a bit yecchy - need to revamp
61       */
62      public Dom4jNodeImpl( Element e )
63      {
64          element = e;
65      }
66  
67      public Dom4jNodeImpl( Document e )
68      {
69          element = e;
70      }
71  
72      public Dom4jNodeImpl( Text e )
73      {
74          element = e;
75      }
76  
77      public Dom4jNodeImpl( Attribute e )
78      {
79          element = e;
80      }
81  
82      public Dom4jNodeImpl( Comment e )
83      {
84          element = e;
85      }
86  
87      public Dom4jNodeImpl( CDATA e )
88      {
89          element = e;
90      }
91  
92      private Dom4jNodeImpl()
93      {
94      }
95  
96      /**
97       *  returns the name of the node
98       */
99      public String name()
100     {
101         return element.getName();
102     }
103 
104     /**
105      *  returns a specificed attributeattribute
106      */
107     public String attribute( String attribute )
108     {
109         if ( element instanceof Element )
110             return ( (Element) element).attributeValue( attribute );
111 
112         return null;
113     }
114 
115     /**
116      *  returns a list of nodes that satisfy
117      *  the xpath
118      */
119     public List selectNodes( String xpath )
120     {
121         List l = element.selectNodes( xpath );
122 
123         List list = new ArrayList();
124 
125         for( int i = 0; i < l.size(); i++)
126         {
127             Node n = (Node) l.get( i );
128 
129             if ( n != null )
130             {
131                 list.add( makeDVSLNode( n ) );
132             }
133         }
134 
135         return list;
136     }
137 
138     public DVSLNode selectSingleNode( String xpath )
139     {
140         org.dom4j.Node n = element.selectSingleNode( xpath );
141 
142         return makeDVSLNode( n );
143     }
144 
145     public DVSLNode get( String xpath )
146     {
147         return selectSingleNode( xpath );
148     }
149 
150     public String value()
151     {
152         return element.getText();
153     }
154 
155     public Object valueOf( String xpath )
156     {
157         return element.valueOf( xpath );
158     }
159 
160     public String toString()
161     {
162         return value();
163     }
164 
165     public List children()
166     {
167         List list = new ArrayList();
168 
169         if ( element.getNodeType() == Node.ELEMENT_NODE )
170         {
171             List nodes = ( (Element) element).content();
172 
173             for( int i = 0; i < nodes.size(); i++)
174                 list.add(  makeDVSLNode( (Node) nodes.get(i)) );
175         }
176 
177         return list;
178     }
179 
180     /**
181      *  assumes a list of DVSLNodes
182      */
183     public String copy( List nodes )
184     {
185         if ( nodes == null)
186             return "";
187 
188         StringWriter sw = new StringWriter();
189 
190         for( int i = 0; i < nodes.size(); i++)
191         {
192             DVSLNode dn = (DVSLNode) nodes.get(i);
193 
194             copy( (Node) dn.getNodeImpl(), sw );
195         }
196 
197         return sw.toString();
198     }
199 
200     public String copy()
201     {
202         StringWriter sw = new StringWriter();
203         copy(element, sw );
204         return sw.toString();
205     }
206 
207     private void copy( Node node, Writer writer )
208     {
209         XMLWriter xw = new XMLWriter( writer );
210 
211         try
212         {
213             xw.write( node );
214         }
215         catch( Exception  ee )
216         {
217         }
218         finally
219         {
220             try
221             {
222                 xw.flush();
223             }
224             catch( Exception eee)
225                 {}
226         }
227     }
228 
229     public String render()
230     {
231         try
232         {
233             StringWriter sw = new StringWriter();
234 
235             element.write(sw);
236 
237             return sw.toString();
238         }
239         catch(Exception e )
240         {
241         }
242 
243         return "";
244     }
245 
246     public String attrib( String name )
247     {
248         if ( element instanceof Element )
249         {
250             Attribute attrib = ( (Element) element).attribute( name );
251 
252             if( attrib != null)
253                 return attrib.getValue();
254         }
255 
256         return null;
257     }
258 
259     public Object getNodeImpl()
260     {
261         return element;
262     }
263 
264     public Map getAttribMap()
265     {
266         /*
267          *  $$$ GMJ sync issue? yes.  Do I care?
268          */
269 
270         if (attributes == null)
271             attributes = new HashMap();
272 
273         /*
274          * only Elements have attributes
275          */
276 
277         if ( element instanceof Element )
278         {
279             Iterator it = ( (Element) element).attributeIterator();
280 
281             while(it.hasNext())
282             {
283                 Attribute at = (Attribute) it.next();
284 
285                 attributes.put( at.getName(), at.getValue() );
286             }
287         }
288 
289         return attributes;
290     }
291 
292     /**
293      *  will recast all of this later
294      */
295     private DVSLNode makeDVSLNode( Node n )
296     {
297         if ( n == null)
298             return null;
299 
300         short type = n.getNodeType();
301 
302         if ( type == Node.ELEMENT_NODE )
303         {
304             return new Dom4jNodeImpl( (Element) n );
305         }
306         else if( type == Node.TEXT_NODE )
307         {
308             return new Dom4jNodeImpl( (Text) n );
309         }
310         else if ( type == Node.ATTRIBUTE_NODE )
311         {
312             return new Dom4jNodeImpl( (Attribute) n );
313         }
314         else if ( type == Node.COMMENT_NODE )
315         {
316             return new Dom4jNodeImpl( (Comment) n );
317         }
318         else if ( type == Node.CDATA_SECTION_NODE )
319         {
320             return new Dom4jNodeImpl( (CDATA) n );
321         }
322 
323         return null;
324     }
325 }