View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.util;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.dom4j.Document;
28  import org.dom4j.DocumentException;
29  import org.dom4j.Element;
30  import org.dom4j.io.OutputFormat;
31  import org.dom4j.io.XMLWriter;
32  
33  /**
34   * A set of XML utility functions that make use of the Dom4j package.
35   * 
36   * This class originally existed as a means of accomplishing much of what
37   * Dom4j now allows us to do.  As such, the expectation is that it will
38   * be phased out over time.
39   * 
40   * @author muzquiano
41   */
42  public class XMLUtil
43  {
44      
45      /** The logger. */
46      protected static Log logger = LogFactory.getLog(XMLUtil.class);
47  
48      /**
49       * Adds the child value.
50       * 
51       * @param parent the parent
52       * @param childName the child name
53       * @param childValue the child value
54       */
55      public static void addChildValue(Element parent, String childName,
56              String childValue)
57      {
58          setChildValue(parent, childName, childValue);
59      }
60  
61      /**
62       * Gets the child.
63       * 
64       * @param parent the parent
65       * @param name the name
66       * 
67       * @return the child
68       */
69      public static Element getChild(Element parent, String name)
70      {
71          return parent.element(name);
72      }
73  
74      /**
75       * Gets the value.
76       * 
77       * @param element the element
78       * 
79       * @return the value
80       */
81      public static String getValue(Element element)
82      {
83          return element.getTextTrim();
84      }
85  
86      /**
87       * Sets the value.
88       * 
89       * @param element the element
90       * @param value the value
91       */
92      public static void setValue(Element element, String value)
93      {
94          element.clearContent();
95          element.setText(value);
96      }
97  
98      /**
99       * Gets the child value.
100      * 
101      * @param element the element
102      * @param name the name
103      * 
104      * @return the child value
105      */
106     public static String getChildValue(Element element, String name)
107     {
108         Element child = getChild(element, name);
109         if (child != null)
110         {
111             return getValue(child);
112         }
113         return null;
114     }
115 
116     /**
117      * Sets the child value.
118      * 
119      * @param element the element
120      * @param name the name
121      * @param value the value
122      */
123     public static void setChildValue(Element element, String name, String value)
124     {
125         Element child = getChild(element, name);
126         if (child == null)
127         {
128             /**
129              * A child did not yet exist, so create one
130              */
131             child = element.addElement(name);
132         }
133         if (child != null)
134         {
135             setValue(child, value);
136         }
137     }
138 
139     /**
140      * Gets the children.
141      * 
142      * @param element the element
143      * 
144      * @return the children
145      */
146     public static List getChildren(Element element)
147     {
148         return getChildren(element, null);
149     }
150 
151     /**
152      * Gets the children.
153      * 
154      * @param element the element
155      * @param name the name
156      * 
157      * @return the children
158      */
159     public static List getChildren(Element element, String name)
160     {
161         if (name == null)
162         {
163             return element.elements();
164         }
165         return element.elements(name);
166     }
167 
168     /**
169      * Gets an attribute from the given element with the given attribute name
170      * 
171      * @param element the element
172      * @param attributeName the attribute name
173      * 
174      * @return the attribute
175      */
176     public static String getAttribute(Element element, String attributeName)
177     {
178         return element.attributeValue(attributeName);
179     }
180 
181     /**
182      * Gets the document child value.
183      * 
184      * @param d the d
185      * @param name the name
186      * 
187      * @return the document child value
188      */
189     public static String getDocumentChildValue(Document d, String name)
190     {
191         return getChildValue(d.getRootElement(), name);
192     }
193 
194     /**
195      * Sets the document child value.
196      * 
197      * @param d the d
198      * @param name the name
199      * @param value the value
200      * 
201      * @throws Exception the exception
202      */
203     public static void setDocumentChildValue(Document d, String name,
204             String value) throws Exception
205     {
206         setChildValue(d.getRootElement(), name, value);
207     }
208 
209     /**
210      * Parses the.
211      * 
212      * @param xml the xml
213      * 
214      * @return the document
215      * 
216      * @throws DocumentException the document exception
217      */
218     public static Document parse(String xml) throws DocumentException
219     {
220         return org.dom4j.DocumentHelper.parseText(xml);
221     }
222 
223     /**
224      * Parses the given stream to an XML document (in UTF-8 format).
225      * 
226      * @param stream the stream
227      * 
228      * @return the document
229      * 
230      * @throws DocumentException the document exception
231      * @throws IOException Signals that an I/O exception has occurred.
232      */
233     public static Document parse(InputStream stream) throws DocumentException,
234             IOException
235     {
236         return parse(DataUtil.copyToString(stream, "UTF-8", true));
237     }
238 
239     /**
240      * Converts the document to XML.  This uses an efficient approach so
241      * that the XML is kept to a minimal.
242      * 
243      * @param document the document
244      * 
245      * @return the string
246      */
247     public static String toXML(Document document)
248     {
249         return toXML(document, false);
250     }
251 
252     /**
253      * Converts the document to XML.  The pretty switch can be used to produce
254      * human readable, or pretty, XML.
255      * 
256      * @param document the document
257      * @param pretty whether to produce human readable XML
258      * 
259      * @return the string
260      */
261     public static String toXML(Document document, boolean pretty)
262     {
263         String xml = null;
264         
265         if (pretty)
266         {
267             OutputFormat format = OutputFormat.createPrettyPrint();
268             format.setSuppressDeclaration(false);
269 
270             StringBuilderWriter writer = new StringBuilderWriter(256);
271             XMLWriter xmlWriter = new XMLWriter(writer, format);
272             try
273             {
274                 xmlWriter.write(document);
275                 xmlWriter.flush();
276                 xml = writer.toString();
277             }
278             catch (IOException ioe)
279             {
280                 /**
281                  * If this exception occurs, we'll log a note to the console
282                  * and then proceed to serialze the old fashioned way.
283                  */
284                 logger.debug(ioe);
285             }
286         }
287 
288         /**
289          * If the XML wasn't created already, we'll serialize the
290          * standard way.
291          */
292         if (xml == null)
293         {
294             xml = document.asXML();
295         }
296         
297         return xml;
298     }
299 }