update copyright
[fedora-idea.git] / plugins / cvs / cvs-plugin / src / com / intellij / cvsSupport2 / actions / merge / CvsConflictsParser.java
blob52b297fc408d0ddf3849c181a19882a50c69f079
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.cvsSupport2.actions.merge;
18 import org.jetbrains.annotations.Nullable;
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.util.Stack;
26 /**
27 * Created by IntelliJ IDEA.
28 * User: lesya
29 * Date: Jul 13, 2005
30 * Time: 11:05:20 PM
31 * To change this template use File | Settings | File Templates.
35 public class CvsConflictsParser {
36 private static final String RIGHT = "<<<<<<<";
37 private static final String LEFT = "=======";
38 private static final String END = ">>>>>>>";
40 enum State {
41 RIGHT, LEFT
44 private final StringBuffer myLeftBuffer = new StringBuffer();
45 private final StringBuffer myCenterBuffer = new StringBuffer();
46 private final StringBuffer myRightBuffer = new StringBuffer();
47 private final Stack<State> myStateStack;
49 public String getLeftVersion() {
50 return myLeftBuffer.toString();
53 public String getCenterVersion() {
54 return myCenterBuffer.toString();
57 public String getRightVersion() {
58 return myRightBuffer.toString();
61 private CvsConflictsParser() {
63 myStateStack = new Stack<State>();
66 public static CvsConflictsParser createOn(InputStream merged) throws IOException {
67 final CvsConflictsParser result = new CvsConflictsParser();
68 result.parseFile(merged);
69 return result;
72 private void parseFile(final InputStream merged) throws IOException {
73 final InputStreamReader isr = new InputStreamReader(merged);
74 final BufferedReader br = new BufferedReader(isr);
75 try {
76 String line;
77 while ((line = br.readLine()) != null) {
78 String cutLine = findMarkerAndWriteTail(line, RIGHT);
79 if (cutLine != null) {
80 processRightMarker(cutLine);
81 continue;
84 cutLine = findMarkerAndWriteTail(line, LEFT);
85 if (cutLine != null) {
86 processLeftMarker(cutLine);
87 continue;
90 cutLine = findMarkerAndWriteTail(line, END);
91 if (cutLine != null) {
92 processEndMarker(cutLine);
93 continue;
96 appendToMainOrCurrent(line);
99 finally {
100 br.close();
104 private void appendToMainOrCurrent(final String line) {
105 if (myStateStack.isEmpty()) {
106 appendLine(line);
107 } else {
108 appendToCurrentBuffer(line);
112 @Nullable
113 private String findMarkerAndWriteTail(final String s, final String marker) {
114 final int idx = s.indexOf(marker);
115 if (idx == -1) {
116 return null;
118 final String startFragment = s.substring(0, idx);
119 if (startFragment.length() > 0) {
120 appendToMainOrCurrent(startFragment);
122 return s.substring(idx);
125 private void processEndMarker(final String line) {
126 if (myStateStack.isEmpty()) {
127 appendLine(line);
129 else {
130 myStateStack.pop();
131 if (!myStateStack.isEmpty()) {
132 appendToCurrentBuffer(line);
137 private void processLeftMarker(final String line) {
138 if (myStateStack.isEmpty()) {
139 appendLine(line);
141 else if (myStateStack.peek() == State.LEFT) {
142 myStateStack.pop();
143 myStateStack.push(State.RIGHT);
144 if (myStateStack.size() > 1) {
145 appendToCurrentBuffer(line);
148 else {
149 appendToCurrentBuffer(line);
153 private void processRightMarker(final String line) {
154 if (myStateStack.isEmpty()) {
155 myStateStack.push(State.LEFT);
157 else {
158 myStateStack.push(State.LEFT);
159 appendToCurrentBuffer(line);
163 private void appendToCurrentBuffer(final String line) {
164 if (myStateStack.get(0) == State.RIGHT) {
165 appendLine(line, myRightBuffer);
166 } else {
167 appendLine(line, myLeftBuffer);
171 private void appendLine(final String line) {
172 appendLine(line, myLeftBuffer);
173 appendLine(line, myCenterBuffer);
174 appendLine(line, myRightBuffer);
177 private static void appendLine(final String line, final StringBuffer buffer) {
178 if (buffer.length() > 0) {
179 buffer.append("\n");
181 buffer.append(line);