Browse Source

Use DOM utility classes

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277976 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 20 years ago
parent
commit
46632032bc
2 changed files with 66 additions and 45 deletions
  1. +35
    -22
      src/main/org/apache/tools/ant/taskdefs/cvslib/ChangeLogWriter.java
  2. +31
    -23
      src/main/org/apache/tools/ant/taskdefs/cvslib/CvsTagDiff.java

+ 35
- 22
src/main/org/apache/tools/ant/taskdefs/cvslib/ChangeLogWriter.java View File

@@ -16,11 +16,18 @@
*/ */
package org.apache.tools.ant.taskdefs.cvslib; package org.apache.tools.ant.taskdefs.cvslib;


import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.TimeZone; import java.util.TimeZone;


import org.apache.tools.ant.util.DOMElementWriter;
import org.apache.tools.ant.util.DOMUtils;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/** /**
* Class used to generate an XML changelog. * Class used to generate an XML changelog.
* *
@@ -32,6 +39,8 @@ public class ChangeLogWriter {
/** output format for times written to xml file */ /** output format for times written to xml file */
private static final SimpleDateFormat OUTPUT_TIME private static final SimpleDateFormat OUTPUT_TIME
= new SimpleDateFormat("HH:mm"); = new SimpleDateFormat("HH:mm");
/** stateless helper for writing the XML document */
private static final DOMElementWriter DOM_WRITER = new DOMElementWriter();


static { static {
TimeZone utc = TimeZone.getTimeZone("UTC"); TimeZone utc = TimeZone.getTimeZone("UTC");
@@ -47,55 +56,59 @@ public class ChangeLogWriter {
*/ */
public void printChangeLog(final PrintWriter output, public void printChangeLog(final PrintWriter output,
final CVSEntry[] entries) { final CVSEntry[] entries) {
try {
output.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); output.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
output.println("<changelog>");
Document doc = DOMUtils.newDocument();
Element root = doc.createElement("changelog");
DOM_WRITER.openElement(root, output, 0, "\t");
output.println();
for (int i = 0; i < entries.length; i++) { for (int i = 0; i < entries.length; i++) {
final CVSEntry entry = entries[i]; final CVSEntry entry = entries[i];


printEntry(output, entry);
printEntry(doc, output, entry);
} }
output.println("</changelog>");
DOM_WRITER.closeElement(root, output, 0, "\t", true);
output.flush(); output.flush();
output.close(); output.close();
} catch (IOException e) {
throw new org.apache.tools.ant.BuildException(e);
}
} }




/** /**
* Print out an individual entry in changelog. * Print out an individual entry in changelog.
* *
* @param doc Document used to create elements.
* @param entry the entry to print * @param entry the entry to print
* @param output writer to which to send output. * @param output writer to which to send output.
*/ */
private void printEntry(final PrintWriter output, final CVSEntry entry) {
output.println("\t<entry>");
output.println("\t\t<date>" + OUTPUT_DATE.format(entry.getDate())
+ "</date>");
output.println("\t\t<time>" + OUTPUT_TIME.format(entry.getDate())
+ "</time>");
output.println("\t\t<author><![CDATA[" + entry.getAuthor()
+ "]]></author>");
private void printEntry(Document doc, final PrintWriter output,
final CVSEntry entry) throws IOException {
Element ent = doc.createElement("entry");
DOMUtils.appendTextElement(ent, "date",
OUTPUT_DATE.format(entry.getDate()));
DOMUtils.appendTextElement(ent, "time",
OUTPUT_TIME.format(entry.getDate()));
DOMUtils.appendCDATAElement(ent, "author", entry.getAuthor());


final Enumeration enumeration = entry.getFiles().elements(); final Enumeration enumeration = entry.getFiles().elements();


while (enumeration.hasMoreElements()) { while (enumeration.hasMoreElements()) {
final RCSFile file = (RCSFile) enumeration.nextElement(); final RCSFile file = (RCSFile) enumeration.nextElement();


output.println("\t\t<file>");
output.println("\t\t\t<name><![CDATA[" + file.getName() + "]]></name>");
output.println("\t\t\t<revision>" + file.getRevision()
+ "</revision>");
Element f = DOMUtils.createChildElement(ent, "file");
DOMUtils.appendCDATAElement(f, "name", file.getName());
DOMUtils.appendTextElement(f, "revision", file.getRevision());


final String previousRevision = file.getPreviousRevision(); final String previousRevision = file.getPreviousRevision();

if (previousRevision != null) { if (previousRevision != null) {
output.println("\t\t\t<prevrevision>" + previousRevision
+ "</prevrevision>");
DOMUtils.appendTextElement(f, "prevrevision",
previousRevision);
} }

output.println("\t\t</file>");
} }
output.println("\t\t<msg><![CDATA[" + entry.getComment() + "]]></msg>");
output.println("\t</entry>");
DOMUtils.appendCDATAElement(ent, "msg", entry.getComment());
DOM_WRITER.write(ent, output, 1, "\t");
} }
} }



