blob: 0c352c23af7d26c2c8ea01444de4d08123330728 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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: DOMKeyName.java,v 1.12 2005/05/10 18:15:32 mullan Exp $
27 */
28package org.jcp.xml.dsig.internal.dom;
29
30import javax.xml.crypto.*;
31import javax.xml.crypto.dom.DOMCryptoContext;
32import javax.xml.crypto.dsig.*;
33import javax.xml.crypto.dsig.keyinfo.KeyName;
34
35import org.w3c.dom.Document;
36import org.w3c.dom.Element;
37import org.w3c.dom.Node;
38
39/**
40 * DOM-based implementation of KeyName.
41 *
42 * @author Sean Mullan
43 */
44public final class DOMKeyName extends DOMStructure implements KeyName {
45
46 private final String name;
47
48 /**
49 * Creates a <code>DOMKeyName</code>.
50 *
51 * @param name the name of the key identifier
52 * @throws NullPointerException if <code>name</code> is null
53 */
54 public DOMKeyName(String name) {
55 if (name == null) {
56 throw new NullPointerException("name cannot be null");
57 }
58 this.name = name;
59 }
60
61 /**
62 * Creates a <code>DOMKeyName</code> from a KeyName element.
63 *
64 * @param knElem a KeyName element
65 */
66 public DOMKeyName(Element knElem) {
67 name = knElem.getFirstChild().getNodeValue();
68 }
69
70 public String getName() {
71 return name;
72 }
73
74 public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
75 throws MarshalException {
76 Document ownerDoc = DOMUtils.getOwnerDocument(parent);
77 // prepend namespace prefix, if necessary
78 Element knElem = DOMUtils.createElement
79 (ownerDoc, "KeyName", XMLSignature.XMLNS, dsPrefix);
80 knElem.appendChild(ownerDoc.createTextNode(name));
81 parent.appendChild(knElem);
82 }
83
84 public boolean equals(Object obj) {
85 if (this == obj) {
86 return true;
87 }
88 if (!(obj instanceof KeyName)) {
89 return false;
90 }
91 KeyName okn = (KeyName) obj;
92 return name.equals(okn.getName());
93 }
94}