KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > string > JoinTag


1 /*
2  * Copyright 1999,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 package org.apache.taglibs.string;
17
18 import javax.servlet.jsp.JspException JavaDoc;
19 import org.apache.commons.lang.StringUtils;
20
21 /**
22  * Joins the elements of the provided array.
23  * A separator may be passed in to put between each element.
24  *
25  * <dl>
26  * <dt>items</dt><dd>
27  * Array of elements to be joined.
28  * </dd>
29  * <dt>separator</dt><dd>
30  * Separator string to be used.
31  * </dd>
32  * </dl>
33  *
34  * @author Felipe Leme (felipeal at apache dot org)
35  */

36 public class JoinTag extends StringTagSupport {
37
38   private String JavaDoc separator;
39   private Object JavaDoc[] items;
40
41   public JoinTag() {
42     super();
43   }
44
45   /**
46    * Define the elements to be joined.
47    *
48    * @param items elements to be joined
49    */

50   public void setItems(Object JavaDoc[] items) {
51     this.items = items;
52   }
53
54
55   /**
56    * Set the separator to be used.
57    *
58    * @param separator separator to be used
59    */

60   public void setSeparator(String JavaDoc separator) {
61     this.separator = separator;
62   }
63
64   public String JavaDoc changeString(String JavaDoc text) throws JspException JavaDoc {
65     return this.separator == null ?
66       StringUtils.join( this.items ) :
67       StringUtils.join( this.items, this.separator );
68   }
69
70   public void initAttributes() {
71     this.separator = null;
72     this.items = null;
73   }
74
75 }
76
Popular Tags