diff options
Diffstat (limited to 'framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/resources/PropertyResource.java')
-rw-r--r-- | framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/resources/PropertyResource.java | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/resources/PropertyResource.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/resources/PropertyResource.java new file mode 100644 index 00000000..a7cecb4b --- /dev/null +++ b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/resources/PropertyResource.java @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.types.resources; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.tools.ant.Project; +import org.apache.tools.ant.PropertyHelper; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.util.PropertyOutputStream; + +/** + * Exposes an Ant property as a Resource. + * @since Ant 1.7 + */ +public class PropertyResource extends Resource { + + /** Magic number */ + private static final int PROPERTY_MAGIC + = Resource.getMagicNumber("PropertyResource".getBytes()); + + private static final InputStream UNSET = new InputStream() { + public int read() { + return -1; + } + }; + + /** + * Default constructor. + */ + public PropertyResource() { + } + + /** + * Construct a new PropertyResource with the specified name. + * @param p the project to use. + * @param n the String name of this PropertyResource (Ant property name/key). + */ + public PropertyResource(Project p, String n) { + super(n); + setProject(p); + } + + /** + * Get the value of this PropertyResource. + * @return the value of the specified Property. + */ + public String getValue() { + if (isReference()) { + return ((PropertyResource) getCheckedRef()).getValue(); + } + Project p = getProject(); + return p == null ? null : p.getProperty(getName()); + } + + /** + * Get the Object value of this PropertyResource. + * @return the Object value of the specified Property. + * @since Ant 1.8.1 + */ + public Object getObjectValue() { + if (isReference()) { + return ((PropertyResource) getCheckedRef()).getObjectValue(); + } + Project p = getProject(); + return p == null ? null : PropertyHelper.getProperty(p, getName()); + } + + /** + * Find out whether this Resource exists. + * @return true if the Property is set, false otherwise. + */ + public boolean isExists() { + if (isReferenceOrProxy()) { + return getReferencedOrProxied().isExists(); + } + return getObjectValue() != null; + } + + /** + * Get the size of this Resource. + * @return the size, as a long, 0 if the Resource does not exist (for + * compatibility with java.io.File), or UNKNOWN_SIZE if not known. + */ + public long getSize() { + if (isReferenceOrProxy()) { + return getReferencedOrProxied().getSize(); + } + Object o = getObjectValue(); + return o == null ? 0L : (long) String.valueOf(o).length(); + } + + /** + * Override to implement equality with equivalent Resources, + * since we are capable of proxying them. + * @param o object to compare + * @return true if equal to o + */ + public boolean equals(Object o) { + if (super.equals(o)) { + return true; + } + return isReferenceOrProxy() && getReferencedOrProxied().equals(o); + } + + /** + * Get the hash code for this Resource. + * @return hash code as int. + */ + public int hashCode() { + if (isReferenceOrProxy()) { + return getReferencedOrProxied().hashCode(); + } + return super.hashCode() * PROPERTY_MAGIC; + } + + /** + * {@inheritDoc} + */ + public String toString() { + if (isReferenceOrProxy()) { + return getReferencedOrProxied().toString(); + } + return getValue(); + } + + /** + * Get an InputStream for the Resource. + * @return an InputStream containing this Resource's content. + * @throws IOException if unable to provide the content of this + * Resource as a stream. + * @throws UnsupportedOperationException if InputStreams are not + * supported for this Resource type. + */ + public InputStream getInputStream() throws IOException { + if (isReferenceOrProxy()) { + return getReferencedOrProxied().getInputStream(); + } + Object o = getObjectValue(); + return o == null ? UNSET : new ByteArrayInputStream(String.valueOf(o).getBytes()); + } + + /** + * Get an OutputStream for the Resource. + * @return an OutputStream to which content can be written. + * @throws IOException if unable to provide the content of this + * Resource as a stream. + * @throws UnsupportedOperationException if OutputStreams are not + * supported for this Resource type. + */ + public OutputStream getOutputStream() throws IOException { + if (isReferenceOrProxy()) { + return getReferencedOrProxied().getOutputStream(); + } + if (isExists()) { + throw new ImmutableResourceException(); + } + return new PropertyOutputStream(getProject(), getName()); + } + + /** + * Learn whether this PropertyResource either refers to another Resource + * or proxies another Resource due to its object property value being said Resource. + * @return boolean + */ + protected boolean isReferenceOrProxy() { + return isReference() || getObjectValue() instanceof Resource; + } + + /** + * Get the referenced or proxied Resource, if applicable. + * @return Resource + * @throws IllegalStateException if this PropertyResource neither proxies nor + * references another Resource. + */ + protected Resource getReferencedOrProxied() { + if (isReference()) { + return (Resource) getCheckedRef(Resource.class, "resource"); + } + Object o = getObjectValue(); + if (o instanceof Resource) { + return (Resource) o; + } + throw new IllegalStateException( + "This PropertyResource does not reference or proxy another Resource"); + } +} |