Author: stevel Date: Fri Apr 27 07:20:45 2007 New Revision: 533115 URL: http://svn.apache.org/viewvc?view=rev&rev=533115 Log: bug 42276: support nested text bug 42277: String resources can have properties double expanded
Nested text is allowed, and property expansion is done at the tail end of a write, not on every read. Modified: ant/core/trunk/docs/manual/CoreTypes/resources.html ant/core/trunk/src/main/org/apache/tools/ant/types/resources/StringResource.java Modified: ant/core/trunk/docs/manual/CoreTypes/resources.html URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTypes/resources.html?view=diff&rev=533115&r1=533114&r2=533115 ============================================================================== --- ant/core/trunk/docs/manual/CoreTypes/resources.html (original) +++ ant/core/trunk/docs/manual/CoreTypes/resources.html Fri Apr 27 07:20:45 2007 @@ -249,7 +249,8 @@ <h4><a name="string">string</a></h4> -<p>Represents a Java String. As such a string is readable but not writable.</p> +<p>Represents a Java String. It can be written to, but only once, after which +it will be an error to write to again.</p> <table border="1" cellpadding="2" cellspacing="0"> <tr> @@ -260,9 +261,19 @@ <tr> <td valign="top">value</td> <td valign="top">The value of this resource</td> - <td align="center" valign="top">Yes</td> + <td align="center" valign="top">No</td> </tr> </table> + +<p>The resource also supports nested text, which can only be supplied if the <code>value</code> attribute is unset: + </p> +<pre> + <string> + self.log("Ant version =${ant.version}"); + </string> +</pre> + +</p> <h4><a name="propertyresource">propertyresource</a></h4> Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/resources/StringResource.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/resources/StringResource.java?view=diff&rev=533115&r1=533114&r2=533115 ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/types/resources/StringResource.java (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/types/resources/StringResource.java Fri Apr 27 07:20:45 2007 @@ -83,18 +83,40 @@ } /** - * Get the value of this StringResource. + * Get the value of this StringResource, resolving to the root reference if needed. * @return the represented String. */ public synchronized String getValue() { return getName(); } + + /** + * The exists attribute tells whether a resource exists. + * + * @return true if this resource exists. + */ + public boolean isExists() { + return getValue()!=null; + } + + /** + * Add nested text to this resource. + * Properties will be expanded during this process. + * @since Ant1.7.1 + * @param text text to use as the string resource + */ + public void addText(String text) { + checkChildrenAllowed(); + setValue(getProject().replaceProperties(text)); + } + /** * Set the encoding to be used for this StringResource. * @param s the encoding name. */ public synchronized void setEncoding(String s) { + checkAttributesAllowed(); encoding = s; } @@ -129,15 +151,12 @@ } /** - * Get the string. + * Get the string. See [EMAIL PROTECTED] #getContent()} * * @return the string contents of the resource. * @since Ant 1.7 */ public String toString() { - if (isReference()) { - return getCheckedRef().toString(); - } return String.valueOf(getContent()); } @@ -175,14 +194,7 @@ if (getValue() != null) { throw new ImmutableResourceException(); } - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - return new FilterOutputStream(baos) { - public void close() throws IOException { - super.close(); - StringResource.this.setValue(encoding == null - ? baos.toString() : baos.toString(encoding)); - } - }; + return new StringResourceFilterOutputStream(); } /** @@ -197,20 +209,42 @@ } /** - * Get the content of this StringResource. - * @return a String; if the Project has been set properties - * replacement will be attempted. + * Get the content of this StringResource. See [EMAIL PROTECTED] #getValue()} + * @return a String or null if there is no value. */ protected synchronized String getContent() { - if (isReference()) { - return ((StringResource) getCheckedRef()).getContent(); - } - String value = getValue(); - if (value == null) { - return value; + return getValue(); + } + + /** + * This method is only for use by our private helper output stream. + * It contains specific logic for expanding properties. + * @param output the output + */ + private void setValueFromOutputStream(String output) { + String value; + if(getProject()!=null) { + value = getProject().replaceProperties(output); + } else { + value=output; } - return getProject() == null - ? value : getProject().replaceProperties(value); + setValue(value); } + private class StringResourceFilterOutputStream extends FilterOutputStream { + private final ByteArrayOutputStream baos; + + public StringResourceFilterOutputStream() { + super(new ByteArrayOutputStream()); + baos =(ByteArrayOutputStream) out; + } + + public void close() throws IOException { + super.close(); + String result = encoding == null + ? baos.toString() : baos.toString(encoding); + + StringResource.this.setValueFromOutputStream(result); + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]