001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public License
010: * as published by the Free Software Foundation; either version 2 of
011: * the License, or (at your option) any later version.
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: */
021:
022: package gruntspud.jedit;
023:
024: import gruntspud.Constants;
025:
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.InputStreamReader;
029: import java.util.ArrayList;
030: import java.util.Iterator;
031:
032: /**
033: * @author BRETTS
034: *
035: * Parses a stream for CVS conflict marks provides a list of <code>Conflict</code> objects,
036: * one for each conflict found
037: */
038:
039: public class ConflictReader extends InputStreamReader {
040:
041: private java.util.List conflicts;
042: private Conflict conflict;
043: private int startMarkers;
044: private int endMarkers;
045: private int middleMarkers;
046: private boolean inLocal, inRemote;
047: private int line = 0;
048:
049: public ConflictReader(InputStream in) {
050: super (in);
051: conflicts = new ArrayList();
052: }
053:
054: public Iterator conflicts() {
055: return conflicts.iterator();
056: }
057:
058: public int read(char cbuf[], int offset, int length)
059: throws IOException {
060: int r = 0;
061: int read = 0;
062: for (int i = 0; i < length; i++) {
063: r = read();
064: if (r == -1) {
065: return read;
066: }
067: read++;
068: cbuf[offset + i] = (char) r;
069: }
070: return read;
071: }
072:
073: public Conflict[] getConflicts() {
074: Conflict[] f = new Conflict[conflicts.size()];
075: conflicts.toArray(f);
076: return f;
077: }
078:
079: public int read() throws IOException {
080: int i = super .read();
081: if (i == -1) {
082: return i;
083: }
084: char ch = (char) i;
085: if (ch == '\n') {
086: line++;
087: } else if (ch == '\r') {
088: //
089: } else if (ch == '<') {
090: startMarkers++;
091: if (startMarkers == 7) {
092: conflict = new Conflict();
093: conflict.setStartLocalLine(line);
094: inLocal = true;
095: Constants.SYSTEM_LOG
096: .debug("Found start marker at line " + line);
097: }
098: } else if (ch == '=' && inLocal) {
099: middleMarkers++;
100: if (middleMarkers == 7) {
101: conflict.setEndLocalLine(line);
102: inLocal = false;
103: inRemote = true;
104: conflict.setStartRemoteLine(line);
105: Constants.SYSTEM_LOG
106: .debug("Found middle marker at line " + line);
107: }
108:
109: } else if (ch == '>' && inRemote) {
110: endMarkers++;
111: if (endMarkers == 7) {
112: inRemote = false;
113: conflict.setEndRemoteLine(line);
114: conflicts.add(conflict);
115: Constants.SYSTEM_LOG.debug("Found end marker at line "
116: + line);
117: }
118: } else {
119: startMarkers = 0;
120: middleMarkers = 0;
121: endMarkers = 0;
122: }
123: return i;
124: }
125:
126: public void readConflicts() throws IOException {
127: while (true) {
128: int i = read();
129: if (i == -1) {
130: break;
131: }
132: }
133: }
134:
135: }
|