1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 2005 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 * 20 */ 21 /* 22 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 23 */ 24 /* 25 * $Id: DOMXPathTransform.java,v 1.2 2008/07/24 15:20:32 mullan Exp $ 26 */ 27 package org.jcp.xml.dsig.internal.dom; 28 29 import javax.xml.crypto.*; 30 import javax.xml.crypto.dsig.*; 31 import javax.xml.crypto.dsig.spec.TransformParameterSpec; 32 import javax.xml.crypto.dsig.spec.XPathFilterParameterSpec; 33 import java.security.InvalidAlgorithmParameterException; 34 import java.util.Iterator; 35 import java.util.Map; 36 import java.util.HashMap; 37 import org.w3c.dom.Attr; 38 import org.w3c.dom.Element; 39 import org.w3c.dom.NamedNodeMap; 40 41 /** 42 * DOM-based implementation of XPath Filtering Transform. 43 * (Uses Apache XML-Sec Transform implementation) 44 * 45 * @author Sean Mullan 46 */ 47 public final class DOMXPathTransform extends ApacheTransform { 48 49 public void init(TransformParameterSpec params) 50 throws InvalidAlgorithmParameterException { 51 if (params == null) { 52 throw new InvalidAlgorithmParameterException("params are required"); 53 } else if (!(params instanceof XPathFilterParameterSpec)) { 54 throw new InvalidAlgorithmParameterException 55 ("params must be of type XPathFilterParameterSpec"); 56 } 57 this.params = params; 58 } 59 60 public void init(XMLStructure parent, XMLCryptoContext context) 61 throws InvalidAlgorithmParameterException { 62 63 super.init(parent, context); 64 unmarshalParams(DOMUtils.getFirstChildElement(transformElem)); 65 } 66 67 private void unmarshalParams(Element paramsElem) { 68 String xPath = paramsElem.getFirstChild().getNodeValue(); 69 // create a Map of namespace prefixes 70 NamedNodeMap attributes = paramsElem.getAttributes(); 71 if (attributes != null) { 72 int length = attributes.getLength(); 73 Map namespaceMap = new HashMap(length); 74 for (int i = 0; i < length; i++) { 75 Attr attr = (Attr) attributes.item(i); 76 String prefix = attr.getPrefix(); 77 if (prefix != null && prefix.equals("xmlns")) { 78 namespaceMap.put(attr.getLocalName(), attr.getValue()); 79 } 80 } 81 this.params = new XPathFilterParameterSpec(xPath, namespaceMap); 82 } else { 83 this.params = new XPathFilterParameterSpec(xPath); 84 } 85 } 86 87 public void marshalParams(XMLStructure parent, XMLCryptoContext context) 88 throws MarshalException { 89 90 super.marshalParams(parent, context); 91 XPathFilterParameterSpec xp = 92 (XPathFilterParameterSpec) getParameterSpec(); 93 Element xpathElem = DOMUtils.createElement 94 (ownerDoc, "XPath", XMLSignature.XMLNS, 95 DOMUtils.getSignaturePrefix(context)); 96 xpathElem.appendChild(ownerDoc.createTextNode(xp.getXPath())); 97 98 // add namespace attributes, if necessary 99 Iterator i = xp.getNamespaceMap().entrySet().iterator(); 100 while (i.hasNext()) { 101 Map.Entry entry = (Map.Entry) i.next(); 102 xpathElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" 103 + (String) entry.getKey(), (String) entry.getValue()); 104 } 105 106 transformElem.appendChild(xpathElem); 107 } 108 } | 1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /** 6 * Licensed to the Apache Software Foundation (ASF) under one 7 * or more contributor license agreements. See the NOTICE file 8 * distributed with this work for additional information 9 * regarding copyright ownership. The ASF licenses this file 10 * to you under the Apache License, Version 2.0 (the 11 * "License"); you may not use this file except in compliance 12 * with the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, 17 * software distributed under the License is distributed on an 18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19 * KIND, either express or implied. See the License for the 20 * specific language governing permissions and limitations 21 * under the License. 22 */ 23 /* 24 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 25 */ 26 /* 27 * $Id: DOMXPathTransform.java 1203789 2011-11-18 18:46:07Z mullan $ 28 */ 29 package org.jcp.xml.dsig.internal.dom; 30 31 import javax.xml.crypto.*; 32 import javax.xml.crypto.dsig.*; 33 import javax.xml.crypto.dsig.spec.TransformParameterSpec; 34 import javax.xml.crypto.dsig.spec.XPathFilterParameterSpec; 35 import java.security.InvalidAlgorithmParameterException; 36 import java.util.HashMap; 37 import java.util.Map; 38 import java.util.Set; 39 import org.w3c.dom.Attr; 40 import org.w3c.dom.Element; 41 import org.w3c.dom.NamedNodeMap; 42 43 /** 44 * DOM-based implementation of XPath Filtering Transform. 45 * (Uses Apache XML-Sec Transform implementation) 46 * 47 * @author Sean Mullan 48 */ 49 public final class DOMXPathTransform extends ApacheTransform { 50 51 public void init(TransformParameterSpec params) 52 throws InvalidAlgorithmParameterException 53 { 54 if (params == null) { 55 throw new InvalidAlgorithmParameterException("params are required"); 56 } else if (!(params instanceof XPathFilterParameterSpec)) { 57 throw new InvalidAlgorithmParameterException 58 ("params must be of type XPathFilterParameterSpec"); 59 } 60 this.params = params; 61 } 62 63 public void init(XMLStructure parent, XMLCryptoContext context) 64 throws InvalidAlgorithmParameterException 65 { 66 super.init(parent, context); 67 unmarshalParams(DOMUtils.getFirstChildElement(transformElem)); 68 } 69 70 private void unmarshalParams(Element paramsElem) { 71 String xPath = paramsElem.getFirstChild().getNodeValue(); 72 // create a Map of namespace prefixes 73 NamedNodeMap attributes = paramsElem.getAttributes(); 74 if (attributes != null) { 75 int length = attributes.getLength(); 76 Map<String, String> namespaceMap = 77 new HashMap<String, String>(length); 78 for (int i = 0; i < length; i++) { 79 Attr attr = (Attr)attributes.item(i); 80 String prefix = attr.getPrefix(); 81 if (prefix != null && prefix.equals("xmlns")) { 82 namespaceMap.put(attr.getLocalName(), attr.getValue()); 83 } 84 } 85 this.params = new XPathFilterParameterSpec(xPath, namespaceMap); 86 } else { 87 this.params = new XPathFilterParameterSpec(xPath); 88 } 89 } 90 91 public void marshalParams(XMLStructure parent, XMLCryptoContext context) 92 throws MarshalException 93 { 94 super.marshalParams(parent, context); 95 XPathFilterParameterSpec xp = 96 (XPathFilterParameterSpec)getParameterSpec(); 97 Element xpathElem = DOMUtils.createElement(ownerDoc, "XPath", 98 XMLSignature.XMLNS, DOMUtils.getSignaturePrefix(context)); 99 xpathElem.appendChild(ownerDoc.createTextNode(xp.getXPath())); 100 101 // add namespace attributes, if necessary 102 @SuppressWarnings("unchecked") 103 Set<Map.Entry<String, String>> entries = 104 xp.getNamespaceMap().entrySet(); 105 for (Map.Entry<String, String> entry : entries) { 106 xpathElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + 107 entry.getKey(), 108 entry.getValue()); 109 } 110 111 transformElem.appendChild(xpathElem); 112 } 113 } |