KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jrcs > diff > Delta


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowledgement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgement may appear in the software itself,
26  * if and wherever such third-party acknowledgements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
29  * Foundation" must not be used to endorse or promote products derived
30  * from this software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * nor may "Apache" appear in their names without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  *
56  */

57
58 package org.apache.commons.jrcs.diff;
59
60 import java.util.List JavaDoc;
61
62 /**
63  * Holds a "delta" difference between to revisions of a text.
64  *
65  * @version $Revision: 1.2 $ $Date: 2005/06/16 18:11:11 $
66  *
67  * @author <a HREF="mailto:juanco@suigeneris.org">Juanco Anez</a>
68  * @author <a HREF="mailto:bwm@hplb.hpl.hp.com">Brian McBride</a>
69  * @see Diff
70  * @see Chunk
71  * @see Revision
72  *
73  * modifications
74  *
75  * 27 Apr 2003 bwm
76  *
77  * Added getOriginal() and getRevised() accessor methods
78  * Added visitor pattern accept() method
79  */

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 JavaDoc[][] DeltaClass;
90
91     static
92     {
93         DeltaClass = new Class JavaDoc[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 JavaDoc o)
102         {
103             // do nothing
104
}
105     }
106
107     /**
108      * Returns a Delta that corresponds to the given chunks in the
109      * original and revised text respectively.
110      * @param orig the chunk in the original text.
111      * @param rev the chunk in the revised text.
112      */

113     public static Delta newDelta(Chunk orig, Chunk rev)
114     {
115         Class JavaDoc 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 JavaDoc e)
123         {
124             return null;
125         }
126         result.init(orig, rev);
127         return result;
128     }
129
130     /**
131      * Creates an uninitialized delta.
132      */

133     protected Delta()
134     {
135         // do nothing
136
}
137
138     /**
139      * Creates a delta object with the given chunks from the original
140      * and revised texts.
141      */

142     protected Delta(Chunk orig, Chunk rev)
143     {
144         init(orig, rev);
145     }
146
147     /**
148      * Initializaes the delta with the given chunks from the original
149      * and revised texts.
150      */

151     protected void init(Chunk orig, Chunk rev)
152     {
153         original = orig;
154         revised = rev;
155     }
156
157     /**
158      * Verifies that this delta can be used to patch the given text.
159      * @param target the text to patch.
160      * @throws PatchFailedException if the patch cannot be applied.
161      */

162     public abstract void verify(List JavaDoc target)
163         throws PatchFailedException;
164
165     /**
166      * Applies this delta as a patch to the given text.
167      * @param target the text to patch.
168      * @throws PatchFailedException if the patch cannot be applied.
169      */

170     public final void patch(List JavaDoc target)
171         throws PatchFailedException
172     {
173         verify(target);
174         try
175         {
176             applyTo(target);
177         }
178         catch (Exception JavaDoc e)
179         {
180             throw new PatchFailedException(e.getMessage());
181         }
182     }
183
184     /**
185      * Applies this delta as a patch to the given text.
186      * @param target the text to patch.
187      * @throws PatchFailedException if the patch cannot be applied.
188      */

189     public abstract void applyTo(List JavaDoc target);
190
191     /**
192      * Converts this delta into its Unix diff style string representation.
193      * @param s a {@link StringBuffer StringBuffer} to which the string
194      * representation will be appended.
195      */

196     public void toString(StringBuffer JavaDoc 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     /**
209      * Converts this delta into its RCS style string representation.
210      * @param s a {@link StringBuffer StringBuffer} to which the string
211      * representation will be appended.
212      * @param EOL the string to use as line separator.
213      * @deprecated Replaced by the RCSPrint visitor
214      */

215     public abstract void toRCSString(StringBuffer JavaDoc s, String JavaDoc EOL);
216
217     /**
218      * Converts this delta into its RCS style string representation.
219      * @param EOL the string to use as line separator.
220      * @deprecated Replaced by the RCPrint visitor
221      */

222     public String JavaDoc toRCSString(String JavaDoc EOL)
223     {
224         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
225         toRCSString(s, EOL);
226         return s.toString();
227     }
228
229     /**
230      * Accessor method to return the chunk representing the original
231      * sequence of items
232      *
233      * @return the original sequence
234      */

235     public Chunk getOriginal()
236     {
237         return original;
238     }
239
240     /**
241      * Accessor method to return the chunk representing the updated
242      * sequence of items.
243      *
244      * @return the updated sequence
245      */

246     public Chunk getRevised()
247     {
248         return revised;
249     }
250
251     /**
252      * Accepts a visitor.
253      * <p>
254      * See the Visitor pattern in "Design Patterns" by the GOF4.
255      * @param visitor The visitor.
256      */

257     public abstract void accept(RevisionVisitor visitor);
258 }
259
Popular Tags