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.transforms.params;
  24 
  25 import java.util.Set;
  26 import java.util.SortedSet;
  27 import java.util.TreeSet;
  28 
  29 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
  30 import com.sun.org.apache.xml.internal.security.transforms.TransformParam;
  31 import com.sun.org.apache.xml.internal.security.utils.ElementProxy;
  32 import org.w3c.dom.Document;
  33 import org.w3c.dom.Element;
  34 
  35 /**
  36  * This Object serves as Content for the ds:Transforms for exclusive
  37  * Canonicalization.
  38  * <BR />
  39  * It implements the {@link Element} interface
  40  * and can be used directly in a DOM tree.
  41  *
  42  * @author Christian Geuer-Pollmann
  43  */
  44 public class InclusiveNamespaces extends ElementProxy implements TransformParam {
  45 
  46     /** Field _TAG_EC_INCLUSIVENAMESPACES */
  47     public static final String _TAG_EC_INCLUSIVENAMESPACES =
  48         "InclusiveNamespaces";
  49 
  50     /** Field _ATT_EC_PREFIXLIST */
  51     public static final String _ATT_EC_PREFIXLIST = "PrefixList";
  52 
  53     /** Field ExclusiveCanonicalizationNamespace */
  54     public static final String ExclusiveCanonicalizationNamespace =
  55         "http://www.w3.org/2001/10/xml-exc-c14n#";
  56 
  57     /**
  58      * Constructor XPathContainer
  59      *
  60      * @param doc
  61      * @param prefixList
  62      */
  63     public InclusiveNamespaces(Document doc, String prefixList) {
  64         this(doc, InclusiveNamespaces.prefixStr2Set(prefixList));
  65     }
  66 
  67     /**
  68      * Constructor InclusiveNamespaces
  69      *
  70      * @param doc
  71      * @param prefixes
  72      */
  73     public InclusiveNamespaces(Document doc, Set<String> prefixes) {
  74         super(doc);
  75 
  76         SortedSet<String> prefixList = null;
  77         if (prefixes instanceof SortedSet<?>) {
  78             prefixList = (SortedSet<String>)prefixes;
  79         } else {
  80             prefixList = new TreeSet<String>(prefixes);
  81         }
  82 
  83         StringBuilder sb = new StringBuilder();
  84         for (String prefix : prefixList) {
  85             if (prefix.equals("xmlns")) {
  86                 sb.append("#default ");
  87             } else {
  88                 sb.append(prefix + " ");
  89             }
  90         }
  91 
  92         this.constructionElement.setAttributeNS(
  93             null, InclusiveNamespaces._ATT_EC_PREFIXLIST, sb.toString().trim());
  94     }
  95 
  96     /**
  97      * Constructor InclusiveNamespaces
  98      *
  99      * @param element
 100      * @param BaseURI
 101      * @throws XMLSecurityException
 102      */
 103     public InclusiveNamespaces(Element element, String BaseURI)
 104         throws XMLSecurityException {
 105         super(element, BaseURI);
 106     }
 107 
 108     /**
 109      * Method getInclusiveNamespaces
 110      *
 111      * @return The Inclusive Namespace string
 112      */
 113     public String getInclusiveNamespaces() {
 114         return this.constructionElement.getAttributeNS(null, InclusiveNamespaces._ATT_EC_PREFIXLIST);
 115     }
 116 
 117     /**
 118      * Decodes the <code>inclusiveNamespaces</code> String and returns all
 119      * selected namespace prefixes as a Set. The <code>#default</code>
 120      * namespace token is represented as an empty namespace prefix
 121      * (<code>"xmlns"</code>).
 122      * <BR/>
 123      * The String <code>inclusiveNamespaces=" xenc    ds #default"</code>
 124      * is returned as a Set containing the following Strings:
 125      * <UL>
 126      * <LI><code>xmlns</code></LI>
 127      * <LI><code>xenc</code></LI>
 128      * <LI><code>ds</code></LI>
 129      * </UL>
 130      *
 131      * @param inclusiveNamespaces
 132      * @return A set to string
 133      */
 134     public static SortedSet<String> prefixStr2Set(String inclusiveNamespaces) {
 135         SortedSet<String> prefixes = new TreeSet<String>();
 136 
 137         if ((inclusiveNamespaces == null) || (inclusiveNamespaces.length() == 0)) {
 138             return prefixes;
 139         }
 140 
 141         String[] tokens = inclusiveNamespaces.split("\\s");
 142         for (String prefix : tokens) {
 143             if (prefix.equals("#default")) {
 144                 prefixes.add("xmlns");
 145             } else {
 146                 prefixes.add(prefix);
 147             }
 148         }
 149 
 150         return prefixes;
 151     }
 152 
 153     /**
 154      * Method getBaseNamespace
 155      *
 156      * @inheritDoc
 157      */
 158     public String getBaseNamespace() {
 159         return InclusiveNamespaces.ExclusiveCanonicalizationNamespace;
 160     }
 161 
 162     /**
 163      * Method getBaseLocalName
 164      *
 165      * @inheritDoc
 166      */
 167     public String getBaseLocalName() {
 168         return InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES;
 169     }
 170 }