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 org.apache.commons.jrcs.rcs;
059:
060: import java.util.Iterator;
061: import java.util.LinkedList;
062: import java.util.List;
063:
064: import org.apache.commons.jrcs.diff.PatchFailedException;
065:
066: /**
067: * A path from the head revision to a given revision in an Archive.
068: * Path collaborates with Node in applying the set of deltas contained
069: * in archive nodes to arrive at the text of the revision corresponding
070: * to the last node in the path.
071: * This class is NOT thread safe.
072: *
073: * @see Archive
074: * @see Node
075: *
076: * @author <a href="mailto:juanco@suigeneris.org">Juanco Anez</a>
077: * @version $Id: Path.java 2967 2005-10-26 10:52:33Z ian@caret.cam.ac.uk $
078: */
079: class Path {
080: private List path = new LinkedList();
081:
082: /**
083: * Creates an empty Path
084: */
085: public Path() {
086: }
087:
088: /**
089: * Add a node to the Path.
090: * @param node The Node to add.
091: */
092: public void add(Node node) {
093: path.add(node);
094: }
095:
096: /**
097: * The size of the Path.
098: * @return The size of the Path
099: */
100: public int size() {
101: return path.size();
102: }
103:
104: /**
105: * Return the last node in the path or null if the path is empty.
106: * @return the last node in the path or null if the path is empty.
107: */
108: public Node last() {
109: if (size() == 0) {
110: return null;
111: } else {
112: return (Node) path.get(size() - 1);
113: }
114: }
115:
116: /**
117: * Returns the text that corresponds to applying the patches
118: * in the list of nodes in the Path.
119: * Assume that the text of the first node is plaintext and not
120: * deltatext.
121: * @return The resulting text after the patches
122: */
123: public List patch() throws InvalidFileFormatException,
124: PatchFailedException, NodeNotFoundException {
125: return patch(false);
126: }
127:
128: /**
129: * Returns the text that corresponds to applying the patches
130: * in the list of nodes in the Path.
131: * Assume that the text of the first node is plaintext and not
132: * deltatext.
133: * @param annotate if true, then each text line is a
134: * {@link Line Line} with the original text annotated with
135: * the revision in which it was last changed or added.
136: * @return The resulting text after the patches
137: */
138: public List patch(boolean annotate)
139: throws InvalidFileFormatException, PatchFailedException,
140: NodeNotFoundException {
141: return patch(new Lines(), annotate);
142: }
143:
144: /**
145: * Returns the text that corresponds to applying the patches
146: * in the list of nodes in the Path.
147: * Assume that the text of the first node is plaintext and not
148: * deltatext.
149: * @param lines The list to where the text must be added and the
150: * patches applied.
151: * {@link Line Line} with the original text annotated with
152: * the revision in which it was last changed or added.
153: * @return The resulting text after the patches
154: */
155: public List patch(List lines) throws InvalidFileFormatException,
156: PatchFailedException, NodeNotFoundException {
157: return patch(lines, false);
158: }
159:
160: /**
161: * Returns the text that corresponds to applying the patches
162: * in the list of nodes in the Path.
163: * Assume that the text of the first node is plaintext and not
164: * deltatext.
165: * @param lines The list to where the text must be added and the
166: * patches applied.
167: * @param annotate if true, then each text line is a
168: * {@link Line Line} with the original text annotated with
169: * the revision in which it was last changed or added.
170: * @return The resulting text after the patches
171: */
172: public List patch(List lines, boolean annotate)
173: throws InvalidFileFormatException, PatchFailedException,
174: NodeNotFoundException {
175: Iterator p = path.iterator();
176:
177: // get full text of first node
178: TrunkNode head = (TrunkNode) p.next();
179: head.patch0(lines, annotate);
180:
181: // the rest are patches
182: while (p.hasNext()) {
183: Node n = (Node) p.next();
184: n.patch(lines, annotate);
185: }
186: return lines;
187: }
188: }
|