001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types.resources;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.io.FilterOutputStream;
025: import java.io.ByteArrayInputStream;
026: import java.io.ByteArrayOutputStream;
027:
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.types.Resource;
030: import org.apache.tools.ant.types.Reference;
031:
032: /**
033: * Exposes a string as a Resource.
034: * @since Ant 1.7
035: */
036: public class StringResource extends Resource {
037:
038: /** Magic number */
039: private static final int STRING_MAGIC = Resource
040: .getMagicNumber("StringResource".getBytes());
041:
042: private String encoding = null;
043:
044: /**
045: * Default constructor.
046: */
047: public StringResource() {
048: }
049:
050: /**
051: * Construct a StringResource with the supplied value.
052: * @param value the value of this StringResource.
053: */
054: public StringResource(String value) {
055: setValue(value);
056: }
057:
058: /**
059: * Enforce String immutability.
060: * @param s the new name/value for this StringResource.
061: */
062: public synchronized void setName(String s) {
063: if (getName() != null) {
064: throw new BuildException(new ImmutableResourceException());
065: }
066: super .setName(s);
067: }
068:
069: /**
070: * The value attribute is a semantically superior alias for the name attribute.
071: * @param s the String's value.
072: */
073: public synchronized void setValue(String s) {
074: setName(s);
075: }
076:
077: /**
078: * Synchronize access.
079: * @return the name/value of this StringResource.
080: */
081: public synchronized String getName() {
082: return super .getName();
083: }
084:
085: /**
086: * Get the value of this StringResource.
087: * @return the represented String.
088: */
089: public synchronized String getValue() {
090: return getName();
091: }
092:
093: /**
094: * Set the encoding to be used for this StringResource.
095: * @param s the encoding name.
096: */
097: public synchronized void setEncoding(String s) {
098: encoding = s;
099: }
100:
101: /**
102: * Get the encoding used by this StringResource.
103: * @return the encoding name.
104: */
105: public synchronized String 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() ? ((Resource) getCheckedRef()).getSize()
116: : (long) getContent().length();
117: }
118:
119: /**
120: * Get the hash code for this Resource.
121: * @return hash code as int.
122: */
123: public synchronized int hashCode() {
124: if (isReference()) {
125: return getCheckedRef().hashCode();
126: }
127: return super .hashCode() * STRING_MAGIC;
128: }
129:
130: /**
131: * Get the string.
132: *
133: * @return the string contents of the resource.
134: * @since Ant 1.7
135: */
136: public String toString() {
137: if (isReference()) {
138: return getCheckedRef().toString();
139: }
140: return String.valueOf(getContent());
141: }
142:
143: /**
144: * Get an InputStream for the Resource.
145: * @return an InputStream containing this Resource's content.
146: * @throws IOException if unable to provide the content of this
147: * Resource as a stream.
148: * @throws UnsupportedOperationException if InputStreams are not
149: * supported for this Resource type.
150: */
151: public synchronized InputStream getInputStream() throws IOException {
152: if (isReference()) {
153: return ((Resource) getCheckedRef()).getInputStream();
154: }
155: //I can't get my head around this; is encoding treatment needed here?
156: return
157: //new oata.util.ReaderInputStream(new InputStreamReader(
158: new ByteArrayInputStream(getContent().getBytes());
159: //, encoding), encoding);
160: }
161:
162: /**
163: * Get an OutputStream for the Resource.
164: * @return an OutputStream to which content can be written.
165: * @throws IOException if unable to provide the content of this
166: * Resource as a stream.
167: * @throws UnsupportedOperationException if OutputStreams are not
168: * supported for this Resource type.
169: */
170: public synchronized OutputStream getOutputStream()
171: 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 ? baos
183: .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 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 ? value : getProject()
213: .replaceProperties(value);
214: }
215:
216: }
|