1 package com.quikj.server.framework; 2 3 import org.w3c.dom.*; 5 6 7 public class AceXMLHelper 8 { 9 10 18 public static String processXMLElement(Node node, 19 String node_name, 20 String attribute_name) 21 throws AceException 22 { 23 if (node.getNodeType() != Node.ELEMENT_NODE) 24 { 25 27 throw new AceException("Node element type not a node"); 28 } 29 30 if (node.getNodeName().equals(node_name) == false) 31 { 32 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 getXMLAttribute(Node node, 45 String attribute_name) 46 throws AceException 47 { 48 NamedNodeMap attribs = node.getAttributes(); 50 if (attribs.getLength() <= 0) 51 { 52 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 61 throw new AceException("Attribute name -" 63 + attribute_name 64 + " does not match, received - " 65 + attr.getNodeName()); 66 } 67 68 return new String (attr.getNodeValue()); 69 } 70 71 public static String [] getXMLAttributes(Node node, String [] attributes) 72 { 73 NamedNodeMap attribs = node.getAttributes(); 74 int length = attribs.getLength(); 75 76 String [] ret = new String [attributes.length]; 77 for (int i = 0; i < length; i++) 78 { 79 Node attr = attribs.item(i); 80 String 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 (attr.getNodeValue()); 87 break; 88 } 89 } 90 } 91 92 return ret; 93 } 94 95 public static String encodeXMLString(java.lang.String input) 96 { 97 char[] str = input.toCharArray(); 98 StringBuffer buffer = new StringBuffer (); 99 100 for (int i = 0; i < str.length; i++) 101 { 102 switch (str[i]) 103 { 104 case '<': 105 buffer.append("<"); 106 break; 107 108 case '>': 109 buffer.append(">"); 110 break; 111 112 case '&': 113 buffer.append("&"); 114 break; 115 116 case '\'': 117 buffer.append("'"); 118 break; 119 120 case '\"': 121 buffer.append("""); 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 decodeXMLString(String text) 142 { 143 StringBuffer buffer = new StringBuffer (); 144 char[] str = text.toCharArray(); 145 146 for (int i = 0; i < str.length; i++) 147 { 148 if (str[i] == '&') 149 { 150 StringBuffer sbuffer = new StringBuffer (); 151 152 int index = text.indexOf(';', i); 154 if (index < 0) return null; 155 156 String escape = text.substring(i, index); 157 158 try 159 { 160 if (escape.equals(">") == true) 161 { 162 buffer.append('>'); 163 } 164 else if (escape.equals("<") == true) 165 { 166 buffer.append('<'); 167 } 168 else if (escape.equals("&") == true) 169 { 170 buffer.append('&'); 171 } 172 else if (escape.equals("&apos") == true) 173 { 174 buffer.append("'"); 175 } 176 else if (escape.equals(""") == true) 177 { 178 buffer.append('"'); 179 } 180 else if (escape.startsWith("&#")) 181 { 182 String 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 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 [] args) 213 { 214 String str = "á."; 215 String 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 |