jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pier...@apache.org
Subject cvs commit: jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/xml XPathUtil.java
Date Fri, 09 May 2003 00:16:16 GMT
pierred     2003/05/08 17:16:16

  Modified:    standard/src/org/apache/taglibs/standard/tag/common/xml
                        XPathUtil.java
  Log:
  Patch from Ramesh to properly handle NodeLists.
  
  Revision  Changes    Path
  1.13      +60 -30    jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/xml/XPathUtil.java
  
  Index: XPathUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/xml/XPathUtil.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- XPathUtil.java	1 May 2003 00:02:54 -0000	1.12
  +++ XPathUtil.java	9 May 2003 00:16:16 -0000	1.13
  @@ -443,6 +443,23 @@
           }
           return null;
       }
  +
  +     static Document getDummyDocumentWithoutRoot( ) {
  +        try {
  +            if ( dbf == null ) {
  +                dbf = DocumentBuilderFactory.newInstance();
  +                dbf.setNamespaceAware( true );
  +                dbf.setValidating( false );
  +            }
  +            db = dbf.newDocumentBuilder();
  +
  +            d = db.newDocument();
  +            return d;
  +        } catch ( Exception e ) {
  +            e.printStackTrace();
  +        }
  +        return null;
  +    }
       
       // The following variable is used for holding the modified xpath string
       // when adapting parameter for Xalan XPath engine, where we need to have
  @@ -633,6 +650,7 @@
           
           modifiedXPath = xpath;
           String origXPath = xpath ;
  +        boolean whetherOrigXPath = true;
           
           // If contextNode is not null then  just pass the values to Xalan XPath
           if ( n != null ) {
  @@ -657,6 +675,7 @@
                   xpath = xpath.substring( xpath.indexOf("/"));
               } else  {
                   xpath = "/*";
  +                whetherOrigXPath = false; 
               }
              
               
  @@ -675,48 +694,59 @@
                       //System.out.println("Creating a Dummy document to pass " +
                       // " onto as context node " );
                       
  -                    if ( Class.forName("org.apache.taglibs.standard.tag.common.xml.JSTLNodeList").isInstance(
  -                    varObject ) ) {
  +                    if ( Class.forName("org.apache.taglibs.standard.tag.common.xml.JSTLNodeList").isInstance(
varObject ) ) {
                           Document newDocument = getDummyDocument();
   
                           JSTLNodeList jstlNodeList = (JSTLNodeList)varObject;
  -                        if  ( ( jstlNodeList.getLength() == 1 ) && 
  -   (!Class.forName("org.w3c.dom.Node").isInstance( jstlNodeList.elementAt(0) ) ) ) { 
  -
  -                            //Nodelist with primitive type
  -                            Object myObject = jstlNodeList.elementAt(0);
  -
  -                            //p("Single Element of primitive type");
  -                            //p("Type => " + myObject.getClass());
  -
  -                            xpath = myObject.toString();
  -
  -                            //p("String value ( xpathwould be this) => " + xpath);
  -                            boundDocument = newDocument;
  +                        if   ( jstlNodeList.getLength() == 1 ) { 
  +                            if ( Class.forName("org.w3c.dom.Node").isInstance(
  +                                jstlNodeList.elementAt(0) ) ) { 
  +                                Node node = (Node)jstlNodeList.elementAt(0);
  +                                Document doc = getDummyDocumentWithoutRoot();
  +                                Node importedNode = doc.importNode( node, true);
  +                                doc.appendChild (importedNode );
  +                                boundDocument = doc;
  +                                if ( whetherOrigXPath ) {
  +                                    xpath="/*" + xpath;
  +                                }
  +
  +                            } else {
  +
  +                                //Nodelist with primitive type
  +                                Object myObject = jstlNodeList.elementAt(0);
  +
  +                                //p("Single Element of primitive type");
  +                                //p("Type => " + myObject.getClass());
  +
  +                                xpath = myObject.toString();
  +
  +                                //p("String value ( xpathwould be this) => " + xpath);
  +                                boundDocument = newDocument;
  +                            } 
                               
                           } else {
   
                               Element dummyroot = newDocument.getDocumentElement();
  -                        for ( int i=0; i< jstlNodeList.getLength(); i++ ) {
  -                            Node currNode = (Node)jstlNodeList.item(i);
  +                            for ( int i=0; i< jstlNodeList.getLength(); i++ ) {
  +                                Node currNode = (Node)jstlNodeList.item(i);
                               
  -                            Node importedNode = newDocument.importNode(
  -                            currNode, true );
  +                                Node importedNode = newDocument.importNode(
  +                                    currNode, true );
   
  -                            //printDetails ( newDocument);
  +                                //printDetails ( newDocument);
   
  -                            dummyroot.appendChild( importedNode );
  +                                dummyroot.appendChild( importedNode );
   
  -                            //p( "Details of the document After importing  " );
  -                            //printDetails ( newDocument);
  -                        }
  -                        boundDocument = newDocument;
  -                        // printDetails ( boundDocument );
  -                        // Verify : As we are adding Document element we need to
  -                        // change the xpath expression. Hopefully this won't
  -                        // change the result
  +                                //p( "Details of the document After importing");
  +                                //printDetails ( newDocument);
  +                            }
  +                            boundDocument = newDocument;
  +                            // printDetails ( boundDocument );
  +                            //Verify :As we are adding Document element we need
  +                            // to change the xpath expression.Hopefully this
  +                            // won't  change the result
   
  -                        xpath = "/*" +  xpath;
  +                            xpath = "/*" +  xpath;
                           }
                       } else if ( Class.forName("org.w3c.dom.Node").isInstance(
                       varObject ) ) {
  
  
  

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


Mime
View raw message