001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.core.wc;
013:
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.HashMap;
017: import java.util.Map;
018:
019: import org.tmatesoft.svn.core.SVNException;
020: import org.tmatesoft.svn.core.internal.wc.admin.SVNWCAccess;
021:
022: /**
023: * The <b>SVNCommitPacket</b> is a storage for <b>SVNCommitItem</b>
024: * objects which represent information on versioned items intended
025: * for being committed to a repository.
026: *
027: * <p>
028: * Used by <b>SVNCommitClient</b> in {@link SVNCommitClient#doCollectCommitItems(File[], boolean, boolean, boolean) doCollectCommitItems()}
029: * to collect and hold information on paths that have local changes.
030: *
031: * @version 1.1.1
032: * @author TMate Software Ltd.
033: * @see SVNCommitItem
034: */
035: public class SVNCommitPacket {
036: /**
037: * This constant denotes an empty commit items storage (contains no
038: * {@link SVNCommitItem} objects).
039: */
040: public static final SVNCommitPacket EMPTY = new SVNCommitPacket(
041: null, new SVNCommitItem[0], null);
042:
043: private SVNCommitItem[] myCommitItems;
044: private Map myLockTokens;
045: private boolean[] myIsSkipped;
046: private boolean myIsDisposed;
047:
048: SVNCommitPacket(SVNWCAccess wcAccess, SVNCommitItem[] items,
049: Map lockTokens) {
050: myCommitItems = items;
051: myLockTokens = lockTokens;
052: myIsSkipped = new boolean[items == null ? 0 : items.length];
053: myIsDisposed = false;
054:
055: if (wcAccess != null) {
056: for (int i = 0; i < items.length; i++) {
057: if (items[i].getWCAccess() == null) {
058: items[i].setWCAccess(wcAccess);
059: }
060: }
061: }
062: }
063:
064: /**
065: * Gets an array of <b>SVNCommitItem</b> objects stored in this
066: * object.
067: *
068: * @return an array of <b>SVNCommitItem</b> objects containing
069: * info of versioned items to be committed
070: */
071: public SVNCommitItem[] getCommitItems() {
072: return myCommitItems;
073: }
074:
075: /**
076: * Sets or unsets a versioned item to be skipped -
077: * whether or not it should be committed.
078: *
079: *
080: * @param item an item that should be marked skipped
081: * @param skipped if <span class="javakeyword">true</span> the item is
082: * set to be skipped (a commit operation should skip
083: * the item), otherwise - unskipped if it was
084: * previously marked skipped
085: * @see #isCommitItemSkipped(SVNCommitItem)
086: *
087: */
088: public void setCommitItemSkipped(SVNCommitItem item, boolean skipped) {
089: int index = getItemIndex(item);
090: if (index >= 0 && index < myIsSkipped.length) {
091: myIsSkipped[index] = skipped;
092: }
093: }
094:
095: /**
096: * Determines if an item intended for a commit is set to
097: * be skipped - that is not to be committed.
098: *
099: * @param item an item to check
100: * @return <span class="javakeyword">true</span> if the item
101: * is set to be skipped, otherwise <span class="javakeyword">false</span>
102: * @see #setCommitItemSkipped(SVNCommitItem, boolean)
103: */
104: public boolean isCommitItemSkipped(SVNCommitItem item) {
105: int index = getItemIndex(item);
106: if (index >= 0 && index < myIsSkipped.length) {
107: return myIsSkipped[index];
108: }
109: return true;
110: }
111:
112: /**
113: * Determines if this object is disposed.
114: *
115: * @return <span class="javakeyword">true</span> if disposed
116: * otherwise <span class="javakeyword">false</span>
117: */
118: public boolean isDisposed() {
119: return myIsDisposed;
120: }
121:
122: /**
123: * Disposes the current object.
124: *
125: * @throws SVNException
126: */
127: public void dispose() throws SVNException {
128: try {
129: for (int i = 0; i < myCommitItems.length; i++) {
130: if (myCommitItems[i] != null
131: && myCommitItems[i].getWCAccess() != null) {
132: myCommitItems[i].getWCAccess().close();
133: }
134: }
135: } finally {
136: myIsDisposed = true;
137: }
138: }
139:
140: private int getItemIndex(SVNCommitItem item) {
141: for (int i = 0; myCommitItems != null
142: && i < myCommitItems.length; i++) {
143: SVNCommitItem commitItem = myCommitItems[i];
144: if (commitItem == item) {
145: return i;
146: }
147: }
148: return -1;
149: }
150:
151: Map getLockTokens() {
152: return myLockTokens;
153: }
154:
155: SVNCommitPacket removeSkippedItems() {
156: if (this == EMPTY) {
157: return EMPTY;
158: }
159: Collection items = new ArrayList();
160: Map lockTokens = myLockTokens == null ? null : new HashMap(
161: myLockTokens);
162: for (int i = 0; myCommitItems != null
163: && i < myCommitItems.length; i++) {
164: SVNCommitItem commitItem = myCommitItems[i];
165: if (!myIsSkipped[i]) {
166: items.add(commitItem);
167: } else if (lockTokens != null) {
168: lockTokens.remove(commitItem.getURL().toString());
169: }
170: }
171: SVNCommitItem[] filteredItems = (SVNCommitItem[]) items
172: .toArray(new SVNCommitItem[items.size()]);
173: return new SVNCommitPacket(null, filteredItems, lockTokens);
174: }
175:
176: /**
177: * Gives a string representation of this object.
178: *
179: * @return a string representing this object.
180: */
181: public String toString() {
182: if (EMPTY == this ) {
183: return "[EMPTY]";
184: }
185: StringBuffer result = new StringBuffer();
186: result.append("SVNCommitPacket: ");
187: for (int i = 0; i < myCommitItems.length; i++) {
188: SVNCommitItem commitItem = myCommitItems[i];
189: result.append("\n");
190: if (commitItem.isAdded()) {
191: result.append("A");
192: } else if (commitItem.isDeleted()) {
193: result.append("D");
194: } else if (commitItem.isContentsModified()) {
195: result.append("M");
196: } else {
197: result.append("_");
198: }
199: if (commitItem.isPropertiesModified()) {
200: result.append("M");
201: } else {
202: result.append(" ");
203: }
204: result.append(" ");
205: if (commitItem.getPath() != null) {
206: result.append(commitItem.getPath());
207: result.append(" ");
208: }
209: result.append(commitItem.getFile().getAbsolutePath());
210: result.append("\n");
211: result.append(commitItem.getRevision());
212: result.append(" ");
213: result.append(commitItem.getURL());
214: if (commitItem.isCopied()) {
215: result.append("\n");
216: result.append("+");
217: result.append(commitItem.getCopyFromURL());
218: }
219: if (commitItem.isLocked()) {
220: result.append("\n");
221: result.append("LOCKED");
222: }
223: }
224: return result.toString();
225: }
226: }
|