KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webharvest > runtime > variables > ListVariable


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16
17     * The name of Web-Harvest may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
35     subject line.
36 */

37 package org.webharvest.runtime.variables;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43
44 /**
45  * List variable - String wrapper.
46  */

47 public class ListVariable implements IVariable {
48
49     private List JavaDoc list;
50
51     public ListVariable() {
52         this.list = new ArrayList JavaDoc();
53     }
54
55     public ListVariable(List JavaDoc list) {
56         this.list = new ArrayList JavaDoc();
57         
58         if (list != null) {
59             Iterator JavaDoc it = list.iterator();
60             while (it.hasNext()) {
61                 Object JavaDoc object = it.next();
62                 IVariable var = object instanceof IVariable ? (IVariable) object : new NodeVariable(object);
63                 if ( !var.isEmpty() ) {
64                     this.list.add(var);
65                 }
66             }
67         }
68     }
69
70     public String JavaDoc toString() {
71         String JavaDoc result = "";
72
73         Iterator JavaDoc it = list.iterator();
74         while (it.hasNext()) {
75             IVariable var = (IVariable) it.next();
76             result += var.toString();
77         }
78
79         return result;
80     }
81
82     public byte[] toBinary() {
83         byte[] result = null;
84         
85         Iterator JavaDoc it = list.iterator();
86         while (it.hasNext()) {
87             IVariable currVar = (IVariable) it.next();
88             byte[] curr = currVar.toBinary();
89             if (curr != null) {
90                 if (result == null) {
91                     result = curr;
92                 } else {
93                     byte[] newResult = new byte[result.length + curr.length];
94                     System.arraycopy(result, 0, newResult, 0, result.length);
95                     System.arraycopy(curr, 0, newResult, result.length, curr.length);
96                     result = newResult;
97                 }
98             }
99         }
100
101         return result;
102     }
103
104     public List JavaDoc toList() {
105         return list;
106     }
107
108     public String JavaDoc toText() {
109         String JavaDoc result = "";
110
111         Iterator JavaDoc it = list.iterator();
112         while (it.hasNext()) {
113             IVariable var = (IVariable) it.next();
114             result += var.toText();
115         }
116
117         return result;
118     }
119
120     public boolean isEmpty() {
121         Iterator JavaDoc it = list.iterator();
122         while (it.hasNext()) {
123             IVariable var = (IVariable) it.next();
124             if (!var.isEmpty()) {
125                 return false;
126             }
127         }
128
129         return true;
130     }
131
132     public void addVariable(IVariable variable) {
133         if (variable instanceof ListVariable) {
134             list.addAll( ((ListVariable)variable).getList() );
135         } else {
136             list.add(variable);
137         }
138     }
139
140     private Collection JavaDoc getList() {
141         return this.list;
142     }
143     
144     /**
145      * Checks if list contains specified object's string representation
146      * @param item
147      */

148     public boolean contains(Object JavaDoc item) {
149         Iterator JavaDoc it = list.iterator();
150         while (it.hasNext()) {
151             IVariable currVariable = (IVariable) it.next();
152             if ( currVariable != null && currVariable.toString().equals(item.toString()) ) {
153                 return true;
154             }
155         }
156         
157         return false;
158     }
159     
160     public Object JavaDoc getWrappedObject() {
161         return this.list;
162     }
163
164 }
Popular Tags