Changeset 25819


Ignore:
Timestamp:
2012-06-26T15:32:17+12:00 (12 years ago)
Author:
kjdon
Message:

added a few new methods that are used by GSXSLT

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java

    r25692 r25819  
    704704        return node_list;
    705705    }
     706  public static NodeList getChildrenByTagNameNS(Node n, String namespace, String local_name)
     707    {
     708        MyNodeList node_list = new MyNodeList();
     709        Node child = n.getFirstChild();
     710        while (child != null)
     711        {
     712          System.err.println("node name="+child.getNodeName()+", ns="+child.getNamespaceURI());
     713          if (child.getNodeType() == Node.ELEMENT_NODE && child.getNamespaceURI().equals(namespace) && child.getLocalName() != null && child.getLocalName().equals(local_name))
     714            {
     715                node_list.addNode(child);
     716            }
     717            child = child.getNextSibling();
     718        }
     719        return node_list;
     720    }
    706721
    707722    /** Duplicates an element, but gives it a new name */
     
    905920    }
    906921
     922  public static Element getNamedElementNS(Element parent, String namespace_uri, String node_local_name, String attribute_name, String attribute_value)
     923  {
     924        NodeList children = parent.getChildNodes();
     925        for (int i = 0; i < children.getLength(); i++)
     926        {
     927            Node child = children.item(i);
     928            if (child.getNodeType() == Node.ELEMENT_NODE &&child.getNamespaceURI().equals(namespace_uri) && child.getLocalName().equals(node_local_name))
     929            {
     930                if (((Element) child).getAttribute(attribute_name).equals(attribute_value))
     931                    return (Element) child;
     932            }
     933        }
     934        // not found
     935        return null;
     936
     937
     938  }
     939
     940    // In element main, tries to find any previous occurrence of elements with xsl-template-name=templateName,
     941    // and whose named attribute (attributeName) has the same value as the same attribute in node.
     942    // If this is the case, such a previous occurrence is removed from element main, since
     943    // the new node will contain a more specific redefinition of this element.
     944  public static void removeNamedElementNS(Element parent, String namespace_uri, String node_local_name, String attribute_name, String attribute_value)
     945  {
     946    System.err.println("removeNmaedElement "+namespace_uri+node_local_name +attribute_name+attribute_value);
     947    if (attribute_value.equals("")) {
     948          // it has no identifying attribute, so we can't find any matches
     949          return;
     950        }
     951       
     952        Element old_elem = GSXML.getNamedElementNS(parent, namespace_uri, node_local_name, attribute_name, attribute_value);
     953        if (old_elem != null)
     954          {
     955            System.err.println("removing "+old_elem.getNodeName());
     956            parent.removeChild(old_elem);
     957          }
     958       
     959  }
     960 
     961  public static void removeNamedElementsNS(Element parent, String namespace, String node_local_name, String attribute_name, String attribute_value)
     962  {
     963        if (attribute_value.equals("")) {
     964          // it has no identifying attribute, so we can't find any matches
     965          return;
     966        }
     967
     968        NodeList children = parent.getChildNodes();
     969  for (int i = children.getLength()-1; i >= 0; i--)
     970        {
     971            Node child = children.item(i);
     972            if (child.getNodeType() == Node.ELEMENT_NODE && child.getNamespaceURI().equals(namespace) && child.getLocalName() != null && child.getLocalName().equals(node_local_name))
     973            {
     974                if (((Element) child).getAttribute(attribute_name).equals(attribute_value))
     975                  parent.removeChild(child);
     976            }
     977        }
     978  }
     979
     980
     981
    907982    /**
    908983     * returns a NodeList of elements:
     
    9291004        return node_list;
    9301005    }
     1006
     1007  public static Element getLastElementByTagNameNS(Element main, String namespace, String node_name) {
     1008
     1009    NodeList nodes = main.getElementsByTagNameNS(namespace, node_name);
     1010    int len = nodes.getLength();
     1011    if (len==0) {
     1012      return null;
     1013    }
     1014    return (Element)nodes.item(len-1);
     1015  }
     1016
    9311017
    9321018    public static int SORT_TYPE_STRING = 0;
Note: See TracChangeset for help on using the changeset viewer.