commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly maven.xml project.xml
Date Wed, 31 Jul 2002 11:19:21 GMT
jstrachan    2002/07/31 04:19:21

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/betwixt
                        ParseTag.java IntrospectorTag.java
               jelly    maven.xml project.xml
  Added:       jelly/src/test/org/apache/commons/jelly/betwixt suite.jelly
                        rss-example.xml common.jelly TestJelly.java
  Removed:     jelly/src/java/org/apache/commons/jelly/tags/betwixt
                        IntrospectorUser.java
  Log:
  Pached the betwixt tags so that the <betwixt:parse> tag can either take a URI to the
XML or can parse its body.
  
  Added a JellyUnit test case of both these ways of using the <betwixt:parse> tag.
  
  Revision  Changes    Path
  1.2       +107 -10   jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/betwixt/ParseTag.java
  
  Index: ParseTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/betwixt/ParseTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ParseTag.java	30 Jul 2002 17:57:19 -0000	1.1
  +++ ParseTag.java	31 Jul 2002 11:19:20 -0000	1.2
  @@ -66,6 +66,7 @@
   import org.apache.commons.betwixt.XMLIntrospector;
   import org.apache.commons.betwixt.io.BeanReader;
   
  +import org.apache.commons.jelly.JellyException;
   import org.apache.commons.jelly.MissingAttributeException;
   import org.apache.commons.jelly.TagSupport;
   import org.apache.commons.jelly.XMLOutput;
  @@ -81,7 +82,7 @@
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision$
    */
  -public class ParseTag extends TagSupport implements IntrospectorUser {
  +public class ParseTag extends TagSupport {
   
       /** The Log to which logging calls will be made. */
       private static final Log log = LogFactory.getLog(ParseTag.class);
  @@ -91,6 +92,11 @@
   
       private String uri;
       private String var;
  +    private String rootClass;
  +    private String path;
  +    private XMLIntrospector introspector;
  +    private boolean useContextClassLoader;
  +    private ClassLoader classLoader;
       
       
       public ParseTag() {
  @@ -99,30 +105,70 @@
       // Tag interface
       //-------------------------------------------------------------------------       
            
       public void doTag(final XMLOutput output) throws Exception {
  -        if ( uri == null ) {
  -            throw new MissingAttributeException( "uri" );
  -        }
           if ( var == null ) {
               throw new MissingAttributeException( "var" );
           }
  +        if ( rootClass == null ) {
  +            throw new MissingAttributeException( "rootClass" );
  +        }
  +        
  +        reader.setXMLIntrospector(getIntrospector());
  +        
  +        Class theClass = null;
  +        try {
  +            theClass = getClassLoader().loadClass( rootClass );
  +        }
  +        catch (Exception e) {
  +            throw new JellyException( "Could not load class called: " + rootClass, e );
  +        }
           
  -        // allow nested introspector tags
  -        invokeBody(output);
  +        if ( theClass == null ) {
  +            throw new JellyException( "Could not load class called: " + rootClass );
  +        }        
  +        if ( path != null ) {
  +            reader.registerBeanClass( path, theClass );
  +        }
  +        else {
  +            reader.registerBeanClass( theClass );
  +        }
           
  -        URL url = context.getResource( uri );
  -        Object value = reader.parse( url.toString() );
  +        Object value = null;
  +        if ( uri != null ) {
  +            invokeBody(output);
  +        
  +            URL url = context.getResource( uri );
  +            value = reader.parse( url.toString() );
  +        }
  +        else {
  +
  +            // invoke the body and pass that into the reader
  +            XMLOutput newOutput = new XMLOutput( reader );
  +            
  +            invokeBody(newOutput);
  +            
  +            value = reader.getRoot();
  +        }
           context.setVariable( var, value );
       }
       
       // Properties
       //-------------------------------------------------------------------------       
            
  -    
  +
  +    /**
  +     * @return the introspector to be used, lazily creating one if required.
  +     */
  +    public XMLIntrospector getIntrospector() {
  +        if (introspector == null) {
  +            introspector = new XMLIntrospector();
  +        }
  +        return introspector;
  +    }
       /**
        * Sets the Betwixt XMLIntrospector instance used to define the metadata for how a

        * bean should appear as XML.
        */
       public void setIntrospector(XMLIntrospector introspector) {
  -        reader.setXMLIntrospector(introspector);
  +        this.introspector = introspector;
       }
       
       /**
  @@ -140,8 +186,59 @@
           this.var = var;
       }
       
  +    /**
  +     * Sets the name of the root class to use for parsing the XML
  +     */
  +    public void setRootClass(String rootClass) {
  +        this.rootClass = rootClass;
  +    }
  +    
  +    /**
  +     * Sets the path that the root class should be bound to. 
  +     * This is optional and often unnecessary though can be used to ignore some wrapping
  +     * elements, such as the &lt;rss&gt; element in the RSS unit test.
  +     */
  +    public void setPath(String path) {
  +        this.path = path;
  +    }
  +    
  +
  +    /**
  +     * Sets whether or not the current threads's context class loader
  +     * should be used to load the bean classes or not.
  +     * This can be useful if running inside a web application or inside some
  +     * application server.
  +     */
  +    public void setUseContextClassLoader(boolean useContextClassLoader) {
  +        this.useContextClassLoader = useContextClassLoader;
  +    }    
  +
  +    /**
  +     * Sets the ClassLoader to be used to load bean classes from.
  +     * If this is not specified then either the ClassLoader used to load this tag library
  +     * is used or, if the 'useContextClassLoader' property is true, then the 
  +     * current threads context class loader is used instead.
  +     */    
  +    public void setClassLoader(ClassLoader classLoader) {
  +        this.classLoader = classLoader;
  +    }
       
       
       // Implementation methods
       //-------------------------------------------------------------------------       
            
  +    
  +    /**
  +     * @return the ClassLoader to be used to load bean classes.
  +     */
  +    protected ClassLoader getClassLoader() {
  +        if ( classLoader != null ) {
  +            return classLoader;
  +        }
  +        if ( useContextClassLoader ) {
  +            return Thread.currentThread().getContextClassLoader();
  +        }
  +        else {
  +            return getClass().getClassLoader();
  +        }
  +    }
   }
  
  
  
  1.2       +6 -16     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/betwixt/IntrospectorTag.java
  
  Index: IntrospectorTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/betwixt/IntrospectorTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- IntrospectorTag.java	30 Jul 2002 17:57:19 -0000	1.1
  +++ IntrospectorTag.java	31 Jul 2002 11:19:20 -0000	1.2
  @@ -71,8 +71,8 @@
   import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
   import org.apache.commons.betwixt.strategy.NameMapper;
           
  -
   import org.apache.commons.jelly.JellyException;
  +import org.apache.commons.jelly.MissingAttributeException;
   import org.apache.commons.jelly.TagSupport;
   import org.apache.commons.jelly.XMLOutput;
   
  @@ -80,7 +80,7 @@
   import org.apache.commons.logging.LogFactory;
   
   /** 
  - * Creates a Betwixt XMLIntrospector instance that can be used with other Betwixt tags.</p>
  + * Creates a Betwixt XMLIntrospector instance that can be used by the other Betwixt tags.</p>
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision$
  @@ -127,24 +127,14 @@
       //-------------------------------------------------------------------------       
            
       public void doTag(final XMLOutput output) throws Exception {
   
  +        if ( var == null ) {
  +            throw new MissingAttributeException( "var" );
  +        }
           invokeBody(output);        
           
           XMLIntrospector introspector = getIntrospector();
           
  -        if ( var == null ) {
  -            // the parent tag should be a <parse> or <output> tag
  -            IntrospectorUser tag = (IntrospectorUser) findAncestorWithClass( IntrospectorUser.class
);
  -            if ( tag == null ) {
  -                throw new JellyException( 
  -                    "This tag must be nested inside a <parse> or <output> tag,"
  -                    + " or the 'var' attribute should be specified" 
  -                );
  -            }
  -            tag.setIntrospector( introspector );
  -        }
  -        else {
  -            context.setVariable( var, introspector );
  -        }
  +        context.setVariable( var, introspector );
           
           // now lets clear this introspector so that its recreated again next time
           this.introspector = null;
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/betwixt/suite.jelly
  
  Index: suite.jelly
  ===================================================================
  <?xml version="1.0"?>
  <test:suite xmlns:j="jelly:core" xmlns:test="jelly:junit" xmlns:b="jelly:betwixt">
  
  <b:introspector var="introspector" 
    attributesForPrimitives="false" elementNameMapper="lowercase"/>
  	
  <test:case name="rssParseViaURI">
  
    <b:parse var="channel" 
  	uri="rss-example.xml" 
  	introspector="${introspector}"
  	rootClass="org.apache.commons.digester.rss.Channel"
  	path="rss/channel"/>
    
    <j:include uri="common.jelly"/>
  </test:case>
     
  <test:case name="rssParseViaTagBody">
  
    <b:parse var="channel" 
  	introspector="${introspector}"
  	rootClass="org.apache.commons.digester.rss.Channel"
  	path="rss/channel">
    	
  	<rss version="0.91">
  	
  	  <channel>
  	
  	    <title>MozillaZine</title>
  	    <link>http://www.mozillazine.org</link>
  	    <description>Your source for Mozilla news, advocacy,
  	      interviews, builds, and more!</description>
  	    <language>en-US</language>
  	    <rating>(PICS-1.1 "http://www.rsac.org/ratingsv01.html"
  	      2 gen true comment "RSACi North America Server"
  	      for "http://www.rsac.org" on "1996.04.16T08:15-0500"
  	      r (n 0 s 0 v 0 l 0))</rating>
  	
  	    <image>
  	      <title>MozillaZine</title>
  	      <url>http://www.mozillazine.org/image/mynetscape88.gif</url>
  	      <link>http://www.mozillazine.org</link>
  	      <width>88</width>
  	      <height>31</height>
  	      <description>Articles, discussions, builds, and more...</description>
  	    </image>
  	
  	    <item>
  	      <title>Java2 in Navigator 5?</title>
  	      <link>http://www.mozillazine.org/talkback.html?article=607</link>
  	      <description>Will Java2 be an integrated part of Navigator 5?
  	      Read more about it in this discussion...</description>
  	    </item>
  	
  	    <item>
  	      <title>Communicator 4.61 Out</title>
  	      <link>http://www.mozillazine.org/talkback.html?article=606</link>
  	      <description>The latest version of Communicator is now
  	      available.  It includes security enhancements
  	      and various bug fixes.</description>
  	    </item>
  	
  	    <item>
  	      <title>Mozilla Dispenses with Old,
  	      Proprietary DOM</title>
  	      <link>http://www.mozillazine.org/talkback.html?article=604</link>
  	    </item>
  	
  	    <item>
  	      <title>The Animation Contest is Now Closed</title>
  	      <link>http://www.mozillazine.org/talkback.html?article=603</link>
  	    </item>
  	
  	    <textinput>
  	      <title>Send</title>
  	      <description>Comments about MozillaZine?</description>
  	      <name>responseText</name>
  	      <link>http://www.mozillazine.org/cgi-bin/sampleonly.cgi</link>
  	    </textinput>
  	
  	    <skipDays>
  	      <day>Tuesday</day>
  	    </skipDays>
  	  </channel>
  	
  	</rss>
    
    </b:parse>
  	
    <j:include uri="common.jelly"/>
  </test:case>
  
  </test:suite>
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/betwixt/rss-example.xml
  
  Index: rss-example.xml
  ===================================================================
  <?xml version="1.0"?>
  
  <!DOCTYPE rss PUBLIC
   "-//Netscape Communications//DTD RSS 0.91//EN"
   "http://my.netscape.com/publish/formats/rss-0.91.dtd">
  
  <rss version="0.91">
  
    <channel>
  
      <title>MozillaZine</title>
      <link>http://www.mozillazine.org</link>
      <description>Your source for Mozilla news, advocacy,
        interviews, builds, and more!</description>
      <language>en-US</language>
      <rating>(PICS-1.1 "http://www.rsac.org/ratingsv01.html"
        2 gen true comment "RSACi North America Server"
        for "http://www.rsac.org" on "1996.04.16T08:15-0500"
        r (n 0 s 0 v 0 l 0))</rating>
  
      <image>
        <title>MozillaZine</title>
        <url>http://www.mozillazine.org/image/mynetscape88.gif</url>
        <link>http://www.mozillazine.org</link>
        <width>88</width>
        <height>31</height>
        <description>Articles, discussions, builds, and more...</description>
      </image>
  
      <item>
        <title>Java2 in Navigator 5?</title>
        <link>http://www.mozillazine.org/talkback.html?article=607</link>
        <description>Will Java2 be an integrated part of Navigator 5?
        Read more about it in this discussion...</description>
      </item>
  
      <item>
        <title>Communicator 4.61 Out</title>
        <link>http://www.mozillazine.org/talkback.html?article=606</link>
        <description>The latest version of Communicator is now
        available.  It includes security enhancements
        and various bug fixes.</description>
      </item>
  
      <item>
        <title>Mozilla Dispenses with Old,
        Proprietary DOM</title>
        <link>http://www.mozillazine.org/talkback.html?article=604</link>
      </item>
  
      <item>
        <title>The Animation Contest is Now Closed</title>
        <link>http://www.mozillazine.org/talkback.html?article=603</link>
      </item>
  
      <textinput>
        <title>Send</title>
        <description>Comments about MozillaZine?</description>
        <name>responseText</name>
        <link>http://www.mozillazine.org/cgi-bin/sampleonly.cgi</link>
      </textinput>
  
      <skipDays>
        <day>Tuesday</day>
      </skipDays>
    </channel>
  
  </rss>
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/betwixt/common.jelly
  
  Index: common.jelly
  ===================================================================
  <?xml version="1.0"?>
  
  <!-- common tests that the channel has been created properly -->
  
  <test:suite xmlns:j="jelly:core" xmlns:test="jelly:junit" xmlns:b="jelly:betwixt">
  
    Parsed channel with title: ${channel.title}
    	
    <test:assert test="${channel != null}"/>
    	
    <test:assertEquals actual="${channel.title}" expected="MozillaZine"/>
    	
  </test:suite>
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/betwixt/TestJelly.java
  
  Index: TestJelly.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/JellyTestSuite.java,v
1.8 2002/07/06 13:53:39 dion Exp $
   * $Revision: 1.8 $
   * $Date: 2002/07/06 13:53:39 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: JellyTestSuite.java,v 1.8 2002/07/06 13:53:39 dion Exp $
   */
  package org.apache.commons.jelly.betwixt;
  
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.jelly.tags.junit.JellyTestSuite;
  
  /** 
   * A helper class to run jelly test cases as part of Ant's JUnit tests
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.8 $
   */
  public class TestJelly extends JellyTestSuite {
  
      public static void main( String[] args ) throws Exception {
          TestRunner.run( suite() );
      }
      
      public static TestSuite suite() throws Exception {
          return createTestSuite(TestJelly.class, "suite.jelly");        
      }
  }
  
  
  
  1.25      +8 -0      jakarta-commons-sandbox/jelly/maven.xml
  
  Index: maven.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/maven.xml,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- maven.xml	29 Jul 2002 18:14:21 -0000	1.24
  +++ maven.xml	31 Jul 2002 11:19:21 -0000	1.25
  @@ -261,6 +261,14 @@
       </java>
     </goal>
   
  +  <goal name="demo:betwixt" prereqs="jelly-task" 
  +	description="Runs the betwixt test suite">
  +	
  +    <java classname="org.apache.commons.jelly.betwixt.TestJelly" fork="yes">
  +      <classpath refid="test.classpath"/>
  +    </java>
  +  </goal>
  +
     <goal name="demo:junitui" prereqs="jelly-task" 
   	description="Runs the Swing JUnit runner on the Jelly JUnit test suite">
   	
  
  
  
  1.48      +1 -1      jakarta-commons-sandbox/jelly/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/project.xml,v
  retrieving revision 1.47
  retrieving revision 1.48
  diff -u -r1.47 -r1.48
  --- project.xml	30 Jul 2002 17:57:20 -0000	1.47
  +++ project.xml	31 Jul 2002 11:19:21 -0000	1.48
  @@ -132,7 +132,7 @@
   
       <dependency>
         <id>commons-digester</id>
  -      <version>1.2</version>
  +      <version>SNAPSHOT</version>
       </dependency>
   
       <dependency>
  
  
  

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


Mime
View raw message