EnvelopeIdResolverUtilities.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.security.message.signature;

  21. import org.w3c.dom.Element;
  22. import org.w3c.dom.Node;

  23. /**
  24.  * EnvelopeIdResolverUtilities
  25.  *
  26.  * @author Andrea Poli (apoli@link.it)
  27.  * @author $Author$
  28.  * @version $Rev$, $Date$
  29.  */
  30. public class EnvelopeIdResolverUtilities {

  31.     public static Element findElementById(Node startNode, String value, String ns) throws Exception
  32.     {
  33.         Element foundElement = null;
  34.         if(startNode == null)
  35.             return null;
  36.         Node startParent = startNode.getParentNode();
  37.         Node processedNode = null;
  38.         while(startNode != null)
  39.         {
  40.             if(startNode.getNodeType() == 1)
  41.             {
  42.                 Element se = (Element)startNode;
  43.                 if(se.hasAttributeNS(ns, "Id") && value.equals(se.getAttributeNS(ns, "Id")))
  44.                     if(foundElement == null)
  45.                         foundElement = se;
  46.                     else
  47.                         throw new Exception((new StringBuilder()).append("Multiple elements with the same 'Id' attribute : ").append(value).append(" has been found").toString());
  48.             }
  49.             processedNode = startNode;
  50.             startNode = startNode.getFirstChild();
  51.             if(startNode == null)
  52.                 startNode = processedNode.getNextSibling();
  53.             while(startNode == null)
  54.             {
  55.                 processedNode = processedNode.getParentNode();
  56.                 if(processedNode == startParent)
  57.                     return foundElement;
  58.                 startNode = processedNode.getNextSibling();
  59.             }
  60.         }
  61.         return foundElement;
  62.     }

  63. }