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   */
33  public class SvnInfoConsumer extends AbstractConsumer {
34      private final List<InfoItem> infoItems = new ArrayList<>();
35  
36      private InfoItem currentItem = new InfoItem();
37  
38      private static final DateTimeFormatter DT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
39  
40      /** {@inheritDoc} */
41      public void consumeLine(String s) {
42          if (s.equals("")) {
43              if (currentItem != null) {
44                  infoItems.add(currentItem);
45              }
46  
47              currentItem = new InfoItem();
48          } else if (s.startsWith("Path: ")) {
49              currentItem.setPath(getValue(s));
50          } else if (s.startsWith("URL: ")) {
51              currentItem.setURL(getValue(s));
52          } else if (s.startsWith("Repository Root: ")) {
53              currentItem.setRepositoryRoot(getValue(s));
54          } else if (s.startsWith("Repository UUID: ")) {
55              currentItem.setRepositoryUUID(getValue(s));
56          } else if (s.startsWith("Revision: ")) {
57              currentItem.setRevision(getValue(s));
58          } else if (s.startsWith("Node Kind: ")) {
59              currentItem.setNodeKind(getValue(s));
60          } else if (s.startsWith("Schedule: ")) {
61              currentItem.setSchedule(getValue(s));
62          } else if (s.startsWith("Last Changed Author: ")) {
63              currentItem.setLastChangedAuthor(getValue(s));
64          } else if (s.startsWith("Last Changed Rev: ")) {
65              currentItem.setLastChangedRevision(getValue(s));
66          } else if (s.startsWith("Last Changed Date: ")) {
67              currentItem.setLastChangedDateTime(parseDate(getValue(s)));
68              currentItem.setLastChangedDate(getValue(s));
69          }
70      }
71  
72      private static String getValue(String s) {
73          int idx = s.indexOf(": ");
74  
75          if (idx < 0) {
76              // FIXME: Can't throw any exceptions in consumeLine..
77              return null;
78          } else {
79              return s.substring(idx + 2);
80          }
81      }
82  
83      public List<InfoItem> getInfoItems() {
84          return infoItems;
85      }
86  
87      static TemporalAccessor parseDate(String dateText) {
88          // strip the tailing text in parenthesis
89          int startSuffix = dateText.indexOf('(');
90          if (startSuffix != -1) {
91              dateText = dateText.substring(0, startSuffix);
92          }
93          return DT_FORMATTER.parse(dateText.trim());
94      }
95  }