KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > cvslib > ChangeLogWriter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.cvslib;
19
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.TimeZone JavaDoc;
25
26 import org.apache.tools.ant.util.DOMElementWriter;
27 import org.apache.tools.ant.util.DOMUtils;
28
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31
32 /**
33  * Class used to generate an XML changelog.
34  *
35  */

36 public class ChangeLogWriter {
37     /** output format for dates written to xml file */
38     private static final SimpleDateFormat JavaDoc OUTPUT_DATE
39         = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
40     /** output format for times written to xml file */
41     private static final SimpleDateFormat JavaDoc OUTPUT_TIME
42         = new SimpleDateFormat JavaDoc("HH:mm");
43     /** stateless helper for writing the XML document */
44     private static final DOMElementWriter DOM_WRITER = new DOMElementWriter();
45
46     static {
47         TimeZone JavaDoc utc = TimeZone.getTimeZone("UTC");
48         OUTPUT_DATE.setTimeZone(utc);
49         OUTPUT_TIME.setTimeZone(utc);
50     }
51
52     /**
53      * Print out the specified entries.
54      *
55      * @param output writer to which to send output.
56      * @param entries the entries to be written.
57      */

58     public void printChangeLog(final PrintWriter JavaDoc output,
59                                final CVSEntry[] entries) {
60         try {
61             output.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
62             Document JavaDoc doc = DOMUtils.newDocument();
63             Element JavaDoc root = doc.createElement("changelog");
64             DOM_WRITER.openElement(root, output, 0, "\t");
65             output.println();
66             for (int i = 0; i < entries.length; i++) {
67                 final CVSEntry entry = entries[i];
68
69                 printEntry(doc, output, entry);
70             }
71             DOM_WRITER.closeElement(root, output, 0, "\t", true);
72             output.flush();
73             output.close();
74         } catch (IOException JavaDoc e) {
75             throw new org.apache.tools.ant.BuildException(e);
76         }
77     }
78
79
80     /**
81      * Print out an individual entry in changelog.
82      *
83      * @param doc Document used to create elements.
84      * @param entry the entry to print
85      * @param output writer to which to send output.
86      */

87     private void printEntry(Document JavaDoc doc, final PrintWriter JavaDoc output,
88                             final CVSEntry entry) throws IOException JavaDoc {
89         Element JavaDoc ent = doc.createElement("entry");
90         DOMUtils.appendTextElement(ent, "date",
91                                    OUTPUT_DATE.format(entry.getDate()));
92         DOMUtils.appendTextElement(ent, "time",
93                                    OUTPUT_TIME.format(entry.getDate()));
94         DOMUtils.appendCDATAElement(ent, "author", entry.getAuthor());
95
96         final Enumeration JavaDoc enumeration = entry.getFiles().elements();
97
98         while (enumeration.hasMoreElements()) {
99             final RCSFile file = (RCSFile) enumeration.nextElement();
100
101             Element JavaDoc f = DOMUtils.createChildElement(ent, "file");
102             DOMUtils.appendCDATAElement(f, "name", file.getName());
103             DOMUtils.appendTextElement(f, "revision", file.getRevision());
104
105             final String JavaDoc previousRevision = file.getPreviousRevision();
106             if (previousRevision != null) {
107                 DOMUtils.appendTextElement(f, "prevrevision",
108                                            previousRevision);
109             }
110         }
111         DOMUtils.appendCDATAElement(ent, "msg", entry.getComment());
112         DOM_WRITER.write(ent, output, 1, "\t");
113     }
114 }
115
116
Popular Tags