KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > c14n > helper > C14nHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

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 JavaDoc;
23 import org.w3c.dom.Document JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.NamedNodeMap JavaDoc;
26
27
28 /**
29  * Temporary swapped static functions from the normalizer Section
30  *
31  * @author Christian Geuer-Pollmann
32  */

33 public class C14nHelper {
34
35    /**
36     * Constructor C14nHelper
37     *
38     */

39    private C14nHelper() {
40
41       // don't allow instantiation
42
}
43
44    /**
45     * Method namespaceIsRelative
46     *
47     * @param namespace
48     * @return true if the given namespace is relative.
49     */

50    public static boolean namespaceIsRelative(Attr JavaDoc namespace) {
51       return !namespaceIsAbsolute(namespace);
52    }
53
54    /**
55     * Method namespaceIsRelative
56     *
57     * @param namespaceValue
58     * @return true if the given namespace is relative.
59     */

60    public static boolean namespaceIsRelative(String JavaDoc namespaceValue) {
61       return !namespaceIsAbsolute(namespaceValue);
62    }
63
64    /**
65     * Method namespaceIsAbsolute
66     *
67     * @param namespace
68     * @return true if the given namespace is absolute.
69     */

70    public static boolean namespaceIsAbsolute(Attr JavaDoc namespace) {
71       return namespaceIsAbsolute(namespace.getValue());
72    }
73
74    /**
75     * Method namespaceIsAbsolute
76     *
77     * @param namespaceValue
78     * @return true if the given namespace is absolute.
79     */

80    public static boolean namespaceIsAbsolute(String JavaDoc namespaceValue) {
81
82       // assume empty namespaces are absolute
83
if (namespaceValue.length() == 0) {
84          return true;
85       }
86       return namespaceValue.indexOf(':')>0;
87    }
88
89    /**
90     * This method throws an exception if the Attribute value contains
91     * a relative URI.
92     *
93     * @param attr
94     * @throws CanonicalizationException
95     */

96    public static void assertNotRelativeNS(Attr JavaDoc attr)
97            throws CanonicalizationException {
98
99       if (attr == null) {
100          return;
101       }
102
103       String JavaDoc 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 JavaDoc parentName = attr.getOwnerElement().getTagName();
110             String JavaDoc attrValue = attr.getValue();
111             Object JavaDoc exArgs[] = { parentName, nodeAttrName, attrValue };
112
113             throw new CanonicalizationException(
114                "c14n.Canonicalizer.RelativeNamespace", exArgs);
115          }
116       }
117    }
118
119    /**
120     * This method throws a CanonicalizationException if the supplied Document
121     * is not able to be traversed using a TreeWalker.
122     *
123     * @param document
124     * @throws CanonicalizationException
125     */

126    public static void checkTraversability(Document JavaDoc document)
127            throws CanonicalizationException {
128
129       if (!document.isSupported("Traversal", "2.0")) {
130          Object JavaDoc exArgs[] = {
131             document.getImplementation().getClass().getName() };
132
133          throw new CanonicalizationException(
134             "c14n.Canonicalizer.TraversalNotSupported", exArgs);
135       }
136    }
137
138    /**
139     * This method throws a CanonicalizationException if the supplied Element
140     * contains any relative namespaces.
141     *
142     * @param ctxNode
143     * @throws CanonicalizationException
144     * @see C14nHelper#assertNotRelativeNS(Attr)
145     */

146    public static void checkForRelativeNamespace(Element JavaDoc ctxNode)
147            throws CanonicalizationException {
148
149       if (ctxNode != null) {
150          NamedNodeMap JavaDoc attributes = ctxNode.getAttributes();
151
152          for (int i = 0; i < attributes.getLength(); i++) {
153             C14nHelper.assertNotRelativeNS((Attr JavaDoc) attributes.item(i));
154          }
155       } else {
156          throw new CanonicalizationException(
157             "Called checkForRelativeNamespace() on null");
158       }
159    }
160 }
161
Popular Tags