01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
22: import java.io.Reader;
23:
24: import java.util.Vector;
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 from) {
43: throw new UnsupportedAttributeException(
44: "filtermapper doesn't support the \"from\" attribute.",
45: "from");
46: }
47:
48: /**
49: * From attribute not supported.
50: * @param to a string
51: * @throws BuildException always
52: */
53: public void setTo(String to) {
54: throw new UnsupportedAttributeException(
55: "filtermapper doesn't support the \"to\" attribute.",
56: "to");
57: }
58:
59: /**
60: * Return the result of the filters on the sourcefilename.
61: * @param sourceFileName the filename to map
62: * @return a one-element array of converted filenames, or null if
63: * the filterchain returns an empty string.
64: */
65: public String[] mapFileName(String sourceFileName) {
66: try {
67: Reader stringReader = new StringReader(sourceFileName);
68: ChainReaderHelper helper = new ChainReaderHelper();
69: helper.setBufferSize(8192);
70: helper.setPrimaryReader(stringReader);
71: helper.setProject(getProject());
72: Vector filterChains = new Vector();
73: filterChains.add(this );
74: helper.setFilterChains(filterChains);
75: String result = FileUtils.readFully(helper
76: .getAssembledReader());
77: if (result.length() == 0) {
78: return null;
79: } else {
80: return new String[] { result };
81: }
82: } catch (BuildException ex) {
83: throw ex;
84: } catch (Exception ex) {
85: throw new BuildException(ex);
86: }
87: }
88: }
|