KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > WhitespaceIgnoringDiff


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.java.source.gen;
20
21 import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.util.EnumSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.api.java.lexer.JavaTokenId;
28 import org.netbeans.api.lexer.Token;
29 import org.netbeans.api.lexer.TokenHierarchy;
30 import org.netbeans.api.lexer.TokenSequence;
31 import org.netbeans.api.lexer.TokenUtilities;
32 import org.netbeans.junit.diff.Diff;
33 import org.netbeans.junit.diff.LineDiff;
34
35 /**
36  *
37  * @author Jan Lahoda
38  */

39 public class WhitespaceIgnoringDiff implements Diff {
40     
41     /** Creates a new instance of WhitespaceIgnoringDiff */
42     public WhitespaceIgnoringDiff() {
43     }
44
45     private Set JavaDoc<JavaTokenId> IGNORED_TOKENS = EnumSet.of(JavaTokenId.WHITESPACE, JavaTokenId.LINE_COMMENT, JavaTokenId.BLOCK_COMMENT, JavaTokenId.JAVADOC_COMMENT);
46     
47     public boolean diff(File JavaDoc first, File JavaDoc second, File JavaDoc diff) throws IOException JavaDoc {
48         boolean result = diffImpl(first, second);
49         
50         if (result) {
51             new LineDiff().diff(first, second, diff);
52         }
53         
54         return result;
55     }
56     
57     private boolean diffImpl(File JavaDoc first, File JavaDoc second) throws IOException JavaDoc {
58         Reader JavaDoc firstReader = new FileReader JavaDoc(first);
59         Reader JavaDoc secondReader = new FileReader JavaDoc(second);
60         try {
61             TokenHierarchy firstH = TokenHierarchy.create(firstReader, JavaTokenId.language(), /*IGNORED_TOKENS*/null, null);
62             TokenHierarchy secondH = TokenHierarchy.create(secondReader, JavaTokenId.language(), /*IGNORED_TOKENS*/null, null);
63             TokenSequence<JavaTokenId> firstTS = firstH.tokenSequence(JavaTokenId.language());
64             TokenSequence<JavaTokenId> secondTS = secondH.tokenSequence(JavaTokenId.language());
65             
66 // if (firstTS.tokenCount() != secondTS.tokenCount()) {
67
// return true;
68
// }
69

70             firstTS.moveNext();
71             secondTS.moveNext();
72             
73             boolean firstHasNext = true;
74             boolean secondHasNext = true;
75             
76             do {
77                 Token<JavaTokenId> firstToken = firstTS.token();
78                 Token<JavaTokenId> secondToken = secondTS.token();
79                 
80                 while (IGNORED_TOKENS.contains(firstToken.id()) && firstHasNext) {
81                     firstHasNext = firstTS.moveNext();
82                     firstToken = firstTS.token();
83                 }
84                 
85                 while (IGNORED_TOKENS.contains(secondToken.id()) && secondHasNext) {
86                     secondHasNext = secondTS.moveNext();
87                     secondToken = secondTS.token();
88                 }
89                 
90                 if (!firstHasNext || !secondHasNext)
91                     break;
92                 
93                 if (firstToken.id() != secondToken.id() || !TokenUtilities.equals(firstToken.text(), secondToken.text()))
94                     return true;
95                 
96                 firstHasNext = firstTS.moveNext();
97                 secondHasNext = secondTS.moveNext();
98             } while (firstHasNext && secondHasNext);
99             
100             if (firstHasNext || secondHasNext)
101                 return true;
102         } finally {
103             firstReader.close();
104             secondReader.close();
105         }
106         
107         return false;
108     }
109
110     public boolean diff(String JavaDoc first, String JavaDoc second, String JavaDoc diff) throws IOException JavaDoc {
111         File JavaDoc fFirst = new File JavaDoc(first);
112         File JavaDoc fSecond = new File JavaDoc(second);
113         File JavaDoc fDiff = null != diff ? new File JavaDoc(diff) : null;
114         return diff(fFirst, fSecond, fDiff);
115     }
116     
117 }
118
Popular Tags