1 18 package org.apache.tools.ant.types.resources; 19 20 import java.io.InputStreamReader ; 21 import java.io.IOException ; 22 import java.io.UnsupportedEncodingException ; 23 import java.util.Stack ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 28 import org.apache.tools.ant.Project; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.types.DataType; 31 import org.apache.tools.ant.types.ResourceCollection; 32 import org.apache.tools.ant.util.ConcatResourceInputStream; 33 import org.apache.tools.ant.util.LineTokenizer; 34 import org.apache.tools.ant.util.Tokenizer; 35 36 41 public class Tokens extends BaseResourceCollectionWrapper { 42 43 private Tokenizer tokenizer; 44 private String encoding; 45 46 50 protected synchronized Collection getCollection() { 51 ResourceCollection rc = getResourceCollection(); 52 if (rc.size() == 0) { 53 return Collections.EMPTY_SET; 54 } 55 if (tokenizer == null) { 56 tokenizer = new LineTokenizer(); 57 } 58 ConcatResourceInputStream cat = new ConcatResourceInputStream(rc); 59 cat.setManagingComponent(this); 60 61 InputStreamReader rdr = null; 62 if (encoding == null) { 63 rdr = new InputStreamReader (cat); 64 } else { 65 try { 66 rdr = new InputStreamReader (cat, encoding); 67 } catch (UnsupportedEncodingException e) { 68 throw new BuildException(e); 69 } 70 } 71 ArrayList result = new ArrayList (); 72 try { 73 for (String s = tokenizer.getToken(rdr); s != null; s = tokenizer.getToken(rdr)) { 74 result.add(new StringResource(s)); 75 } 76 } catch (IOException e) { 77 throw new BuildException("Error reading tokens", e); 78 } 79 return result; 80 } 81 82 86 public synchronized void setEncoding(String encoding) { 87 this.encoding = encoding; 88 } 89 90 95 public synchronized void add(Tokenizer tokenizer) { 96 if (isReference()) { 97 throw noChildrenAllowed(); 98 } 99 if (this.tokenizer != null) { 100 throw new BuildException("Only one nested tokenizer allowed."); 101 } 102 this.tokenizer = tokenizer; 103 } 104 105 112 protected synchronized void dieOnCircularReference(Stack stk, Project p) 113 throws BuildException { 114 if (isChecked()) { 115 return; 116 } 117 if (isReference()) { 118 super.dieOnCircularReference(stk, p); 119 } else { 120 if (tokenizer instanceof DataType) { 121 stk.push(tokenizer); 122 invokeCircularReferenceCheck((DataType) tokenizer, stk, p); 123 } 124 setChecked(true); 125 } 126 } 127 128 } 129 | Popular Tags |