KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > server > framework > AceXMLHelper


1 package com.quikj.server.framework;
2
3 // JAXP packages
4
import org.w3c.dom.*;
5
6
7 public class AceXMLHelper
8 {
9     
10     /**
11      * This function handles a node with an attribute as shown below
12      *
13      * <node_name attribute_name="value">
14      *
15      * The function verifies the node_name and attribute_name and returns
16      * the value. If the verification fails, it throws an AceException
17      */

18     public static String JavaDoc processXMLElement(Node node,
19     String JavaDoc node_name,
20     String JavaDoc attribute_name)
21     throws AceException
22     {
23         if (node.getNodeType() != Node.ELEMENT_NODE)
24         {
25             // the node type must be an element
26

27             throw new AceException("Node element type not a node");
28         }
29         
30         if (node.getNodeName().equals(node_name) == false)
31         {
32             // the node names do not match
33

34             throw new AceException("Node name - "
35             + node_name
36             + " does not match, received - "
37             + node.getNodeName());
38         }
39         
40         return getXMLAttribute(node, attribute_name);
41     }
42     
43     
44     public static String JavaDoc getXMLAttribute(Node node,
45     String JavaDoc attribute_name)
46     throws AceException
47     {
48         // must have at least one attribute
49
NamedNodeMap attribs = node.getAttributes();
50         if (attribs.getLength() <= 0)
51         {
52             // not a single attribute was found
53
throw new AceException("Node does not have any attributes");
54         }
55         
56         Node attr = attribs.item(0);
57         if (attr.getNodeName().equals(attribute_name) == false)
58         {
59             // the first attribute must be the attribute_name
60

61             // print error message
62
throw new AceException("Attribute name -"
63             + attribute_name
64             + " does not match, received - "
65             + attr.getNodeName());
66         }
67         
68         return new String JavaDoc(attr.getNodeValue());
69     }
70     
71     public static String JavaDoc[] getXMLAttributes(Node node, String JavaDoc[] attributes)
72     {
73         NamedNodeMap attribs = node.getAttributes();
74         int length = attribs.getLength();
75         
76         String JavaDoc[] ret = new String JavaDoc[attributes.length];
77         for (int i = 0; i < length; i++)
78         {
79             Node attr = attribs.item(i);
80             String JavaDoc name = attr.getNodeName();
81             
82             for (int j = 0; j < attributes.length; j++)
83             {
84                 if (attributes[j].equals(name) == true)
85                 {
86                     ret[j] = new String JavaDoc(attr.getNodeValue());
87                     break;
88                 }
89             }
90         }
91         
92         return ret;
93     }
94     
95     public static String JavaDoc encodeXMLString(java.lang.String JavaDoc input)
96     {
97         char[] str = input.toCharArray();
98         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
99         
100         for (int i = 0; i < str.length; i++)
101         {
102             switch (str[i])
103             {
104                 case '<':
105                     buffer.append("&lt;");
106                     break;
107                     
108                 case '>':
109                     buffer.append("&gt;");
110                     break;
111                     
112                 case '&':
113                     buffer.append("&amp;");
114                     break;
115                     
116                 case '\'':
117                     buffer.append("&apos;");
118                     break;
119                     
120                 case '\"':
121                     buffer.append("&quot;");
122                     break;
123                     
124                 default:
125                     int intv = str[i];
126                     if (intv > 127)
127                     {
128                         buffer.append("&#x" + Integer.toHexString(intv) + ";");
129                     }
130                     else
131                     {
132                         buffer.append(str[i]);
133                     }
134             }
135         }
136         
137         return buffer.toString();
138     }
139     
140     
141     public static String JavaDoc decodeXMLString(String JavaDoc text)
142     {
143         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
144         char[] str = text.toCharArray();
145         
146         for (int i = 0; i < str.length; i++)
147         {
148             if (str[i] == '&')
149             {
150                 StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
151                 
152                 // find the semi-colon
153
int index = text.indexOf(';', i);
154                 if (index < 0) return null;
155                 
156                 String JavaDoc escape = text.substring(i, index);
157                 
158                 try
159                 {
160                     if (escape.equals("&gt") == true)
161                     {
162                         buffer.append('>');
163                     }
164                     else if (escape.equals("&lt") == true)
165                     {
166                         buffer.append('<');
167                     }
168                     else if (escape.equals("&amp") == true)
169                     {
170                         buffer.append('&');
171                     }
172                     else if (escape.equals("&apos") == true)
173                     {
174                         buffer.append("'");
175                     }
176                     else if (escape.equals("&quot") == true)
177                     {
178                         buffer.append('"');
179                     }
180                     else if (escape.startsWith("&#"))
181                     {
182                         String JavaDoc value = escape.substring(2);
183                         
184                         if (value.startsWith("x"))
185                         {
186                             value = "0" + value;
187                         }
188                         char c = (char)(Integer.decode(value).intValue());
189                         buffer.append(c);
190                     }
191                     else
192                     {
193                         return null;
194                     }
195                 }
196                 catch (NumberFormatException JavaDoc ex)
197                 {
198                     return null;
199                 }
200                 
201                 i = index;
202             }
203             else
204             {
205                 buffer.append(str[i]);
206             }
207         }
208         
209         return buffer.toString();
210     }
211     
212     public static void main (String JavaDoc[] args)
213     {
214         String JavaDoc str = "&#xe1;.";
215         String JavaDoc decoded_str = AceXMLHelper.decodeXMLString(str);
216         System.out.println (decoded_str);
217     }
218     
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
Popular Tags