KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > StringResource


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
19 package org.apache.tools.ant.types.resources;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.FilterOutputStream JavaDoc;
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
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 /**
33  * Exposes a string as a Resource.
34  * @since Ant 1.7
35  */

36 public class StringResource extends Resource {
37
38     /** Magic number */
39     private static final int STRING_MAGIC
40         = Resource.getMagicNumber("StringResource".getBytes());
41
42     private String JavaDoc encoding = null;
43
44     /**
45      * Default constructor.
46      */

47     public StringResource() {
48     }
49
50     /**
51      * Construct a StringResource with the supplied value.
52      * @param value the value of this StringResource.
53      */

54     public StringResource(String JavaDoc value) {
55         setValue(value);
56     }
57
58     /**
59      * Enforce String immutability.
60      * @param s the new name/value for this StringResource.
61      */

62     public synchronized void setName(String JavaDoc s) {
63         if (getName() != null) {
64             throw new BuildException(new ImmutableResourceException());
65         }
66         super.setName(s);
67     }
68
69     /**
70      * The value attribute is a semantically superior alias for the name attribute.
71      * @param s the String's value.
72      */

73     public synchronized void setValue(String JavaDoc s) {
74         setName(s);
75     }
76
77     /**
78      * Synchronize access.
79      * @return the name/value of this StringResource.
80      */

81     public synchronized String JavaDoc getName() {
82         return super.getName();
83     }
84
85     /**
86      * Get the value of this StringResource.
87      * @return the represented String.
88      */

89     public synchronized String JavaDoc getValue() {
90         return getName();
91     }
92
93     /**
94      * Set the encoding to be used for this StringResource.
95      * @param s the encoding name.
96      */

97     public synchronized void setEncoding(String JavaDoc s) {
98         encoding = s;
99     }
100
101     /**
102      * Get the encoding used by this StringResource.
103      * @return the encoding name.
104      */

105     public synchronized String JavaDoc getEncoding() {
106         return encoding;
107     }
108
109     /**
110      * Get the size of this Resource.
111      * @return the size, as a long, 0 if the Resource does not exist (for
112      * compatibility with java.io.File), or UNKNOWN_SIZE if not known.
113      */

114     public synchronized long getSize() {
115         return isReference()
116             ? ((Resource) getCheckedRef()).getSize()
117             : (long) getContent().length();
118     }
119
120     /**
121      * Get the hash code for this Resource.
122      * @return hash code as int.
123      */

124     public synchronized int hashCode() {
125         if (isReference()) {
126             return getCheckedRef().hashCode();
127         }
128         return super.hashCode() * STRING_MAGIC;
129     }
130
131     /**
132      * Get the string.
133      *
134      * @return the string contents of the resource.
135      * @since Ant 1.7
136      */

137     public String JavaDoc toString() {
138         if (isReference()) {
139             return getCheckedRef().toString();
140         }
141         return String.valueOf(getContent());
142     }
143
144     /**
145      * Get an InputStream for the Resource.
146      * @return an InputStream containing this Resource's content.
147      * @throws IOException if unable to provide the content of this
148      * Resource as a stream.
149      * @throws UnsupportedOperationException if InputStreams are not
150      * supported for this Resource type.
151      */

152     public synchronized InputStream JavaDoc getInputStream() throws IOException JavaDoc {
153         if (isReference()) {
154             return ((Resource) getCheckedRef()).getInputStream();
155         }
156         //I can't get my head around this; is encoding treatment needed here?
157
return
158             //new oata.util.ReaderInputStream(new InputStreamReader(
159
new ByteArrayInputStream JavaDoc(getContent().getBytes());
160             //, encoding), encoding);
161
}
162
163     /**
164      * Get an OutputStream for the Resource.
165      * @return an OutputStream to which content can be written.
166      * @throws IOException if unable to provide the content of this
167      * Resource as a stream.
168      * @throws UnsupportedOperationException if OutputStreams are not
169      * supported for this Resource type.
170      */

171     public synchronized OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
172         if (isReference()) {
173             return ((Resource) getCheckedRef()).getOutputStream();
174         }
175         if (getValue() != null) {
176             throw new ImmutableResourceException();
177         }
178         final ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
179         return new FilterOutputStream JavaDoc(baos) {
180             public void close() throws IOException JavaDoc {
181                 super.close();
182                 StringResource.this.setValue(encoding == null
183                     ? baos.toString() : baos.toString(encoding));
184             }
185         };
186     }
187
188     /**
189      * Overrides the super version.
190      * @param r the Reference to set.
191      */

192     public void setRefid(Reference r) {
193         if (encoding != null) {
194             throw tooManyAttributes();
195         }
196         super.setRefid(r);
197     }
198
199     /**
200      * Get the content of this StringResource.
201      * @return a String; if the Project has been set properties
202      * replacement will be attempted.
203      */

204     protected synchronized String JavaDoc getContent() {
205         if (isReference()) {
206             return ((StringResource) getCheckedRef()).getContent();
207         }
208         String JavaDoc 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