1 2 18 package com.sun.org.apache.xml.internal.security.utils; 19 20 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 28 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; 29 import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer; 30 import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; 31 import org.w3c.dom.Attr ; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.NamedNodeMap ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 import org.w3c.dom.Text ; 38 39 40 41 46 public class XMLUtils { 47 48 52 private XMLUtils() { 53 54 } 56 57 58 64 public static void getSet(Node rootNode,Set result,Node exclude ,boolean com) { 65 if ((exclude!=null) && isDescendantOrSelf(exclude,rootNode)){ 66 return; 67 } 68 getSetRec(rootNode,result,exclude,com); 69 } 70 static final void getSetRec(final Node rootNode,final Set result, 71 final Node exclude ,final boolean com) { 72 if (rootNode==exclude) { 74 return; 75 } 76 switch (rootNode.getNodeType()) { 77 case Node.ELEMENT_NODE: 78 result.add(rootNode); 79 Element el=(Element )rootNode; 80 if (el.hasAttributes()) { 81 NamedNodeMap nl = ((Element )rootNode).getAttributes(); 82 for (int i=0;i<nl.getLength();i++) { 83 result.add(nl.item(i)); 84 } 85 } 86 case Node.DOCUMENT_NODE: 88 for (Node r=rootNode.getFirstChild();r!=null;r=r.getNextSibling()){ 89 if (r.getNodeType()==Node.TEXT_NODE) { 90 result.add(r); 91 while ((r!=null) && (r.getNodeType()==Node.TEXT_NODE)) { 92 r=r.getNextSibling(); 93 } 94 if (r==null) 95 return; 96 } 97 getSetRec(r,result,exclude,com); 98 } 99 return; 100 case Node.COMMENT_NODE: 101 if (com) { 102 result.add(rootNode); 103 } 104 return; 105 case Node.DOCUMENT_TYPE_NODE: 106 return; 107 default: 108 result.add(rootNode); 109 } 110 return; 111 } 112 113 114 120 public static void outputDOM(Node contextNode, OutputStream os) { 121 XMLUtils.outputDOM(contextNode, os, false); 122 } 123 124 133 public static void outputDOM(Node contextNode, OutputStream os, 134 boolean addPreamble) { 135 136 try { 137 if (addPreamble) { 138 os.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes()); 139 } 140 141 os.write( 142 Canonicalizer.getInstance( 143 Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree( 144 contextNode)); 145 } catch (IOException ex) {} 146 catch (InvalidCanonicalizerException ex) { 147 ex.printStackTrace(); 148 } catch (CanonicalizationException ex) { 149 ex.printStackTrace(); 150 } 151 } 152 153 166 public static void outputDOMc14nWithComments(Node contextNode, 167 OutputStream os) { 168 169 try { 170 os.write( 171 Canonicalizer.getInstance( 172 Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree( 173 contextNode)); 174 } catch (IOException ex) { 175 176 } catch (InvalidCanonicalizerException ex) { 178 179 } catch (CanonicalizationException ex) { 181 182 } 184 } 185 186 187 193 public static String getFullTextChildrenFromElement(Element element) { 194 195 StringBuffer sb = new StringBuffer (); 196 NodeList children = element.getChildNodes(); 197 int iMax = children.getLength(); 198 199 for (int i = 0; i < iMax; i++) { 200 Node curr = children.item(i); 201 202 if (curr.getNodeType() == Node.TEXT_NODE) { 203 sb.append(((Text ) curr).getData()); 204 } 205 } 206 207 return sb.toString(); 208 } 209 210 211 218 public static Element createElementInSignatureSpace(Document doc, 219 String elementName) { 220 221 if (doc == null) { 222 throw new RuntimeException ("Document is null"); 223 } 224 225 String ds = Constants.getSignatureSpecNSprefix(); 226 227 if ((ds == null) || (ds.length() == 0)) { 228 Element element = doc.createElementNS(Constants.SignatureSpecNS, 229 elementName); 230 231 element.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", 232 Constants.SignatureSpecNS); 233 234 return element; 235 } 236 Element element = doc.createElementNS(Constants.SignatureSpecNS, 237 ds + ":" + elementName); 238 239 element.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + ds, 240 Constants.SignatureSpecNS); 241 242 return element; 243 244 } 245 246 247 255 public static boolean elementIsInSignatureSpace(Element element, 256 String localName) { 257 258 if ((element == null) || 259 !Constants.SignatureSpecNS.equals(element.getNamespaceURI()) ){ 260 return false; 261 } 262 263 if (!element.getLocalName().equals(localName)) { 264 return false; 265 } 266 267 return true; 268 } 269 270 278 public static boolean elementIsInEncryptionSpace(Element element, 279 String localName) { 280 281 if ((element == null) || 282 !EncryptionConstants.EncryptionSpecNS.equals(element.getNamespaceURI()) 283 ){ 284 return false; 285 } 286 287 if (!element.getLocalName().equals(localName)) { 288 return false; 289 } 290 291 return true; 292 } 293 294 303 public static Document getOwnerDocument(Node node) { 304 305 if (node.getNodeType() == Node.DOCUMENT_NODE) { 306 return (Document ) node; 307 } 308 try { 309 return node.getOwnerDocument(); 310 } catch (NullPointerException npe) { 311 throw new NullPointerException (I18n.translate("endorsed.jdk1.4.0") 312 + " Original message was \"" 313 + npe.getMessage() + "\""); 314 } 315 316 } 317 318 327 public static Document getOwnerDocument(Set xpathNodeSet) { 328 NullPointerException npe = null; 329 Iterator iterator = xpathNodeSet.iterator(); 330 while(iterator.hasNext()) { 331 Node node = (Node ) iterator.next(); 332 int nodeType =node.getNodeType(); 333 if (nodeType == Node.DOCUMENT_NODE) { 334 return (Document ) node; 335 } 336 try { 337 if (nodeType==Node.ATTRIBUTE_NODE) { 338 return ((Attr )node).getOwnerElement().getOwnerDocument(); 339 } 340 return node.getOwnerDocument(); 341 } catch (NullPointerException e) { 342 npe = e; 343 } 344 345 } 346 throw new NullPointerException (I18n.translate("endorsed.jdk1.4.0") 347 + " Original message was \"" 348 + (npe == null ? "" : npe.getMessage()) + "\""); 349 } 350 351 352 353 361 public static Element createDSctx(Document doc, String prefix, 362 String namespace) { 363 364 if ((prefix == null) || (prefix.trim().length() == 0)) { 365 throw new IllegalArgumentException ("You must supply a prefix"); 366 } 367 368 Element ctx = doc.createElementNS(null, "namespaceContext"); 369 370 ctx.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + prefix.trim(), 371 namespace); 372 373 return ctx; 374 } 375 376 377 378 383 public static void addReturnToElement(Element e) { 384 385 Document doc = e.getOwnerDocument(); 386 387 e.appendChild(doc.createTextNode("\n")); 388 } 389 390 396 public static Set convertNodelistToSet(NodeList xpathNodeSet) { 397 398 if (xpathNodeSet == null) { 399 return new HashSet (); 400 } 401 402 int length = xpathNodeSet.getLength(); 403 Set set = new HashSet (length); 404 405 for (int i = 0; i < length; i++) { 406 set.add(xpathNodeSet.item(i)); 407 } 408 409 return set; 410 } 411 412 413 424 public static void circumventBug2650(Document doc) { 425 426 Element documentElement = doc.getDocumentElement(); 427 428 Attr xmlnsAttr = 430 documentElement.getAttributeNodeNS(Constants.NamespaceSpecNS, "xmlns"); 431 432 if (xmlnsAttr == null) { 433 documentElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", ""); 434 } 435 436 XMLUtils.circumventBug2650internal(doc); 437 } 438 439 445 private static void circumventBug2650internal(Node node) { 446 Node parent=null; 447 Node sibling=null; 448 final String namespaceNs=Constants.NamespaceSpecNS; 449 do { 450 switch (node.getNodeType()) { 451 case Node.ELEMENT_NODE : 452 Element element = (Element ) node; 453 if (!element.hasChildNodes()) 454 break; 455 if (element.hasAttributes()) { 456 NamedNodeMap attributes = element.getAttributes(); 457 int attributesLength = attributes.getLength(); 458 459 for (Node child = element.getFirstChild(); child!=null; 460 child=child.getNextSibling()) { 461 462 if (child.getNodeType() != Node.ELEMENT_NODE) { 463 continue; 464 } 465 Element childElement = (Element ) child; 466 467 for (int i = 0; i < attributesLength; i++) { 468 Attr currentAttr = (Attr ) attributes.item(i); 469 if (!namespaceNs.equals(currentAttr.getNamespaceURI())) 470 continue; 471 if (childElement.hasAttributeNS(namespaceNs, 472 currentAttr.getLocalName())) { 473 continue; 474 } 475 childElement.setAttributeNS(namespaceNs, 476 currentAttr.getName(), 477 currentAttr.getNodeValue()); 478 479 480 } 481 } 482 } 483 case Node.ENTITY_REFERENCE_NODE : 484 case Node.DOCUMENT_NODE : 485 parent=node; 486 sibling=node.getFirstChild(); 487 break; 488 } 489 while ((sibling==null) && (parent!=null)) { 490 sibling=parent.getNextSibling(); 491 parent=parent.getParentNode(); 492 }; 493 if (sibling==null) { 494 return; 495 } 496 497 node=sibling; 498 sibling=node.getNextSibling(); 499 } while (true); 500 } 501 502 508 public static Element selectDsNode(Node sibling, String nodeName, int number) { 509 while (sibling!=null) { 510 if (nodeName.equals(sibling.getLocalName()) 511 && Constants.SignatureSpecNS.equals(sibling.getNamespaceURI())) { 512 if (number==0){ 513 return (Element )sibling; 514 } 515 number--; 516 } 517 sibling=sibling.getNextSibling(); 518 } 519 return null; 520 } 521 522 528 529 public static Element selectXencNode(Node sibling, String nodeName, int number) { 530 while (sibling!=null) { 531 if (nodeName.equals(sibling.getLocalName()) 532 && EncryptionConstants.EncryptionSpecNS.equals(sibling.getNamespaceURI())) { 533 if (number==0){ 534 return (Element )sibling; 535 } 536 number--; 537 } 538 sibling=sibling.getNextSibling(); 539 } 540 return null; 541 } 542 543 544 550 public static Text selectDsNodeText(Node sibling, String nodeName, int number) { 551 Node n=selectDsNode(sibling,nodeName,number); 552 if (n==null) { 553 return null; 554 } 555 n=n.getFirstChild(); 556 while (n!=null && n.getNodeType()!=Node.TEXT_NODE) { 557 n=n.getNextSibling(); 558 } 559 return (Text )n; 560 } 561 562 569 public static Text selectNodeText(Node sibling, String uri, String nodeName, int number) { 570 Node n=selectNode(sibling,uri,nodeName,number); 571 if (n==null) { 572 return null; 573 } 574 n=n.getFirstChild(); 575 while (n!=null && n.getNodeType()!=Node.TEXT_NODE) { 576 n=n.getNextSibling(); 577 } 578 return (Text )n; 579 } 580 581 588 public static Element selectNode(Node sibling, String uri,String nodeName, int number) { 589 while (sibling!=null) { 590 if (nodeName.equals(sibling.getLocalName()) 591 && uri.equals(sibling.getNamespaceURI())) { 592 if (number==0){ 593 return (Element )sibling; 594 } 595 number--; 596 } 597 sibling=sibling.getNextSibling(); 598 } 599 return null; 600 } 601 602 607 public static Element [] selectDsNodes(Node sibling,String nodeName) { 608 return selectNodes(sibling,Constants.SignatureSpecNS,nodeName); 609 } 610 611 617 public static Element [] selectNodes(Node sibling,String uri,String nodeName) { 618 int size=20; 619 Element [] a= new Element [size]; 620 int curr=0; 621 while (sibling!=null) { 623 if (nodeName.equals(sibling.getLocalName()) 624 && uri.equals(sibling.getNamespaceURI())) { 625 a[curr++]=(Element )sibling; 626 if (size<=curr) { 627 int cursize= size<<2; 628 Element []cp=new Element [cursize]; 629 System.arraycopy(a,0,cp,0,size); 630 a=cp; 631 size=cursize; 632 } 633 } 634 sibling=sibling.getNextSibling(); 635 } 636 Element []af=new Element [curr]; 637 System.arraycopy(a,0,af,0,curr); 638 return af; 639 } 640 641 646 public static Set excludeNodeFromSet(Node signatureElement, Set inputSet) { 647 Set resultSet = new HashSet (); 648 Iterator iterator = inputSet.iterator(); 649 650 while (iterator.hasNext()) { 651 Node inputNode = (Node ) iterator.next(); 652 653 if (!XMLUtils 654 .isDescendantOrSelf(signatureElement, inputNode)) { 655 resultSet.add(inputNode); 656 } 657 } 658 return resultSet; 659 } 660 661 669 static public boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) { 670 671 if (ctx == descendantOrSelf) { 672 return true; 673 } 674 675 Node parent = descendantOrSelf; 676 677 while (true) { 678 if (parent == null) { 679 return false; 680 } 681 682 if (parent == ctx) { 683 return true; 684 } 685 686 if (parent.getNodeType() == Node.ATTRIBUTE_NODE) { 687 parent = ((Attr ) parent).getOwnerElement(); 688 } else { 689 parent = parent.getParentNode(); 690 } 691 } 692 } 693 } 694 | Popular Tags |