View Javadoc

1   /*   Copyright (C) 2003 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.util;
19  
20  import com.lowagie.text.html.HtmlEncoder;
21  
22  /***
23   * This class represents a line of text from a source file that conflicted during the diff process.
24   *
25   * @author Michael O'Connor - Finalist IT Group.
26   */
27  public class DiffConflictLine {
28  
29     private String line;
30     private int number;
31     private boolean firstFile;
32  
33     /*** A special case of DiffConflictLine, used to represent the last line in a file. */
34     public static final DiffConflictLine EOF = new DiffConflictLine();
35  
36  
37     /***
38      * Constructs a DiffConflictLine.
39      * @param firstFile <code>true</code> if this line comes from the 'first' file (a diff involves 2 files).
40      * @param number the line number within the original file.
41      * @param line the text of the line.
42      */
43     public DiffConflictLine(boolean firstFile, int number, String line) {
44        this.number = number;
45        this.firstFile = firstFile;
46        this.line = line;
47     }
48  
49     private DiffConflictLine() {
50     }
51  
52  
53     /***
54      * Checks if the given line has the same text as this one (ignoring whitespace).
55      * @param line2 the other line.
56      * @return <code>true</code> if equal.
57      */
58     public boolean lineEquals(DiffConflictLine line2) {
59        return line.trim().equals(line2.getLine().trim());
60     }
61  
62     public String getLine() {
63        return line;
64     }
65  
66     public boolean isEof() {
67        return (this == EOF);
68     }
69  
70     public int getLineNumber() {
71        return number;
72     }
73  
74     public boolean isFirstFile() {
75        return firstFile;
76     }
77     /***
78      * By default this renders a HTML result.
79      * @return
80      */
81     public String toString() {
82        return "<font class='file" + (firstFile ? "1" : "2") + "-code'>" +
83              (firstFile ? "&lt;" : "&gt;") + HtmlEncoder.encode(getLine()) + "</font><br>";
84     }
85  
86     /***
87      * Checks if a given conflict line precedes this one.
88      *
89      * @param next
90      * @return
91      */
92     public boolean precedes(DiffConflictLine next) {
93        return next != null &&
94                 (firstFile == next.firstFile) &&
95                 next.getLineNumber() == number + 1;
96     }
97  
98  }