DOMExcC14NMethod.java revision 0
0N/A/*
0N/A * Copyright 2005 Sun Microsystems, Inc. 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
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun 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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A/*
0N/A * $Id: DOMExcC14NMethod.java,v 1.28 2005/09/23 20:20:41 mullan Exp $
0N/A */
0N/Apackage org.jcp.xml.dsig.internal.dom;
0N/A
0N/Aimport javax.xml.crypto.*;
0N/Aimport javax.xml.crypto.dsig.*;
0N/Aimport javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
0N/Aimport javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
0N/Aimport javax.xml.crypto.dsig.spec.TransformParameterSpec;
0N/A
0N/Aimport java.security.InvalidAlgorithmParameterException;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/Aimport java.util.*;
0N/Aimport org.w3c.dom.Element;
0N/A
0N/Aimport com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
0N/Aimport com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
0N/A
0N/A/**
0N/A * DOM-based implementation of CanonicalizationMethod for Exclusive
0N/A * Canonical XML algorithm (with or without comments).
0N/A * Uses Apache XML-Sec Canonicalizer.
0N/A *
0N/A * @author Sean Mullan
0N/A */
0N/Apublic final class DOMExcC14NMethod extends ApacheCanonicalizer {
0N/A
0N/A public void init(TransformParameterSpec params)
0N/A throws InvalidAlgorithmParameterException {
0N/A if (params != null) {
0N/A if (!(params instanceof ExcC14NParameterSpec)) {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("params must be of type ExcC14NParameterSpec");
0N/A }
0N/A this.params = (C14NMethodParameterSpec) params;
0N/A }
0N/A }
0N/A
0N/A public void init(XMLStructure parent, XMLCryptoContext context)
0N/A throws InvalidAlgorithmParameterException {
0N/A super.init(parent, context);
0N/A Element paramsElem = DOMUtils.getFirstChildElement(transformElem);
0N/A if (paramsElem == null) {
0N/A this.params = null;
0N/A this.inclusiveNamespaces = null;
0N/A return;
0N/A }
0N/A unmarshalParams(paramsElem);
0N/A }
0N/A
0N/A private void unmarshalParams(Element paramsElem) {
0N/A String prefixListAttr = paramsElem.getAttributeNS(null, "PrefixList");
0N/A this.inclusiveNamespaces = prefixListAttr;
0N/A int begin = 0;
0N/A int end = prefixListAttr.indexOf(' ');
0N/A List prefixList = new ArrayList();
0N/A while (end != -1) {
0N/A prefixList.add(prefixListAttr.substring(begin, end));
0N/A begin = end + 1;
0N/A end = prefixListAttr.indexOf(' ', begin);
0N/A }
0N/A if (begin <= prefixListAttr.length()) {
0N/A prefixList.add(prefixListAttr.substring(begin));
0N/A }
0N/A this.params = new ExcC14NParameterSpec(prefixList);
0N/A }
0N/A
0N/A public void marshalParams(XMLStructure parent, XMLCryptoContext context)
0N/A throws MarshalException {
0N/A
0N/A super.marshalParams(parent, context);
0N/A AlgorithmParameterSpec spec = getParameterSpec();
0N/A if (spec == null) {
0N/A return;
0N/A }
0N/A
0N/A String prefix =
0N/A DOMUtils.getNSPrefix(context, CanonicalizationMethod.EXCLUSIVE);
0N/A Element excElem = DOMUtils.createElement
0N/A (ownerDoc, "InclusiveNamespaces",
0N/A CanonicalizationMethod.EXCLUSIVE, prefix);
0N/A if (prefix == null) {
0N/A excElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
0N/A CanonicalizationMethod.EXCLUSIVE);
0N/A } else {
0N/A excElem.setAttributeNS("http://www.w3.org/2000/xmlns/",
0N/A "xmlns:" + prefix, CanonicalizationMethod.EXCLUSIVE);
0N/A }
0N/A
0N/A ExcC14NParameterSpec params = (ExcC14NParameterSpec) spec;
0N/A StringBuffer prefixListAttr = new StringBuffer("");
0N/A List prefixList = params.getPrefixList();
0N/A for (int i = 0, size = prefixList.size(); i < size; i++) {
0N/A prefixListAttr.append((String) prefixList.get(i));
0N/A if (i < size - 1) {
0N/A prefixListAttr.append(" ");
0N/A }
0N/A }
0N/A DOMUtils.setAttribute(excElem, "PrefixList", prefixListAttr.toString());
0N/A this.inclusiveNamespaces = prefixListAttr.toString();
0N/A transformElem.appendChild(excElem);
0N/A }
0N/A
0N/A public String getParamsNSURI() {
0N/A return CanonicalizationMethod.EXCLUSIVE;
0N/A }
0N/A
0N/A public Data transform(Data data, XMLCryptoContext xc)
0N/A throws TransformException {
0N/A
0N/A // ignore comments if dereferencing same-document URI that require
0N/A // you to omit comments, even if the Transform says otherwise -
0N/A // this is to be compliant with section 4.3.3.3 of W3C Rec.
0N/A if (data instanceof DOMSubTreeData) {
0N/A DOMSubTreeData subTree = (DOMSubTreeData) data;
0N/A if (subTree.excludeComments()) {
0N/A try {
0N/A apacheCanonicalizer = Canonicalizer.getInstance
0N/A (CanonicalizationMethod.EXCLUSIVE);
0N/A } catch (InvalidCanonicalizerException ice) {
0N/A throw new TransformException
0N/A ("Couldn't find Canonicalizer for: " +
0N/A CanonicalizationMethod.EXCLUSIVE + ": " +
0N/A ice.getMessage(), ice);
0N/A }
0N/A }
0N/A }
0N/A
0N/A return canonicalize(data, xc);
0N/A }
0N/A}