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.svn.svnexe.command.blame;
020
021import java.text.ParseException;
022import java.text.SimpleDateFormat;
023import java.util.ArrayList;
024import java.util.Date;
025import java.util.List;
026import java.util.TimeZone;
027import java.util.regex.Matcher;
028import java.util.regex.Pattern;
029
030import org.apache.maven.scm.command.blame.BlameLine;
031import org.apache.maven.scm.util.AbstractConsumer;
032
033/**
034 * @author Evgeny Mandrikov
035 * @author Olivier Lamy
036 * @since 1.4
037 */
038public class SvnBlameConsumer extends AbstractConsumer {
039    private static final String SVN_TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss";
040
041    private static final Pattern LINE_PATTERN = Pattern.compile("line-number=\"(.*)\"");
042
043    private static final Pattern REVISION_PATTERN = Pattern.compile("revision=\"(.*)\"");
044
045    private static final Pattern AUTHOR_PATTERN = Pattern.compile("<author>(.*)</author>");
046
047    private static final Pattern DATE_PATTERN = Pattern.compile("<date>(.*)T(.*)\\.(.*)Z</date>");
048
049    private final SimpleDateFormat dateFormat;
050
051    private final List<BlameLine> lines = new ArrayList<>();
052
053    public SvnBlameConsumer() {
054        dateFormat = new SimpleDateFormat(SVN_TIMESTAMP_PATTERN);
055        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
056    }
057
058    private int lineNumber;
059
060    private String revision;
061
062    private String author;
063
064    public void consumeLine(String line) {
065        Matcher matcher;
066        if ((matcher = LINE_PATTERN.matcher(line)).find()) {
067            String lineNumberStr = matcher.group(1);
068            lineNumber = Integer.parseInt(lineNumberStr);
069        } else if ((matcher = REVISION_PATTERN.matcher(line)).find()) {
070            revision = matcher.group(1);
071        } else if ((matcher = AUTHOR_PATTERN.matcher(line)).find()) {
072            author = matcher.group(1);
073        } else if ((matcher = DATE_PATTERN.matcher(line)).find()) {
074            String date = matcher.group(1);
075            String time = matcher.group(2);
076            Date dateTime = parseDateTime(date + " " + time);
077            lines.add(new BlameLine(dateTime, revision, author));
078            if (logger.isDebugEnabled()) {
079                logger.debug("Author of line " + lineNumber + ": " + author + " (" + date + ")");
080            }
081        }
082    }
083
084    protected Date parseDateTime(String dateTimeStr) {
085        try {
086            return dateFormat.parse(dateTimeStr);
087        } catch (ParseException e) {
088            logger.error("skip ParseException: " + e.getMessage() + " during parsing date " + dateTimeStr, e);
089            return null;
090        }
091    }
092
093    public List<BlameLine> getLines() {
094        return lines;
095    }
096}