001 /*
002 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 /*
026 * $Id: DOMSignContext.java,v 1.9 2005/05/10 16:31:14 mullan Exp $
027 */
028 package javax.xml.crypto.dsig.dom;
029
030 import javax.xml.crypto.KeySelector;
031 import javax.xml.crypto.dom.DOMCryptoContext;
032 import javax.xml.crypto.dsig.XMLSignContext;
033 import javax.xml.crypto.dsig.XMLSignature;
034 import java.security.Key;
035 import org.w3c.dom.Node;
036
037 /**
038 * A DOM-specific {@link XMLSignContext}. This class contains additional methods
039 * to specify the location in a DOM tree where an {@link XMLSignature}
040 * object is to be marshalled when generating the signature.
041 *
042 * <p>Note that <code>DOMSignContext</code> instances can contain
043 * information and state specific to the XML signature structure it is
044 * used with. The results are unpredictable if a
045 * <code>DOMSignContext</code> is used with different signature structures
046 * (for example, you should not use the same <code>DOMSignContext</code>
047 * instance to sign two different {@link XMLSignature} objects).
048 *
049 * @author Sean Mullan
050 * @author JSR 105 Expert Group
051 * @since 1.6
052 */
053 public class DOMSignContext extends DOMCryptoContext implements
054 XMLSignContext {
055
056 private Node parent;
057 private Node nextSibling;
058
059 /**
060 * Creates a <code>DOMSignContext</code> with the specified signing key
061 * and parent node. The signing key is stored in a
062 * {@link KeySelector#singletonKeySelector singleton KeySelector} that is
063 * returned by the {@link #getKeySelector getKeySelector} method.
064 * The marshalled <code>XMLSignature</code> will be added as the last
065 * child element of the specified parent node unless a next sibling node is
066 * specified by invoking the {@link #setNextSibling setNextSibling} method.
067 *
068 * @param signingKey the signing key
069 * @param parent the parent node
070 * @throws NullPointerException if <code>signingKey</code> or
071 * <code>parent</code> is <code>null</code>
072 */
073 public DOMSignContext(Key signingKey, Node parent) {
074 if (signingKey == null) {
075 throw new NullPointerException("signingKey cannot be null");
076 }
077 if (parent == null) {
078 throw new NullPointerException("parent cannot be null");
079 }
080 setKeySelector(KeySelector.singletonKeySelector(signingKey));
081 this .parent = parent;
082 }
083
084 /**
085 * Creates a <code>DOMSignContext</code> with the specified signing key,
086 * parent and next sibling nodes. The signing key is stored in a
087 * {@link KeySelector#singletonKeySelector singleton KeySelector} that is
088 * returned by the {@link #getKeySelector getKeySelector} method.
089 * The marshalled <code>XMLSignature</code> will be inserted as a child
090 * element of the specified parent node and immediately before the
091 * specified next sibling node.
092 *
093 * @param signingKey the signing key
094 * @param parent the parent node
095 * @param nextSibling the next sibling node
096 * @throws NullPointerException if <code>signingKey</code>,
097 * <code>parent</code> or <code>nextSibling</code> is <code>null</code>
098 */
099 public DOMSignContext(Key signingKey, Node parent, Node nextSibling) {
100 if (signingKey == null) {
101 throw new NullPointerException("signingKey cannot be null");
102 }
103 if (parent == null) {
104 throw new NullPointerException("parent cannot be null");
105 }
106 if (nextSibling == null) {
107 throw new NullPointerException("nextSibling cannot be null");
108 }
109 setKeySelector(KeySelector.singletonKeySelector(signingKey));
110 this .parent = parent;
111 this .nextSibling = nextSibling;
112 }
113
114 /**
115 * Creates a <code>DOMSignContext</code> with the specified key selector
116 * and parent node. The marshalled <code>XMLSignature</code> will be added
117 * as the last child element of the specified parent node unless a next
118 * sibling node is specified by invoking the
119 * {@link #setNextSibling setNextSibling} method.
120 *
121 * @param ks the key selector
122 * @param parent the parent node
123 * @throws NullPointerException if <code>ks</code> or <code>parent</code>
124 * is <code>null</code>
125 */
126 public DOMSignContext(KeySelector ks, Node parent) {
127 if (ks == null) {
128 throw new NullPointerException(
129 "key selector cannot be null");
130 }
131 if (parent == null) {
132 throw new NullPointerException("parent cannot be null");
133 }
134 setKeySelector(ks);
135 this .parent = parent;
136 }
137
138 /**
139 * Creates a <code>DOMSignContext</code> with the specified key selector,
140 * parent and next sibling nodes. The marshalled <code>XMLSignature</code>
141 * will be inserted as a child element of the specified parent node and
142 * immediately before the specified next sibling node.
143 *
144 * @param ks the key selector
145 * @param parent the parent node
146 * @param nextSibling the next sibling node
147 * @throws NullPointerException if <code>ks</code>, <code>parent</code> or
148 * <code>nextSibling</code> is <code>null</code>
149 */
150 public DOMSignContext(KeySelector ks, Node parent, Node nextSibling) {
151 if (ks == null) {
152 throw new NullPointerException(
153 "key selector cannot be null");
154 }
155 if (parent == null) {
156 throw new NullPointerException("parent cannot be null");
157 }
158 if (nextSibling == null) {
159 throw new NullPointerException("nextSibling cannot be null");
160 }
161 setKeySelector(ks);
162 this .parent = parent;
163 this .nextSibling = nextSibling;
164 }
165
166 /**
167 * Sets the parent node.
168 *
169 * @param parent the parent node. The marshalled <code>XMLSignature</code>
170 * will be added as a child element of this node.
171 * @throws NullPointerException if <code>parent</code> is <code>null</code>
172 * @see #getParent
173 */
174 public void setParent(Node parent) {
175 if (parent == null) {
176 throw new NullPointerException("parent is null");
177 }
178 this .parent = parent;
179 }
180
181 /**
182 * Sets the next sibling node.
183 *
184 * @param nextSibling the next sibling node. The marshalled
185 * <code>XMLSignature</code> will be inserted immediately before this
186 * node. Specify <code>null</code> to remove the current setting.
187 * @see #getNextSibling
188 */
189 public void setNextSibling(Node nextSibling) {
190 this .nextSibling = nextSibling;
191 }
192
193 /**
194 * Returns the parent node.
195 *
196 * @return the parent node (never <code>null</code>)
197 * @see #setParent(Node)
198 */
199 public Node getParent() {
200 return parent;
201 }
202
203 /**
204 * Returns the nextSibling node.
205 *
206 * @return the nextSibling node, or <code>null</code> if not specified.
207 * @see #setNextSibling(Node)
208 */
209 public Node getNextSibling() {
210 return nextSibling;
211 }
212 }
|