+ 31
- 23
src/main/org/apache/tools/ant/taskdefs/cvslib/CvsTagDiff.java View File

@@ -30,8 +30,13 @@ import java.util.StringTokenizer;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.AbstractCvsTask; import org.apache.tools.ant.taskdefs.AbstractCvsTask;
import org.apache.tools.ant.util.DOMElementWriter;
import org.apache.tools.ant.util.DOMUtils;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;


import org.w3c.dom.Document;
import org.w3c.dom.Element;

/** /**
* Examines the output of cvs rdiff between two tags. * Examines the output of cvs rdiff between two tags.
* *
@@ -70,6 +75,9 @@ public class CvsTagDiff extends AbstractCvsTask {
*/ */
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();


/** stateless helper for writing the XML document */
private static final DOMElementWriter DOM_WRITER = new DOMElementWriter();

/** /**
* Token to identify the word file in the rdiff log * Token to identify the word file in the rdiff log
*/ */
@@ -352,26 +360,27 @@ public class CvsTagDiff extends AbstractCvsTask {
PrintWriter writer = new PrintWriter( PrintWriter writer = new PrintWriter(
new OutputStreamWriter(output, "UTF-8")); new OutputStreamWriter(output, "UTF-8"));
writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
writer.print("<tagdiff ");
Document doc = DOMUtils.newDocument();
Element root = doc.createElement("tagdiff");
if (mystartTag != null) { if (mystartTag != null) {
writer.print("startTag=\"" + mystartTag + "\" ");
root.setAttribute("startTag", mystartTag);
} else { } else {
writer.print("startDate=\"" + mystartDate + "\" ");
root.setAttribute("startDate", mystartDate);
} }
if (myendTag != null) { if (myendTag != null) {
writer.print("endTag=\"" + myendTag + "\" ");
root.setAttribute("endTag", myendTag);
} else { } else {
writer.print("endDate=\"" + myendDate + "\" ");
root.setAttribute("endDate", myendDate);
} }


writer.print("cvsroot=\"" + getCvsRoot() + "\" ");
writer.print("package=\"" + mypackage + "\" ");
writer.println(">");
root.setAttribute("cvsroot", getCvsRoot());
root.setAttribute("package", mypackage);
DOM_WRITER.openElement(root, writer, 0, "\t");
writer.println();
for (int i = 0, c = entries.length; i < c; i++) { for (int i = 0, c = entries.length; i < c; i++) {
writeTagEntry(writer, entries[i]);
writeTagEntry(doc, writer, entries[i]);
} }
writer.println("</tagdiff>");
DOM_WRITER.closeElement(root, writer, 0, "\t", true);
writer.flush(); writer.flush();
writer.close(); writer.close();
} catch (UnsupportedEncodingException uee) { } catch (UnsupportedEncodingException uee) {
@@ -392,23 +401,22 @@ public class CvsTagDiff extends AbstractCvsTask {
/** /**
* Write a single entry to the given writer. * Write a single entry to the given writer.
* *
* @param doc Document used to create elements.
* @param writer a <code>PrintWriter</code> value * @param writer a <code>PrintWriter</code> value
* @param entry a <code>CvsTagEntry</code> value * @param entry a <code>CvsTagEntry</code> value
*/ */
private void writeTagEntry(PrintWriter writer, CvsTagEntry entry) {
writer.println("\t<entry>");
writer.println("\t\t<file>");
writer.println("\t\t\t<name>" + entry.getFile() + "</name>");
if (entry.getRevision() != null) {
writer.println("\t\t\t<revision>" + entry.getRevision()
+ "</revision>");
}
private void writeTagEntry(Document doc, PrintWriter writer,
CvsTagEntry entry)
throws IOException {
Element ent = doc.createElement("entry");
Element f = DOMUtils.createChildElement(ent, "file");
DOMUtils.appendCDATAElement(f, "name", entry.getFile());
DOMUtils.appendTextElement(f, "revision", entry.getRevision());
if (entry.getPreviousRevision() != null) { if (entry.getPreviousRevision() != null) {
writer.println("\t\t\t<prevrevision>"
+ entry.getPreviousRevision() + "</prevrevision>");
DOMUtils.appendTextElement(f, "prevrevision",
entry.getPreviousRevision());
} }
writer.println("\t\t</file>");
writer.println("\t</entry>");
DOM_WRITER.write(ent, writer, 1, "\t");
} }


/** /**


Loading…
Cancel
Save