1 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 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 e1, Object 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 } 60 monitor.done(); 61 } 62 } 63 64 protected boolean shouldIgnoreWhitespace() { 65 return ignoreWhitespace; 66 } 67 68 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) return true; 85 86 if (is1 == null || is2 == null) 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 } 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 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 |