1 17 package com.sun.org.apache.xml.internal.security.c14n.helper; 18 19 20 21 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; 22 import org.w3c.dom.Attr ; 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.NamedNodeMap ; 26 27 28 33 public class C14nHelper { 34 35 39 private C14nHelper() { 40 41 } 43 44 50 public static boolean namespaceIsRelative(Attr namespace) { 51 return !namespaceIsAbsolute(namespace); 52 } 53 54 60 public static boolean namespaceIsRelative(String namespaceValue) { 61 return !namespaceIsAbsolute(namespaceValue); 62 } 63 64 70 public static boolean namespaceIsAbsolute(Attr namespace) { 71 return namespaceIsAbsolute(namespace.getValue()); 72 } 73 74 80 public static boolean namespaceIsAbsolute(String namespaceValue) { 81 82 if (namespaceValue.length() == 0) { 84 return true; 85 } 86 return namespaceValue.indexOf(':')>0; 87 } 88 89 96 public static void assertNotRelativeNS(Attr attr) 97 throws CanonicalizationException { 98 99 if (attr == null) { 100 return; 101 } 102 103 String nodeAttrName = attr.getNodeName(); 104 boolean definesDefaultNS = nodeAttrName.equals("xmlns"); 105 boolean definesNonDefaultNS = nodeAttrName.startsWith("xmlns:"); 106 107 if (definesDefaultNS || definesNonDefaultNS) { 108 if (namespaceIsRelative(attr)) { 109 String parentName = attr.getOwnerElement().getTagName(); 110 String attrValue = attr.getValue(); 111 Object exArgs[] = { parentName, nodeAttrName, attrValue }; 112 113 throw new CanonicalizationException( 114 "c14n.Canonicalizer.RelativeNamespace", exArgs); 115 } 116 } 117 } 118 119 126 public static void checkTraversability(Document document) 127 throws CanonicalizationException { 128 129 if (!document.isSupported("Traversal", "2.0")) { 130 Object exArgs[] = { 131 document.getImplementation().getClass().getName() }; 132 133 throw new CanonicalizationException( 134 "c14n.Canonicalizer.TraversalNotSupported", exArgs); 135 } 136 } 137 138 146 public static void checkForRelativeNamespace(Element ctxNode) 147 throws CanonicalizationException { 148 149 if (ctxNode != null) { 150 NamedNodeMap attributes = ctxNode.getAttributes(); 151 152 for (int i = 0; i < attributes.getLength(); i++) { 153 C14nHelper.assertNotRelativeNS((Attr ) attributes.item(i)); 154 } 155 } else { 156 throw new CanonicalizationException( 157 "Called checkForRelativeNamespace() on null"); 158 } 159 } 160 } 161 | Popular Tags |