src/share/classes/org/jcp/xml/dsig/internal/dom/DOMXPathFilter2Transform.java

Print this page


   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  * ===========================================================================
  23  *
  24  * (C) Copyright IBM Corp. 2003 All Rights Reserved.
  25  *
  26  * ===========================================================================
  27  */
  28 /*
  29  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
  30  */
  31 /*
  32  * $Id: DOMXPathFilter2Transform.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
  33  */
  34 package org.jcp.xml.dsig.internal.dom;
  35 
  36 import javax.xml.crypto.*;
  37 import javax.xml.crypto.dsig.*;
  38 import javax.xml.crypto.dsig.spec.TransformParameterSpec;
  39 import javax.xml.crypto.dsig.spec.XPathType;
  40 import javax.xml.crypto.dsig.spec.XPathFilter2ParameterSpec;
  41 import java.security.InvalidAlgorithmParameterException;
  42 import java.util.ArrayList;
  43 import java.util.Iterator;
  44 import java.util.List;
  45 import java.util.Map;
  46 import java.util.HashMap;
  47 import org.w3c.dom.Attr;
  48 import org.w3c.dom.Element;
  49 import org.w3c.dom.NamedNodeMap;
  50 
  51 /**
  52  * DOM-based implementation of XPath Filter 2.0 Transform.
  53  * (Uses Apache XML-Sec Transform implementation)
  54  *
  55  * @author Joyce Leung
  56  */
  57 public final class DOMXPathFilter2Transform extends ApacheTransform {
  58 
  59     public void init(TransformParameterSpec params)
  60         throws InvalidAlgorithmParameterException {

  61         if (params == null) {
  62             throw new InvalidAlgorithmParameterException("params are required");
  63         } else if (!(params instanceof XPathFilter2ParameterSpec)) {
  64             throw new InvalidAlgorithmParameterException
  65                 ("params must be of type XPathFilter2ParameterSpec");
  66         }
  67         this.params = params;
  68     }
  69 
  70     public void init(XMLStructure parent, XMLCryptoContext context)
  71         throws InvalidAlgorithmParameterException {
  72 
  73         super.init(parent, context);
  74         try {
  75             unmarshalParams(DOMUtils.getFirstChildElement(transformElem));
  76         } catch (MarshalException me) {
  77             throw (InvalidAlgorithmParameterException)
  78                 new InvalidAlgorithmParameterException().initCause(me);
  79         }
  80     }
  81 
  82     private void unmarshalParams(Element curXPathElem) throws MarshalException {
  83         List list = new ArrayList();

  84         while (curXPathElem != null) {
  85             String xPath = curXPathElem.getFirstChild().getNodeValue();
  86             String filterVal =
  87                 DOMUtils.getAttributeValue(curXPathElem, "Filter");
  88             if (filterVal == null) {
  89                 throw new MarshalException("filter cannot be null");
  90             }
  91             XPathType.Filter filter = null;
  92             if (filterVal.equals("intersect")) {
  93                 filter = XPathType.Filter.INTERSECT;
  94             } else if (filterVal.equals("subtract")) {
  95                 filter = XPathType.Filter.SUBTRACT;
  96             } else if (filterVal.equals("union")) {
  97                 filter = XPathType.Filter.UNION;
  98             } else {
  99                 throw new MarshalException("Unknown XPathType filter type"
 100                     + filterVal);
 101             }
 102             NamedNodeMap attributes = curXPathElem.getAttributes();
 103             if (attributes != null) {
 104                 int length = attributes.getLength();
 105                 Map namespaceMap = new HashMap(length);

 106                 for (int i = 0; i < length; i++) {
 107                     Attr attr = (Attr) attributes.item(i);
 108                     String prefix = attr.getPrefix();
 109                     if (prefix != null && prefix.equals("xmlns")) {
 110                         namespaceMap.put(attr.getLocalName(), attr.getValue());
 111                     }
 112                 }
 113                 list.add(new XPathType(xPath, filter, namespaceMap));
 114             } else {
 115                 list.add(new XPathType(xPath, filter));
 116             }
 117 
 118             curXPathElem = DOMUtils.getNextSiblingElement(curXPathElem);
 119         }
 120         this.params = new XPathFilter2ParameterSpec(list);
 121     }
 122 
 123     public void marshalParams(XMLStructure parent, XMLCryptoContext context)
 124         throws MarshalException {
 125 
 126         super.marshalParams(parent, context);
 127         XPathFilter2ParameterSpec xp =
 128             (XPathFilter2ParameterSpec) getParameterSpec();
 129         String prefix = DOMUtils.getNSPrefix(context, Transform.XPATH2);
 130         String qname = (prefix == null || prefix.length() == 0)
 131                        ? "xmlns" : "xmlns:" + prefix;
 132         List list = xp.getXPathList();
 133         for (int i = 0, size = list.size(); i < size; i++) {
 134             XPathType xpathType = (XPathType) list.get(i);
 135             Element elem = DOMUtils.createElement
 136                 (ownerDoc, "XPath", Transform.XPATH2, prefix);
 137             elem.appendChild
 138                 (ownerDoc.createTextNode(xpathType.getExpression()));
 139             DOMUtils.setAttribute
 140                 (elem, "Filter", xpathType.getFilter().toString());
 141             elem.setAttributeNS("http://www.w3.org/2000/xmlns/", qname,
 142                 Transform.XPATH2);
 143 
 144             // add namespace attributes, if necessary
 145             Iterator it = xpathType.getNamespaceMap().entrySet().iterator();
 146             while (it.hasNext()) {
 147                 Map.Entry entry = (Map.Entry) it.next();
 148                 elem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:"
 149                     + (String) entry.getKey(), (String) entry.getValue());


 150             }
 151 
 152             transformElem.appendChild(elem);
 153         }
 154     }
 155 }
   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  * ===========================================================================
  25  *
  26  * (C) Copyright IBM Corp. 2003 All Rights Reserved.
  27  *
  28  * ===========================================================================
  29  */
  30 /*
  31  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
  32  */
  33 /*
  34  * $Id: DOMXPathFilter2Transform.java 1203789 2011-11-18 18:46:07Z mullan $
  35  */
  36 package org.jcp.xml.dsig.internal.dom;
  37 
  38 import javax.xml.crypto.*;
  39 import javax.xml.crypto.dsig.*;
  40 import javax.xml.crypto.dsig.spec.TransformParameterSpec;
  41 import javax.xml.crypto.dsig.spec.XPathType;
  42 import javax.xml.crypto.dsig.spec.XPathFilter2ParameterSpec;
  43 import java.security.InvalidAlgorithmParameterException;
  44 import java.util.ArrayList;
  45 import java.util.HashMap;
  46 import java.util.List;
  47 import java.util.Map;
  48 import java.util.Set;
  49 import org.w3c.dom.Attr;
  50 import org.w3c.dom.Element;
  51 import org.w3c.dom.NamedNodeMap;
  52 
  53 /**
  54  * DOM-based implementation of XPath Filter 2.0 Transform.
  55  * (Uses Apache XML-Sec Transform implementation)
  56  *
  57  * @author Joyce Leung
  58  */
  59 public final class DOMXPathFilter2Transform extends ApacheTransform {
  60 
  61     public void init(TransformParameterSpec params)
  62         throws InvalidAlgorithmParameterException
  63     {
  64         if (params == null) {
  65             throw new InvalidAlgorithmParameterException("params are required");
  66         } else if (!(params instanceof XPathFilter2ParameterSpec)) {
  67             throw new InvalidAlgorithmParameterException
  68                 ("params must be of type XPathFilter2ParameterSpec");
  69         }
  70         this.params = params;
  71     }
  72 
  73     public void init(XMLStructure parent, XMLCryptoContext context)
  74         throws InvalidAlgorithmParameterException
  75     {
  76         super.init(parent, context);
  77         try {
  78             unmarshalParams(DOMUtils.getFirstChildElement(transformElem));
  79         } catch (MarshalException me) {
  80             throw new InvalidAlgorithmParameterException(me);

  81         }
  82     }
  83 
  84     private void unmarshalParams(Element curXPathElem) throws MarshalException
  85     {
  86         List<XPathType> list = new ArrayList<XPathType>();
  87         while (curXPathElem != null) {
  88             String xPath = curXPathElem.getFirstChild().getNodeValue();
  89             String filterVal = DOMUtils.getAttributeValue(curXPathElem,
  90                                                           "Filter");
  91             if (filterVal == null) {
  92                 throw new MarshalException("filter cannot be null");
  93             }
  94             XPathType.Filter filter = null;
  95             if (filterVal.equals("intersect")) {
  96                 filter = XPathType.Filter.INTERSECT;
  97             } else if (filterVal.equals("subtract")) {   
  98                 filter = XPathType.Filter.SUBTRACT;
  99             } else if (filterVal.equals("union")) {
 100                 filter = XPathType.Filter.UNION;
 101             } else {
 102                 throw new MarshalException("Unknown XPathType filter type" +
 103                                            filterVal);
 104             }
 105             NamedNodeMap attributes = curXPathElem.getAttributes();
 106             if (attributes != null) {
 107                 int length = attributes.getLength();
 108                 Map<String, String> namespaceMap =
 109                     new HashMap<String, String>(length);
 110                 for (int i = 0; i < length; i++) {
 111                     Attr attr = (Attr)attributes.item(i);
 112                     String prefix = attr.getPrefix();
 113                     if (prefix != null && prefix.equals("xmlns")) {
 114                         namespaceMap.put(attr.getLocalName(), attr.getValue());
 115                     }
 116                 }
 117                 list.add(new XPathType(xPath, filter, namespaceMap));
 118             } else {
 119                 list.add(new XPathType(xPath, filter));
 120             }
 121 
 122             curXPathElem = DOMUtils.getNextSiblingElement(curXPathElem);
 123         }
 124         this.params = new XPathFilter2ParameterSpec(list);
 125     }
 126 
 127     public void marshalParams(XMLStructure parent, XMLCryptoContext context)
 128         throws MarshalException
 129     {
 130         super.marshalParams(parent, context);
 131         XPathFilter2ParameterSpec xp = 
 132             (XPathFilter2ParameterSpec)getParameterSpec();
 133         String prefix = DOMUtils.getNSPrefix(context, Transform.XPATH2);
 134         String qname = (prefix == null || prefix.length() == 0) 
 135                        ? "xmlns" : "xmlns:" + prefix;
 136         @SuppressWarnings("unchecked")
 137         List<XPathType> xpathList = xp.getXPathList();
 138         for (XPathType xpathType : xpathList) {
 139             Element elem = DOMUtils.createElement(ownerDoc, "XPath",
 140                                                   Transform.XPATH2, prefix);
 141             elem.appendChild
 142                 (ownerDoc.createTextNode(xpathType.getExpression()));
 143             DOMUtils.setAttribute(elem, "Filter",
 144                                   xpathType.getFilter().toString());
 145             elem.setAttributeNS("http://www.w3.org/2000/xmlns/", qname,
 146                                 Transform.XPATH2);
 147 
 148             // add namespace attributes, if necessary
 149             @SuppressWarnings("unchecked")
 150             Set<Map.Entry<String, String>> entries =
 151                 xpathType.getNamespaceMap().entrySet();
 152             for (Map.Entry<String, String> entry : entries) {
 153                 elem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" +
 154                                     entry.getKey(), 
 155                                     entry.getValue());
 156             }
 157 
 158             transformElem.appendChild(elem);
 159         }
 160     }
 161 }