View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.command.changelog;
20  
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import java.util.List;
24  
25  import org.apache.maven.scm.ChangeSet;
26  import org.apache.maven.scm.ScmVersion;
27  
28  /**
29   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
30   *
31   */
32  public class ChangeLogSet {
33      public static final String DEFAULT_ENCODING = "ISO-8859-1";
34  
35      private List<ChangeSet> entries;
36  
37      private Date startDate;
38  
39      private Date endDate;
40  
41      private ScmVersion startVersion;
42  
43      private ScmVersion endVersion;
44  
45      /**
46       * Initializes a new instance of this class.
47       *
48       * @param startDate the start date/tag for this set.
49       * @param endDate   the end date/tag for this set, or <code>null</code> if this set goes to the present time.
50       */
51      public ChangeLogSet(Date startDate, Date endDate) {
52          this.startDate = startDate;
53          this.endDate = endDate;
54      }
55  
56      /**
57       * Initializes a new instance of this class.
58       *
59       * @param entries   collection of {@link org.apache.maven.scm.ChangeSet} objects for this set.
60       * @param startDate the start date/tag for this set.
61       * @param endDate   the end date/tag for this set, or <code>null</code> if this set goes to the present time.
62       */
63      public ChangeLogSet(List<ChangeSet> entries, Date startDate, Date endDate) {
64          this(startDate, endDate);
65          setChangeSets(entries);
66      }
67  
68      /**
69       * Returns the start date.
70       *
71       * @return the start date.
72       */
73      public Date getStartDate() {
74          return startDate;
75      }
76  
77      /**
78       * Returns the end date for this set.
79       *
80       * @return the end date for this set, or <code>null</code> if this set goes to the present time.
81       */
82      public Date getEndDate() {
83          return endDate;
84      }
85  
86      /**
87       * Returns the start version (revision/branch/label) for this set.
88       *
89       * @return the start version (revision/branch/label) for this set, or <code>null</code>.
90       */
91      public ScmVersion getStartVersion() {
92          return startVersion;
93      }
94  
95      public void setStartVersion(ScmVersion startVersion) {
96          this.startVersion = startVersion;
97      }
98  
99      /**
100      * Returns the end version (revision/branch/label) for this set.
101      *
102      * @return the end version (revision/branch/label) for this set, or <code>null</code>.
103      */
104     public ScmVersion getEndVersion() {
105         return endVersion;
106     }
107 
108     public void setEndVersion(ScmVersion endVersion) {
109         this.endVersion = endVersion;
110     }
111 
112     /**
113      * Returns the collection of changeSet.
114      *
115      * @return the collection of {@link org.apache.maven.scm.ChangeSet} objects for this set.
116      */
117     public List<ChangeSet> getChangeSets() {
118         return entries;
119     }
120 
121     public void setChangeSets(List<ChangeSet> changeSets) {
122         this.entries = changeSets;
123     }
124 
125     /**
126      * Creates an XML representation of this change log set with a default encoding (ISO-8859-1).
127      *
128      * @return TODO
129      */
130     public String toXML() {
131         return toXML(DEFAULT_ENCODING);
132     }
133 
134     /**
135      * Creates an XML representation of this change log set.
136      *
137      * @param encoding encoding of output
138      * @return TODO
139      */
140     public String toXML(String encoding) {
141         String encodingString = encoding;
142 
143         if (encodingString == null) {
144             encodingString = DEFAULT_ENCODING;
145         }
146 
147         StringBuilder buffer = new StringBuilder();
148         String pattern = "yyyyMMdd HH:mm:ss z";
149         SimpleDateFormat formatter = new SimpleDateFormat(pattern);
150 
151         buffer.append("<?xml version=\"1.0\" encoding=\"" + encodingString + "\"?>\n");
152         buffer.append("<changeset datePattern=\"").append(pattern).append("\"");
153 
154         if (startDate != null) {
155             buffer.append(" start=\"").append(formatter.format(getStartDate())).append("\"");
156         }
157         if (endDate != null) {
158             buffer.append(" end=\"").append(formatter.format(getEndDate())).append("\"");
159         }
160 
161         if (startVersion != null) {
162             buffer.append(" startVersion=\"").append(getStartVersion()).append("\"");
163         }
164         if (endVersion != null) {
165             buffer.append(" endVersion=\"").append(getEndVersion()).append("\"");
166         }
167 
168         buffer.append(">\n");
169 
170         //  Write out the entries
171         for (ChangeSet changeSet : getChangeSets()) {
172             buffer.append(changeSet.toXML());
173         }
174 
175         buffer.append("</changeset>\n");
176 
177         return buffer.toString();
178     }
179 }