XPathType.java revision 11001:3e276a212a96
1/*
2 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25/*
26 * $Id: XPathType.java,v 1.4 2005/05/10 16:40:17 mullan Exp $
27 */
28package javax.xml.crypto.dsig.spec;
29
30import java.util.Collections;
31import java.util.HashMap;
32import java.util.Map;
33
34/**
35 * The XML Schema Definition of the <code>XPath</code> element as defined in the
36 * <a href="http://www.w3.org/TR/xmldsig-filter2">
37 * W3C Recommendation for XML-Signature XPath Filter 2.0</a>:
38 * <pre><code>
39 * &lt;schema xmlns="http://www.w3.org/2001/XMLSchema"
40 *         xmlns:xf="http://www.w3.org/2002/06/xmldsig-filter2"
41 *         targetNamespace="http://www.w3.org/2002/06/xmldsig-filter2"
42 *         version="0.1" elementFormDefault="qualified"&gt;
43 *
44 * &lt;element name="XPath"
45 *          type="xf:XPathType"/&gt;
46 *
47 * &lt;complexType name="XPathType"&gt;
48 *   &lt;simpleContent&gt;
49 *     &lt;extension base="string"&gt;
50 *       &lt;attribute name="Filter"&gt;
51 *         &lt;simpleType&gt;
52 *           &lt;restriction base="string"&gt;
53 *             &lt;enumeration value="intersect"/&gt;
54 *             &lt;enumeration value="subtract"/&gt;
55 *             &lt;enumeration value="union"/&gt;
56 *           &lt;/restriction&gt;
57 *         &lt;/simpleType&gt;
58 *       &lt;/attribute&gt;
59 *     &lt;/extension&gt;
60 *   &lt;/simpleContent&gt;
61 * &lt;/complexType&gt;
62 * </code></pre>
63 *
64 * @author Sean Mullan
65 * @author JSR 105 Expert Group
66 * @since 1.6
67 * @see XPathFilter2ParameterSpec
68 */
69public class XPathType {
70
71    /**
72     * Represents the filter set operation.
73     */
74    public static class Filter {
75        private final String operation;
76
77        private Filter(String operation) {
78            this.operation = operation;
79        }
80
81        /**
82         * Returns the string form of the operation.
83         *
84         * @return the string form of the operation
85         */
86        public String toString() {
87            return operation;
88        }
89
90        /**
91         * The intersect filter operation.
92         */
93        public static final Filter INTERSECT = new Filter("intersect");
94
95        /**
96         * The subtract filter operation.
97         */
98        public static final Filter SUBTRACT = new Filter("subtract");
99
100        /**
101         * The union filter operation.
102         */
103        public static final Filter UNION = new Filter("union");
104    }
105
106    private final String expression;
107    private final Filter filter;
108    private final Map<String,String> nsMap;
109
110    /**
111     * Creates an <code>XPathType</code> instance with the specified XPath
112     * expression and filter.
113     *
114     * @param expression the XPath expression to be evaluated
115     * @param filter the filter operation ({@link Filter#INTERSECT},
116     *    {@link Filter#SUBTRACT}, or {@link Filter#UNION})
117     * @throws NullPointerException if <code>expression</code> or
118     *    <code>filter</code> is <code>null</code>
119     */
120    public XPathType(String expression, Filter filter) {
121        if (expression == null) {
122            throw new NullPointerException("expression cannot be null");
123        }
124        if (filter == null) {
125            throw new NullPointerException("filter cannot be null");
126        }
127        this.expression = expression;
128        this.filter = filter;
129        this.nsMap = Collections.emptyMap();
130    }
131
132    /**
133     * Creates an <code>XPathType</code> instance with the specified XPath
134     * expression, filter, and namespace map. The map is copied to protect
135     * against subsequent modification.
136     *
137     * @param expression the XPath expression to be evaluated
138     * @param filter the filter operation ({@link Filter#INTERSECT},
139     *    {@link Filter#SUBTRACT}, or {@link Filter#UNION})
140     * @param namespaceMap the map of namespace prefixes. Each key is a
141     *    namespace prefix <code>String</code> that maps to a corresponding
142     *    namespace URI <code>String</code>.
143     * @throws NullPointerException if <code>expression</code>,
144     *    <code>filter</code> or <code>namespaceMap</code> are
145     *    <code>null</code>
146     * @throws ClassCastException if any of the map's keys or entries are
147     *    not of type <code>String</code>
148     */
149    public XPathType(String expression, Filter filter,
150        Map<String,String> namespaceMap) {
151        if (expression == null) {
152            throw new NullPointerException("expression cannot be null");
153        }
154        if (filter == null) {
155            throw new NullPointerException("filter cannot be null");
156        }
157        if (namespaceMap == null) {
158            throw new NullPointerException("namespaceMap cannot be null");
159        }
160        this.expression = expression;
161        this.filter = filter;
162        Map<String,String> tempMap = Collections.checkedMap(new HashMap<>(),
163                                                            String.class,
164                                                            String.class);
165        tempMap.putAll(namespaceMap);
166        this.nsMap = Collections.unmodifiableMap(tempMap);
167    }
168
169    /**
170     * Returns the XPath expression to be evaluated.
171     *
172     * @return the XPath expression to be evaluated
173     */
174    public String getExpression() {
175        return expression;
176    }
177
178    /**
179     * Returns the filter operation.
180     *
181     * @return the filter operation
182     */
183    public Filter getFilter() {
184        return filter;
185    }
186
187    /**
188     * Returns a map of namespace prefixes. Each key is a namespace prefix
189     * <code>String</code> that maps to a corresponding namespace URI
190     * <code>String</code>.
191     * <p>
192     * This implementation returns an {@link Collections#unmodifiableMap
193     * unmodifiable map}.
194     *
195     * @return a <code>Map</code> of namespace prefixes to namespace URIs
196     *    (may be empty, but never <code>null</code>)
197     */
198    public Map<String,String> getNamespaceMap() {
199        return nsMap;
200    }
201}
202