1 18 19 package org.apache.tools.ant.types.resources; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.io.FilterOutputStream ; 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.types.Resource; 30 import org.apache.tools.ant.types.Reference; 31 32 36 public class StringResource extends Resource { 37 38 39 private static final int STRING_MAGIC 40 = Resource.getMagicNumber("StringResource".getBytes()); 41 42 private String encoding = null; 43 44 47 public StringResource() { 48 } 49 50 54 public StringResource(String value) { 55 setValue(value); 56 } 57 58 62 public synchronized void setName(String s) { 63 if (getName() != null) { 64 throw new BuildException(new ImmutableResourceException()); 65 } 66 super.setName(s); 67 } 68 69 73 public synchronized void setValue(String s) { 74 setName(s); 75 } 76 77 81 public synchronized String getName() { 82 return super.getName(); 83 } 84 85 89 public synchronized String getValue() { 90 return getName(); 91 } 92 93 97 public synchronized void setEncoding(String s) { 98 encoding = s; 99 } 100 101 105 public synchronized String getEncoding() { 106 return encoding; 107 } 108 109 114 public synchronized long getSize() { 115 return isReference() 116 ? ((Resource) getCheckedRef()).getSize() 117 : (long) getContent().length(); 118 } 119 120 124 public synchronized int hashCode() { 125 if (isReference()) { 126 return getCheckedRef().hashCode(); 127 } 128 return super.hashCode() * STRING_MAGIC; 129 } 130 131 137 public String toString() { 138 if (isReference()) { 139 return getCheckedRef().toString(); 140 } 141 return String.valueOf(getContent()); 142 } 143 144 152 public synchronized InputStream getInputStream() throws IOException { 153 if (isReference()) { 154 return ((Resource) getCheckedRef()).getInputStream(); 155 } 156 return 158 new ByteArrayInputStream (getContent().getBytes()); 160 } 162 163 171 public synchronized OutputStream getOutputStream() throws IOException { 172 if (isReference()) { 173 return ((Resource) getCheckedRef()).getOutputStream(); 174 } 175 if (getValue() != null) { 176 throw new ImmutableResourceException(); 177 } 178 final ByteArrayOutputStream baos = new ByteArrayOutputStream (); 179 return new FilterOutputStream (baos) { 180 public void close() throws IOException { 181 super.close(); 182 StringResource.this.setValue(encoding == null 183 ? baos.toString() : baos.toString(encoding)); 184 } 185 }; 186 } 187 188 192 public void setRefid(Reference r) { 193 if (encoding != null) { 194 throw tooManyAttributes(); 195 } 196 super.setRefid(r); 197 } 198 199 204 protected synchronized String getContent() { 205 if (isReference()) { 206 return ((StringResource) getCheckedRef()).getContent(); 207 } 208 String value = getValue(); 209 if (value == null) { 210 return value; 211 } 212 return getProject() == null 213 ? value : getProject().replaceProperties(value); 214 } 215 216 } 217 | Popular Tags |