1 33 34 package com.icesoft.faces.util; 35 36 import org.w3c.dom.*; 37 38 import javax.faces.component.UIComponent; 39 import java.util.List ; 40 import java.util.Vector ; 41 42 public class DOMUtils { 43 private static int DEFAULT_DOM_STRING_PRESIZE = 4096; 44 private static int DEFAULT_NODE_STRING_PRESIZE = 256; 45 46 public static String DocumentTypetoString(String publicID, String systemID, 47 String root) { 48 return "<!DOCTYPE " + root + " PUBLIC \"" + publicID + "\" \"" + 49 systemID + "\">"; 50 } 51 52 public static String DOMtoString(Document document) { 53 return printNode( 54 document, 55 new StringBuffer (DEFAULT_DOM_STRING_PRESIZE)); 56 } 57 58 public static String nodeToString(Node node) { 59 return printNode( 60 node, 61 new StringBuffer (DEFAULT_NODE_STRING_PRESIZE)); 62 } 63 64 public static String childrenToString(Node node) { 65 NodeList children = node.getChildNodes(); 66 Node child; 67 StringBuffer stringbuffer = 68 new StringBuffer (DEFAULT_DOM_STRING_PRESIZE); 69 int l = children.getLength(); 70 for (int i = 0; i < l; i++) { 71 child = children.item(i); 72 stringbuffer.append(nodeToString(child)); 73 } 74 return stringbuffer.toString(); 75 } 76 77 private static String printNode(Node node, StringBuffer stringbuffer) { 78 printNode(node, stringbuffer, 0, true, false); 79 return stringbuffer.toString(); 80 } 81 82 private static void printNode( 83 Node node, StringBuffer stringbuffer, 84 int depth, boolean allowAddingWhitespace, 85 boolean addTrailingNewline) { 86 87 switch (node.getNodeType()) { 88 89 case Node.DOCUMENT_NODE: 90 NodeList nodes = node.getChildNodes(); 93 if (nodes != null) { 94 for (int i = 0; i < nodes.getLength(); i++) { 95 printNode(nodes.item(i), stringbuffer, depth + 1, 96 allowAddingWhitespace, false); 97 } 98 } 99 break; 100 101 case Node.ELEMENT_NODE: 102 String name = node.getNodeName(); 103 if (name.equalsIgnoreCase("br")) { 104 stringbuffer.append("<br>"); 105 break; 106 } 107 stringbuffer.append("<"); 108 stringbuffer.append(name); 109 NamedNodeMap attributes = node.getAttributes(); 110 for (int i = 0; i < attributes.getLength(); i++) { 111 Node current = attributes.item(i); 112 stringbuffer.append(" "); 113 stringbuffer.append(current.getNodeName()); 114 stringbuffer.append("=\""); 115 stringbuffer.append(escapeAnsi(current.getNodeValue())); 116 stringbuffer.append("\""); 117 } 118 119 128 129 stringbuffer.append(">"); 130 NodeList children = node.getChildNodes(); 132 133 if (children != null) { 134 int childrenLength = children.getLength(); 135 for (int i = 0; i < childrenLength; i++) { 136 boolean childAddTrailingNewline = false; 137 if (allowAddingWhitespace) { 138 if ((i + 1) < childrenLength) { 139 Node nextChild = children.item(i + 1); 140 childAddTrailingNewline = 145 !isWhitespaceText(nextChild) && 146 !isTD(nextChild); 147 } 148 } 149 printNode(children.item(i), stringbuffer, depth + 1, 150 allowAddingWhitespace, 151 childAddTrailingNewline); 152 } 153 } 154 155 stringbuffer.append("</"); 156 stringbuffer.append(name); 157 stringbuffer.append(">"); 158 if (allowAddingWhitespace && addTrailingNewline) 159 stringbuffer.append("\n"); 160 break; 161 162 case Node.TEXT_NODE: 163 stringbuffer.append(node.getNodeValue()); 164 break; 165 } 166 } 167 168 private static boolean isWhitespaceText(Node node) { 169 if (node.getNodeType() == Node.TEXT_NODE) { 170 String val = node.getNodeValue(); 171 for (int i = val.length() - 1; i >= 0; i--) { 173 if (!Character.isWhitespace(val.charAt(i))) 174 return false; 175 } 176 return true; 177 } 178 return false; 179 } 180 181 private static boolean isTD(Node node) { 182 if (node.getNodeType() == Node.ELEMENT_NODE) { 183 String name = node.getNodeName(); 184 if (name != null && name.equalsIgnoreCase("td")) 185 return true; 186 } 187 return false; 188 } 189 190 194 public static Node getChildByNodeName(Node node, String name) { 195 NodeList children = node.getChildNodes(); 196 Node child; 197 int l = children.getLength(); 198 for (int i = 0; i < l; i++) { 199 child = children.item(i); 200 if (child.getNodeName().equalsIgnoreCase(name)) { 201 return child; 202 } 203 } 204 return null; 205 } 206 207 215 public static Node[] domDiff(Document oldDOM, Document newDOM) { 216 List nodeDiffs = new Vector (); 217 compareNodes(nodeDiffs, oldDOM.getDocumentElement(), 218 newDOM.getDocumentElement()); 219 return ((Node[]) nodeDiffs.toArray(new Node[0])); 220 } 221 222 223 231 public static boolean compareNodes(List nodeDiffs, 232 Node oldNode, Node newNode) { 233 if (!oldNode.getNodeName().equals(newNode.getNodeName())) { 234 nodeDiffs.add(newNode.getParentNode()); 236 return false; 237 } 238 if (!compareIDs(oldNode, newNode)) { 239 nodeDiffs.add(newNode.getParentNode()); 241 return false; 242 } 243 if (!compareAttributes(oldNode, newNode)) { 244 nodeDiffs.add(newNode); 245 return false; 246 } 247 if (!compareStrings(oldNode.getNodeValue(), 248 newNode.getNodeValue())) { 249 nodeDiffs.add(newNode); 251 return false; 252 } 253 254 NodeList oldChildNodes = oldNode.getChildNodes(); 255 NodeList newChildNodes = newNode.getChildNodes(); 256 257 int oldChildLength = oldChildNodes.getLength(); 258 int newChildLength = newChildNodes.getLength(); 259 260 if (oldChildLength != newChildLength) { 261 nodeDiffs.add(newNode); 262 return false; 263 } 264 265 boolean allChildrenMatch = true; 266 for (int i = 0; i < newChildLength; i++) { 267 if (!compareNodes(nodeDiffs, oldChildNodes.item(i), 268 newChildNodes.item(i))) { 269 allChildrenMatch = false; 270 } 271 } 272 273 return allChildrenMatch; 274 } 275 276 private static boolean compareStrings(String oldString, String newString) { 277 if ((null == oldString) && (null == newString)) { 278 return true; 279 } 280 try { 281 return (oldString.equals(newString)); 282 } catch (NullPointerException e) { 283 } 284 return false; 285 } 286 287 292 public static boolean compareIDs(Node oldNode, Node newNode) { 293 if (!(oldNode instanceof Element) && 294 !(newNode instanceof Element)) { 295 return true; 297 } 298 try { 299 return ((Element) oldNode).getAttribute("id").equals( 300 ((Element) newNode).getAttribute("id")); 301 } catch (Exception e) { 302 } 303 return false; 304 } 305 306 311 public static boolean compareAttributes(Node oldNode, Node newNode) { 312 boolean oldHasAttributes = oldNode.hasAttributes(); 313 boolean newHasAttributes = newNode.hasAttributes(); 314 315 if (!oldHasAttributes && !newHasAttributes) { 316 return true; 317 } 318 if (oldHasAttributes != newHasAttributes) { 319 return false; 320 } 321 322 NamedNodeMap oldMap = oldNode.getAttributes(); 323 NamedNodeMap newMap = newNode.getAttributes(); 324 325 int oldLength = oldMap.getLength(); 326 int newLength = newMap.getLength(); 327 328 if (oldLength != newLength) { 329 return false; 330 } 331 332 Node newAttribute = null; 333 Node oldAttribute = null; 334 for (int i = 0; i < newLength; i++) { 335 newAttribute = newMap.item(i); 336 oldAttribute = oldMap.getNamedItem(newAttribute.getNodeName()); 337 if (null == oldAttribute) { 338 return false; 339 } 340 if (!(String.valueOf(oldAttribute.getNodeValue()).equals( 341 String.valueOf(newAttribute.getNodeValue())))) { 342 return false; 343 } 344 } 345 346 return true; 347 348 } 349 350 public static Element ascendToNodeWithID(Node node) { 351 while (null != node) { 352 if (node instanceof Element) { 353 String id = ((Element) node).getAttribute("id"); 354 if ((null != id) && (!"".equals(id))) { 355 return (Element) node; 356 } 357 } 358 node = node.getParentNode(); 359 } 360 return (Element) node; 362 } 363 364 372 public static boolean escapeIsRequired(UIComponent uiComponent) { 373 Object escapeAttribute = uiComponent.getAttributes().get("escape"); 374 if (escapeAttribute != null) { 375 if (escapeAttribute instanceof String ) { 376 return Boolean.valueOf((String ) escapeAttribute).booleanValue(); 377 } else if (escapeAttribute instanceof Boolean ) { 378 return ((Boolean ) escapeAttribute).booleanValue(); 379 } 380 } 381 return true; } 383 384 public static String escapeAnsi(String text) { 385 if(null == text){ 386 return ""; 387 } 388 char[] chars = text.toCharArray(); 389 StringBuffer buffer = new StringBuffer (chars.length); 390 for (int index = 0; index < chars.length; index++) { 391 char ch = chars[index]; 392 if (ch <= 31) { 394 if (ch == '\t' || ch == '\n' || ch == '\r') { 395 buffer.append(ch); 396 } 397 } else if (ch == 127) { 399 } else if (ch == '>') { 401 buffer.append(">"); 402 } else if (ch == '<') { 403 buffer.append("<"); 404 } else if (ch == '&') { 405 buffer.append("&"); 406 } else if (ch == '\'') { 407 buffer.append("'"); 408 } else if (ch == '"') { 409 buffer.append("""); 410 } else if (ch >= 0xA0 && ch <= 0xff) { 411 buffer.append("&" + escapeAnsi(ch) + ";"); 412 } else if (ch == 0x20AC) { buffer.append("€"); 414 } else { 415 buffer.append(ch); 416 } 417 } 418 419 return buffer.toString(); 420 } 421 422 426 private static String escapeAnsi(char character) { 427 int indexOfEscapedCharacter = character - 0xA0; 428 return ansiCharacters[indexOfEscapedCharacter]; 429 } 430 431 438 private static String [] ansiCharacters = new String []{ 439 "nbsp" 440 , 441 "iexcl" , 442 "cent" , 443 "pound" , 444 "curren" , 445 "yen" , 446 "brvbar" 447 , 448 "sect" , 449 "uml" 450 , 451 "copy" , 452 "ordf" 453 , 454 "laquo" 455 , 456 "not" , 457 "shy" 458 , 459 "reg" 460 , 461 "macr" 462 , 463 "deg" , 464 "plusmn" 465 , 466 "sup2" 467 , 468 "sup3" 469 , 470 "acute" 471 , 472 "micro" , 473 "para" 474 , 475 "middot" 476 , 477 "cedil" , 478 "sup1" 479 , 480 "ordm" 481 , 482 "raquo" 483 , 484 "frac14" 485 , 486 "frac12" 487 , 488 "frac34" 489 , 490 "iquest" 491 , 492 "Agrave" 493 , 494 "Aacute" 495 , 496 "Acirc" 497 , 498 "Atilde" 499 , 500 "Auml" 501 , 502 "Aring" 503 , 504 "AElig" 505 , 506 "Ccedil" 507 , 508 "Egrave" 509 , 510 "Eacute" 511 , 512 "Ecirc" 513 , 514 "Euml" 515 , 516 "Igrave" 517 , 518 "Iacute" 519 , 520 "Icirc" 521 , 522 "Iuml" 523 , 524 "ETH" , 525 "Ntilde" 526 , 527 "Ograve" 528 , 529 "Oacute" 530 , 531 "Ocirc" 532 , 533 "Otilde" 534 , 535 "Ouml" 536 , 537 "times" , 538 "Oslash" 539 , 540 "Ugrave" 541 , 542 "Uacute" 543 , 544 "Ucirc" 545 , 546 "Uuml" 547 , 548 "Yacute" 549 , 550 "THORN" 551 , 552 "szlig" 553 , 554 "agrave" 555 , 556 "aacute" 557 , 558 "acirc" 559 , 560 "atilde" 561 , 562 "auml" 563 , 564 "aring" 565 , 566 "aelig" 567 , 568 "ccedil" 569 , 570 "egrave" 571 , 572 "eacute" 573 , 574 "ecirc" 575 , 576 "euml" 577 , 578 "igrave" 579 , 580 "iacute" 581 , 582 "icirc" 583 , 584 "iuml" 585 , 586 "eth" , 587 "ntilde" 588 , 589 "ograve" 590 , 591 "oacute" 592 , 593 "ocirc" 594 , 595 "otilde" 596 , 597 "ouml" 598 , 599 "divide" , 600 "oslash" 601 , 602 "ugrave" 603 , 604 "uacute" 605 , 606 "ucirc" 607 , 608 "uuml" 609 , 610 "yacute" 611 , 612 "thorn" , 613 "yuml" 614 , 615 }; 616 617 } 618 | Popular Tags |