You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2003/01/22 11:56:27 UTC

cvs commit: jakarta-commons-sandbox/jelly/jelly-tags/xml/src/test/org/apache/commons/jelly/tags/xml suite.jelly

jstrachan    2003/01/22 02:56:27

  Modified:    jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml
                        ParseTagSupport.java ParseTag.java
               jelly/jelly-tags/xml/src/test/org/apache/commons/jelly/tags/xml
                        suite.jelly
  Log:
  Added support for parsing XML which is inside a String variable. This can now be done via
  
  <x:parse var="doc" text="${whatever}"/>
  
  Also added a JellyUnit test case to check this works
  
  Revision  Changes    Path
  1.2       +32 -13    jakarta-commons-sandbox/jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml/ParseTagSupport.java
  
  Index: ParseTagSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml/ParseTagSupport.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ParseTagSupport.java	15 Jan 2003 23:56:45 -0000	1.1
  +++ ParseTagSupport.java	22 Jan 2003 10:56:27 -0000	1.2
  @@ -64,6 +64,7 @@
   import java.io.File;
   import java.io.InputStream;
   import java.io.Reader;
  +import java.io.StringReader;
   import java.net.URL;
   
   import org.apache.commons.jelly.TagSupport;
  @@ -87,6 +88,9 @@
   
       /** The variable that will be generated for the document */
       private String var;
  +    
  +    /** The markup text to be parsed */
  +    private String text;
   
       /** The SAXReader used to parser the document */
       private SAXReader saxReader;
  @@ -108,6 +112,23 @@
       public void setVar(String var) {
           this.var = var;
       }
  +
  +    /**
  +     * Returns the text to be parsed
  +     * @return String
  +     */
  +    public String getText() {
  +        return text;
  +    }
  +
  +    /**
  +     * Sets the text to be parsed by this parser
  +     * @param text The text to be parsed by this parser
  +     */
  +    public void setText(String text) {
  +        this.text = text;
  +    }
  +
       
       /** @return the SAXReader used for parsing, creating one lazily if need be  */
       public SAXReader getSAXReader() throws Exception {
  @@ -142,18 +163,16 @@
           invokeBody( newOutput);
           handler.endDocument();
           return handler.getDocument();
  -
  -/*
  -        // the following is inefficient as it requires a parse of the text
  -        // but is left here in the code to see how it could be done.
  -
  -        String text = getBodyText();
  -        
  +    }
  +    
  +    /**
  +     * Parses the give piece of text as being markup
  +     */
  +    protected Document parseText(String text) throws Exception {
           if ( log.isDebugEnabled() ) {
               log.debug( "About to parse: " + text );
           }
           return getSAXReader().read( new StringReader( text ) );
  -*/
       }
   
       /**
  
  
  
  1.2       +12 -6     jakarta-commons-sandbox/jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml/ParseTag.java
  
  Index: ParseTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml/ParseTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ParseTag.java	15 Jan 2003 23:56:45 -0000	1.1
  +++ ParseTag.java	22 Jan 2003 10:56:27 -0000	1.2
  @@ -138,7 +138,13 @@
           Document document = null;
           Object xmlObj = this.getXml();
           if (xmlObj == null) {
  -            document = parseBody(output);
  +            String text = getText();
  +            if (text != null) {
  +                document = parseText(text);
  +            }
  +            else {            
  +                document = parseBody(output);
  +            }
           }
           else {
               document = parse(xmlObj);
  
  
  
  1.3       +25 -0     jakarta-commons-sandbox/jelly/jelly-tags/xml/src/test/org/apache/commons/jelly/tags/xml/suite.jelly
  
  Index: suite.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/jelly-tags/xml/src/test/org/apache/commons/jelly/tags/xml/suite.jelly,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- suite.jelly	17 Jan 2003 04:53:14 -0000	1.2
  +++ suite.jelly	22 Jan 2003 10:56:27 -0000	1.3
  @@ -4,12 +4,37 @@
   	xmlns:x="jelly:xml" 
   	xmlns:test="jelly:junit">
   
  +
   	<test:case name="testUriParse">
   		<x:parse var="doc" xml="dummy.xml"/>
   		
   		<test:assert xpath="$doc/dummy/entry[@id='2']"/>		
   	</test:case>
   	
  +  <test:case name="testBodyParse">
  +  	
  +  	<x:parse var="doc">
  +  		<foo>
  +  			<bar a="1"/>
  +  		</foo>
  +  	</x:parse>
  +	
  +		<test:assert xpath="$doc/foo/bar[@a='1']"/>
  +  </test:case>
  +  
  +  <test:case name="testTextParse">
  +  	
  +  	<j:set var="text" encode="false">
  +  		<foo>
  +  			<bar a="1"/>
  +  		</foo>
  +  	</j:set>
  +	
  +  	<x:parse var="doc" text="${text}"/>
  +	
  +		<test:assert xpath="$doc/foo/bar[@a='1']"/>
  +  </test:case>
  +  
     <test:case name="testElementAndAttribute">
     	
     	<x:parse var="doc">
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>