commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cos...@apache.org
Subject cvs commit: jakarta-commons/modeler/src/java/org/apache/commons/modeler/util DomUtil.java
Date Sun, 13 Apr 2003 17:08:00 GMT
costin      2003/04/13 10:08:00

  Modified:    modeler/src/java/org/apache/commons/modeler/util
                        DomUtil.java
  Log:
  Few more methods used for saving.
  
  Revision  Changes    Path
  1.5       +69 -11    jakarta-commons/modeler/src/java/org/apache/commons/modeler/util/DomUtil.java
  
  Index: DomUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/util/DomUtil.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DomUtil.java	9 Apr 2003 07:25:18 -0000	1.4
  +++ DomUtil.java	13 Apr 2003 17:08:00 -0000	1.5
  @@ -83,12 +83,13 @@
   
       // -------------------- DOM utils --------------------
   
  -    /** Get the content of a node
  +    /** Get the trimed text content of a node or null if there is no text
        */
       public static String getContent(Node n ) {
           if( n==null ) return null;
  -        Node n1=n.getFirstChild();
  -        // XXX Check if it's a text node
  +        Node n1=DomUtil.getChild(n, Node.TEXT_NODE);
  +
  +        if( n1==null ) return null;
   
           String s1=n1.getNodeValue();
           return s1.trim();
  @@ -128,16 +129,58 @@
       }
   
       public static void setAttribute(Node node, String attName, String val) {
  -        Element element=(Element)node;
  -        if( element.getAttribute(attName) != null ) {
  -            element.removeAttribute(attName);
  +        NamedNodeMap attributes=node.getAttributes();
  +        Node attNode=node.getOwnerDocument().createAttribute(attName);
  +        attNode.setNodeValue( val );
  +        attributes.setNamedItem(attNode);
  +    }
  +    
  +    public static void removeAttribute( Node node, String attName ) {
  +        NamedNodeMap attributes=node.getAttributes();
  +        attributes.removeNamedItem(attName);                
  +    }
  +    
  +    
  +    /** Set or replace the text value 
  +     */ 
  +    public static void setText(Node node, String val) {
  +        Node chld=DomUtil.getChild(node, Node.TEXT_NODE);
  +        if( chld == null ) {
  +            Node textN=node.getOwnerDocument().createTextNode(val);
  +            node.appendChild(textN);
  +            return;
           }
  -//        Node attNode=element.getOwnerDocument().createAttribute(attName);
  -//        attNode.setNodeValue(val);
  -        element.setAttribute(attName, val);
  -//        element.appendChild(attNode);
  +        // change the value
  +        chld.setNodeValue(val);           
       }
   
  +    /** Find the first direct child with a given attribute.
  +     * @param parent
  +     * @param elemName name of the element, or null for any 
  +     * @param attName attribute we're looking for
  +     * @param attVal attribute value or null if we just want any
  +     */ 
  +    public static Node findChildWithAtt(Node parent, String elemName,
  +                                        String attName, String attVal) {
  +        
  +        Node child=DomUtil.getChild(parent, Node.ELEMENT_NODE);
  +        if( attVal== null ) {
  +            while( child!= null &&
  +                    ( elemName==null || elemName.equals( child.getNodeName())) &&

  +                    DomUtil.getAttribute(child, attName) != null ) {
  +                child=getNext(child, elemName, Node.ELEMENT_NODE );
  +            }
  +        } else {
  +            while( child!= null && 
  +                    ( elemName==null || elemName.equals( child.getNodeName())) &&

  +                    ! attVal.equals( DomUtil.getAttribute(child, attName)) ) {
  +                child=getNext(child, elemName, Node.ELEMENT_NODE );
  +            }
  +        }
  +        return child;        
  +    }    
  +    
  +
       /** Get the first child's content ( ie it's included TEXT node ).
        */
       public static String getChildContent( Node parent, String name ) {
  @@ -153,6 +196,17 @@
           return null;
       }
   
  +    /** Get the first direct child with a given type
  +     */
  +    public static Node getChild( Node parent, int type ) {
  +        Node n=parent.getFirstChild();
  +        while( n!=null && type != n.getNodeType() ) {
  +            n=n.getNextSibling();
  +        }
  +        if( n==null ) return null;
  +        return n;
  +    }
  +
       /** Get the next sibling with the same name and type
        */
       public static Node getNext( Node current ) {
  @@ -161,12 +215,16 @@
           return getNext( current, name, type);
       }
   
  +    /** Return the next sibling with a given name and type
  +     */ 
       public static Node getNext( Node current, String name, int type) {
           Node first=current.getNextSibling();
           if( first==null ) return null;
  +
           for (Node node = first; node != null;
                node = node.getNextSibling()) {
  -            if( node.getNodeType() != type ) continue;
  +            
  +            if( type >= 0 && node.getNodeType() != type ) continue;
               //System.out.println("getNode: " + name + " " + node.getNodeName());
               if( name==null )
                   return node;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message