Browse Source

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.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@533115 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 18 years ago
parent
commit
0fdff180f1
2 changed files with 71 additions and 26 deletions
  1. +13
    -2
      docs/manual/CoreTypes/resources.html
  2. +58
    -24
      src/main/org/apache/tools/ant/types/resources/StringResource.java

+ 13
- 2
docs/manual/CoreTypes/resources.html View File

@@ -249,7 +249,8 @@ element.</p>

<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,10 +261,20 @@ element.</p>
<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>
&lt;string>
self.log("Ant version =${ant.version}");
&lt;/string>
</pre>
</p>

<h4><a name="propertyresource">propertyresource</a></h4>

<p>Represents an Ant property.</p>


+ 58
- 24
src/main/org/apache/tools/ant/types/resources/StringResource.java View File

@@ -83,18 +83,40 @@ public class StringResource extends Resource {
}

/**
* 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 @@ public class StringResource extends Resource {
}

/**
* Get the string.
* Get the string. See {@link #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 @@ public class StringResource extends Resource {
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 @@ public class StringResource extends Resource {
}

/**
* 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 {@link #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);
}
}
}

Loading…
Cancel
Save