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, 2013, Oracle and/or its affiliates. All rights reserved.
  25  */
  26 /*
  27  * $Id: ApacheNodeSetData.java 1203890 2011-11-18 22:47:56Z mullan $
  28  */
  29 package org.jcp.xml.dsig.internal.dom;
  30 
  31 import java.util.Collections;
  32 import java.util.Iterator;
  33 import java.util.LinkedHashSet;
  34 import java.util.List;
  35 import java.util.Set;
  36 import javax.xml.crypto.NodeSetData;
  37 import org.w3c.dom.Node;
  38 import com.sun.org.apache.xml.internal.security.signature.NodeFilter;
  39 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
  40 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
  41 
  42 public class ApacheNodeSetData implements ApacheData, NodeSetData {
  43 
  44     private XMLSignatureInput xi;
  45 
  46     public ApacheNodeSetData(XMLSignatureInput xi) {
  47         this.xi = xi;
  48     }
  49 
  50     public Iterator iterator() {
  51         // If nodefilters are set, must execute them first to create node-set
  52         if (xi.getNodeFilters() != null && !xi.getNodeFilters().isEmpty()) {
  53             return Collections.unmodifiableSet
  54                 (getNodeSet(xi.getNodeFilters())).iterator();
  55         }
  56         try {
  57             return Collections.unmodifiableSet(xi.getNodeSet()).iterator();
  58         } catch (Exception e) {
  59             // should not occur
  60             throw new RuntimeException
  61                 ("unrecoverable error retrieving nodeset", e);
  62         }
  63     }
  64 
  65     public XMLSignatureInput getXMLSignatureInput() {
  66         return xi;
  67     }
  68 
  69     private Set<Node> getNodeSet(List<NodeFilter> nodeFilters) {
  70         if (xi.isNeedsToBeExpanded()) {
  71             XMLUtils.circumventBug2650
  72                 (XMLUtils.getOwnerDocument(xi.getSubNode()));
  73         }
  74 
  75         Set<Node> inputSet = new LinkedHashSet<Node>();
  76         XMLUtils.getSet(xi.getSubNode(), inputSet,
  77                         null, !xi.isExcludeComments());
  78         Set<Node> nodeSet = new LinkedHashSet<Node>();
  79         for (Node currentNode : inputSet) {
  80             Iterator<NodeFilter> it = nodeFilters.iterator();
  81             boolean skipNode = false;
  82             while (it.hasNext() && !skipNode) {
  83                 NodeFilter nf = it.next();
  84                 if (nf.isNodeInclude(currentNode) != 1) {
  85                     skipNode = true;
  86                 }
  87             }
  88             if (!skipNode) {
  89                 nodeSet.add(currentNode);
  90             }
  91         }
  92         return nodeSet;
  93     }
  94 }