blob: d5ef5b80b426e25430f228738306b9d79b037819 [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: DOMCryptoBinary.java,v 1.14 2005/05/12 19:28:29 mullan Exp $
27 */
28package org.jcp.xml.dsig.internal.dom;
29
30import java.math.BigInteger;
31import javax.xml.crypto.*;
32import javax.xml.crypto.dom.DOMCryptoContext;
33import javax.xml.crypto.dsig.*;
34import org.w3c.dom.Node;
35import org.w3c.dom.Text;
36
37import com.sun.org.apache.xml.internal.security.utils.Base64;
38
39/**
40 * A DOM-based representation of the XML <code>CryptoBinary</code> simple type
41 * as defined in the W3C specification for XML-Signature Syntax and Processing.
42 * The XML Schema Definition is defined as:
43 *
44 * <xmp>
45 * <simpleType name="CryptoBinary">
46 * <restriction base = "base64Binary">
47 * </restriction>
48 * </simpleType>
49 * </xmp>
50 *
51 * @author Sean Mullan
52 */
53public final class DOMCryptoBinary extends DOMStructure {
54
55 private final BigInteger bigNum;
56 private final String value;
57
58 /**
59 * Create a <code>DOMCryptoBinary</code> instance from the specified
60 * <code>BigInteger</code>
61 *
62 * @param bigNum the arbitrary-length integer
63 * @throws NullPointerException if <code>bigNum</code> is <code>null</code>
64 */
65 public DOMCryptoBinary(BigInteger bigNum) {
66 if (bigNum == null) {
67 throw new NullPointerException("bigNum is null");
68 }
69 this.bigNum = bigNum;
70 // convert to bitstring
71 value = Base64.encode(bigNum);
72 }
73
74 /**
75 * Creates a <code>DOMCryptoBinary</code> from a node.
76 *
77 * @param cbNode a CryptoBinary text node
78 * @throws MarshalException if value cannot be decoded (invalid format)
79 */
80 public DOMCryptoBinary(Node cbNode) throws MarshalException {
81 value = cbNode.getNodeValue();
82 try {
83 bigNum = Base64.decodeBigIntegerFromText((Text) cbNode);
84 } catch (Exception ex) {
85 throw new MarshalException(ex);
86 }
87 }
88
89 /**
90 * Returns the <code>BigInteger</code> that this object contains.
91 *
92 * @return the <code>BigInteger</code> that this object contains
93 */
94 public BigInteger getBigNum() {
95 return bigNum;
96 }
97
98 public void marshal(Node parent, String prefix, DOMCryptoContext context)
99 throws MarshalException {
100 parent.appendChild
101 (DOMUtils.getOwnerDocument(parent).createTextNode(value));
102 }
103}