001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.command.changelog;
020
021import java.text.SimpleDateFormat;
022import java.util.Date;
023import java.util.List;
024
025import org.apache.maven.scm.ChangeSet;
026import org.apache.maven.scm.ScmVersion;
027
028/**
029 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
030 */
031public class ChangeLogSet {
032    public static final String DEFAULT_ENCODING = "ISO-8859-1";
033
034    private List<ChangeSet> entries;
035
036    private Date startDate;
037
038    private Date endDate;
039
040    private ScmVersion startVersion;
041
042    private ScmVersion endVersion;
043
044    /**
045     * Initializes a new instance of this class.
046     *
047     * @param startDate the start date/tag for this set
048     * @param endDate   the end date/tag for this set, or <code>null</code> if this set goes to the present time
049     */
050    public ChangeLogSet(Date startDate, Date endDate) {
051        this.startDate = startDate;
052        this.endDate = endDate;
053    }
054
055    /**
056     * Initializes a new instance of this class.
057     *
058     * @param entries   collection of {@link org.apache.maven.scm.ChangeSet} objects for this set
059     * @param startDate the start date/tag for this set
060     * @param endDate   the end date/tag for this set, or <code>null</code> if this set goes to the present time
061     */
062    public ChangeLogSet(List<ChangeSet> entries, Date startDate, Date endDate) {
063        this(startDate, endDate);
064        setChangeSets(entries);
065    }
066
067    /**
068     * Returns the start date.
069     *
070     * @return the start date
071     */
072    public Date getStartDate() {
073        return startDate;
074    }
075
076    /**
077     * Returns the end date for this set.
078     *
079     * @return the end date for this set, or <code>null</code> if this set goes to the present time
080     */
081    public Date getEndDate() {
082        return endDate;
083    }
084
085    /**
086     * Returns the start version (revision/branch/label) for this set.
087     *
088     * @return the start version (revision/branch/label) for this set, or <code>null</code>
089     */
090    public ScmVersion getStartVersion() {
091        return startVersion;
092    }
093
094    public void setStartVersion(ScmVersion startVersion) {
095        this.startVersion = startVersion;
096    }
097
098    /**
099     * Returns the end version (revision/branch/label) for this set.
100     *
101     * @return the end version (revision/branch/label) for this set, or <code>null</code>
102     */
103    public ScmVersion getEndVersion() {
104        return endVersion;
105    }
106
107    public void setEndVersion(ScmVersion endVersion) {
108        this.endVersion = endVersion;
109    }
110
111    /**
112     * Returns the collection of changeSet.
113     *
114     * @return the collection of {@link org.apache.maven.scm.ChangeSet} objects for this set
115     */
116    public List<ChangeSet> getChangeSets() {
117        return entries;
118    }
119
120    public void setChangeSets(List<ChangeSet> changeSets) {
121        this.entries = changeSets;
122    }
123
124    /**
125     * Creates an XML representation of this change log set with a default encoding (ISO-8859-1).
126     *
127     * @return TODO
128     */
129    public String toXML() {
130        return toXML(DEFAULT_ENCODING);
131    }
132
133    /**
134     * Creates an XML representation of this change log set.
135     *
136     * @param encoding encoding of output
137     * @return TODO
138     */
139    public String toXML(String encoding) {
140        String encodingString = encoding;
141
142        if (encodingString == null) {
143            encodingString = DEFAULT_ENCODING;
144        }
145
146        StringBuilder buffer = new StringBuilder();
147        String pattern = "yyyyMMdd HH:mm:ss z";
148        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
149
150        buffer.append("<?xml version=\"1.0\" encoding=\"" + encodingString + "\"?>\n");
151        buffer.append("<changeset datePattern=\"").append(pattern).append("\"");
152
153        if (startDate != null) {
154            buffer.append(" start=\"").append(formatter.format(getStartDate())).append("\"");
155        }
156        if (endDate != null) {
157            buffer.append(" end=\"").append(formatter.format(getEndDate())).append("\"");
158        }
159
160        if (startVersion != null) {
161            buffer.append(" startVersion=\"").append(getStartVersion()).append("\"");
162        }
163        if (endVersion != null) {
164            buffer.append(" endVersion=\"").append(getEndVersion()).append("\"");
165        }
166
167        buffer.append(">\n");
168
169        //  Write out the entries
170        for (ChangeSet changeSet : getChangeSets()) {
171            buffer.append(changeSet.toXML());
172        }
173
174        buffer.append("</changeset>\n");
175
176        return buffer.toString();
177    }
178}