1 18 package org.apache.tools.ant.filters; 19 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import org.apache.tools.ant.BuildException; 25 26 27 47 public final class ClassConstants 48 extends BaseFilterReader 49 implements ChainableReader { 50 51 private String queuedData = null; 52 53 54 private static final String JAVA_CLASS_HELPER = 55 "org.apache.tools.ant.filters.util.JavaClassHelper"; 56 57 62 public ClassConstants() { 63 super(); 64 } 65 66 74 public ClassConstants(final Reader in) { 75 super(in); 76 } 77 78 88 public int read() throws IOException { 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 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 javaClassHelper = 110 Class.forName(JAVA_CLASS_HELPER); 111 if (javaClassHelper != null) { 112 final Class [] params = { 113 byte[].class 114 }; 115 final Method getConstants = 116 javaClassHelper.getMethod("getConstants", params); 117 final Object [] args = { 118 bytes 119 }; 120 final StringBuffer sb = (StringBuffer ) 123 getConstants.invoke(null, args); 124 if (sb.length() > 0) { 125 queuedData = sb.toString(); 126 return read(); 127 } 128 } 129 } catch (NoClassDefFoundError ex) { 130 throw ex; 131 } catch (RuntimeException ex) { 132 throw ex; 133 } catch (InvocationTargetException ex) { 134 Throwable t = ex.getTargetException(); 135 if (t instanceof NoClassDefFoundError ) { 136 throw (NoClassDefFoundError ) t; 137 } 138 if (t instanceof RuntimeException ) { 139 throw (RuntimeException ) t; 140 } 141 throw new BuildException(t); 142 } catch (Exception ex) { 143 throw new BuildException(ex); 144 } 145 } 146 } 147 return ch; 148 } 149 150 160 public Reader chain(final Reader rdr) { 161 ClassConstants newFilter = new ClassConstants(rdr); 162 return newFilter; 163 } 164 } 165 | Popular Tags |