0N/A/*
661N/A * reserved comment block
661N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
661N/A * Copyright 2005 The Apache Software Foundation.
661N/A *
661N/A * Licensed under the Apache License, Version 2.0 (the "License");
661N/A * you may not use this file except in compliance with the License.
661N/A * You may obtain a copy of the License at
661N/A *
661N/A * http://www.apache.org/licenses/LICENSE-2.0
661N/A *
661N/A * Unless required by applicable law or agreed to in writing, software
661N/A * distributed under the License is distributed on an "AS IS" BASIS,
661N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
661N/A * See the License for the specific language governing permissions and
661N/A * limitations under the License.
661N/A *
661N/A */
661N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
661N/A */
661N/A/*
661N/A * $Id: DOMExcC14NMethod.java,v 1.2 2008/07/24 15:20:32 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);
661N/A if (prefix == null || prefix.length() == 0) {
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}