KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
25
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.types.Resource;
28 import org.apache.tools.ant.util.PropertyOutputStream;
29
30 /**
31  * Exposes an Ant property as a Resource.
32  * @since Ant 1.7
33  */

34 public class PropertyResource extends Resource {
35
36     /** Magic number */
37     private static final int PROPERTY_MAGIC
38         = Resource.getMagicNumber("PropertyResource".getBytes());
39
40     private static final InputStream JavaDoc UNSET = new InputStream JavaDoc() {
41         public int read() {
42             return -1;
43         }
44     };
45
46     /**
47      * Default constructor.
48      */

49     public PropertyResource() {
50     }
51
52     /**
53      * Construct a new PropertyResource with the specified name.
54      * @param p the project to use.
55      * @param n the String name of this PropertyResource (Ant property name/key).
56      */

57     public PropertyResource(Project p, String JavaDoc n) {
58         super(n);
59         setProject(p);
60     }
61
62     /**
63      * Get the value of this PropertyResource.
64      * @return the value of the specified Property.
65      */

66     public String JavaDoc getValue() {
67         Project p = getProject();
68         return p == null ? null : p.getProperty(getName());
69     }
70
71     /**
72      * Find out whether this Resource exists.
73      * @return true if the Property is set, false otherwise.
74      */

75     public boolean isExists() {
76         return getValue() != null;
77     }
78
79     /**
80      * Get the size of this Resource.
81      * @return the size, as a long, 0 if the Resource does not exist (for
82      * compatibility with java.io.File), or UNKNOWN_SIZE if not known.
83      */

84     public long getSize() {
85         if (isReference()) {
86             return ((Resource) getCheckedRef()).getSize();
87         }
88         return isExists() ? (long) getValue().length() : 0L;
89     }
90
91     /**
92      * Get the hash code for this Resource.
93      * @return hash code as int.
94      */

95     public int hashCode() {
96         if (isReference()) {
97             return getCheckedRef().hashCode();
98         }
99         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 JavaDoc 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 JavaDoc getInputStream() throws IOException JavaDoc {
124         if (isReference()) {
125             return ((Resource) getCheckedRef()).getInputStream();
126         }
127         return isExists() ? new ByteArrayInputStream JavaDoc(getValue().getBytes()) : UNSET;
128     }
129
130     /**
131      * Get an OutputStream for the Resource.
132      * @return an OutputStream to which content can be written.
133      * @throws IOException if unable to provide the content of this
134      * Resource as a stream.
135      * @throws UnsupportedOperationException if OutputStreams are not
136      * supported for this Resource type.
137      */

138     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
139         if (isReference()) {
140             return ((Resource) getCheckedRef()).getOutputStream();
141         }
142         if (isExists()) {
143             throw new ImmutableResourceException();
144         }
145         return new PropertyOutputStream(getProject(), getName());
146     }
147
148 }
149
Popular Tags