1. /*
  2. * Copyright 2002,2004 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. package org.apache.tools.ant.filters;
  18. import java.io.IOException;
  19. import java.io.Reader;
  20. import java.lang.reflect.InvocationTargetException;
  21. import java.lang.reflect.Method;
  22. /**
  23. * Assembles the constants declared in a Java class in
  24. * <code>key1=value1(line separator)key2=value2</code>
  25. * format.
  26. *<p>
  27. * Notes:
  28. * <ol>
  29. * <li>This filter uses the BCEL external toolkit.
  30. * <li>This assembles only those constants that are not created
  31. * using the syntax <code>new whatever()</code>
  32. * <li>This assembles constants declared using the basic datatypes
  33. * and String only.</li>
  34. * <li>The access modifiers of the declared constants do not matter.</li>
  35. *</ol>
  36. * Example:<br>
  37. * <pre><classconstants/></pre>
  38. * Or:
  39. * <pre><filterreader
  40. * classname="org.apache.tools.ant.filters.ClassConstants"/></pre>
  41. */
  42. public final class ClassConstants
  43. extends BaseFilterReader
  44. implements ChainableReader {
  45. /** Data that must be read from, if not null. */
  46. private String queuedData = null;
  47. /** Helper Class to be invoked via reflection. */
  48. private static final String JAVA_CLASS_HELPER =
  49. "org.apache.tools.ant.filters.util.JavaClassHelper";
  50. /**
  51. * Constructor for "dummy" instances.
  52. *
  53. * @see BaseFilterReader#BaseFilterReader()
  54. */
  55. public ClassConstants() {
  56. super();
  57. }
  58. /**
  59. * Creates a new filtered reader. The contents of the passed-in reader
  60. * are expected to be the name of the class from which to produce a
  61. * list of constants.
  62. *
  63. * @param in A Reader object providing the underlying stream.
  64. * Must not be <code>null</code>.
  65. */
  66. public ClassConstants(final Reader in) {
  67. super(in);
  68. }
  69. /**
  70. * Reads and assembles the constants declared in a class file.
  71. *
  72. * @return the next character in the list of constants, or -1
  73. * if the end of the resulting stream has been reached
  74. *
  75. * @exception IOException if the underlying stream throws an IOException
  76. * during reading, or if the constants for the specified class cannot
  77. * be read (for example due to the class not being found).
  78. */
  79. public final int read() throws IOException {
  80. int ch = -1;
  81. if (queuedData != null && queuedData.length() == 0) {
  82. queuedData = null;
  83. }
  84. if (queuedData != null) {
  85. ch = queuedData.charAt(0);
  86. queuedData = queuedData.substring(1);
  87. if (queuedData.length() == 0) {
  88. queuedData = null;
  89. }
  90. } else {
  91. final String clazz = readFully();
  92. if (clazz == null) {
  93. ch = -1;
  94. } else {
  95. final byte[] bytes = clazz.getBytes();
  96. try {
  97. final Class javaClassHelper =
  98. Class.forName(JAVA_CLASS_HELPER);
  99. if (javaClassHelper != null) {
  100. final Class[] params = {
  101. byte[].class
  102. };
  103. final Method getConstants =
  104. javaClassHelper.getMethod("getConstants", params);
  105. final Object[] args = {
  106. bytes
  107. };
  108. // getConstants is a static method, no need to
  109. // pass in the object
  110. final StringBuffer sb = (StringBuffer)
  111. getConstants.invoke(null, args);
  112. if (sb.length() > 0) {
  113. queuedData = sb.toString();
  114. return read();
  115. }
  116. }
  117. } catch (ClassNotFoundException cnfe) {
  118. throw new IOException(cnfe.getMessage());
  119. } catch (NoSuchMethodException nsme) {
  120. throw new IOException(nsme.getMessage());
  121. } catch (IllegalAccessException iae) {
  122. throw new IOException(iae.getMessage());
  123. } catch (IllegalArgumentException iarge) {
  124. throw new IOException(iarge.getMessage());
  125. } catch (InvocationTargetException ite) {
  126. throw new IOException(ite.getMessage());
  127. }
  128. }
  129. }
  130. return ch;
  131. }
  132. /**
  133. * Creates a new ClassConstants using the passed in
  134. * Reader for instantiation.
  135. *
  136. * @param rdr A Reader object providing the underlying stream.
  137. * Must not be <code>null</code>.
  138. *
  139. * @return a new filter based on this configuration, but filtering
  140. * the specified reader
  141. */
  142. public final Reader chain(final Reader rdr) {
  143. ClassConstants newFilter = new ClassConstants(rdr);
  144. return newFilter;
  145. }
  146. }