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