KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > subscribers > ContentComparator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.core.subscribers;
12
13 import java.io.*;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.team.core.TeamException;
18 import org.eclipse.team.core.history.IFileRevision;
19 import org.eclipse.team.core.variants.IResourceVariant;
20 import org.eclipse.team.internal.core.Policy;
21 import org.eclipse.team.internal.core.TeamPlugin;
22
23 /**
24  * This is an internal class that is used by the <code>ContentComparisonSyncInfoFilter</code>
25  * to compare the contents of the local and remote resources
26  */

27 public class ContentComparator {
28
29     private boolean ignoreWhitespace = false;
30
31     public ContentComparator(boolean ignoreWhitespace) {
32         this.ignoreWhitespace = ignoreWhitespace;
33     }
34         
35     public boolean compare(Object JavaDoc e1, Object JavaDoc e2, IProgressMonitor monitor) {
36         InputStream is1 = null;
37         InputStream is2 = null;
38         try {
39             monitor.beginTask(null, 100);
40             is1 = getContents(e1, Policy.subMonitorFor(monitor, 50));
41             is2 = getContents(e2, Policy.subMonitorFor(monitor, 50));
42             return contentsEqual(is1, is2, shouldIgnoreWhitespace());
43         } catch(TeamException e) {
44             TeamPlugin.log(e);
45             return false;
46         } finally {
47             try {
48                 try {
49                     if (is1 != null) {
50                         is1.close();
51                     }
52                 } finally {
53                     if (is2 != null) {
54                         is2.close();
55                     }
56                 }
57             } catch (IOException e) {
58                 // Ignore
59
}
60             monitor.done();
61         }
62     }
63
64     protected boolean shouldIgnoreWhitespace() {
65         return ignoreWhitespace;
66     }
67
68     /**
69      * Returns <code>true</code> if both input streams byte contents is
70      * identical.
71      *
72      * @param input1
73      * first input to contents compare
74      * @param input2
75      * second input to contents compare
76      * @return <code>true</code> if content is equal
77      */

78     private boolean contentsEqual(InputStream is1, InputStream is2, boolean ignoreWhitespace) {
79         try {
80             if (is1 == is2)
81                 return true;
82
83             if (is1 == null && is2 == null) // no byte contents
84
return true;
85
86             if (is1 == null || is2 == null) // only one has
87
// contents
88
return false;
89
90             while (true) {
91                 int c1 = is1.read();
92                 while (shouldIgnoreWhitespace() && isWhitespace(c1))
93                     c1 = is1.read();
94                 int c2 = is2.read();
95                 while (shouldIgnoreWhitespace() && isWhitespace(c2))
96                     c2 = is2.read();
97                 if (c1 == -1 && c2 == -1)
98                     return true;
99                 if (c1 != c2)
100                     break;
101
102             }
103         } catch (IOException ex) {
104         } finally {
105             try {
106                 try {
107                     if (is1 != null) {
108                         is1.close();
109                     }
110                 } finally {
111                     if (is2 != null) {
112                         is2.close();
113                     }
114                 }
115             } catch (IOException e) {
116                 // Ignore
117
}
118         }
119         return false;
120     }
121
122     private boolean isWhitespace(int c) {
123         if (c == -1)
124             return false;
125         return Character.isWhitespace((char) c);
126     }
127
128     private InputStream getContents(Object JavaDoc resource, IProgressMonitor monitor) throws TeamException {
129         try {
130             if (resource instanceof IFile) {
131                 return new BufferedInputStream(((IFile) resource).getContents());
132             } else if(resource instanceof IResourceVariant) {
133                 IResourceVariant remote = (IResourceVariant)resource;
134                 if (!remote.isContainer()) {
135                     return new BufferedInputStream(remote.getStorage(monitor).getContents());
136                 }
137             } else if(resource instanceof IFileRevision) {
138                 IFileRevision remote = (IFileRevision)resource;
139                 return new BufferedInputStream(remote.getStorage(monitor).getContents());
140             }
141             return null;
142         } catch (CoreException e) {
143             throw TeamException.asTeamException(e);
144         }
145     }
146 }
147
Popular Tags