Path: blob/master/src/java.xml.crypto/share/classes/javax/xml/crypto/dom/DOMCryptoContext.java
41161 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24/*25* $Id: DOMCryptoContext.java,v 1.3 2005/05/09 18:33:26 mullan Exp $26*/27package javax.xml.crypto.dom;2829import javax.xml.crypto.KeySelector;30import javax.xml.crypto.URIDereferencer;31import javax.xml.crypto.XMLCryptoContext;32import java.util.Collections;33import java.util.HashMap;34import java.util.Iterator;35import java.util.Map;36import org.w3c.dom.Element;3738/**39* This class provides a DOM-specific implementation of the40* {@link XMLCryptoContext} interface. It also includes additional41* methods that are specific to a DOM-based implementation for registering42* and retrieving elements that contain attributes of type ID.43*44* @author Sean Mullan45* @author JSR 105 Expert Group46* @since 1.647*/48public class DOMCryptoContext implements XMLCryptoContext {4950private HashMap<String,String> nsMap = new HashMap<>();51private HashMap<String,Element> idMap = new HashMap<>();52private HashMap<Object,Object> objMap = new HashMap<>();53private String baseURI;54private KeySelector ks;55private URIDereferencer dereferencer;56private HashMap<String,Object> propMap = new HashMap<>();57private String defaultPrefix;5859/**60* Default constructor. (For invocation by subclass constructors).61*/62protected DOMCryptoContext() {}6364/**65* This implementation uses an internal {@link HashMap} to get the prefix66* that the specified URI maps to. It returns the <code>defaultPrefix</code>67* if it maps to <code>null</code>.68*69* @throws NullPointerException {@inheritDoc}70*/71public String getNamespacePrefix(String namespaceURI,72String defaultPrefix) {73if (namespaceURI == null) {74throw new NullPointerException("namespaceURI cannot be null");75}76String prefix = nsMap.get(namespaceURI);77return (prefix != null ? prefix : defaultPrefix);78}7980/**81* This implementation uses an internal {@link HashMap} to map the URI82* to the specified prefix.83*84* @throws NullPointerException {@inheritDoc}85*/86public String putNamespacePrefix(String namespaceURI, String prefix) {87if (namespaceURI == null) {88throw new NullPointerException("namespaceURI is null");89}90return nsMap.put(namespaceURI, prefix);91}9293public String getDefaultNamespacePrefix() {94return defaultPrefix;95}9697public void setDefaultNamespacePrefix(String defaultPrefix) {98this.defaultPrefix = defaultPrefix;99}100101public String getBaseURI() {102return baseURI;103}104105/**106* @throws IllegalArgumentException {@inheritDoc}107*/108public void setBaseURI(String baseURI) {109if (baseURI != null) {110java.net.URI.create(baseURI);111}112this.baseURI = baseURI;113}114115public URIDereferencer getURIDereferencer() {116return dereferencer;117}118119public void setURIDereferencer(URIDereferencer dereferencer) {120this.dereferencer = dereferencer;121}122123/**124* This implementation uses an internal {@link HashMap} to get the object125* that the specified name maps to.126*127* @throws NullPointerException {@inheritDoc}128*/129public Object getProperty(String name) {130if (name == null) {131throw new NullPointerException("name is null");132}133return propMap.get(name);134}135136/**137* This implementation uses an internal {@link HashMap} to map the name138* to the specified object.139*140* @throws NullPointerException {@inheritDoc}141*/142public Object setProperty(String name, Object value) {143if (name == null) {144throw new NullPointerException("name is null");145}146return propMap.put(name, value);147}148149public KeySelector getKeySelector() {150return ks;151}152153public void setKeySelector(KeySelector ks) {154this.ks = ks;155}156157/**158* Returns the <code>Element</code> with the specified ID attribute value.159*160* <p>This implementation uses an internal {@link HashMap} to get the161* element that the specified attribute value maps to.162*163* @param idValue the value of the ID164* @return the <code>Element</code> with the specified ID attribute value,165* or <code>null</code> if none.166* @throws NullPointerException if <code>idValue</code> is <code>null</code>167* @see #setIdAttributeNS168*/169public Element getElementById(String idValue) {170if (idValue == null) {171throw new NullPointerException("idValue is null");172}173return idMap.get(idValue);174}175176/**177* Registers the element's attribute specified by the namespace URI and178* local name to be of type ID. The attribute must have a non-empty value.179*180* <p>This implementation uses an internal {@link HashMap} to map the181* attribute's value to the specified element.182*183* @param element the element184* @param namespaceURI the namespace URI of the attribute (specify185* <code>null</code> if not applicable)186* @param localName the local name of the attribute187* @throws IllegalArgumentException if <code>localName</code> is not an188* attribute of the specified element or it does not contain a specific189* value190* @throws NullPointerException if <code>element</code> or191* <code>localName</code> is <code>null</code>192* @see #getElementById193*/194public void setIdAttributeNS(Element element, String namespaceURI,195String localName) {196if (element == null) {197throw new NullPointerException("element is null");198}199if (localName == null) {200throw new NullPointerException("localName is null");201}202String idValue = element.getAttributeNS(namespaceURI, localName);203if (idValue == null || idValue.length() == 0) {204throw new IllegalArgumentException(localName + " is not an " +205"attribute");206}207idMap.put(idValue, element);208}209210/**211* Returns a read-only iterator over the set of Id/Element mappings of212* this <code>DOMCryptoContext</code>. Attempts to modify the set via the213* {@link Iterator#remove} method throw an214* <code>UnsupportedOperationException</code>. The mappings are returned215* in no particular order. Each element in the iteration is represented as a216* {@link java.util.Map.Entry}. If the <code>DOMCryptoContext</code> is217* modified while an iteration is in progress, the results of the218* iteration are undefined.219*220* @return a read-only iterator over the set of mappings221*/222public Iterator<Map.Entry<String, Element>> iterator() {223return Collections.unmodifiableMap(idMap).entrySet().iterator();224}225226/**227* This implementation uses an internal {@link HashMap} to get the object228* that the specified key maps to.229*/230public Object get(Object key) {231return objMap.get(key);232}233234/**235* This implementation uses an internal {@link HashMap} to map the key236* to the specified object.237*238* @throws IllegalArgumentException {@inheritDoc}239*/240public Object put(Object key, Object value) {241return objMap.put(key, value);242}243}244245246