org.jcp.xml.dsig.internal.dom.DOMExcC14NMethod Maven / Gradle / Ivy
The newest version!
/* */ package org.jcp.xml.dsig.internal.dom;
/* */
/* */ import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
/* */ import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
/* */ import java.security.InvalidAlgorithmParameterException;
/* */ import java.security.spec.AlgorithmParameterSpec;
/* */ import java.util.ArrayList;
/* */ import java.util.List;
/* */ import javax.xml.crypto.Data;
/* */ import javax.xml.crypto.MarshalException;
/* */ import javax.xml.crypto.XMLCryptoContext;
/* */ import javax.xml.crypto.XMLStructure;
/* */ import javax.xml.crypto.dsig.TransformException;
/* */ import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
/* */ import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
/* */ import javax.xml.crypto.dsig.spec.TransformParameterSpec;
/* */ import org.w3c.dom.Element;
/* */
/* */ public final class DOMExcC14NMethod extends ApacheCanonicalizer
/* */ {
/* */ public void init(TransformParameterSpec params)
/* */ throws InvalidAlgorithmParameterException
/* */ {
/* 34 */ if (params != null) {
/* 35 */ if (!(params instanceof ExcC14NParameterSpec)) {
/* 36 */ throw new InvalidAlgorithmParameterException("params must be of type ExcC14NParameterSpec");
/* */ }
/* */
/* 39 */ this.params = ((C14NMethodParameterSpec)params);
/* */ }
/* */ }
/* */
/* */ public void init(XMLStructure parent, XMLCryptoContext context) throws InvalidAlgorithmParameterException
/* */ {
/* 45 */ super.init(parent, context);
/* 46 */ Element paramsElem = DOMUtils.getFirstChildElement(this.transformElem);
/* 47 */ if (paramsElem == null) {
/* 48 */ this.params = null;
/* 49 */ this.inclusiveNamespaces = null;
/* 50 */ return;
/* */ }
/* 52 */ unmarshalParams(paramsElem);
/* */ }
/* */
/* */ private void unmarshalParams(Element paramsElem) {
/* 56 */ String prefixListAttr = paramsElem.getAttributeNS(null, "PrefixList");
/* 57 */ this.inclusiveNamespaces = prefixListAttr;
/* 58 */ int begin = 0;
/* 59 */ int end = prefixListAttr.indexOf(' ');
/* 60 */ List prefixList = new ArrayList();
/* 61 */ while (end != -1) {
/* 62 */ prefixList.add(prefixListAttr.substring(begin, end));
/* 63 */ begin = end + 1;
/* 64 */ end = prefixListAttr.indexOf(' ', begin);
/* */ }
/* 66 */ if (begin <= prefixListAttr.length()) {
/* 67 */ prefixList.add(prefixListAttr.substring(begin));
/* */ }
/* 69 */ this.params = new ExcC14NParameterSpec(prefixList);
/* */ }
/* */
/* */ public void marshalParams(XMLStructure parent, XMLCryptoContext context)
/* */ throws MarshalException
/* */ {
/* 75 */ super.marshalParams(parent, context);
/* 76 */ AlgorithmParameterSpec spec = getParameterSpec();
/* 77 */ if (spec == null) {
/* 78 */ return;
/* */ }
/* */
/* 81 */ String prefix = DOMUtils.getNSPrefix(context, "http://www.w3.org/2001/10/xml-exc-c14n#");
/* */
/* 83 */ Element excElem = DOMUtils.createElement(this.ownerDoc, "InclusiveNamespaces", "http://www.w3.org/2001/10/xml-exc-c14n#", prefix);
/* */
/* 86 */ if (prefix == null) {
/* 87 */ excElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.w3.org/2001/10/xml-exc-c14n#");
/* */ }
/* */ else {
/* 90 */ excElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, "http://www.w3.org/2001/10/xml-exc-c14n#");
/* */ }
/* */
/* 94 */ ExcC14NParameterSpec params = (ExcC14NParameterSpec)spec;
/* 95 */ StringBuffer prefixListAttr = new StringBuffer("");
/* 96 */ List prefixList = params.getPrefixList();
/* 97 */ int i = 0; for (int size = prefixList.size(); i < size; i++) {
/* 98 */ prefixListAttr.append((String)prefixList.get(i));
/* 99 */ if (i < size - 1) {
/* 100 */ prefixListAttr.append(" ");
/* */ }
/* */ }
/* 103 */ DOMUtils.setAttribute(excElem, "PrefixList", prefixListAttr.toString());
/* 104 */ this.inclusiveNamespaces = prefixListAttr.toString();
/* 105 */ this.transformElem.appendChild(excElem);
/* */ }
/* */
/* */ public String getParamsNSURI() {
/* 109 */ return "http://www.w3.org/2001/10/xml-exc-c14n#";
/* */ }
/* */
/* */ public Data transform(Data data, XMLCryptoContext xc)
/* */ throws TransformException
/* */ {
/* 118 */ if ((data instanceof DOMSubTreeData)) {
/* 119 */ DOMSubTreeData subTree = (DOMSubTreeData)data;
/* 120 */ if (subTree.excludeComments()) {
/* */ try {
/* 122 */ this.apacheCanonicalizer = Canonicalizer.getInstance("http://www.w3.org/2001/10/xml-exc-c14n#");
/* */ }
/* */ catch (InvalidCanonicalizerException ice) {
/* 125 */ throw new TransformException("Couldn't find Canonicalizer for: http://www.w3.org/2001/10/xml-exc-c14n#: " + ice.getMessage(), ice);
/* */ }
/* */
/* */ }
/* */
/* */ }
/* */
/* 133 */ return canonicalize(data, xc);
/* */ }
/* */ }
/* Location: E:\HYN\Java\trunk\ref\lib-dep\xmldsig\xmldsig.jar
* Qualified Name: org.jcp.xml.dsig.internal.dom.DOMExcC14NMethod
* JD-Core Version: 0.6.2
*/