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