Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/xml/crypto/dom/DOMCryptoContext.java
38918 views
/*1* Copyright (c) 2005, 2011, 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 org.w3c.dom.Element;3637/**38* This class provides a DOM-specific implementation of the39* {@link XMLCryptoContext} interface. It also includes additional40* methods that are specific to a DOM-based implementation for registering41* and retrieving elements that contain attributes of type ID.42*43* @author Sean Mullan44* @author JSR 105 Expert Group45* @since 1.646*/47public class DOMCryptoContext implements XMLCryptoContext {4849private HashMap<String,String> nsMap = new HashMap<>();50private HashMap<String,Element> idMap = new HashMap<>();51private HashMap<Object,Object> objMap = new HashMap<>();52private String baseURI;53private KeySelector ks;54private URIDereferencer dereferencer;55private HashMap<String,Object> propMap = new HashMap<>();56private String defaultPrefix;5758/**59* Default constructor. (For invocation by subclass constructors).60*/61protected DOMCryptoContext() {}6263/**64* This implementation uses an internal {@link HashMap} to get the prefix65* that the specified URI maps to. It returns the <code>defaultPrefix</code>66* if it maps to <code>null</code>.67*68* @throws NullPointerException {@inheritDoc}69*/70public String getNamespacePrefix(String namespaceURI,71String defaultPrefix) {72if (namespaceURI == null) {73throw new NullPointerException("namespaceURI cannot be null");74}75String prefix = nsMap.get(namespaceURI);76return (prefix != null ? prefix : defaultPrefix);77}7879/**80* This implementation uses an internal {@link HashMap} to map the URI81* to the specified prefix.82*83* @throws NullPointerException {@inheritDoc}84*/85public String putNamespacePrefix(String namespaceURI, String prefix) {86if (namespaceURI == null) {87throw new NullPointerException("namespaceURI is null");88}89return nsMap.put(namespaceURI, prefix);90}9192public String getDefaultNamespacePrefix() {93return defaultPrefix;94}9596public void setDefaultNamespacePrefix(String defaultPrefix) {97this.defaultPrefix = defaultPrefix;98}99100public String getBaseURI() {101return baseURI;102}103104/**105* @throws IllegalArgumentException {@inheritDoc}106*/107public void setBaseURI(String baseURI) {108if (baseURI != null) {109java.net.URI.create(baseURI);110}111this.baseURI = baseURI;112}113114public URIDereferencer getURIDereferencer() {115return dereferencer;116}117118public void setURIDereferencer(URIDereferencer dereferencer) {119this.dereferencer = dereferencer;120}121122/**123* This implementation uses an internal {@link HashMap} to get the object124* that the specified name maps to.125*126* @throws NullPointerException {@inheritDoc}127*/128public Object getProperty(String name) {129if (name == null) {130throw new NullPointerException("name is null");131}132return propMap.get(name);133}134135/**136* This implementation uses an internal {@link HashMap} to map the name137* to the specified object.138*139* @throws NullPointerException {@inheritDoc}140*/141public Object setProperty(String name, Object value) {142if (name == null) {143throw new NullPointerException("name is null");144}145return propMap.put(name, value);146}147148public KeySelector getKeySelector() {149return ks;150}151152public void setKeySelector(KeySelector ks) {153this.ks = ks;154}155156/**157* Returns the <code>Element</code> with the specified ID attribute value.158*159* <p>This implementation uses an internal {@link HashMap} to get the160* element that the specified attribute value maps to.161*162* @param idValue the value of the ID163* @return the <code>Element</code> with the specified ID attribute value,164* or <code>null</code> if none.165* @throws NullPointerException if <code>idValue</code> is <code>null</code>166* @see #setIdAttributeNS167*/168public Element getElementById(String idValue) {169if (idValue == null) {170throw new NullPointerException("idValue is null");171}172return idMap.get(idValue);173}174175/**176* Registers the element's attribute specified by the namespace URI and177* local name to be of type ID. The attribute must have a non-empty value.178*179* <p>This implementation uses an internal {@link HashMap} to map the180* attribute's value to the specified element.181*182* @param element the element183* @param namespaceURI the namespace URI of the attribute (specify184* <code>null</code> if not applicable)185* @param localName the local name of the attribute186* @throws IllegalArgumentException if <code>localName</code> is not an187* attribute of the specified element or it does not contain a specific188* value189* @throws NullPointerException if <code>element</code> or190* <code>localName</code> is <code>null</code>191* @see #getElementById192*/193public void setIdAttributeNS(Element element, String namespaceURI,194String localName) {195if (element == null) {196throw new NullPointerException("element is null");197}198if (localName == null) {199throw new NullPointerException("localName is null");200}201String idValue = element.getAttributeNS(namespaceURI, localName);202if (idValue == null || idValue.length() == 0) {203throw new IllegalArgumentException(localName + " is not an " +204"attribute");205}206idMap.put(idValue, element);207}208209/**210* Returns a read-only iterator over the set of Id/Element mappings of211* this <code>DOMCryptoContext</code>. Attempts to modify the set via the212* {@link Iterator#remove} method throw an213* <code>UnsupportedOperationException</code>. The mappings are returned214* in no particular order. Each element in the iteration is represented as a215* {@link java.util.Map.Entry}. If the <code>DOMCryptoContext</code> is216* modified while an iteration is in progress, the results of the217* iteration are undefined.218*219* @return a read-only iterator over the set of mappings220*/221@SuppressWarnings("rawtypes")222public Iterator 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