0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A * $Id: DOMCryptoContext.java,v 1.3 2005/05/09 18:33:26 mullan Exp $
0N/A */
0N/Apackage javax.xml.crypto.dom;
0N/A
0N/Aimport javax.xml.crypto.KeySelector;
0N/Aimport javax.xml.crypto.URIDereferencer;
0N/Aimport javax.xml.crypto.XMLCryptoContext;
0N/Aimport java.util.Collections;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Iterator;
0N/Aimport org.w3c.dom.Element;
0N/A
0N/A/**
0N/A * This class provides a DOM-specific implementation of the
0N/A * {@link XMLCryptoContext} interface. It also includes additional
0N/A * methods that are specific to a DOM-based implementation for registering
0N/A * and retrieving elements that contain attributes of type ID.
0N/A *
0N/A * @author Sean Mullan
0N/A * @author JSR 105 Expert Group
0N/A * @since 1.6
0N/A */
0N/Apublic class DOMCryptoContext implements XMLCryptoContext {
0N/A
0N/A private HashMap nsMap = new HashMap();
0N/A private HashMap idMap = new HashMap();
0N/A private HashMap objMap = new HashMap();
0N/A private String baseURI;
0N/A private KeySelector ks;
0N/A private URIDereferencer dereferencer;
0N/A private HashMap propMap = new HashMap();
0N/A private String defaultPrefix;
0N/A
0N/A /**
0N/A * Default constructor. (For invocation by subclass constructors).
0N/A */
0N/A protected DOMCryptoContext() {}
0N/A
0N/A /**
0N/A * This implementation uses an internal {@link HashMap} to get the prefix
0N/A * that the specified URI maps to. It returns the <code>defaultPrefix</code>
0N/A * if it maps to <code>null</code>.
0N/A *
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public String getNamespacePrefix(String namespaceURI,
0N/A String defaultPrefix) {
0N/A if (namespaceURI == null) {
0N/A throw new NullPointerException("namespaceURI cannot be null");
0N/A }
0N/A String prefix = (String) nsMap.get(namespaceURI);
0N/A return (prefix != null ? prefix : defaultPrefix);
0N/A }
0N/A
0N/A /**
0N/A * This implementation uses an internal {@link HashMap} to map the URI
0N/A * to the specified prefix.
0N/A *
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public String putNamespacePrefix(String namespaceURI, String prefix) {
0N/A if (namespaceURI == null) {
0N/A throw new NullPointerException("namespaceURI is null");
0N/A }
0N/A return (String) nsMap.put(namespaceURI, prefix);
0N/A }
0N/A
0N/A public String getDefaultNamespacePrefix() {
0N/A return defaultPrefix;
0N/A }
0N/A
0N/A public void setDefaultNamespacePrefix(String defaultPrefix) {
0N/A this.defaultPrefix = defaultPrefix;
0N/A }
0N/A
0N/A public String getBaseURI() {
0N/A return baseURI;
0N/A }
0N/A
0N/A /**
0N/A * @throws IllegalArgumentException {@inheritDoc}
0N/A */
0N/A public void setBaseURI(String baseURI) {
0N/A if (baseURI != null) {
0N/A java.net.URI.create(baseURI);
0N/A }
0N/A this.baseURI = baseURI;
0N/A }
0N/A
0N/A public URIDereferencer getURIDereferencer() {
0N/A return dereferencer;
0N/A }
0N/A
0N/A public void setURIDereferencer(URIDereferencer dereferencer) {
0N/A this.dereferencer = dereferencer;
0N/A }
0N/A
0N/A /**
0N/A * This implementation uses an internal {@link HashMap} to get the object
0N/A * that the specified name maps to.
0N/A *
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public Object getProperty(String name) {
0N/A if (name == null) {
0N/A throw new NullPointerException("name is null");
0N/A }
0N/A return propMap.get(name);
0N/A }
0N/A
0N/A /**
0N/A * This implementation uses an internal {@link HashMap} to map the name
0N/A * to the specified object.
0N/A *
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public Object setProperty(String name, Object value) {
0N/A if (name == null) {
0N/A throw new NullPointerException("name is null");
0N/A }
0N/A return propMap.put(name, value);
0N/A }
0N/A
0N/A public KeySelector getKeySelector() {
0N/A return ks;
0N/A }
0N/A
0N/A public void setKeySelector(KeySelector ks) {
0N/A this.ks = ks;
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Element</code> with the specified ID attribute value.
0N/A *
0N/A * <p>This implementation uses an internal {@link HashMap} to get the
0N/A * element that the specified attribute value maps to.
0N/A *
0N/A * @param idValue the value of the ID
0N/A * @return the <code>Element</code> with the specified ID attribute value,
0N/A * or <code>null</code> if none.
0N/A * @throws NullPointerException if <code>idValue</code> is <code>null</code>
0N/A * @see #setIdAttributeNS
0N/A */
0N/A public Element getElementById(String idValue) {
0N/A if (idValue == null) {
0N/A throw new NullPointerException("idValue is null");
0N/A }
0N/A return (Element) idMap.get(idValue);
0N/A }
0N/A
0N/A /**
0N/A * Registers the element's attribute specified by the namespace URI and
0N/A * local name to be of type ID. The attribute must have a non-empty value.
0N/A *
0N/A * <p>This implementation uses an internal {@link HashMap} to map the
0N/A * attribute's value to the specified element.
0N/A *
0N/A * @param element the element
0N/A * @param namespaceURI the namespace URI of the attribute (specify
0N/A * <code>null</code> if not applicable)
0N/A * @param localName the local name of the attribute
0N/A * @throws IllegalArgumentException if <code>localName</code> is not an
0N/A * attribute of the specified element or it does not contain a specific
0N/A * value
0N/A * @throws NullPointerException if <code>element</code> or
0N/A * <code>localName</code> is <code>null</code>
0N/A * @see #getElementById
0N/A */
0N/A public void setIdAttributeNS(Element element, String namespaceURI,
0N/A String localName) {
0N/A if (element == null) {
0N/A throw new NullPointerException("element is null");
0N/A }
0N/A if (localName == null) {
0N/A throw new NullPointerException("localName is null");
0N/A }
0N/A String idValue = element.getAttributeNS(namespaceURI, localName);
0N/A if (idValue == null || idValue.length() == 0) {
0N/A throw new IllegalArgumentException(localName + " is not an " +
0N/A "attribute");
0N/A }
0N/A idMap.put(idValue, element);
0N/A }
0N/A
0N/A /**
0N/A * Returns a read-only iterator over the set of Id/Element mappings of
0N/A * this <code>DOMCryptoContext</code>. Attempts to modify the set via the
0N/A * {@link Iterator#remove} method throw an
0N/A * <code>UnsupportedOperationException</code>. The mappings are returned
0N/A * in no particular order. Each element in the iteration is represented as a
0N/A * {@link java.util.Map.Entry}. If the <code>DOMCryptoContext</code> is
0N/A * modified while an iteration is in progress, the results of the
0N/A * iteration are undefined.
0N/A *
0N/A * @return a read-only iterator over the set of mappings
0N/A */
0N/A public Iterator iterator() {
0N/A return Collections.unmodifiableMap(idMap).entrySet().iterator();
0N/A }
0N/A
0N/A /**
0N/A * This implementation uses an internal {@link HashMap} to get the object
0N/A * that the specified key maps to.
0N/A */
0N/A public Object get(Object key) {
0N/A return objMap.get(key);
0N/A }
0N/A
0N/A /**
0N/A * This implementation uses an internal {@link HashMap} to map the key
0N/A * to the specified object.
0N/A *
0N/A * @throws IllegalArgumentException {@inheritDoc}
0N/A */
0N/A public Object put(Object key, Object value) {
0N/A return objMap.put(key, value);
0N/A }
0N/A}