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.ByteArrayInputStream;
025:
026: import org.apache.tools.ant.Project;
027: import org.apache.tools.ant.types.Resource;
028: import org.apache.tools.ant.util.PropertyOutputStream;
029:
030: /**
031: * Exposes an Ant property as a Resource.
032: * @since Ant 1.7
033: */
034: public class PropertyResource extends Resource {
035:
036: /** Magic number */
037: private static final int PROPERTY_MAGIC = Resource
038: .getMagicNumber("PropertyResource".getBytes());
039:
040: private static final InputStream UNSET = new InputStream() {
041: public int read() {
042: return -1;
043: }
044: };
045:
046: /**
047: * Default constructor.
048: */
049: public PropertyResource() {
050: }
051:
052: /**
053: * Construct a new PropertyResource with the specified name.
054: * @param p the project to use.
055: * @param n the String name of this PropertyResource (Ant property name/key).
056: */
057: public PropertyResource(Project p, String n) {
058: super (n);
059: setProject(p);
060: }
061:
062: /**
063: * Get the value of this PropertyResource.
064: * @return the value of the specified Property.
065: */
066: public String getValue() {
067: Project p = getProject();
068: return p == null ? null : p.getProperty(getName());
069: }
070:
071: /**
072: * Find out whether this Resource exists.
073: * @return true if the Property is set, false otherwise.
074: */
075: public boolean isExists() {
076: return getValue() != null;
077: }
078:
079: /**
080: * Get the size of this Resource.
081: * @return the size, as a long, 0 if the Resource does not exist (for
082: * compatibility with java.io.File), or UNKNOWN_SIZE if not known.
083: */
084: public long getSize() {
085: if (isReference()) {
086: return ((Resource) getCheckedRef()).getSize();
087: }
088: return isExists() ? (long) getValue().length() : 0L;
089: }
090:
091: /**
092: * Get the hash code for this Resource.
093: * @return hash code as int.
094: */
095: public int hashCode() {
096: if (isReference()) {
097: return getCheckedRef().hashCode();
098: }
099: return super .hashCode() * PROPERTY_MAGIC;
100: }
101:
102: /**
103: * Get the string.
104: *
105: * @return the string contents of the resource.
106: * @since Ant 1.7
107: */
108: public String toString() {
109: if (isReference()) {
110: return getCheckedRef().toString();
111: }
112: return String.valueOf(getValue());
113: }
114:
115: /**
116: * Get an InputStream for the Resource.
117: * @return an InputStream containing this Resource's content.
118: * @throws IOException if unable to provide the content of this
119: * Resource as a stream.
120: * @throws UnsupportedOperationException if InputStreams are not
121: * supported for this Resource type.
122: */
123: public InputStream getInputStream() throws IOException {
124: if (isReference()) {
125: return ((Resource) getCheckedRef()).getInputStream();
126: }
127: return isExists() ? new ByteArrayInputStream(getValue()
128: .getBytes()) : UNSET;
129: }
130:
131: /**
132: * Get an OutputStream for the Resource.
133: * @return an OutputStream to which content can be written.
134: * @throws IOException if unable to provide the content of this
135: * Resource as a stream.
136: * @throws UnsupportedOperationException if OutputStreams are not
137: * supported for this Resource type.
138: */
139: public OutputStream getOutputStream() throws IOException {
140: if (isReference()) {
141: return ((Resource) getCheckedRef()).getOutputStream();
142: }
143: if (isExists()) {
144: throw new ImmutableResourceException();
145: }
146: return new PropertyOutputStream(getProject(), getName());
147: }
148:
149: }
|