You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by di...@apache.org on 2004/09/12 18:39:22 UTC

cvs commit: jakarta-commons/jelly/jelly-tags/regexp/src/test/org/apache/commons/jelly/tags/regexp MatchTagTest.java ContainsTagTest.java

dion        2004/09/12 09:39:22

  Added:       jelly/jelly-tags/regexp/xdocs navigation.xml changes.xml
                        index.xml
               jelly/jelly-tags/regexp/src/java/org/apache/commons/jelly/tags/regexp
                        RegexpTag.java ContainsTag.java MatchTag.java
                        RegexpTagLibrary.java
               jelly/jelly-tags/regexp .cvsignore project.xml
                        project.properties
               jelly/jelly-tags/regexp/src/test/org/apache/commons/jelly/tags/regexp
                        MatchTagTest.java ContainsTagTest.java
  Log:
  Add regexp tag library. Jelly-49
  
  Revision  Changes    Path
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/xdocs/navigation.xml
  
  Index: navigation.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  <!--
    Copyright 2002-2004 The Apache Software Foundation
  
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
  
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  -->
  <project name="SOAP Tag Library">
  
    <title>SOAP Tag Library</title>
    
    <body>
      <menu name="SOAP Tag Library">
        <item name="Overview"                href="/index.html"/>
        <item name="Tags"                    href="/tags.html"/>
      </menu>
    </body>
  </project>
  
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  
  <!-- 
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
   -->
  
  <document>
    <properties>
      <title>Changes</title>
      <author email="dion@apache.org">dIon Gillard</author>
    </properties>
    <body>
      <release version="1.0-SNAPSHOT" date="in CVS">
      </release>
    </body>
  </document>
  
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/xdocs/index.xml
  
  Index: index.xml
  ===================================================================
  <?xml version="1.0"?>
  <!--
    Copyright 2002-2004 The Apache Software Foundation
  
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
  
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  -->
  
  <document>
    <properties>
      <title>Regexp Tag Library</title>
      <author email="dion@apache.org">dIon Gillard</author>
    </properties>
  
    <body>
      <section name="Overview">
        <p>
          Tags for working with regular expressions. See the <a href="tags.html">tags</a> for more information.
        </p>
      </section>
    </body>
  </document>
  
  
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/src/java/org/apache/commons/jelly/tags/regexp/RegexpTag.java
  
  Index: RegexpTag.java
  ===================================================================
  /*
   * Copyright 2002,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.jelly.tags.regexp;
  
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  import org.apache.commons.jelly.MissingAttributeException;
  import org.apache.commons.jelly.JellyTagException;
  import org.apache.oro.text.regex.Perl5Matcher;
  import org.apache.oro.text.regex.Perl5Compiler;
  import org.apache.oro.text.regex.Pattern;
  import org.apache.oro.text.regex.MalformedPatternException;
  
  /**
   * Base class for tags using the Oro Regexp library.
   *
   * @author <a href="mailto:christian@inx-soft.com">Christian Amor Kvalheim</a>
   * @version $Revision: 1.1 $
   */
  public abstract class RegexpTag extends TagSupport {
      private Perl5Matcher patternMatcher = new Perl5Matcher();
      private Pattern pattern;
      private String var;
      private String text;
      private String scope;
  
      protected final String getText() {
          return text;
      }
  
      protected final Pattern getPattern() {
          return pattern;
      }
  
      protected final Perl5Matcher getPatternMatcher() {
          return patternMatcher;
      }
  
      public final void setExpr(String expr) throws MalformedPatternException {
          Perl5Compiler patternCompiler = new Perl5Compiler();
          pattern = patternCompiler.compile(expr);
      }
  
      public final void setText(String text) {
          this.text = text;
      }
  
      // Sets the variable name to define for this expression
      public final void setVar(String var) {
          this.var = var;
      }
  
      /**
       * Sets the variable scope for this variable. For example setting this value to 'parent' will
       * set this value in the parent scope. When Jelly is run from inside a Servlet environment
       * then other scopes will be available such as 'request', 'session' or 'application'.
       *
       * Other applications may implement their own custom scopes.
       */
      public final void setScope(String scope) {
          this.scope = scope;
      }
  
      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
          // Check required properties
          if (getText() == null || getText().length() == 0)
              throw new MissingAttributeException("text must be provided");
  
          if (pattern == null)
              throw new MissingAttributeException("expr must be provided");
  
          if (var == null || var.length() == 0)
              throw new MissingAttributeException("var must be provided");
  
          // Evaluate pattern against text string
          boolean result = getResult();
          String resultString = result ? "true" : "false";
  
          if (var != null) {
              if (scope != null) {
                  context.setVariable(var, scope, resultString);
              } else {
                  context.setVariable(var, resultString);
              }
          }
      }
  
      protected abstract boolean getResult();
  }
  
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/src/java/org/apache/commons/jelly/tags/regexp/ContainsTag.java
  
  Index: ContainsTag.java
  ===================================================================
  /*
   * Copyright 2002,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.jelly.tags.regexp;
  
  /**
   * This tag checks whether a regexp contains a string.
   *
   * @author <a href="mailto:christian@inx-soft.com">Christian Amor Kvalheim</a>
   * @version $Revision: 1.1 $
   */
  public class ContainsTag extends RegexpTag {
      protected boolean getResult() {
          return getPatternMatcher().contains(getText(), getPattern());
      }
  }
  
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/src/java/org/apache/commons/jelly/tags/regexp/MatchTag.java
  
  Index: MatchTag.java
  ===================================================================
  /*
   * Copyright 2002,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.jelly.tags.regexp;
  
  /**
   * This tag checks whether a regexp matches a string.
   *
   * @author <a href="mailto:christian@inx-soft.com">Christian Amor Kvalheim</a>
   * @version $Revision: 1.1 $
   */
  public class MatchTag extends RegexpTag {
      protected boolean getResult() {
          return getPatternMatcher().matches(getText(), getPattern());
      }
  }
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/src/java/org/apache/commons/jelly/tags/regexp/RegexpTagLibrary.java
  
  Index: RegexpTagLibrary.java
  ===================================================================
  /*
   * Copyright 2002,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.jelly.tags.regexp;
  
  import org.apache.commons.jelly.TagLibrary;
  /** Implements regexp tags.
   *
   *  @author <a href="mailto:christian@inx-soft.com">Christian Amor Kvalheim</a>
   *  @version $Revision: 1.1 $
   */
  public class RegexpTagLibrary extends TagLibrary {
      
      public RegexpTagLibrary()
      {
          registerTag("match", MatchTag.class);
          registerTag("contains", ContainsTag.class);
      }
  }
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/.cvsignore
  
  Index: .cvsignore
  ===================================================================
  maven.log
  target
  velocity.log
  jcoverage.ser
  
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/project.xml
  
  Index: project.xml
  ===================================================================
  <?xml version="1.0" ?>
  <!--
    Copyright 2002-2004 The Apache Software Foundation
  
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
  
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  -->
  <project>
    <extend>${basedir}/../tag-project.xml</extend>
    <id>commons-jelly-tags-regexp</id>
    <name>commons-jelly-tags-regexp</name>
    <currentVersion>1.0-SNAPSHOT</currentVersion>
    <package>org.apache.commons.jelly.tags.regexp</package>
  
    <description>
        This is a Jelly interface for ORO.
    </description>
    <shortDescription>Commons Jelly Regular Expression Tag Library</shortDescription>
    
    <!--versions>
      <version>
        <id>1.0</id>
        <name>1.0</name>
        <tag>COMMONS-JELLY-REGEXP-1_0</tag>
      </version>
    </versions-->
  
    <dependencies>
  
      <dependency>
        <id>commons-jelly</id>
        <version>1.0-beta-4</version>
      </dependency>
      
      <dependency>
        <groupId>oro</groupId>
        <artifactId>oro</artifactId>
        <version>2.0.8</version>
      </dependency>
  
    </dependencies>
    
  </project>
  
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/project.properties
  
  Index: project.properties
  ===================================================================
  # Copyright 2002-2004 The Apache Software Foundation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  # -------------------------------------------------------------------
  # P R O J E C T  P R O P E R T I E S
  # -------------------------------------------------------------------
  
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/src/test/org/apache/commons/jelly/tags/regexp/MatchTagTest.java
  
  Index: MatchTagTest.java
  ===================================================================
  /*
   * Copyright 2002,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.jelly.tags.regexp;
  
  import org.apache.commons.jelly.XMLOutput;
  import org.apache.commons.jelly.JellyContext;
  import junit.framework.TestCase;
  import org.apache.commons.jelly.tags.regexp.MatchTag;
  
  /*** <p><code>MatchTagTest</code> a class that is useful to perform regexp matches
  * in strings.</p>
  *
  * @author <a href="mailto:christian@inx-soft.com">Christian Amor Kvalheim</a>
  * @version $Revision: 1.1 $
  */
  public class MatchTagTest extends TestCase {
  
    public MatchTagTest(String name)
    {
      super(name);
    }
  
    public void setUp() throws Exception
    {
    }
  
    public void testDoTag() throws Exception
    {
      MatchTag matchExpTag = new MatchTag();
      XMLOutput xmlOutput = new XMLOutput();
  
      matchExpTag.setText("ID1234");
      matchExpTag.setExpr("[A-Z][A-Z][0-9]{4}");
      matchExpTag.setVar("testvar");
      matchExpTag.setContext(new JellyContext());
      matchExpTag.doTag(xmlOutput);
  
      assertEquals("TRUE", matchExpTag.getContext().getVariable("testvar").toString().toUpperCase());
    }
  
    public void tearDown()
    {
    }
  }
  
  
  1.1                  jakarta-commons/jelly/jelly-tags/regexp/src/test/org/apache/commons/jelly/tags/regexp/ContainsTagTest.java
  
  Index: ContainsTagTest.java
  ===================================================================
  /*
   * Copyright 2002,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.jelly.tags.regexp;
  
  import org.apache.commons.jelly.XMLOutput;
  import org.apache.commons.jelly.JellyContext;
  import junit.framework.TestCase;
  import org.apache.commons.jelly.tags.regexp.ContainsTag;
  
  /*** <p><code>ContainsTagTest</code> a class that is useful to perform regexp matches
  * in strings.</p>
  *
  * @author <a href="mailto:christian@inx-soft.com">Christian Amor Kvalheim</a>
  * @version $Revision: 1.1 $
  */
  public class ContainsTagTest extends TestCase {
  
      public ContainsTagTest(String name)
      {
        super(name);
      }
  
      public void setUp() throws Exception
      {
      }
  
      public void testDoTag() throws Exception
      {
        ContainsTag containsExpTag = new ContainsTag();
        XMLOutput xmlOutput = new XMLOutput();
  
        containsExpTag.setText("Hello World");
        containsExpTag.setExpr("World");
        containsExpTag.setVar("testvar");
        containsExpTag.setContext(new JellyContext());
        containsExpTag.doTag(xmlOutput);
  
        assertEquals("TRUE", containsExpTag.getContext().getVariable("testvar").toString().toUpperCase());
      }
  
      public void tearDown()
      {
      }
  
  }
  
  
  

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