KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > mappers > FilterMapper


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
19 package org.apache.tools.ant.types.mappers;
20
21 import java.io.StringReader JavaDoc;
22 import java.io.Reader JavaDoc;
23
24 import java.util.Vector JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.UnsupportedAttributeException;
28 import org.apache.tools.ant.filters.util.ChainReaderHelper;
29 import org.apache.tools.ant.types.FilterChain;
30 import org.apache.tools.ant.util.FileNameMapper;
31 import org.apache.tools.ant.util.FileUtils;
32
33 /**
34  * This is a FileNameMapper based on a FilterChain.
35  */

36 public class FilterMapper extends FilterChain implements FileNameMapper {
37     /**
38      * From attribute not supported.
39      * @param from a string
40      * @throws BuildException always
41      */

42     public void setFrom(String JavaDoc from) {
43         throw new UnsupportedAttributeException(
44             "filtermapper doesn't support the \"from\" attribute.", "from");
45     }
46
47     /**
48      * From attribute not supported.
49      * @param to a string
50      * @throws BuildException always
51      */

52     public void setTo(String JavaDoc to) {
53         throw new UnsupportedAttributeException(
54             "filtermapper doesn't support the \"to\" attribute.", "to");
55     }
56
57     /**
58      * Return the result of the filters on the sourcefilename.
59      * @param sourceFileName the filename to map
60      * @return a one-element array of converted filenames, or null if
61      * the filterchain returns an empty string.
62      */

63     public String JavaDoc[] mapFileName(String JavaDoc sourceFileName) {
64         try {
65             Reader JavaDoc stringReader = new StringReader JavaDoc(sourceFileName);
66             ChainReaderHelper helper = new ChainReaderHelper();
67             helper.setBufferSize(8192);
68             helper.setPrimaryReader(stringReader);
69             helper.setProject(getProject());
70             Vector JavaDoc filterChains = new Vector JavaDoc();
71             filterChains.add(this);
72             helper.setFilterChains(filterChains);
73             String JavaDoc result = FileUtils.readFully(helper.getAssembledReader());
74             if (result.length() == 0) {
75                 return null;
76             } else {
77                 return new String JavaDoc[] {result};
78             }
79         } catch (BuildException ex) {
80             throw ex;
81         } catch (Exception JavaDoc ex) {
82             throw new BuildException(ex);
83         }
84     }
85 }
86
Popular Tags