KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > filters > ClassConstants


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.filters;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import org.apache.tools.ant.BuildException;
25
26
27 /**
28  * Assembles the constants declared in a Java class in
29  * <code>key1=value1(line separator)key2=value2</code>
30  * format.
31  *<p>
32  * Notes:
33  * <ol>
34  * <li>This filter uses the BCEL external toolkit.
35  * <li>This assembles only those constants that are not created
36  * using the syntax <code>new whatever()</code>
37  * <li>This assembles constants declared using the basic datatypes
38  * and String only.</li>
39  * <li>The access modifiers of the declared constants do not matter.</li>
40  *</ol>
41  * Example:<br>
42  * <pre>&lt;classconstants/&gt;</pre>
43  * Or:
44  * <pre>&lt;filterreader
45  * classname=&quot;org.apache.tools.ant.filters.ClassConstants&quot;/&gt;</pre>
46  */

47 public final class ClassConstants
48     extends BaseFilterReader
49     implements ChainableReader {
50     /** Data that must be read from, if not null. */
51     private String JavaDoc queuedData = null;
52
53     /** Helper Class to be invoked via reflection. */
54     private static final String JavaDoc JAVA_CLASS_HELPER =
55         "org.apache.tools.ant.filters.util.JavaClassHelper";
56
57     /**
58      * Constructor for "dummy" instances.
59      *
60      * @see BaseFilterReader#BaseFilterReader()
61      */

62     public ClassConstants() {
63         super();
64     }
65
66     /**
67      * Creates a new filtered reader. The contents of the passed-in reader
68      * are expected to be the name of the class from which to produce a
69      * list of constants.
70      *
71      * @param in A Reader object providing the underlying stream.
72      * Must not be <code>null</code>.
73      */

74     public ClassConstants(final Reader JavaDoc in) {
75         super(in);
76     }
77
78     /**
79      * Reads and assembles the constants declared in a class file.
80      *
81      * @return the next character in the list of constants, or -1
82      * if the end of the resulting stream has been reached
83      *
84      * @exception IOException if the underlying stream throws an IOException
85      * during reading, or if the constants for the specified class cannot
86      * be read (for example due to the class not being found).
87      */

88     public int read() throws IOException JavaDoc {
89
90         int ch = -1;
91
92         if (queuedData != null && queuedData.length() == 0) {
93             queuedData = null;
94         }
95
96         if (queuedData != null) {
97             ch = queuedData.charAt(0);
98             queuedData = queuedData.substring(1);
99             if (queuedData.length() == 0) {
100                 queuedData = null;
101             }
102         } else {
103             final String JavaDoc clazz = readFully();
104             if (clazz == null) {
105                 ch = -1;
106             } else {
107                 final byte[] bytes = clazz.getBytes("ISO-8859-1");
108                 try {
109                     final Class JavaDoc javaClassHelper =
110                         Class.forName(JAVA_CLASS_HELPER);
111                     if (javaClassHelper != null) {
112                         final Class JavaDoc[] params = {
113                             byte[].class
114                         };
115                         final Method JavaDoc getConstants =
116                             javaClassHelper.getMethod("getConstants", params);
117                         final Object JavaDoc[] args = {
118                             bytes
119                         };
120                         // getConstants is a static method, no need to
121
// pass in the object
122
final StringBuffer JavaDoc sb = (StringBuffer JavaDoc)
123                                 getConstants.invoke(null, args);
124                         if (sb.length() > 0) {
125                             queuedData = sb.toString();
126                             return read();
127                         }
128                     }
129                 } catch (NoClassDefFoundError JavaDoc ex) {
130                     throw ex;
131                 } catch (RuntimeException JavaDoc ex) {
132                     throw ex;
133                 } catch (InvocationTargetException JavaDoc ex) {
134                     Throwable JavaDoc t = ex.getTargetException();
135                     if (t instanceof NoClassDefFoundError JavaDoc) {
136                         throw (NoClassDefFoundError JavaDoc) t;
137                     }
138                     if (t instanceof RuntimeException JavaDoc) {
139                         throw (RuntimeException JavaDoc) t;
140                     }
141                     throw new BuildException(t);
142                 } catch (Exception JavaDoc ex) {
143                     throw new BuildException(ex);
144                 }
145             }
146         }
147         return ch;
148     }
149
150     /**
151      * Creates a new ClassConstants using the passed in
152      * Reader for instantiation.
153      *
154      * @param rdr A Reader object providing the underlying stream.
155      * Must not be <code>null</code>.
156      *
157      * @return a new filter based on this configuration, but filtering
158      * the specified reader
159      */

160     public Reader JavaDoc chain(final Reader JavaDoc rdr) {
161         ClassConstants newFilter = new ClassConstants(rdr);
162         return newFilter;
163     }
164 }
165
Popular Tags