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.info;
020
021import java.time.format.DateTimeFormatter;
022import java.time.temporal.TemporalAccessor;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.maven.scm.command.info.InfoItem;
027import org.apache.maven.scm.util.AbstractConsumer;
028
029/**
030 * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
031 *
032 */
033public class SvnInfoConsumer extends AbstractConsumer {
034    private final List<InfoItem> infoItems = new ArrayList<>();
035
036    private InfoItem currentItem = new InfoItem();
037
038    private static final DateTimeFormatter DT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
039
040    /** {@inheritDoc} */
041    public void consumeLine(String s) {
042        if (s.equals("")) {
043            if (currentItem != null) {
044                infoItems.add(currentItem);
045            }
046
047            currentItem = new InfoItem();
048        } else if (s.startsWith("Path: ")) {
049            currentItem.setPath(getValue(s));
050        } else if (s.startsWith("URL: ")) {
051            currentItem.setURL(getValue(s));
052        } else if (s.startsWith("Repository Root: ")) {
053            currentItem.setRepositoryRoot(getValue(s));
054        } else if (s.startsWith("Repository UUID: ")) {
055            currentItem.setRepositoryUUID(getValue(s));
056        } else if (s.startsWith("Revision: ")) {
057            currentItem.setRevision(getValue(s));
058        } else if (s.startsWith("Node Kind: ")) {
059            currentItem.setNodeKind(getValue(s));
060        } else if (s.startsWith("Schedule: ")) {
061            currentItem.setSchedule(getValue(s));
062        } else if (s.startsWith("Last Changed Author: ")) {
063            currentItem.setLastChangedAuthor(getValue(s));
064        } else if (s.startsWith("Last Changed Rev: ")) {
065            currentItem.setLastChangedRevision(getValue(s));
066        } else if (s.startsWith("Last Changed Date: ")) {
067            currentItem.setLastChangedDateTime(parseDate(getValue(s)));
068            currentItem.setLastChangedDate(getValue(s));
069        }
070    }
071
072    private static String getValue(String s) {
073        int idx = s.indexOf(": ");
074
075        if (idx < 0) {
076            // FIXME: Can't throw any exceptions in consumeLine..
077            return null;
078        } else {
079            return s.substring(idx + 2);
080        }
081    }
082
083    public List<InfoItem> getInfoItems() {
084        return infoItems;
085    }
086
087    static TemporalAccessor parseDate(String dateText) {
088        // strip the tailing text in parenthesis
089        int startSuffix = dateText.indexOf('(');
090        if (startSuffix != -1) {
091            dateText = dateText.substring(0, startSuffix);
092        }
093        return DT_FORMATTER.parse(dateText.trim());
094    }
095}