View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.tags.util;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.StringTokenizer;
22  
23  import org.apache.commons.jelly.JellyTagException;
24  import org.apache.commons.jelly.MissingAttributeException;
25  import org.apache.commons.jelly.TagSupport;
26  import org.apache.commons.jelly.XMLOutput;
27  
28  public class TokenizeTag extends TagSupport
29  {
30      private String var;
31      private String delim;
32  
33      public TokenizeTag()
34      {
35      }
36  
37  
38      // Tag interface
39      //-------------------------------------------------------------------------
40  
41      public void doTag(final XMLOutput output) throws MissingAttributeException, JellyTagException {
42          if ( this.var == null )
43          {
44              throw new MissingAttributeException( "var" );
45          }
46  
47          if ( this.delim == null )
48          {
49              throw new MissingAttributeException( "delim" );
50          }
51  
52          StringTokenizer tokenizer = new StringTokenizer( getBodyText(),
53                                                           this.delim );
54  
55          List tokens = new ArrayList();
56  
57          while ( tokenizer.hasMoreTokens() )
58          {
59              tokens.add( tokenizer.nextToken() );
60          }
61  
62          getContext().setVariable( this.var,
63                                    tokens );
64      }
65  
66      /*** The variable name to hold the list of tokens */
67      public void setVar(String var)
68      {
69          this.var = var;
70      }
71  
72      /*** the delimiter that separates the tokens */
73      public void setDelim(String delim)
74      {
75          this.delim = delim;
76      }
77  
78  }