You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by st...@apache.org on 2005/10/10 23:18:05 UTC
svn commit: r312750 - in /ant/core/trunk/src/main/org/apache/tools/ant:
taskdefs/EchoXML.java util/DOMElementWriter.java util/XMLFragment.java
Author: stevel
Date: Mon Oct 10 14:17:56 2005
New Revision: 312750
URL: http://svn.apache.org/viewcvs?rev=312750&view=rev
Log:
echoXML does property expansion, does not print the XML header when appending. There is some defect here wherein some tailing spaces get into every printed node, so <e>value</e> goes to <e>value </e>
; I havent tracked it down yet.
Modified:
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/EchoXML.java
ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java
ant/core/trunk/src/main/org/apache/tools/ant/util/XMLFragment.java
Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/EchoXML.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/EchoXML.java?rev=312750&r1=312749&r2=312750&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/EchoXML.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/EchoXML.java Mon Oct 10 14:17:56 2005
@@ -34,10 +34,10 @@
*/
public class EchoXML extends XMLFragment {
- private static final DOMElementWriter writer = new DOMElementWriter();
private File file;
private boolean append;
+ public static final String ERROR_NO_XML = "No nested XML specified";
/**
* Set the output file.
@@ -59,6 +59,7 @@
* Execute the task.
*/
public void execute() {
+ DOMElementWriter writer = new DOMElementWriter(!append);
try {
OutputStream os = null;
if (file != null) {
@@ -68,7 +69,7 @@
}
Node n = getFragment().getFirstChild();
if (n == null) {
- throw new BuildException("No nested XML specified");
+ throw new BuildException(ERROR_NO_XML);
}
writer.write((Element) n, os);
} catch (Exception e) {
Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java?rev=312750&r1=312749&r2=312750&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/DOMElementWriter.java Mon Oct 10 14:17:56 2005
@@ -30,7 +30,7 @@
/**
* Writes a DOM tree to a given Writer.
- *
+ * warning: this utility currently does not declare XML Namespaces.
* <p>Utility class used by {@link org.apache.tools.ant.XmlLogger
* XmlLogger} and
* org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter
@@ -39,6 +39,26 @@
*/
public class DOMElementWriter {
+ /** xml declaration is on by default */
+ private boolean xmlDeclaration=true;
+
+ /**
+ * Create an element writer.
+ * The ?xml? declaration will be included.
+ */
+ public DOMElementWriter() {
+ }
+
+ /**
+ * Create an element writer
+ * @param xmlDeclaration flag to indicate whether the ?xml? declaration
+ * should be included.
+ * @since Ant1.7
+ */
+ public DOMElementWriter(boolean xmlDeclaration) {
+ this.xmlDeclaration = xmlDeclaration;
+ }
+
private static String lSep = System.getProperty("line.separator");
/**
@@ -50,7 +70,8 @@
/**
* Writes a DOM tree to a stream in UTF8 encoding. Note that
- * it prepends the <?xml version='1.0' encoding='UTF-8'?>.
+ * it prepends the <?xml version='1.0' encoding='UTF-8'?> if
+ * the xmlDeclaration field is true.
* The indent number is set to 0 and a 2-space indent.
* @param root the root element of the DOM tree.
* @param out the outputstream to write to.
@@ -58,7 +79,9 @@
*/
public void write(Element root, OutputStream out) throws IOException {
Writer wri = new OutputStreamWriter(out, "UTF8");
- wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
+ if(xmlDeclaration) {
+ wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
+ }
write(root, wri, 0, " ");
wri.flush();
}
Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/XMLFragment.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/util/XMLFragment.java?rev=312750&r1=312749&r2=312750&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/XMLFragment.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/XMLFragment.java Mon Oct 10 14:17:56 2005
@@ -60,7 +60,7 @@
}
/**
- * Add nested text.
+ * Add nested text, expanding properties as we go
* @param s the text to add
*/
public void addText(String s) {
@@ -80,9 +80,16 @@
return new Child(e);
}
+ /**
+ * Add text to a node.
+ * @param n node
+ * @param s value
+ */
private void addText(Node n, String s) {
+ s = getProject().replaceProperties(s);
+ //only text nodes that are non null after property expansion are added
if (s != null && !s.trim().equals("")) {
- Text t = doc.createTextNode(s);
+ Text t = doc.createTextNode(s.trim());
n.appendChild(t);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org
Re: property expansion in XmlFragment
Posted by Steve Loughran <st...@apache.org>.
Steve Loughran wrote:
> Stefan Bodewig wrote:
>
>> On Mon, 10 Oct 2005, <st...@apache.org> wrote:
>>
>>
>>> echoXML does property expansion,
>>
>>
>>
>> Could you please introduce a flag in XMLFragement to make this
>> optional?
>
>
> Could do, but see my comments below:
>
>>
>> The .NET tasks use XMLFragement for embedded build files and I do not
>> want Ant to try to expand NAnt properties I've added to that embedded
>> fragment.
>>
>> <property name="foo" value="bar"/>
>> <nant>
>> <build>
>> <property name="foo" value="baz"/>
>> <echo>${foo}</echo>
>> </build>
>> </nant>
>>
>> should give NAnt a chance to echo baz.
>>
>> Stefan
>
>
> Ahh, now that is going to be dangerously confusing. What if you want to
> get an ant property in there. Especially when you consisder that
> attributes do get expanded
>
>
> <nant>
> <build>
> <property name="foo" value="baz"/>
> <echo message="${foo}" />
> </build>
> </nant>
>
> I think you'd be better off $$ escaping the property everywhere you
> want to refer to the NAnt type, just for consistency.
I should add that this is what I do when generating maven POM files.
<version>$${project.version}</version>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org
Re: property expansion in XmlFragment
Posted by Stefan Bodewig <bo...@apache.org>.
On Wed, 12 Oct 2005, Steve Loughran <st...@apache.org> wrote:
> Stefan Bodewig wrote:
>> On Mon, 10 Oct 2005, <st...@apache.org> wrote:
>>
>>>echoXML does property expansion,
>> Could you please introduce a flag in XMLFragement to make this
>> optional?
>
> Could do, but see my comments below:
Forget it. Properties get expanded in attributes anyway, so having a
consistent behvior certainly is better.
Stefan
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org
Re: property expansion in XmlFragment
Posted by Steve Loughran <st...@apache.org>.
Stefan Bodewig wrote:
> On Mon, 10 Oct 2005, <st...@apache.org> wrote:
>
>
>>echoXML does property expansion,
>
>
> Could you please introduce a flag in XMLFragement to make this
> optional?
Could do, but see my comments below:
>
> The .NET tasks use XMLFragement for embedded build files and I do not
> want Ant to try to expand NAnt properties I've added to that embedded
> fragment.
>
> <property name="foo" value="bar"/>
> <nant>
> <build>
> <property name="foo" value="baz"/>
> <echo>${foo}</echo>
> </build>
> </nant>
>
> should give NAnt a chance to echo baz.
>
> Stefan
Ahh, now that is going to be dangerously confusing. What if you want to
get an ant property in there. Especially when you consisder that
attributes do get expanded
<nant>
<build>
<property name="foo" value="baz"/>
<echo message="${foo}" />
</build>
</nant>
I think you'd be better off $$ escaping the property everywhere you
want to refer to the NAnt type, just for consistency.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org
Re: svn commit: r312750 - in
/ant/core/trunk/src/main/org/apache/tools/ant: taskdefs/EchoXML.java
util/DOMElementWriter.java util/XMLFragment.java
Posted by Stefan Bodewig <bo...@apache.org>.
On Mon, 10 Oct 2005, <st...@apache.org> wrote:
> echoXML does property expansion,
Could you please introduce a flag in XMLFragement to make this
optional?
The .NET tasks use XMLFragement for embedded build files and I do not
want Ant to try to expand NAnt properties I've added to that embedded
fragment.
<property name="foo" value="bar"/>
<nant>
<build>
<property name="foo" value="baz"/>
<echo>${foo}</echo>
</build>
</nant>
should give NAnt a chance to echo baz.
Stefan
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org