001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.cvslib;
019:
020: import java.io.IOException;
021: import java.io.PrintWriter;
022: import java.text.SimpleDateFormat;
023: import java.util.Enumeration;
024: import java.util.TimeZone;
025:
026: import org.apache.tools.ant.util.DOMElementWriter;
027: import org.apache.tools.ant.util.DOMUtils;
028:
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Element;
031:
032: /**
033: * Class used to generate an XML changelog.
034: *
035: */
036: public class ChangeLogWriter {
037: /** output format for dates written to xml file */
038: private static final SimpleDateFormat OUTPUT_DATE = new SimpleDateFormat(
039: "yyyy-MM-dd");
040: /** output format for times written to xml file */
041: private static final SimpleDateFormat OUTPUT_TIME = new SimpleDateFormat(
042: "HH:mm");
043: /** stateless helper for writing the XML document */
044: private static final DOMElementWriter DOM_WRITER = new DOMElementWriter();
045:
046: static {
047: TimeZone utc = TimeZone.getTimeZone("UTC");
048: OUTPUT_DATE.setTimeZone(utc);
049: OUTPUT_TIME.setTimeZone(utc);
050: }
051:
052: /**
053: * Print out the specified entries.
054: *
055: * @param output writer to which to send output.
056: * @param entries the entries to be written.
057: */
058: public void printChangeLog(final PrintWriter output,
059: final CVSEntry[] entries) {
060: try {
061: output
062: .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
063: Document doc = DOMUtils.newDocument();
064: Element root = doc.createElement("changelog");
065: DOM_WRITER.openElement(root, output, 0, "\t");
066: output.println();
067: for (int i = 0; i < entries.length; i++) {
068: final CVSEntry entry = entries[i];
069:
070: printEntry(doc, output, entry);
071: }
072: DOM_WRITER.closeElement(root, output, 0, "\t", true);
073: output.flush();
074: output.close();
075: } catch (IOException e) {
076: throw new org.apache.tools.ant.BuildException(e);
077: }
078: }
079:
080: /**
081: * Print out an individual entry in changelog.
082: *
083: * @param doc Document used to create elements.
084: * @param entry the entry to print
085: * @param output writer to which to send output.
086: */
087: private void printEntry(Document doc, final PrintWriter output,
088: final CVSEntry entry) throws IOException {
089: Element ent = doc.createElement("entry");
090: DOMUtils.appendTextElement(ent, "date", OUTPUT_DATE
091: .format(entry.getDate()));
092: DOMUtils.appendTextElement(ent, "time", OUTPUT_TIME
093: .format(entry.getDate()));
094: DOMUtils.appendCDATAElement(ent, "author", entry.getAuthor());
095:
096: final Enumeration enumeration = entry.getFiles().elements();
097:
098: while (enumeration.hasMoreElements()) {
099: final RCSFile file = (RCSFile) enumeration.nextElement();
100:
101: Element f = DOMUtils.createChildElement(ent, "file");
102: DOMUtils.appendCDATAElement(f, "name", file.getName());
103: DOMUtils.appendTextElement(f, "revision", file
104: .getRevision());
105:
106: final String previousRevision = file.getPreviousRevision();
107: if (previousRevision != null) {
108: DOMUtils.appendTextElement(f, "prevrevision",
109: previousRevision);
110: }
111: }
112: DOMUtils.appendCDATAElement(ent, "msg", entry.getComment());
113: DOM_WRITER.write(ent, output, 1, "\t");
114: }
115: }
|