KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > util > XML


1 /*
2  * $Id: XML.java,v 1.8 2003/10/27 11:00:56 thusted Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/util/XML.java,v $
4  */

5 package org.infohazard.maverick.util;
6
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10 import org.jdom.*;
11 import org.jdom.output.XMLOutputter;
12
13 /**
14  * created January 27, 2002
15  * @author Jeff Schnitzer
16  * @version $Revision: 1.8 $ $Date: 2003/10/27 11:00:56 $
17  */

18 public class XML
19 {
20     /** The attribute which represents the "value" of an element. */
21     public final static String JavaDoc ATTR_VALUE = "value";
22     
23     /** The tag for a parameter node */
24     public final static String JavaDoc TAG_PARAM = "param";
25     
26     /** The attribute for a parameter name */
27     public final static String JavaDoc ATTR_PARAM_NAME = "name";
28
29     /** */
30     protected static XMLOutputter outputter = new XMLOutputter();
31
32     static
33     {
34         outputter.setTextNormalize(true);
35     }
36
37     /**
38      * <p>Extracts the named value from the element, by checking (in order):</p>
39      * <ol>
40      * <li>
41      * A system property whose name is constructed like this:
42      * "maverick." + node.getName() + "." + name
43      * </li>
44      * <li>An attribute with the specified name.</li>
45      * <li>The "value" attribute of a subelement with the specified name.</li>
46      * </ol>
47      *
48      * @param node
49      * @param name
50      * @return null if a value with that name is not defined.
51      */

52     public static String JavaDoc getValue(Element node, String JavaDoc name)
53     {
54         String JavaDoc result = System.getProperty("maverick." + node.getName() + "." + name);
55         if (result != null)
56             return result;
57         
58         result = node.getAttributeValue(name);
59         if (result != null)
60             return result;
61
62         Element subnode = node.getChild(name);
63         if (subnode == null)
64             return null;
65
66         return subnode.getAttributeValue(ATTR_VALUE);
67     }
68
69     /**
70      * Converts the specified node (and subnodes) to a nice, pretty,
71      * html escaped XML string.
72      *
73      * @param node
74      * @return
75      */

76     public static String JavaDoc toString(Element node)
77     {
78         return escape(outputter.outputString(node));
79     }
80
81     /**
82      * Escapes any html characters in the input string.
83      *
84      * @param in
85      * @return
86      */

87     public static String JavaDoc escape(String JavaDoc in)
88     {
89         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
90
91         for (int i = 0; i < in.length(); i++)
92         {
93             char c = in.charAt(i);
94
95             switch (c)
96             {
97                 case '<':
98                     out.append("&lt;");
99                     break;
100                 case '>':
101                     out.append("&gt;");
102                     break;
103                 case '&':
104                     out.append("&amp;");
105                     break;
106                 case '"':
107                     out.append("&quot;");
108                     break;
109                 default:
110                     out.append(c);
111                     break;
112             }
113         }
114
115         return out.toString();
116     }
117
118     /**
119      * Extracts a set of param child nodes from the specified node. Expects
120      * that param nodes will look like:
121      * &lt;param name="theName" value="theValue"/&gt;
122      *
123      * @param node Parent element.
124      * @return null if no parameters are found.
125      */

126     public static Map JavaDoc getParams(Element node)
127     {
128         Map JavaDoc params = null;
129
130         Iterator JavaDoc it = node.getChildren(TAG_PARAM).iterator();
131         while (it.hasNext())
132         {
133             Element paramNode = (Element)it.next();
134
135             String JavaDoc name = paramNode.getAttributeValue(ATTR_PARAM_NAME);
136
137             Object JavaDoc value = paramNode.getAttributeValue(ATTR_VALUE);
138             if (value == null)
139             {
140                 if (paramNode.hasChildren())
141                     value = paramNode.getChildren();
142                 else
143                     value = paramNode.getTextTrim();
144             }
145
146             if (params == null)
147                 params = new HashMap JavaDoc();
148                 
149             params.put(name, value);
150         }
151
152         return params;
153     }
154 }
155
156
Popular Tags