e.g. Calendar Search Help
You must enter a value before pressing Search
ant

Class: org.apache.tools.ant.types.FilterSet   ©

 OK to copy?
001 /*
002  * Copyright  2001-2004 The Apache Software Foundation
003  *
004  *  Licensed under the Apache License, Version 2.0 (the "License");
005  *  you may not use this file except in compliance with the License.
006  *  You may obtain a copy of the License at
007  *
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  *
010  *  Unless required by applicable law or agreed to in writing, software
011  *  distributed under the License is distributed on an "AS IS" BASIS,
012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  *  See the License for the specific language governing permissions and
014  *  limitations under the License.
015  *
016  */
017 package org.apache.tools.ant.types;
018 
019 // java io classes
020 import java.io.File;
021 import java.io.FileInputStream;
022 import java.io.IOException;
023 import java.util.Enumeration;
024 import java.util.Hashtable;
025 import java.util.Properties;
026 import java.util.Vector;
027 import org.apache.tools.ant.BuildException;
028 import org.apache.tools.ant.Project;
029 
030 
031 /**
032  * A set of filters to be applied to something.
033  *
034  * A filter set may have begintoken and endtokens defined.
035  *
036  */
037 public class FilterSet extends DataType implements Cloneable {
038 
039     /**
040      * Individual filter component of filterset
041      *
042      */
043     public static class Filter {
044         /** Token which will be replaced in the filter operation */
045         String token;
046 
047         /** The value which will replace the token in the filtering operation */
048         String value;
049 
050         /**
051          * Constructor for the Filter object
052          *
053          * @param token  The token which will be replaced when filtering
054          * @param value  The value which will replace the token when filtering
055          */
056         public Filter(String token, String value) {
057            this.token = token;
058            this.value = value;
059         }
060 
061         /**
062          * No argument conmstructor
063          */
064         public Filter() {
065         }
066 
067         /**
068          * Sets the Token attribute of the Filter object
069          *
070          * @param token  The new Token value
071          */
072         public void setToken(String token) {
073            this.token = token;
074         }
075 
076         /**
077          * Sets the Value attribute of the Filter object
078          *
079          * @param value  The new Value value
080          */
081         public void setValue(String value) {
082            this.value = value;
083         }
084 
085         /**
086          * Gets the Token attribute of the Filter object
087          *
088          * @return   The Token value
089          */
090         public String getToken() {
091            return token;
092         }
093 
094         /**
095          * Gets the Value attribute of the Filter object
096          *
097          * @return   The Value value
098          */
099         public String getValue() {
100            return value;
101         }
102      }
103 
104     /**
105      * The filtersfile nested element.
106      *
107      */
108     public class FiltersFile {
109 
110         /**
111          * Constructor for the Filter object
112          */
113         public FiltersFile() {
114         }
115 
116         /**
117          * Sets the file from which filters will be read.
118          *
119          * @param file the file from which filters will be read.
120          */
121         public void setFile(File file) {
122            readFiltersFromFile(file);
123         }
124     }
125 
126     /** The default token start string */
127     public static final String DEFAULT_TOKEN_START = "@";
128 
129     /** The default token end string */
130     public static final String DEFAULT_TOKEN_END = "@";
131 
132     private String startOfToken = DEFAULT_TOKEN_START;
133     private String endOfToken = DEFAULT_TOKEN_END;
134 
135     /**
136      * List of ordered filters and filter files.
137      */
138     private Vector filters = new Vector();
139 
140     /**
141      * Default constructor
142      */
143     public FilterSet() {
144     }
145 
146     /**
147      * Create a Filterset from another filterset
148      *
149      * @param filterset the filterset upon which this filterset will be based.
150      */
151     protected FilterSet(FilterSet filterset) {
152         super();
Rate153         this.filters = (Vector) filterset.getFilters().clone();
154     }
155 
156     /**
157      * Get the filters in the filter set
158      *
159      * @return a Vector of Filter instances
160      */
161     protected Vector getFilters() {
162         if (isReference()) {
163             return getRef().getFilters();
164         }
165         return filters;
166     }
167 
168     /**
169      * Get the referred filter set
170      *
171      * @return the filterset from the reference.
172      */
173     protected FilterSet getRef() {
174         return (FilterSet) getCheckedRef(FilterSet.class, "filterset");
175     }
176 
177     /**
178      * Gets the filter hash of the FilterSet.
179      *
180      * @return   The hash of the tokens and values for quick lookup.
181      */
182     public Hashtable getFilterHash() {
183         int filterSize = getFilters().size();
184         Hashtable filterHash = new Hashtable(filterSize + 1);
185         for (Enumeration e = getFilters().elements(); e.hasMoreElements();) {
186            Filter filter = (Filter) e.nextElement();
187            filterHash.put(filter.getToken(), filter.getValue());
188         }
189         return filterHash;
190     }
191 
192     /**
193      * set the file containing the filters for this filterset.
194      *
195      * @param filtersFile sets the filter fil to read filters for this filter set from.
196      * @exception BuildException if there is a problem reading the filters
197      */
198     public void setFiltersfile(File filtersFile) throws BuildException {
199         if (isReference()) {
200             throw tooManyAttributes();
201         }
202         readFiltersFromFile(filtersFile);
203     }
204 
205     /**
206      * The string used to id the beginning of a token.
207      *
208      * @param startOfToken  The new Begintoken value
209      */
210     public void setBeginToken(String startOfToken) {
211         if (isReference()) {
212             throw tooManyAttributes();
213         }
214         if (startOfToken == null || "".equals(startOfToken)) {
215             throw new BuildException("beginToken must not be empty");
216         }
217         this.startOfToken = startOfToken;
218     }
219 
220     /**
221      * Get the begin token for this filterset
222      *
223      * @return the filter set's begin token for filtering
224      */
225     public String getBeginToken() {
226         if (isReference()) {
227             return getRef().getBeginToken();
228         }
229         return startOfToken;
230     }
231 
232 
233     /**
234      * The string used to id the end of a token.
235      *
236      * @param endOfToken  The new Endtoken value
237      */
238     public void setEndToken(String endOfToken) {
239         if (isReference()) {
240             throw tooManyAttributes();
241         }
242         if (endOfToken == null || "".equals(endOfToken)) {
243             throw new BuildException("endToken must not be empty");
244         }
245         this.endOfToken = endOfToken;
246     }
247 
248     /**
249      * Get the end token for this filterset
250      *
251      * @return the filter set's end token for replacement delimiting
252      */
253     public String getEndToken() {
254         if (isReference()) {
255             return getRef().getEndToken();
256         }
257         return endOfToken;
258     }
259 
260 
261     /**
262      * Read the filters from the given file.
263      *
264      * @param filtersFile         the file from which filters are read
265      * @exception BuildException  Throw a build exception when unable to read the
266      * file.
267      */
268     public void readFiltersFromFile(File filtersFile) throws BuildException {
269         if (isReference()) {
270             throw tooManyAttributes();
271         }
272 
273         if (!filtersFile.exists()) {
274             throw new BuildException("Could not read filters from file "
275                                      + filtersFile + " as it doesn't exist.");
276         }
277 
278         if (filtersFile.isFile()) {
279            log("Reading filters from " + filtersFile, Project.MSG_VERBOSE);
280            FileInputStream in = null;
281            try {
282               Properties props = new Properties();
283               in = new FileInputStream(filtersFile);
284               props.load(in);
285 
286               Enumeration e = props.propertyNames();
287               Vector filters = getFilters();
288               while (e.hasMoreElements()) {
289                  String strPropName = (String) e.nextElement();
290                  String strValue = props.getProperty(strPropName);
291                  filters.addElement(new Filter(strPropName, strValue));
292               }
293            } catch (Exception ex) {
294               throw new BuildException("Could not read filters from file: "
295                 + filtersFile);
296            } finally {
297               if (in != null) {
298                  try {
299                     in.close();
300                  } catch (IOException ioex) {
301                      // ignore
302                  }
303               }
304            }
305         } else {
306            throw new BuildException("Must specify a file not a directory in "
307             + "the filtersfile attribute:" + filtersFile);
308         }
309     }
310 
311     /**
312      * Does replacement on the given string with token matching.
313      * This uses the defined begintoken and endtoken values which default
314      * to @ for both.
315      * This resets the passedTokens and calls iReplaceTokens to
316      * do the actual replacements.
317      *
318      * @param line  The line to process the tokens in.
319      * @return      The string with the tokens replaced.
320      */
321     public String replaceTokens(String line) {
322         passedTokens = null; // reset for new line
323         return iReplaceTokens(line);
324     }
325 
326     /**
327      * Does replacement on the given string with token matching.
328      * This uses the defined begintoken and endtoken values which default
329      * to @ for both.
330      *
331      * @param line  The line to process the tokens in.
332      * @return      The string with the tokens replaced.
333      */
334     private String iReplaceTokens(String line) {
335         String beginToken = getBeginToken();
336         String endToken = getEndToken();
337         int index = line.indexOf(beginToken);
338 
339         if (index > -1) {
340             Hashtable tokens = getFilterHash();
341             try {
342                 StringBuffer b = new StringBuffer();
343                 int i = 0;
344                 String token = null;
345                 String value = null;
346 
347                 do {
348                     int endIndex = line.indexOf(endToken,
349                         index + beginToken.length() + 1);
350                     if (endIndex == -1) {
351                         break;
352                     }
353                     token
354                         = line.substring(index + beginToken.length(), endIndex);
355                     b.append(line.substring(i, index));
356                     if (tokens.containsKey(token)) {
357                         value = (String) tokens.get(token);
358                         if (!value.equals(token)) {
359                             // we have another token, let's parse it.
360                             value = replaceTokens(value, token);
361                         }
362                         log("Replacing: " + beginToken + token + endToken
363                             + " -> " + value, Project.MSG_VERBOSE);
364                         b.append(value);
365                         i = index + beginToken.length() + token.length()
366                             + endToken.length();
367                     } else {
368                         // just append beginToken and search further
369                         b.append(beginToken);
370                         i = index + beginToken.length();
371                     }
372                 } while ((index = line.indexOf(beginToken, i)) > -1);
373 
374                 b.append(line.substring(i));
375                 return b.toString();
376             } catch (StringIndexOutOfBoundsException e) {
377                 return line;
378             }
379         } else {
380            return line;
381         }
382     }
383 
384     /** Contains a list of parsed tokens */
385     private Vector passedTokens;
386     /** if a ducplicate token is found, this is set to true */
387     private boolean duplicateToken = false;
388 
389     /**
390      * This parses tokens which point to tokens.
391      * It also maintains a list of currently used tokens, so we cannot
392      * get into an infinite loop
393      * @param line the value / token to parse
394      * @param parent the parant token (= the token it was parsed from)
395      */
396     private String replaceTokens(String line, String parent)
397         throws BuildException {
398         if (passedTokens == null) {
399             passedTokens = new Vector();
400         }
401         if (passedTokens.contains(parent) && !duplicateToken) {
402             duplicateToken = true;
403             StringBuffer sb = new StringBuffer();
404             sb.append("Infinite loop in tokens. Currently known tokens : ");
405             sb.append(passedTokens);
406             sb.append("\nProblem token : " + getBeginToken() + parent
407                 + getEndToken());
408             sb.append(" called from " + getBeginToken()
409                 + passedTokens.lastElement());
410             sb.append(getEndToken());
411             System.out.println(sb.toString());
412             return parent;
413         }
414         passedTokens.addElement(parent);
415         String value = iReplaceTokens(line);
416         if (value.indexOf(getBeginToken()) == -1 && !duplicateToken) {
417             duplicateToken = false;
418             passedTokens = null;
419         } else if (duplicateToken) {
420             // should always be the case...
421             if (passedTokens.size() > 0) {
422                 value = (String) passedTokens.lastElement();
423                 passedTokens.removeElementAt(passedTokens.size() - 1);
424                 if (passedTokens.size() == 0) {
425                     value = getBeginToken() + value + getEndToken();
426                     duplicateToken = false;
427                 }
428             }
429         }
430         return value;
431     }
432 
433     /**
434      * Create a new filter
435      *
436      * @param filter the filter to be added
437      */
438     public void addFilter(Filter filter) {
439         if (isReference()) {
440             throw noChildrenAllowed();
441         }
442         filters.addElement(filter);
443     }
444 
445     /**
446      * Create a new FiltersFile
447      *
448      * @return   The filter that was created.
449      */
450     public FiltersFile createFiltersfile() {
451         if (isReference()) {
452             throw noChildrenAllowed();
453         }
454         return new FiltersFile();
455     }
456 
457     /**
458     * Add a new filter made from the given token and value.
459     *
460     * @param token  The token for the new filter.
461     * @param value  The value for the new filter.
462     */
463     public void addFilter(String token, String value) {
464         if (isReference()) {
465             throw noChildrenAllowed();
466         }
467         filters.addElement(new Filter(token, value));
468     }
469 
470     /**
471     * Add a Filterset to this filter set
472     *
473     * @param filterSet the filterset to be added to this filterset
474     */
475     public void addConfiguredFilterSet(FilterSet filterSet) {
476         if (isReference()) {
477             throw noChildrenAllowed();
478         }
479         for (Enumeration e = filterSet.getFilters().elements(); e.hasMoreElements();) {
480             filters.addElement(e.nextElement());
481         }
482     }
483 
484     /**
485     * Test to see if this filter set it empty.
486     *
487     * @return   Return true if there are filter in this set otherwise false.
488     */
489     public boolean hasFilters() {
490         return getFilters().size() > 0;
491     }
492 
493     /**
494      * clone the filterset
495      *
496      * @return a deep clone of this filterset
497      *
498      * @throws BuildException if the clone cannot be performed.
499      */
500     public Object clone() throws BuildException {
501         if (isReference()) {
502             return ((FilterSet) getRef()).clone();
503         } else {
504             try {
505                 FilterSet fs = (FilterSet) super.clone();
Rate506                 fs.filters = (Vector) getFilters().clone();
507                 fs.setProject(getProject());
508                 return fs;
509             } catch (CloneNotSupportedException e) {
510                 throw new BuildException(e);
511             }
512         }
513     }
514 
515 }
516 
517 


            
All Examples in File:
Example
Line
Rating (found
useful by...)
153 0% of 0
506 0% of 0