KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > Tokens


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.types.resources;
19
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.util.Stack JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
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 /**
37  * ResourceCollection consisting of StringResources gathered from tokenizing
38  * another ResourceCollection with a Tokenizer implementation.
39  * @since Ant 1.7
40  */

41 public class Tokens extends BaseResourceCollectionWrapper {
42
43     private Tokenizer tokenizer;
44     private String JavaDoc encoding;
45
46     /**
47      * Sort the contained elements.
48      * @return a Collection of Resources.
49      */

50     protected synchronized Collection JavaDoc 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 JavaDoc rdr = null;
62         if (encoding == null) {
63             rdr = new InputStreamReader JavaDoc(cat);
64         } else {
65             try {
66                 rdr = new InputStreamReader JavaDoc(cat, encoding);
67             } catch (UnsupportedEncodingException JavaDoc e) {
68                 throw new BuildException(e);
69             }
70         }
71         ArrayList JavaDoc result = new ArrayList JavaDoc();
72         try {
73             for (String JavaDoc s = tokenizer.getToken(rdr); s != null; s = tokenizer.getToken(rdr)) {
74                 result.add(new StringResource(s));
75             }
76         } catch (IOException JavaDoc e) {
77             throw new BuildException("Error reading tokens", e);
78         }
79         return result;
80     }
81
82     /**
83      * Set the encoding used to create the tokens.
84      * @param encoding the encoding to use.
85      */

86     public synchronized void setEncoding(String JavaDoc encoding) {
87         this.encoding = encoding;
88     }
89
90     /**
91      * Add the nested Tokenizer to this Tokens ResourceCollection.
92      * A LineTokenizer will be used by default.
93      * @param tokenizer the tokenizer to add.
94      */

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     /**
106      * Overrides the BaseResourceCollectionContainer version
107      * to check the nested Tokenizer.
108      * @param stk the stack of data types to use (recursively).
109      * @param p the project to use to dereference the references.
110      * @throws BuildException on error.
111      */

112     protected synchronized void dieOnCircularReference(Stack JavaDoc 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