1 37 package org.webharvest.runtime.variables; 38 39 import java.util.ArrayList ; 40 import java.util.Collection ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 44 47 public class ListVariable implements IVariable { 48 49 private List list; 50 51 public ListVariable() { 52 this.list = new ArrayList (); 53 } 54 55 public ListVariable(List list) { 56 this.list = new ArrayList (); 57 58 if (list != null) { 59 Iterator it = list.iterator(); 60 while (it.hasNext()) { 61 Object 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 toString() { 71 String result = ""; 72 73 Iterator 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 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 toList() { 105 return list; 106 } 107 108 public String toText() { 109 String result = ""; 110 111 Iterator 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 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 getList() { 141 return this.list; 142 } 143 144 148 public boolean contains(Object item) { 149 Iterator 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 getWrappedObject() { 161 return this.list; 162 } 163 164 } | Popular Tags |