1 | /*
|
---|
2 | * Copyright 2004-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.types.mappers;
|
---|
19 |
|
---|
20 | import java.io.StringReader;
|
---|
21 | import java.io.Reader;
|
---|
22 |
|
---|
23 | import java.util.Vector;
|
---|
24 |
|
---|
25 | import org.apache.tools.ant.BuildException;
|
---|
26 | import org.apache.tools.ant.UnsupportedAttributeException;
|
---|
27 | import org.apache.tools.ant.filters.util.ChainReaderHelper;
|
---|
28 | import org.apache.tools.ant.types.FilterChain;
|
---|
29 | import org.apache.tools.ant.util.FileNameMapper;
|
---|
30 | import org.apache.tools.ant.util.FileUtils;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * This is a FileNameMapper based on a FilterChain.
|
---|
34 | */
|
---|
35 | public class FilterMapper extends FilterChain implements FileNameMapper {
|
---|
36 | /**
|
---|
37 | * From attribute not supported.
|
---|
38 | * @param from a string
|
---|
39 | * @throws BuildException always
|
---|
40 | */
|
---|
41 | public void setFrom(String from) {
|
---|
42 | throw new UnsupportedAttributeException(
|
---|
43 | "filtermapper doesn't support the \"from\" attribute.", "from");
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * From attribute not supported.
|
---|
48 | * @param to a string
|
---|
49 | * @throws BuildException always
|
---|
50 | */
|
---|
51 | public void setTo(String to) {
|
---|
52 | throw new UnsupportedAttributeException(
|
---|
53 | "filtermapper doesn't support the \"to\" attribute.", "to");
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Return the result of the filters on the sourcefilename.
|
---|
58 | * @param sourceFileName the filename to map
|
---|
59 | * @return a one-element array of converted filenames, or null if
|
---|
60 | * the filterchain returns an empty string.
|
---|
61 | */
|
---|
62 | public String[] mapFileName(String sourceFileName) {
|
---|
63 | try {
|
---|
64 | Reader stringReader = new StringReader(sourceFileName);
|
---|
65 | ChainReaderHelper helper = new ChainReaderHelper();
|
---|
66 | helper.setBufferSize(8192);
|
---|
67 | helper.setPrimaryReader(stringReader);
|
---|
68 | helper.setProject(getProject());
|
---|
69 | Vector filterChains = new Vector();
|
---|
70 | filterChains.add(this);
|
---|
71 | helper.setFilterChains(filterChains);
|
---|
72 | String result = FileUtils.readFully(helper.getAssembledReader());
|
---|
73 | if (result.length() == 0) {
|
---|
74 | return null;
|
---|
75 | } else {
|
---|
76 | return new String[] {result};
|
---|
77 | }
|
---|
78 | } catch (BuildException ex) {
|
---|
79 | throw ex;
|
---|
80 | } catch (Exception ex) {
|
---|
81 | throw new BuildException(ex);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|