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.command.diff;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.scm.ScmFile;
28  import org.apache.maven.scm.ScmFileStatus;
29  import org.apache.maven.scm.util.AbstractConsumer;
30  
31  /**
32   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
33   * @author Olivier Lamy
34   *
35   */
36  public class SvnDiffConsumer extends AbstractConsumer {
37      //
38      // Index: plugin.jelly
39      // ===================================================================
40      // --- plugin.jelly        (revision 124799)
41      // +++ plugin.jelly        (working copy)
42      //
43  
44      private static final String INDEX_TOKEN = "Index: ";
45  
46      private static final String FILE_SEPARATOR_TOKEN = "===";
47  
48      private static final String START_REVISION_TOKEN = "---";
49  
50      private static final String END_REVISION_TOKEN = "+++";
51  
52      private static final String ADDED_LINE_TOKEN = "+";
53  
54      private static final String REMOVED_LINE_TOKEN = "-";
55  
56      private static final String UNCHANGED_LINE_TOKEN = " ";
57  
58      private static final String CHANGE_SEPARATOR_TOKEN = "@@";
59  
60      private static final String NO_NEWLINE_TOKEN = "\\ No newline at end of file";
61  
62      private String currentFile;
63  
64      private StringBuilder currentDifference;
65  
66      private final List<ScmFile> changedFiles = new ArrayList<>();
67  
68      private final Map<String, CharSequence> differences = new HashMap<>();
69  
70      private final StringBuilder patch = new StringBuilder();
71  
72      // ----------------------------------------------------------------------
73      //
74      // ----------------------------------------------------------------------
75  
76      public SvnDiffConsumer(File workingDirectory) {
77          // empty
78      }
79  
80      // ----------------------------------------------------------------------
81      // StreamConsumer Implementation
82      // ----------------------------------------------------------------------
83  
84      /** {@inheritDoc} */
85      public void consumeLine(String line) {
86          if (line.startsWith(INDEX_TOKEN)) {
87              // start a new file
88              currentFile = line.substring(INDEX_TOKEN.length());
89  
90              changedFiles.add(new ScmFile(currentFile, ScmFileStatus.MODIFIED));
91  
92              currentDifference = new StringBuilder();
93  
94              differences.put(currentFile, currentDifference);
95  
96              patch.append(line).append("\n");
97  
98              return;
99          }
100 
101         if (currentFile == null) {
102             if (logger.isWarnEnabled()) {
103                 logger.warn("Unparseable line: '" + line + "'");
104             }
105             patch.append(line).append("\n");
106             return;
107         }
108 
109         if (line.startsWith(FILE_SEPARATOR_TOKEN)) {
110             // skip
111             patch.append(line).append("\n");
112         } else if (line.startsWith(START_REVISION_TOKEN)) {
113             // skip, though could parse to verify filename, start revision
114             patch.append(line).append("\n");
115         } else if (line.startsWith(END_REVISION_TOKEN)) {
116             // skip, though could parse to verify filename, end revision
117             patch.append(line).append("\n");
118         } else if (line.startsWith(ADDED_LINE_TOKEN)
119                 || line.startsWith(REMOVED_LINE_TOKEN)
120                 || line.startsWith(UNCHANGED_LINE_TOKEN)
121                 || line.startsWith(CHANGE_SEPARATOR_TOKEN)
122                 || line.equals(NO_NEWLINE_TOKEN)) {
123             // add to buffer
124             currentDifference.append(line).append("\n");
125             patch.append(line).append("\n");
126         } else {
127             // TODO: handle property differences
128 
129             if (logger.isWarnEnabled()) {
130                 logger.warn("Unparseable line: '" + line + "'");
131             }
132             patch.append(line).append("\n");
133             // skip to next file
134             currentFile = null;
135             currentDifference = null;
136         }
137     }
138 
139     public List<ScmFile> getChangedFiles() {
140         return changedFiles;
141     }
142 
143     public Map<String, CharSequence> getDifferences() {
144         return differences;
145     }
146 
147     public String getPatch() {
148         return patch.toString();
149     }
150 }