commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From morg...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/test/xml TestDefaultNamespaceFilter.java nsFilterTest.jelly
Date Wed, 09 Oct 2002 21:03:08 GMT
morgand     2002/10/09 14:03:08

  Modified:    jelly/src/java/org/apache/commons/jelly Jelly.java
               jelly/src/java/org/apache/commons/jelly/parser
                        XMLParser.java
  Added:       jelly/src/java/org/apache/commons/jelly/parser
                        DefaultNamespaceFilter.java
               jelly/src/test/org/apache/commons/jelly/test/xml
                        TestDefaultNamespaceFilter.java nsFilterTest.jelly
  Log:
  added classes and unit tests for "default namespace" feature, in order
  to facilitate seamless conversion of existing XML files to a Jelly driver
  
  Revision  Changes    Path
  1.18      +20 -5     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.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- Jelly.java	4 Oct 2002 11:28:20 -0000	1.17
  +++ Jelly.java	9 Oct 2002 21:03:08 -0000	1.18
  @@ -105,6 +105,8 @@
       
       /** Whether we have loaded the properties yet */
       private boolean loadedProperties = false;
  +
  +    private String defaultNamespaceURI = null;
           
       public Jelly() {
       }
  @@ -245,6 +247,7 @@
           
           XMLParser parser = new XMLParser();
           parser.setContext(getJellyContext());
  +        parser.setDefaultNamespaceURI(defaultNamespaceURI);
           Script script = parser.parse(getUrl().openStream());
           script = script.compile();
           if (log.isDebugEnabled()) {
  @@ -304,6 +307,18 @@
               context = new JellyContext(getRootContext(), new URL(text));
           }
           return context;
  +    }
  +
  +
  +    /**
  +     * Set the jelly namespace to use for unprefixed elements.
  +     * Will be overridden by an explicit namespace in the
  +     * XML document.
  +     * 
  +     * @param namespace jelly namespace to use (e.g. 'jelly:core')
  +     */
  +    public void setDefaultNamespaceURI(String namespace) {
  +        defaultNamespaceURI = namespace;
       }
       
       // Implementation methods
  
  
  
  1.31      +23 -7     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java
  
  Index: XMLParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- XMLParser.java	3 Oct 2002 18:14:43 -0000	1.30
  +++ XMLParser.java	9 Oct 2002 21:03:08 -0000	1.31
  @@ -213,12 +213,12 @@
       /** Flag to indicate if this object has been configured */
       private boolean configured;
   
  +    private String defaultNamespaceURI = null;
  +
       /**
        * The Log to which logging calls will be made.
        */
       private Log log = LogFactory.getLog(XMLParser.class);
  -
  -
       
       /**
        * Construct a new XMLParser with default properties.
  @@ -359,6 +359,17 @@
       }
   
       /**
  +     * Set the jelly namespace to use for unprefixed elements.
  +     * Will be overridden by an explicit namespace in the
  +     * XML document.
  +     * 
  +     * @param namespace jelly namespace to use (e.g. 'jelly:core')
  +     */
  +    public void setDefaultNamespaceURI(String namespace) {
  +        defaultNamespaceURI = namespace;
  +    }
  +
  +    /**
        * Return the class loader to be used for instantiating application objects
        * when required.  This is determined based upon the following rules:
        * <ul>
  @@ -507,12 +518,16 @@
       public synchronized XMLReader getXMLReader() throws SAXException {
           if (reader == null) {
               reader = getParser().getXMLReader();
  +            if (defaultNamespaceURI != null) {
  +                reader = new DefaultNamespaceFilter(defaultNamespaceURI,reader);
  +            }
           }
           //set up the parse
           reader.setContentHandler(this);
           reader.setDTDHandler(this);
           //reader.setEntityResolver(this);
           reader.setErrorHandler(this);
  +
           return reader;
       }
   
  @@ -1165,4 +1180,5 @@
       protected SAXException createSAXException(String message) {
           return createSAXException(message, null);
       }
  +
   }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/DefaultNamespaceFilter.java
  
  Index: DefaultNamespaceFilter.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/DefaultNamespaceFilter.java,v
1.1 2002/10/09 21:03:08 morgand Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/09 21:03:08 $
   *
   * ====================================================================
   *
   * 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: DefaultNamespaceFilter.java,v 1.1 2002/10/09 21:03:08 morgand Exp $
   */
  package org.apache.commons.jelly.parser;
  
  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
  import org.xml.sax.XMLReader;
  import org.xml.sax.helpers.XMLFilterImpl;
  
  /**
   * XMLFilter that can provide a default namespace when
   * one has not been declared by the XML document.  Note:
   * this class does not address the namespace of attributes.
   * 
   * @author Morgan Delagrange
   */
  public class DefaultNamespaceFilter extends XMLFilterImpl {
  
      protected String uriDefault = null;
  
      /**
       * Filter for undefined an undefined namespace
       * 
       * @param defaultNamespace
       *               uri for the jelly namespace
       * @param reader XMLReader to filter
       */
      public DefaultNamespaceFilter(String defaultNamespace, XMLReader reader) {
          super(reader);
          uriDefault = defaultNamespace;
      }
  
      /**
       * All incoming empty URIs will be remapped to the default.
       * 
       * @param prefix incoming prefix
       * @param uri    URI to check and potentially replace
       * @exception SAXException
       */
      public void startPrefixMapping(java.lang.String prefix,
                                     java.lang.String uri)
      throws SAXException {
  
          if (uri.equals("")) {
              super.startPrefixMapping(prefix,uriDefault);
          } else {
              super.startPrefixMapping(prefix,uri);
          }
      }
  
      /**
       * All incoming empty URIs will be remapped to the default.
       * 
       * @param uri       URI to check and potentially replace
       * @param localName
       * @param qName
       * @param atts
       * @exception SAXException
       */
      public void startElement(java.lang.String uri,
                               java.lang.String localName,
                               java.lang.String qName,
                               Attributes atts)
      throws SAXException {
  
          if (uri.equals("")) {
              super.startElement(uriDefault,localName,qName,atts);
          } else {
              super.startElement(uri,localName,qName,atts);
          }
  
      }
  
      /**
       * All incoming empty URIs will be remapped to the default.
       * 
       * @param namespaceURI
       *                  URI to check and potentially replace
       * @param localName
       * @param qName
       * @exception SAXException
       */
      public void endElement(String namespaceURI, String localName, String qName)
      throws SAXException {
          if (namespaceURI.equals("")) {
              super.endElement(uriDefault,localName,qName);
          } else {
              super.endElement(namespaceURI,localName,qName);
          }
      }
  }
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/test/xml/TestDefaultNamespaceFilter.java
  
  Index: TestDefaultNamespaceFilter.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/test/xml/TestDefaultNamespaceFilter.java,v
