1 57 58 package org.apache.commons.jrcs.diff; 59 60 import java.util.List ; 61 62 80 81 public abstract class Delta 82 extends org.apache.commons.jrcs.util.ToString 83 { 84 85 protected Chunk original; 86 87 protected Chunk revised; 88 89 static Class [][] DeltaClass; 90 91 static 92 { 93 DeltaClass = new Class [2][2]; 94 try 95 { 96 DeltaClass[0][0] = org.apache.commons.jrcs.diff.ChangeDelta.class; 97 DeltaClass[0][1] = org.apache.commons.jrcs.diff.AddDelta.class; 98 DeltaClass[1][0] = org.apache.commons.jrcs.diff.DeleteDelta.class; 99 DeltaClass[1][1] = org.apache.commons.jrcs.diff.ChangeDelta.class; 100 } 101 catch (Throwable o) 102 { 103 } 105 } 106 107 113 public static Delta newDelta(Chunk orig, Chunk rev) 114 { 115 Class c = DeltaClass[orig.size() > 0 ? 1 : 0] 116 [rev.size() > 0 ? 1 : 0]; 117 Delta result; 118 try 119 { 120 result = (Delta) c.newInstance(); 121 } 122 catch (Throwable e) 123 { 124 return null; 125 } 126 result.init(orig, rev); 127 return result; 128 } 129 130 133 protected Delta() 134 { 135 } 137 138 142 protected Delta(Chunk orig, Chunk rev) 143 { 144 init(orig, rev); 145 } 146 147 151 protected void init(Chunk orig, Chunk rev) 152 { 153 original = orig; 154 revised = rev; 155 } 156 157 162 public abstract void verify(List target) 163 throws PatchFailedException; 164 165 170 public final void patch(List target) 171 throws PatchFailedException 172 { 173 verify(target); 174 try 175 { 176 applyTo(target); 177 } 178 catch (Exception e) 179 { 180 throw new PatchFailedException(e.getMessage()); 181 } 182 } 183 184 189 public abstract void applyTo(List target); 190 191 196 public void toString(StringBuffer s) 197 { 198 original.rangeString(s); 199 s.append("x"); 200 revised.rangeString(s); 201 s.append(Diff.NL); 202 original.toString(s, "> ", "\n"); 203 s.append("---"); 204 s.append(Diff.NL); 205 revised.toString(s, "< ", "\n"); 206 } 207 208 215 public abstract void toRCSString(StringBuffer s, String EOL); 216 217 222 public String toRCSString(String EOL) 223 { 224 StringBuffer s = new StringBuffer (); 225 toRCSString(s, EOL); 226 return s.toString(); 227 } 228 229 235 public Chunk getOriginal() 236 { 237 return original; 238 } 239 240 246 public Chunk getRevised() 247 { 248 return revised; 249 } 250 251 257 public abstract void accept(RevisionVisitor visitor); 258 } 259 | Popular Tags |