View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.svn.svnexe.command.info;
20  
21  import java.time.format.DateTimeFormatter;
22  import java.time.temporal.TemporalAccessor;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.scm.command.info.InfoItem;
27  import org.apache.maven.scm.util.AbstractConsumer;
28  
29  /**
30   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
31   */
32  public class SvnInfoConsumer extends AbstractConsumer {
33      private final List<InfoItem> infoItems = new ArrayList<>();
34  
35      private InfoItem currentItem = new InfoItem();
36  
37      private static final DateTimeFormatter DT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
38  
39      /**
40       * {@inheritDoc}
41       */
42      public void consumeLine(String s) {
43          if (s.equals("")) {
44              if (currentItem != null) {
45                  infoItems.add(currentItem);
46              }
47  
48              currentItem = new InfoItem();
49          } else if (s.startsWith("Path: ")) {
50              currentItem.setPath(getValue(s));
51          } else if (s.startsWith("URL: ")) {
52              currentItem.setURL(getValue(s));
53          } else if (s.startsWith("Repository Root: ")) {
54              currentItem.setRepositoryRoot(getValue(s));
55          } else if (s.startsWith("Repository UUID: ")) {
56              currentItem.setRepositoryUUID(getValue(s));
57          } else if (s.startsWith("Revision: ")) {
58              currentItem.setRevision(getValue(s));
59          } else if (s.startsWith("Node Kind: ")) {
60              currentItem.setNodeKind(getValue(s));
61          } else if (s.startsWith("Schedule: ")) {
62              currentItem.setSchedule(getValue(s));
63          } else if (s.startsWith("Last Changed Author: ")) {
64              currentItem.setLastChangedAuthor(getValue(s));
65          } else if (s.startsWith("Last Changed Rev: ")) {
66              currentItem.setLastChangedRevision(getValue(s));
67          } else if (s.startsWith("Last Changed Date: ")) {
68              currentItem.setLastChangedDateTime(parseDate(getValue(s)));
69              currentItem.setLastChangedDate(getValue(s));
70          }
71      }
72  
73      private static String getValue(String s) {
74          int idx = s.indexOf(": ");
75  
76          if (idx < 0) {
77              // FIXME: Can't throw any exceptions in consumeLine..
78              return null;
79          } else {
80              return s.substring(idx + 2);
81          }
82      }
83  
84      public List<InfoItem> getInfoItems() {
85          return infoItems;
86      }
87  
88      static TemporalAccessor parseDate(String dateText) {
89          // strip the tailing text in parenthesis
90          int startSuffix = dateText.indexOf('(');
91          if (startSuffix != -1) {
92              dateText = dateText.substring(0, startSuffix);
93          }
94          return DT_FORMATTER.parse(dateText.trim());
95      }
96  }