1.1 2002/10/09 21:03:08 morgand Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/09 21:03:08 $
   *
   * ====================================================================
   *
   * 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: TestDefaultNamespaceFilter.java,v 1.1 2002/10/09 21:03:08 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 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.1 $
   */
  public class TestDefaultNamespaceFilter extends TestCase {
  
      Jelly jelly = null;
      JellyContext context = null;
      XMLOutput xmlOutput = null;
  
      public TestDefaultNamespaceFilter(String name) {
          super(name);
      }
  
      public static TestSuite suite() throws Exception {
          return new TestSuite(TestDefaultNamespaceFilter.class);        
      }
  
      public void setUp() throws Exception {
          context = new JellyContext();
          xmlOutput = XMLOutput.createXMLOutput(new StringWriter());
  
          jelly = new Jelly();
          
          String script = "nsFilterTest.jelly";
          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 testNamespaceDefined() throws Exception {
          jelly.setDefaultNamespaceURI("jelly:core");
          Script script = jelly.compileScript();
          script.run(context,xmlOutput);
          assertTrue("should have set 'usedDefaultNamespace' variable",
                     context.getVariable("usedDefaultNamespace") != null);
      }
  
      public void testNamespaceNotDefined() throws Exception {
          Script script = jelly.compileScript();
          script.run(context,xmlOutput);
          assertTrue("should not have set 'usedDefaultNamespace' variable",
                     context.getVariable("usedDefaultNamespace") == null);
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/test/xml/nsFilterTest.jelly
  
  Index: nsFilterTest.jelly
  ===================================================================
  <?xml version="1.0"?>
  <j:jelly 
  	xmlns:j="jelly:core">
    
    <set var="foo" value="bar"/>
    
    <j:if test="${foo != null}">
        <j:set var="usedDefaultNamespace">true</j:set>
    </j:if>            
  
  </j:jelly>
  
  
  

--
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