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.internal.wc.admin;
013:
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.HashMap;
017: import java.util.Iterator;
018: import java.util.Map;
019:
020: import org.tmatesoft.svn.core.SVNErrorCode;
021: import org.tmatesoft.svn.core.SVNErrorMessage;
022: import org.tmatesoft.svn.core.SVNException;
023: import org.tmatesoft.svn.core.SVNProperty;
024: import org.tmatesoft.svn.core.internal.wc.SVNErrorManager;
025: import org.tmatesoft.svn.core.wc.SVNStatusType;
026:
027: /**
028: * @version 1.1.1
029: * @author TMate Software Ltd.
030: */
031: public abstract class SVNLog {
032: public static final String DELETE_ENTRY = "delete-entry";
033:
034: public static final String MODIFY_ENTRY = "modify-entry";
035:
036: public static final String MODIFY_WC_PROPERTY = "modify-wcprop";
037:
038: public static final String DELETE_LOCK = "delete-lock";
039:
040: public static final String MOVE = "mv";
041:
042: public static final String APPEND = "append";
043:
044: public static final String DELETE = "rm";
045:
046: public static final String READONLY = "readonly";
047:
048: public static final String COPY_AND_TRANSLATE = "cp-and-translate";
049:
050: public static final String COPY_AND_DETRANSLATE = "cp-and-detranslate";
051:
052: public static final String COPY = "cp";
053:
054: public static final String MERGE = "merge";
055:
056: public static final String MAYBE_READONLY = "maybe-readonly";
057:
058: public static final String SET_TIMESTAMP = "set-timestamp";
059:
060: public static final String COMMIT = "committed";
061:
062: public static final String UPGRADE_FORMAT = "upgrade-format";
063:
064: public static final String NAME_ATTR = "name";
065:
066: public static final String PROPERTY_NAME_ATTR = "propname";
067:
068: public static final String PROPERTY_VALUE_ATTR = "propval";
069:
070: public static final String DEST_ATTR = "dest";
071:
072: public static final String TIMESTAMP_ATTR = "timestamp";
073:
074: public static final String REVISION_ATTR = "revision";
075:
076: public static final String FORMAT_ATTR = "format";
077:
078: public static final String ATTR1 = "arg1";
079: public static final String ATTR2 = "arg2";
080: public static final String ATTR3 = "arg3";
081: public static final String ATTR4 = "arg4";
082: public static final String ATTR5 = "arg5";
083: public static final String ATTR6 = "arg6";
084:
085: public static final String WC_TIMESTAMP = "working";
086:
087: protected Collection myCache;
088: protected SVNAdminArea myAdminArea;
089:
090: public abstract void save() throws SVNException;
091:
092: public abstract String toString();
093:
094: public abstract void delete() throws SVNException;
095:
096: public abstract boolean exists();
097:
098: protected SVNLog(SVNAdminArea adminArea) {
099: myAdminArea = adminArea;
100: }
101:
102: public void addCommand(String name, Map attributes, boolean save)
103: throws SVNException {
104: if (myCache == null) {
105: myCache = new ArrayList();
106: }
107: attributes = new HashMap(attributes);
108: attributes.put("", name);
109: myCache.add(attributes);
110: if (save) {
111: save();
112: }
113: }
114:
115: public SVNStatusType logChangedEntryProperties(String name,
116: Map modifiedEntryProps) throws SVNException {
117: SVNStatusType status = SVNStatusType.LOCK_UNCHANGED;
118: if (modifiedEntryProps != null) {
119: Map command = new HashMap();
120: command.put(SVNLog.NAME_ATTR, name);
121: for (Iterator names = modifiedEntryProps.keySet()
122: .iterator(); names.hasNext();) {
123: String propName = (String) names.next();
124: String propValue = (String) modifiedEntryProps
125: .get(propName);
126: String longPropName = propName;
127: if (!(SVNProperty.CACHABLE_PROPS.equals(propName)
128: || SVNProperty.PRESENT_PROPS.equals(propName)
129: || SVNProperty.HAS_PROPS.equals(propName) || SVNProperty.HAS_PROP_MODS
130: .equals(propName))) {
131: longPropName = SVNProperty.SVN_ENTRY_PREFIX
132: + propName;
133: }
134: if (SVNProperty.LOCK_TOKEN.equals(longPropName)) {
135: Map deleteLockCommand = new HashMap();
136: deleteLockCommand.put(SVNLog.NAME_ATTR, name);
137: addCommand(SVNLog.DELETE_LOCK, deleteLockCommand,
138: false);
139: status = SVNStatusType.LOCK_UNLOCKED;
140: } else if (propValue != null) {
141: command.put(propName, propValue);
142: }
143: }
144: addCommand(SVNLog.MODIFY_ENTRY, command, false);
145: command.clear();
146: }
147: return status;
148: }
149:
150: public void logChangedWCProperties(String name, Map modifiedWCProps)
151: throws SVNException {
152: if (modifiedWCProps != null) {
153: Map command = new HashMap();
154: command.put(SVNLog.NAME_ATTR, name);
155: for (Iterator names = modifiedWCProps.keySet().iterator(); names
156: .hasNext();) {
157: String propName = (String) names.next();
158: String propValue = (String) modifiedWCProps
159: .get(propName);
160: command.put(SVNLog.PROPERTY_NAME_ATTR, propName);
161: if (propValue != null) {
162: command.put(SVNLog.PROPERTY_VALUE_ATTR, propValue);
163: } else {
164: command.remove(SVNLog.PROPERTY_VALUE_ATTR);
165: }
166: addCommand(SVNLog.MODIFY_WC_PROPERTY, command, false);
167: command.clear();
168: }
169: }
170: }
171:
172: public void run(SVNLogRunner runner) throws SVNException {
173: Collection commands = readCommands();
174: if (commands == null || commands.isEmpty()) {
175: return;
176: }
177:
178: try {
179: int count = 0;
180: for (Iterator cmds = commands.iterator(); cmds.hasNext();) {
181: Map command = (Map) cmds.next();
182: String name = (String) command.get("");
183: String attrName = (String) command
184: .get(SVNLog.NAME_ATTR);
185: if (attrName == null
186: && !SVNLog.UPGRADE_FORMAT.equals(name)) {
187: SVNErrorCode code = count <= 1 ? SVNErrorCode.WC_BAD_ADM_LOG_START
188: : SVNErrorCode.WC_BAD_ADM_LOG;
189: SVNErrorMessage err = SVNErrorMessage
190: .create(
191: code,
192: "Log entry missing 'name' attribute (entry ''{0}'' for directory ''{1}'')",
193: new Object[] { name,
194: myAdminArea.getRoot() });
195: SVNErrorManager.error(err);
196: }
197: if (runner != null) {
198: runner.runCommand(myAdminArea, name, command,
199: ++count);
200: }
201: cmds.remove();
202: }
203: } catch (SVNException e) {
204: // save failed command and unexecuted commands back to the log file.
205: myCache = null;
206: for (Iterator cmds = commands.iterator(); cmds.hasNext();) {
207: Map command = (Map) cmds.next();
208: String name = (String) command.remove("");
209: addCommand(name, command, false);
210: }
211: save();
212: throw e;
213: }
214: }
215:
216: protected abstract Collection readCommands() throws SVNException;
217:
218: }
|