J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | /* |
| 26 | * $Id: XPathFilterParameterSpec.java,v 1.4 2005/05/10 16:40:17 mullan Exp $ |
| 27 | */ |
| 28 | package javax.xml.crypto.dsig.spec; |
| 29 | |
| 30 | import javax.xml.crypto.dsig.Transform; |
| 31 | import java.util.Collections; |
| 32 | import java.util.HashMap; |
| 33 | import java.util.Iterator; |
| 34 | import java.util.Map; |
| 35 | import java.util.Map.Entry; |
| 36 | |
| 37 | /** |
| 38 | * Parameters for the <a href="http://www.w3.org/TR/xmldsig-core/#sec-XPath"> |
| 39 | * XPath Filtering Transform Algorithm</a>. |
| 40 | * The parameters include the XPath expression and an optional <code>Map</code> |
| 41 | * of additional namespace prefix mappings. The XML Schema Definition of |
| 42 | * the XPath Filtering transform parameters is defined as: |
| 43 | * <pre><code> |
| 44 | * <element name="XPath" type="string"/> |
| 45 | * </code></pre> |
| 46 | * |
| 47 | * @author Sean Mullan |
| 48 | * @author JSR 105 Expert Group |
| 49 | * @since 1.6 |
| 50 | * @see Transform |
| 51 | */ |
| 52 | public final class XPathFilterParameterSpec implements TransformParameterSpec { |
| 53 | |
| 54 | private String xPath; |
| 55 | private Map nsMap; |
| 56 | |
| 57 | /** |
| 58 | * Creates an <code>XPathFilterParameterSpec</code> with the specified |
| 59 | * XPath expression. |
| 60 | * |
| 61 | * @param xPath the XPath expression to be evaluated |
| 62 | * @throws NullPointerException if <code>xPath</code> is <code>null</code> |
| 63 | */ |
| 64 | public XPathFilterParameterSpec(String xPath) { |
| 65 | if (xPath == null) { |
| 66 | throw new NullPointerException(); |
| 67 | } |
| 68 | this.xPath = xPath; |
| 69 | this.nsMap = Collections.EMPTY_MAP; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Creates an <code>XPathFilterParameterSpec</code> with the specified |
| 74 | * XPath expression and namespace map. The map is copied to protect against |
| 75 | * subsequent modification. |
| 76 | * |
| 77 | * @param xPath the XPath expression to be evaluated |
| 78 | * @param namespaceMap the map of namespace prefixes. Each key is a |
| 79 | * namespace prefix <code>String</code> that maps to a corresponding |
| 80 | * namespace URI <code>String</code>. |
| 81 | * @throws NullPointerException if <code>xPath</code> or |
| 82 | * <code>namespaceMap</code> are <code>null</code> |
| 83 | * @throws ClassCastException if any of the map's keys or entries are not |
| 84 | * of type <code>String</code> |
| 85 | */ |
| 86 | public XPathFilterParameterSpec(String xPath, Map namespaceMap) { |
| 87 | if (xPath == null || namespaceMap == null) { |
| 88 | throw new NullPointerException(); |
| 89 | } |
| 90 | this.xPath = xPath; |
| 91 | nsMap = new HashMap(namespaceMap); |
| 92 | Iterator entries = nsMap.entrySet().iterator(); |
| 93 | while (entries.hasNext()) { |
| 94 | Map.Entry me = (Map.Entry) entries.next(); |
| 95 | if (!(me.getKey() instanceof String) || |
| 96 | !(me.getValue() instanceof String)) { |
| 97 | throw new ClassCastException("not a String"); |
| 98 | } |
| 99 | } |
| 100 | nsMap = Collections.unmodifiableMap(nsMap); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns the XPath expression to be evaluated. |
| 105 | * |
| 106 | * @return the XPath expression to be evaluated |
| 107 | */ |
| 108 | public String getXPath() { |
| 109 | return xPath; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns a map of namespace prefixes. Each key is a namespace prefix |
| 114 | * <code>String</code> that maps to a corresponding namespace URI |
| 115 | * <code>String</code>. |
| 116 | * <p> |
| 117 | * This implementation returns an {@link Collections#unmodifiableMap |
| 118 | * unmodifiable map}. |
| 119 | * |
| 120 | * @return a <code>Map</code> of namespace prefixes to namespace URIs (may |
| 121 | * be empty, but never <code>null</code>) |
| 122 | */ |
| 123 | public Map getNamespaceMap() { |
| 124 | return nsMap; |
| 125 | } |
| 126 | } |