001: /*
002: * ====================================================================
003: *
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 1999-2003 The Apache Software Foundation.
007: * All rights reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowledgement:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowledgement may appear in the software itself,
026: * if and wherever such third-party acknowledgements normally appear.
027: *
028: * 4. The names "The Jakarta Project", "Commons", and "Apache Software
029: * Foundation" must not be used to endorse or promote products derived
030: * from this software without prior written permission. For written
031: * permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache"
034: * nor may "Apache" appear in their names without prior written
035: * permission of the Apache Software Foundation.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: *
056: */
057:
058: package wicket.util.diff;
059:
060: import java.util.ArrayList;
061: import java.util.Arrays;
062: import java.util.Iterator;
063: import java.util.LinkedList;
064: import java.util.List;
065: import java.util.ListIterator;
066:
067: /**
068: * A Revision holds the series of deltas that describe the differences between
069: * two sequences.
070: *
071: * @version $Revision: 1.1 $ $Date: 2006/03/12 00:24:21 $
072: *
073: * @author <a href="mailto:juanco@suigeneris.org">Juanco Anez</a>
074: * @author <a href="mailto:bwm@hplb.hpl.hp.com">Brian McBride</a>
075: *
076: * @see Delta
077: * @see Diff
078: * @see Chunk
079: * @see Revision
080: *
081: * modifications 27 Apr 2003 bwm
082: *
083: * Added visitor pattern Visitor interface and accept() method.
084: */
085:
086: public class Revision extends ToString {
087:
088: List deltas_ = new LinkedList();
089:
090: /**
091: * Creates an empty Revision.
092: */
093: public Revision() {
094: }
095:
096: /**
097: * Adds a delta to this revision.
098: *
099: * @param delta
100: * the {@link Delta Delta} to add.
101: */
102: public synchronized void addDelta(Delta delta) {
103: if (delta == null) {
104: throw new IllegalArgumentException("new delta is null");
105: }
106: deltas_.add(delta);
107: }
108:
109: /**
110: * Adds a delta to the start of this revision.
111: *
112: * @param delta
113: * the {@link Delta Delta} to add.
114: */
115: public synchronized void insertDelta(Delta delta) {
116: if (delta == null) {
117: throw new IllegalArgumentException("new delta is null");
118: }
119: deltas_.add(0, delta);
120: }
121:
122: /**
123: * Retrieves a delta from this revision by position.
124: *
125: * @param i
126: * the position of the delta to retrieve.
127: * @return the specified delta
128: */
129: public Delta getDelta(int i) {
130: return (Delta) deltas_.get(i);
131: }
132:
133: /**
134: * Returns the number of deltas in this revision.
135: *
136: * @return the number of deltas.
137: */
138: public int size() {
139: return deltas_.size();
140: }
141:
142: /**
143: * Applies the series of deltas in this revision as patches to the given
144: * text.
145: *
146: * @param src
147: * the text to patch, which the method doesn't change.
148: * @return the resulting text after the patches have been applied.
149: * @throws PatchFailedException
150: * if any of the patches cannot be applied.
151: */
152: public Object[] patch(Object[] src) throws PatchFailedException {
153: List target = new ArrayList(Arrays.asList(src));
154: applyTo(target);
155: return target.toArray();
156: }
157:
158: /**
159: * Applies the series of deltas in this revision as patches to the given
160: * text.
161: *
162: * @param target
163: * the text to patch.
164: * @throws PatchFailedException
165: * if any of the patches cannot be applied.
166: */
167: public synchronized void applyTo(List target)
168: throws PatchFailedException {
169: ListIterator i = deltas_.listIterator(deltas_.size());
170: while (i.hasPrevious()) {
171: Delta delta = (Delta) i.previous();
172: delta.patch(target);
173: }
174: }
175:
176: /**
177: * Converts this revision into its Unix diff style string representation.
178: *
179: * @param s
180: * a {@link StringBuffer StringBuffer} to which the string
181: * representation will be appended.
182: */
183: public synchronized void toString(StringBuffer s) {
184: Iterator i = deltas_.iterator();
185: while (i.hasNext()) {
186: ((Delta) i.next()).toString(s);
187: }
188: }
189:
190: /**
191: * Converts this revision into its RCS style string representation.
192: *
193: * @param s
194: * a {@link StringBuffer StringBuffer} to which the string
195: * representation will be appended.
196: * @param EOL
197: * the string to use as line separator.
198: */
199: public synchronized void toRCSString(StringBuffer s, String EOL) {
200: Iterator i = deltas_.iterator();
201: while (i.hasNext()) {
202: ((Delta) i.next()).toRCSString(s, EOL);
203: }
204: }
205:
206: /**
207: * Converts this revision into its RCS style string representation.
208: *
209: * @param s
210: * a {@link StringBuffer StringBuffer} to which the string
211: * representation will be appended.
212: */
213: public void toRCSString(StringBuffer s) {
214: toRCSString(s, Diff.NL);
215: }
216:
217: /**
218: * Converts this delta into its RCS style string representation.
219: *
220: * @param EOL
221: * the string to use as line separator.
222: * @return String
223: */
224: public String toRCSString(String EOL) {
225: StringBuffer s = new StringBuffer();
226: toRCSString(s, EOL);
227: return s.toString();
228: }
229:
230: /**
231: * Converts this delta into its RCS style string representation using the
232: * default line separator.
233: * @return String
234: */
235: public String toRCSString() {
236: return toRCSString(Diff.NL);
237: }
238:
239: /**
240: * Accepts a visitor.
241: *
242: * @param visitor
243: * the {@link Visitor} visiting this instance
244: */
245: public void accept(RevisionVisitor visitor) {
246: visitor.visit(this );
247: Iterator iter = deltas_.iterator();
248: while (iter.hasNext()) {
249: ((Delta) iter.next()).accept(visitor);
250: }
251: }
252:
253: }
|