1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 1999-2004 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 * 20 */ 21 package com.sun.org.apache.xml.internal.security.c14n.helper; 22 23 24 25 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; 26 import org.w3c.dom.Attr; 27 import org.w3c.dom.Document; 28 import org.w3c.dom.Element; 29 import org.w3c.dom.NamedNodeMap; 30 31 32 /** 33 * Temporary swapped static functions from the normalizer Section 34 * 35 * @author Christian Geuer-Pollmann 36 */ 37 public class C14nHelper { 38 39 /** 40 * Constructor C14nHelper 41 * 42 */ 43 private C14nHelper() { 44 45 // don't allow instantiation 46 } 47 48 /** 49 * Method namespaceIsRelative 50 * 51 * @param namespace 52 * @return true if the given namespace is relative. 53 */ 54 public static boolean namespaceIsRelative(Attr namespace) { 55 return !namespaceIsAbsolute(namespace); 56 } 57 58 /** 59 * Method namespaceIsRelative 60 * 61 * @param namespaceValue 62 * @return true if the given namespace is relative. 63 */ 64 public static boolean namespaceIsRelative(String namespaceValue) { 65 return !namespaceIsAbsolute(namespaceValue); 66 } 67 68 /** 69 * Method namespaceIsAbsolute 70 * 71 * @param namespace 72 * @return true if the given namespace is absolute. 73 */ 74 public static boolean namespaceIsAbsolute(Attr namespace) { 75 return namespaceIsAbsolute(namespace.getValue()); 76 } 77 78 /** 79 * Method namespaceIsAbsolute 80 * 81 * @param namespaceValue 82 * @return true if the given namespace is absolute. 83 */ 84 public static boolean namespaceIsAbsolute(String namespaceValue) { 85 86 // assume empty namespaces are absolute 87 if (namespaceValue.length() == 0) { 88 return true; 89 } 90 return namespaceValue.indexOf(':')>0; 91 } 92 93 /** 94 * This method throws an exception if the Attribute value contains 95 * a relative URI. 96 * 97 * @param attr 98 * @throws CanonicalizationException 99 */ 100 public static void assertNotRelativeNS(Attr attr) 101 throws CanonicalizationException { 102 103 if (attr == null) { 104 return; 105 } 106 107 String nodeAttrName = attr.getNodeName(); 108 boolean definesDefaultNS = nodeAttrName.equals("xmlns"); 109 boolean definesNonDefaultNS = nodeAttrName.startsWith("xmlns:"); 110 111 if (definesDefaultNS || definesNonDefaultNS) { 112 if (namespaceIsRelative(attr)) { 113 String parentName = attr.getOwnerElement().getTagName(); 114 String attrValue = attr.getValue(); 115 Object exArgs[] = { parentName, nodeAttrName, attrValue }; 116 117 throw new CanonicalizationException( 118 "c14n.Canonicalizer.RelativeNamespace", exArgs); 119 } 120 } 121 } 122 123 /** 124 * This method throws a CanonicalizationException if the supplied Document 125 * is not able to be traversed using a TreeWalker. 126 * 127 * @param document 128 * @throws CanonicalizationException 129 */ 130 public static void checkTraversability(Document document) 131 throws CanonicalizationException { 132 133 if (!document.isSupported("Traversal", "2.0")) { 134 Object exArgs[] = { 135 document.getImplementation().getClass().getName() }; 136 137 throw new CanonicalizationException( 138 "c14n.Canonicalizer.TraversalNotSupported", exArgs); 139 } 140 } 141 142 /** 143 * This method throws a CanonicalizationException if the supplied Element 144 * contains any relative namespaces. 145 * 146 * @param ctxNode 147 * @throws CanonicalizationException 148 * @see C14nHelper#assertNotRelativeNS(Attr) 149 */ 150 public static void checkForRelativeNamespace(Element ctxNode) 151 throws CanonicalizationException { 152 153 if (ctxNode != null) { 154 NamedNodeMap attributes = ctxNode.getAttributes(); 155 156 for (int i = 0; i < attributes.getLength(); i++) { 157 C14nHelper.assertNotRelativeNS((Attr) attributes.item(i)); 158 } 159 } else { 160 throw new CanonicalizationException( 161 "Called checkForRelativeNamespace() on null"); 162 } 163 } 164 }