1 28 29 package com.caucho.xml; 30 31 import com.caucho.util.CharBuffer; 32 33 import org.w3c.dom.Comment ; 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.DocumentFragment ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.ProcessingInstruction ; 39 import org.xml.sax.ContentHandler ; 40 import org.xml.sax.SAXException ; 41 42 import java.io.IOException ; 43 import java.util.HashMap ; 44 45 48 public class XmlUtil { 49 55 public static HashMap <String ,String > splitNameList(String name) 56 throws IOException 57 { 58 HashMap <String ,String > attrs = new HashMap <String ,String >(); 59 CharBuffer cb = new CharBuffer(); 60 61 int length = name.length(); 62 int i = 0; 63 int ch = 0; 64 while (i < length) { 65 for (; i < length && XmlChar.isWhitespace(ch = name.charAt(i)); i++) { 66 } 67 68 if (i < length && ! XmlChar.isNameStart(ch)) 69 throw new IOException ("expected name at " + (char) ch); 70 71 cb.clear(); 72 while (i < length && XmlChar.isNameChar(ch)) { 73 cb.append((char) ch); 74 75 ch = name.charAt(++i); 76 } 77 String key = cb.toString(); 78 cb.clear(); 79 80 for (; i < length && XmlChar.isWhitespace(ch = name.charAt(i)); i++) { 81 } 82 83 if (ch != '=') { 84 attrs.put(key, ""); 85 continue; 86 } 87 88 while (++i < length && XmlChar.isWhitespace(ch = name.charAt(i))) { 89 } 90 91 if (i >= length) 92 break; 93 94 cb.clear(); 95 if (ch == '\'') { 96 while (++i < length && (ch = name.charAt(i)) != '\'') 97 cb.append((char) ch); 98 i++; 99 } else if (ch == '"') { 100 while (++i < length && (ch = name.charAt(i)) != '\"') 101 cb.append((char) ch); 102 i++; 103 } else if (XmlChar.isNameChar(ch)) { 104 cb.append((char) ch); 105 while (++i < length && XmlChar.isNameChar(ch = name.charAt(i))) 106 cb.append((char) ch); 107 } else 108 throw new IOException ("unexpected"); 109 110 attrs.put(key, cb.toString()); 111 } 112 113 return attrs; 114 } 115 116 133 public static String getPIAttribute(String pi, String key) 134 { 135 CharBuffer nameBuf = new CharBuffer(); 136 CharBuffer valueBuf = new CharBuffer(); 137 138 int i = 0; 139 int length = pi.length();; 140 while (i < length) { 141 int ch = 0; 142 for (; i < length && XmlChar.isWhitespace(ch = pi.charAt(i)); i++) { 143 } 144 145 nameBuf.clear(); 146 for (; i < length && XmlChar.isNameChar(ch = pi.charAt(i)); i++) 147 nameBuf.append((char) ch); 148 149 for (; i < length && XmlChar.isWhitespace(ch = pi.charAt(i)); i++) { 150 } 151 152 if (i < length && ch != '=') { 153 if (nameBuf.length() == 0) 154 return null; 155 else if (nameBuf.toString().equals(key)) 156 return nameBuf.toString(); 157 else 158 continue; 159 } 160 161 i++; 162 for (; i < length && XmlChar.isWhitespace(ch = pi.charAt(i)); i++) { 163 } 164 165 valueBuf.clear(); 167 if (ch == '\'') { 168 i++; 169 for (; i < length && (ch = pi.charAt(i)) != '\''; i++) 170 valueBuf.append((char) ch); 171 i++; 172 } 173 else if (ch == '\"') { 174 i++; 175 for (; i < length && (ch = pi.charAt(i)) != '\"'; i++) 176 valueBuf.append((char) ch); 177 i++; 178 } 179 else if (XmlChar.isNameChar(ch)) { 180 for (; i < length && XmlChar.isNameChar(ch = pi.charAt(i)); i++) 181 valueBuf.append((char) ch); 182 } 183 else 184 return null; 186 String name = nameBuf.toString(); 187 if (name.equals(key)) 188 return valueBuf.toString(); 189 } 190 191 return null; 192 } 193 194 206 public static Node getNext(Node node) 207 { 208 if (node == null) 209 return null; 210 211 if (node.getFirstChild() != null) 212 return node.getFirstChild(); 213 214 for (; node != null; node = node.getParentNode()) { 215 if (node.getNextSibling() != null) 216 return node.getNextSibling(); 217 } 218 219 return null; 220 } 221 222 228 public static Node getPrevious(Node node) 229 { 230 Node previous; 231 232 if (node == null) 233 return null; 234 235 if ((previous = node.getPreviousSibling()) != null) { 236 for (; 237 previous.getLastChild() != null; 238 previous = previous.getLastChild()) { 239 } 240 241 return previous; 242 } 243 244 return node.getParentNode(); 245 } 246 247 251 public static String textValue(Node node) 252 { 253 if (node instanceof Element || node instanceof DocumentFragment ) { 254 String s = null; 255 CharBuffer cb = null; 256 257 for (Node child = node.getFirstChild(); 258 child != null; 259 child = child.getNextSibling()) { 260 String value = null; 261 262 if (child instanceof Element || child instanceof Document ) { 263 if (cb == null) 264 cb = new CharBuffer(); 265 if (s != null) 266 cb.append(s); 267 s = null; 268 269 textValue(cb, child); 270 } 271 else if ((value = child.getNodeValue()) == null || value == "") { 272 } 273 else if (cb != null) 274 cb.append(value); 275 else if (s == null && s != "") { 276 s = value; 277 } 278 else { 279 cb = new CharBuffer(); 280 281 cb.append(s); 282 cb.append(value); 283 s = null; 284 } 285 } 286 287 if (s != null) 288 return s; 289 else if (cb != null) 290 return cb.toString(); 291 else 292 return ""; 293 } 294 else { 295 String value = node.getNodeValue(); 296 297 if (value != null) 298 return value; 299 else 300 return ""; 301 } 302 } 303 304 308 public static void textValue(CharBuffer cb, Node node) 309 { 310 if (node instanceof Element || node instanceof DocumentFragment ) { 311 for (Node child = node.getFirstChild(); 312 child != null; 313 child = child.getNextSibling()) { 314 textValue(cb, child); 315 } 316 } 317 else if (node instanceof Comment || node instanceof ProcessingInstruction ) { 318 } 319 else 320 cb.append(node.getNodeValue()); 321 } 322 323 327 public static boolean isWhitespace(String text) 328 { 329 for (int i = text.length() - 1; i >= 0; i--) 330 if (! XmlChar.isWhitespace(text.charAt(i))) 331 return false; 332 333 return true; 334 } 335 336 339 public static void toSAX(Node node, ContentHandler handler) 340 throws SAXException 341 { 342 for (; node != null; node = node.getNextSibling()) { 343 if (node instanceof ProcessingInstruction ) { 344 ProcessingInstruction pi = (ProcessingInstruction ) node; 345 346 handler.processingInstruction(pi.getNodeName(), pi.getData()); 347 } 348 else if (node instanceof DocumentFragment ) { 349 toSAX(node.getFirstChild(), handler); 350 } 351 } 352 } 353 354 357 public static String getNamespace(Node node, String prefix) 358 { 359 for (; node != null; node = node.getParentNode()) { 360 if (node instanceof CauchoElement) 361 return ((CauchoElement) node).getNamespace(prefix); 362 } 363 364 return null; 365 } 366 } 367 | Popular Tags |