morgand     2002/10/14 12:17:02

  Modified:    jelly/src/java/org/apache/commons/jelly Jelly.java
  Added:       jelly/src/test/org/apache/commons/jelly/test/xml
                        TestXMLValidation.java invalidScript1.jelly
                        validScript1.jelly
  Log:
  added XML validation configuration to the Jelly class (plus unit tests)
  
  Revision  Changes    Path
  1.19      +25 -6     
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Jelly.java
  
  Index: Jelly.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/Jelly.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- Jelly.java        9 Oct 2002 21:03:08 -0000       1.18
  +++ Jelly.java        14 Oct 2002 19:17:01 -0000      1.19
  @@ -106,7 +106,15 @@
       /** Whether we have loaded the properties yet */
       private boolean loadedProperties = false;
   
  +    /**
  +     * whether to override the default namespace
  +     */
       private String defaultNamespaceURI = null;
  +
  +    /**
  +     * whether or not to validate the Jelly script
  +     */
  +    private boolean validateXML = false;
           
       public Jelly() {
       }
  @@ -248,6 +256,7 @@
           XMLParser parser = new XMLParser();
           parser.setContext(getJellyContext());
           parser.setDefaultNamespaceURI(defaultNamespaceURI);
  +        parser.setValidating(validateXML);
           Script script = parser.parse(getUrl().openStream());
           script = script.compile();
           if (log.isDebugEnabled()) {
  @@ -318,7 +327,17 @@
        * @param namespace jelly namespace to use (e.g. 'jelly:core')
        */
       public void setDefaultNamespaceURI(String namespace) {
  -        defaultNamespaceURI = namespace;
  +        this.defaultNamespaceURI = namespace;
  +    }
  +
  +    /**
  +     * When set to true, the XML parser will attempt to validate
  +     * the Jelly XML before converting it into a Script.
  +     * 
  +     * @param validate whether or not to validate
  +     */
  +    public void setValidateXML(boolean validate) {
  +        this.validateXML = validate;
       }
       
       // Implementation methods
  
  
  
  1.1                  
jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/test/xml/TestXMLValidation.java
  
  Index: TestXMLValidation.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/test/xml/TestXMLValidation.java,v
 1.1 2002/10/14 19:17:01 morgand Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/14 19:17:01 $
   *
   * ====================================================================
   *
   * 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 [EMAIL PROTECTED]
   *
   * 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: TestXMLValidation.java,v 1.1 2002/10/14 19:17:01 morgand Exp $
   */
  package org.apache.commons.jelly.test.xml;
  
  import java.io.StringWriter;
  import java.net.URL;
  
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.jelly.Jelly;
  import org.apache.commons.jelly.JellyContext;
  import org.apache.commons.jelly.Script;
  import org.apache.commons.jelly.XMLOutput;
  
  /**
   * A test to confirm that invalid documents are
   * reject iff jelly.setValidateXML(true)
   * 
   * @author Morgan Delagrange
   * @version $Revision: 1.1 $
   */
  public class TestXMLValidation extends TestCase {
  
      Jelly jelly = null;
      JellyContext context = null;
      XMLOutput xmlOutput = null;
  
      public TestXMLValidation(String name) {
          super(name);
      }
  
      public static TestSuite suite() throws Exception {
          return new TestSuite(TestXMLValidation.class);        
      }
  
      public void setUp(String scriptName) throws Exception {
          context = new JellyContext();
          xmlOutput = XMLOutput.createXMLOutput(new StringWriter());
  
          jelly = new Jelly();
          
          String script = scriptName;
          URL url = this.getClass().getResource(script);
          if ( url == null ) {
              throw new Exception( 
                  "Could not find Jelly script: " + script 
                  + " in package of class: " + this.getClass().getName() 
              );
          }
          jelly.setUrl(url);
      }
  
      public void testInvalidXML1NoValidation() throws Exception {
          // without validation
          setUp("invalidScript1.jelly");
          Script script = jelly.compileScript();
          script.run(context,xmlOutput);
          assertTrue("should have set 'foo' variable to 'bar'",
                     context.getVariable("foo").equals("bar"));
  
          // do it again, explicitly setting the validateXML variable
          setUp("invalidScript1.jelly");
          jelly.setValidateXML(false);
          script = jelly.compileScript();
          script.run(context,xmlOutput);
          assertTrue("should have set 'foo' variable to 'bar'",
                     context.getVariable("foo").equals("bar"));
      }
  
      public void testInvalidXML1Validation() throws Exception {
          // with validation
          setUp("invalidScript1.jelly");
          jelly.setValidateXML(true);
          Script script = jelly.compileScript();
          script.run(context,xmlOutput);
          assertTrue("should have set 'foo' variable to 'bar'",
                     context.getVariable("foo").equals("bar"));
      }
  
      public void testValidXML1Validation()throws Exception {
          // with validation
          setUp("validScript1.jelly");
          jelly.setValidateXML(true);
          Script script = jelly.compileScript();
          script.run(context,xmlOutput);
          assertTrue("should have set 'foo' variable to 'bar'",
                     context.getVariable("foo").equals("bar"));
      }
  
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/test/xml/invalidScript1.jelly
  
  Index: invalidScript1.jelly
  ===================================================================
  <?xml version="1.0"?>
  <!DOCTYPE j:jelly [
  
    <!-- document is not valid -->
    <!ELEMENT j:jelly (j:set)>
    <!ATTLIST j:jelly xmlns:j CDATA 'jelly:core'>
    
    <!ELEMENT j:set EMPTY>
    <!ATTLIST j:set varicose   CDATA #REQUIRED
                    value CDATA #REQUIRED>
  
  ]>
  <j:jelly 
        xmlns:j="jelly:core">
    
    <j:set var="foo" value="bar"/>
    
  </j:jelly>
  
  
  
  1.1                  
jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/test/xml/validScript1.jelly
  
  Index: validScript1.jelly
  ===================================================================
  <?xml version="1.0"?>
  <!DOCTYPE j:jelly [
  
    <!-- document is valid -->
    <!ELEMENT j:jelly (j:set)>
    <!ATTLIST j:jelly xmlns:j CDATA 'jelly:core'>
    
    <!ELEMENT j:set EMPTY>
    <!ATTLIST j:set var   CDATA #REQUIRED
                    value CDATA #REQUIRED>
  
  ]>
  <j:jelly 
        xmlns:j="jelly:core">
    
    <j:set var="foo" value="bar"/>
    
  </j:jelly>
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to