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:
013: package org.tmatesoft.svn.cli.command;
014:
015: import java.io.ByteArrayOutputStream;
016: import java.io.File;
017: import java.io.FileInputStream;
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.PrintStream;
021:
022: import org.tmatesoft.svn.cli.SVNArgument;
023: import org.tmatesoft.svn.cli.SVNCommand;
024: import org.tmatesoft.svn.core.SVNErrorCode;
025: import org.tmatesoft.svn.core.SVNErrorMessage;
026: import org.tmatesoft.svn.core.SVNException;
027: import org.tmatesoft.svn.core.SVNURL;
028: import org.tmatesoft.svn.core.internal.util.SVNFormatUtil;
029: import org.tmatesoft.svn.core.wc.ISVNPropertyHandler;
030: import org.tmatesoft.svn.core.wc.SVNPropertyData;
031: import org.tmatesoft.svn.core.wc.SVNRevision;
032: import org.tmatesoft.svn.core.wc.SVNWCClient;
033:
034: /**
035: * @version 1.1.1
036: * @author TMate Software Ltd.
037: */
038: public class SVNPropsetCommand extends SVNCommand {
039:
040: public void run(InputStream in, PrintStream out, PrintStream err)
041: throws SVNException {
042: run(out, err);
043: }
044:
045: public final void run(final PrintStream out, PrintStream err)
046: throws SVNException {
047: final String propertyName = getCommandLine().getPathAt(0);
048: String propertyValue = getCommandLine().getPathAt(1);
049: final boolean recursive = getCommandLine().hasArgument(
050: SVNArgument.RECURSIVE);
051: boolean force = getCommandLine().hasArgument(SVNArgument.FORCE);
052: boolean revProps = getCommandLine().hasArgument(
053: SVNArgument.REV_PROP);
054:
055: int pathIndex = 2;
056: if (getCommandLine().hasArgument(SVNArgument.FILE)) {
057: File file = new File((String) getCommandLine()
058: .getArgumentValue(SVNArgument.FILE));
059: ByteArrayOutputStream os = new ByteArrayOutputStream();
060: FileInputStream is = null;
061: try {
062: is = new FileInputStream(file);
063: while (true) {
064: int r = is.read();
065: if (r < 0) {
066: break;
067: }
068: os.write(r);
069: }
070: } catch (IOException e) {
071: SVNErrorMessage msg = SVNErrorMessage.create(
072: SVNErrorCode.CL_ARG_PARSING_ERROR, e
073: .getLocalizedMessage());
074: throw new SVNException(msg, e);
075: } finally {
076: try {
077: os.close();
078: } catch (IOException e1) {
079: }
080: if (is != null) {
081: try {
082: is.close();
083: } catch (IOException e) {
084: }
085: }
086: }
087: propertyValue = os.toString();
088: pathIndex = 1;
089: }
090:
091: SVNWCClient wcClient = getClientManager().getWCClient();
092:
093: if (revProps) {
094: SVNRevision revision = SVNRevision.UNDEFINED;
095: if (getCommandLine().hasArgument(SVNArgument.REVISION)) {
096: revision = SVNRevision.parse((String) getCommandLine()
097: .getArgumentValue(SVNArgument.REVISION));
098: }
099: if (getCommandLine().hasURLs()) {
100: wcClient.doSetRevisionProperty(SVNURL
101: .parseURIEncoded(getCommandLine().getURL(0)),
102: revision, propertyName, propertyValue, force,
103: new ISVNPropertyHandler() {
104: public void handleProperty(File path,
105: SVNPropertyData property)
106: throws SVNException {
107: }
108:
109: public void handleProperty(SVNURL url,
110: SVNPropertyData property)
111: throws SVNException {
112: out
113: .println("property '"
114: + propertyName
115: + "' set on repository revision "
116: + url);
117: }
118:
119: public void handleProperty(long revision,
120: SVNPropertyData property)
121: throws SVNException {
122: }
123: });
124:
125: } else {
126: File tgt = new File(".");
127: if (getCommandLine().getPathCount() > 2) {
128: tgt = new File(getCommandLine().getPathAt(2));
129: }
130: wcClient.doSetRevisionProperty(tgt, revision,
131: propertyName, propertyValue, force,
132: new ISVNPropertyHandler() {
133: public void handleProperty(File path,
134: SVNPropertyData property)
135: throws SVNException {
136: }
137:
138: public void handleProperty(SVNURL url,
139: SVNPropertyData property)
140: throws SVNException {
141: out
142: .println("property '"
143: + propertyName
144: + "' set on repository revision "
145: + url);
146: }
147:
148: public void handleProperty(long revision,
149: SVNPropertyData property)
150: throws SVNException {
151: }
152: });
153: }
154:
155: } else {
156: for (int i = pathIndex; i < getCommandLine().getPathCount(); i++) {
157: final String absolutePath = getCommandLine().getPathAt(
158: i);
159: if (!recursive) {
160: wcClient.doSetProperty(new File(absolutePath),
161: propertyName, propertyValue, force,
162: recursive, new ISVNPropertyHandler() {
163: public void handleProperty(File path,
164: SVNPropertyData property)
165: throws SVNException {
166: out.println("property '"
167: + propertyName
168: + "' set on '"
169: + SVNFormatUtil
170: .formatPath(path)
171: + "'");
172: }
173:
174: public void handleProperty(SVNURL url,
175: SVNPropertyData property)
176: throws SVNException {
177: }
178:
179: public void handleProperty(
180: long revision,
181: SVNPropertyData property)
182: throws SVNException {
183: }
184:
185: });
186: } else {
187: final boolean wasSet[] = new boolean[] { false };
188: wcClient.doSetProperty(new File(absolutePath),
189: propertyName, propertyValue, force,
190: recursive, new ISVNPropertyHandler() {
191: public void handleProperty(File path,
192: SVNPropertyData property)
193: throws SVNException {
194: wasSet[0] = true;
195: }
196:
197: public void handleProperty(SVNURL url,
198: SVNPropertyData property)
199: throws SVNException {
200: }
201:
202: public void handleProperty(
203: long revision,
204: SVNPropertyData property)
205: throws SVNException {
206: }
207: });
208: if (wasSet[0]) {
209: out.println("property '" + propertyName
210: + "' set (recursively) on '"
211: + absolutePath + "'");
212: }
213: }
214: }
215: if (getCommandLine().getPathCount() == 2
216: && getCommandLine().hasURLs()) {
217: err.println("Propset is not supported for target '"
218: + getCommandLine().getURL(0) + "'");
219: System.exit(1);
220: }
221: }
222: }
223: }
|