src/share/classes/com/sun/org/apache/xml/internal/security/c14n/helper/C14nHelper.java

Print this page


   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 }
   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /**
   6  * Licensed to the Apache Software Foundation (ASF) under one
   7  * or more contributor license agreements. See the NOTICE file
   8  * distributed with this work for additional information
   9  * regarding copyright ownership. The ASF licenses this file
  10  * to you under the Apache License, Version 2.0 (the
  11  * "License"); you may not use this file except in compliance
  12  * with the License. You may obtain a copy of the License at
  13  *
  14  * http://www.apache.org/licenses/LICENSE-2.0
  15  *
  16  * Unless required by applicable law or agreed to in writing,
  17  * software distributed under the License is distributed on an
  18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19  * KIND, either express or implied. See the License for the
  20  * specific language governing permissions and limitations
  21  * under the License.
  22  */
  23 package com.sun.org.apache.xml.internal.security.c14n.helper;
  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  * Temporary swapped static functions from the normalizer Section
  33  *
  34  * @author Christian Geuer-Pollmann
  35  */
  36 public class C14nHelper {
  37 
  38     /**
  39      * Constructor C14nHelper
  40      *
  41      */
  42     private C14nHelper() {

  43         // don't allow instantiation
  44     }
  45 
  46     /**
  47      * Method namespaceIsRelative
  48      *
  49      * @param namespace
  50      * @return true if the given namespace is relative. 
  51      */
  52     public static boolean namespaceIsRelative(Attr namespace) {
  53         return !namespaceIsAbsolute(namespace);
  54     }
  55 
  56     /**
  57      * Method namespaceIsRelative
  58      *
  59      * @param namespaceValue
  60      * @return true if the given namespace is relative.
  61      */
  62     public static boolean namespaceIsRelative(String namespaceValue) {
  63         return !namespaceIsAbsolute(namespaceValue);
  64     }
  65 
  66     /**
  67      * Method namespaceIsAbsolute
  68      *
  69      * @param namespace
  70      * @return true if the given namespace is absolute.
  71      */
  72     public static boolean namespaceIsAbsolute(Attr namespace) {
  73         return namespaceIsAbsolute(namespace.getValue());
  74     }
  75 
  76     /**
  77      * Method namespaceIsAbsolute
  78      *
  79      * @param namespaceValue
  80      * @return true if the given namespace is absolute.
  81      */
  82     public static boolean namespaceIsAbsolute(String namespaceValue) {

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


  98         if (attr == null) {
  99             return;
 100         }
 101 
 102         String nodeAttrName = attr.getNodeName();
 103         boolean definesDefaultNS = nodeAttrName.equals("xmlns");
 104         boolean definesNonDefaultNS = nodeAttrName.startsWith("xmlns:");
 105 
 106         if ((definesDefaultNS || definesNonDefaultNS) && namespaceIsRelative(attr)) {

 107             String parentName = attr.getOwnerElement().getTagName();
 108             String attrValue = attr.getValue();
 109             Object exArgs[] = { parentName, nodeAttrName, attrValue };
 110 
 111             throw new CanonicalizationException(
 112                 "c14n.Canonicalizer.RelativeNamespace", exArgs
 113             );
 114         }
 115     }
 116 
 117     /**
 118      * This method throws a CanonicalizationException if the supplied Document
 119      * is not able to be traversed using a TreeWalker.
 120      *
 121      * @param document
 122      * @throws CanonicalizationException
 123      */
 124     public static void checkTraversability(Document document)
 125         throws CanonicalizationException {

 126         if (!document.isSupported("Traversal", "2.0")) {
 127             Object exArgs[] = {document.getImplementation().getClass().getName() };

 128 
 129             throw new CanonicalizationException(
 130                 "c14n.Canonicalizer.TraversalNotSupported", exArgs
 131             );
 132         }
 133     }
 134 
 135     /**
 136      * This method throws a CanonicalizationException if the supplied Element
 137      * contains any relative namespaces.
 138      *
 139      * @param ctxNode
 140      * @throws CanonicalizationException
 141      * @see C14nHelper#assertNotRelativeNS(Attr)
 142      */
 143     public static void checkForRelativeNamespace(Element ctxNode)
 144         throws CanonicalizationException {

 145         if (ctxNode != null) {
 146             NamedNodeMap attributes = ctxNode.getAttributes();
 147 
 148             for (int i = 0; i < attributes.getLength(); i++) {
 149                 C14nHelper.assertNotRelativeNS((Attr) attributes.item(i));
 150             }
 151         } else {
 152             throw new CanonicalizationException("Called checkForRelativeNamespace() on null");

 153         }
 154     }
 155 }