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