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.provider.hg.command.changelog;
020
021import java.util.ArrayList;
022import java.util.Date;
023import java.util.List;
024import java.util.Locale;
025
026import org.apache.maven.scm.ChangeFile;
027import org.apache.maven.scm.ChangeSet;
028import org.apache.maven.scm.ScmFileStatus;
029import org.apache.maven.scm.provider.hg.command.HgConsumer;
030
031/**
032 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
033 * @author <a href="mailto:hr.mohr@gmail.com">Mads Mohr Christensen</a>
034 */
035public class HgChangeLogConsumer extends HgConsumer {
036
037    private static final String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss Z";
038
039    private static final String REVNO_TAG = "changeset:";
040
041    private static final String TAG_TAG = "tag:";
042
043    private static final String BRANCH_TAG = "branch:";
044
045    private static final String AUTHOR_TAG = "user:";
046
047    private static final String TIME_STAMP_TOKEN = "date:";
048
049    private static final String MESSAGE_TOKEN = "description:";
050
051    private static final String FILES_TOKEN = "files:";
052
053    private List<ChangeSet> logEntries = new ArrayList<>();
054
055    private ChangeSet currentChange;
056
057    private String currentRevision;
058
059    @SuppressWarnings("unused")
060    private String currentBranch; // don't know what to do with this
061
062    private String userDatePattern;
063
064    public HgChangeLogConsumer(String userDatePattern) {
065        this.userDatePattern = userDatePattern;
066    }
067
068    public List<ChangeSet> getModifications() {
069        return logEntries;
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    public void consumeLine(String line) {
076        // override default behaviour which tries to pick through things for some standard messages.  that
077        // does not apply here
078        String trimmedLine = line.trim();
079        doConsume(null, trimmedLine);
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    public void doConsume(ScmFileStatus status, String line) {
086        String tmpLine;
087
088        // new changeset
089        if (line.startsWith(REVNO_TAG)) {
090            // Init a new changeset
091            currentChange = new ChangeSet();
092            currentChange.setFiles(new ArrayList<>(0));
093            logEntries.add(currentChange);
094
095            // parse revision
096            tmpLine = line.substring(REVNO_TAG.length()).trim();
097            currentRevision = tmpLine.substring(tmpLine.indexOf(':') + 1);
098            currentChange.setRevision(currentRevision);
099        } else if (line.startsWith(BRANCH_TAG)) {
100            tmpLine = line.substring(BRANCH_TAG.length()).trim();
101            currentBranch = tmpLine;
102        } else if (line.startsWith(AUTHOR_TAG)) {
103            tmpLine = line.substring(AUTHOR_TAG.length()).trim();
104            currentChange.setAuthor(tmpLine);
105        } else if (line.startsWith(TIME_STAMP_TOKEN)) {
106            tmpLine = line.substring(TIME_STAMP_TOKEN.length()).trim();
107            Date date = parseDate(tmpLine, userDatePattern, TIME_PATTERN, Locale.ENGLISH);
108            currentChange.setDate(date);
109        } else if (line.startsWith(TAG_TAG)) {
110            tmpLine = line.substring(TAG_TAG.length()).trim();
111            currentChange.addTag(tmpLine);
112        } else if (line.startsWith(FILES_TOKEN)) {
113            tmpLine = line.substring(FILES_TOKEN.length()).trim();
114            String[] files = tmpLine.split(" ");
115            for (int i = 0; i < files.length; i++) {
116                String file = files[i];
117                ChangeFile changeFile = new ChangeFile(file, currentRevision);
118                currentChange.addFile(changeFile);
119            }
120        } else if (line.startsWith(MESSAGE_TOKEN)) {
121            currentChange.setComment("");
122        } else {
123            StringBuilder comment = new StringBuilder(currentChange.getComment());
124            comment.append(line);
125            currentChange.setComment(comment.toString());
126        }
127    }
128}