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.io.File;
015:
016: import org.tmatesoft.svn.core.SVNErrorMessage;
017: import org.tmatesoft.svn.core.SVNLock;
018: import org.tmatesoft.svn.core.SVNNodeKind;
019: import org.tmatesoft.svn.core.internal.wc.admin.SVNAdminArea;
020: import org.tmatesoft.svn.core.internal.wc.admin.SVNAdminAreaInfo;
021:
022: /**
023: * The <b>SVNEvent</b> class is used to provide detailed information on
024: * an operation progress to the <b>ISVNEventHandler</b> (if any) registered
025: * for an <b>SVN</b>*<b>Client</b> object. Such events are generated by
026: * an operation invoked by do*() method of an <b>SVN</b>*<b>Client</b> object
027: * and passed to a developer's event handler for notification. Retrieving
028: * information out of an <b>SVNEvent</b> the developer can decide how it
029: * should be interpreted.
030: *
031: * <p>
032: * This is an example:<br />
033: * implementing <b>ISVNEventHandler</b>
034: * <pre class="javacode">
035: * <span class="javakeyword">import</span> org.tmatesoft.svn.core.wc.ISVNEventHandler;
036: * <span class="javakeyword">import</span> org.tmatesoft.svn.core.SVNCancelException;
037: * <span class="javakeyword">import</span> org.tmatesoft.svn.core.wc.SVNEvent;
038: * <span class="javakeyword">import</span> org.tmatesoft.svn.core.wc.SVNEventAction;
039: * <span class="javakeyword">import</span> org.tmatesoft.svn.core.wc.SVNStatusType;
040: * <span class="javakeyword">import</span> org.tmatesoft.svn.core.SVNNodeKind;
041: * <span class="javakeyword">import</span> java.io.File;
042: * ...
043: *
044: * <span class="javakeyword">public class</span> MyCustomUpdateEventHandler <span class="javakeyword">implements</span> ISVNEventHandler {
045: * <span class="javakeyword">public void</span> handleEvent(SVNEvent event, <span class="javakeyword">double</span> progress) {
046: * <span class="javacomment">//get the action type</span>
047: * <span class="javakeyword">if</span>(event.getAction() == SVNEventAction.UPDATE_UPDATE){
048: * <span class="javacomment">//get the item's node kind</span>
049: * SVNNodeKind kind = even.getNodeKind();
050: * <span class="javacomment">//get the item's contents status</span>
051: * <span class="javakeyword">if</span>(event.getContentsStatus() == SVNStatusType.CHANGED &&
052: * kind == SVNNodeKind.FILE){
053: * ...
054: * }
055: * ...
056: * <span class="javacomment">//get the item's properties status</span>
057: * <span class="javakeyword">if</span>(event.getPropertiesStatus() == SVNStatusType.MERGED){
058: * ...
059: * }
060: * <span class="javacomment">//get the item's lock status</span>
061: * <span class="javakeyword">if</span>(event.getLockStatus() == SVNStatusType.LOCK_UNLOCKED){
062: * ...
063: * }
064: * <span class="javacomment">//get the item's relative path</span>
065: * String path = event.getPath();
066: * <span class="javacomment">//or in a java.io.File representation</span>
067: * File fsEntry = event.getFile();
068: *
069: * <span class="javacomment">//get update revision</span>
070: * long revision = event.getRevision();
071: * ...
072: * }
073: * ...
074: * }
075: *
076: * <span class="javakeyword">public void</span> checkCancelled() <span class="javakeyword">throws</span> SVNCancelException{
077: * <span class="javakeyword">throw new</span> SVNCancelException(<span class="javastring">"cancelled!"</span>);
078: * }
079: * }</pre><br />
080: * then registering a handler:
081: * <pre class="javacode">
082: * <span class="javakeyword">import</span> org.tmatesoft.svn.core.wc.SVNUpdateClient;
083: * ...
084: *
085: * SVNUpdateClient updateClient;
086: * ...
087: * updateClient.setEventHandler(<span class="javakeyword">new</span> MyCustomUpdateEventHandler());
088: * ...</pre><br />
089: * now when invoking an update operation:
090: * <pre class="javacode">
091: * updateClient.doUpdate(...);</pre><br />
092: * the registered instance of the <b>ISVNEventHandler</b> implementation
093: * will be dispatched progress events.
094: * </p>
095: *
096: * @version 1.1.1
097: * @author TMate Software Ltd.
098: * @see ISVNEventHandler
099: * @see SVNStatusType
100: * @see SVNEventAction
101: * @see <a target="_top" href="http://svnkit.com/kb/examples/">Examples</a>
102: */
103: public class SVNEvent {
104:
105: private String myMimeType;
106: private SVNErrorMessage myErrorMessage;
107: private SVNEventAction myAction;
108: private SVNNodeKind myNodeKind;
109: private long myRevision;
110: private SVNStatusType myContentsStatus;
111: private SVNStatusType myPropertiesStatus;
112: private SVNStatusType myLockStatus;
113: private SVNLock myLock;
114: private SVNAdminAreaInfo myAdminAreaInfo;
115: private String myName;
116: private String myPath;
117: private File myRoot;
118: private File myRootFile;
119: private SVNEventAction myExpectedAction;
120:
121: /**
122: * Constructs an <b>SVNEvent</b> object given
123: * an error message for a filed operation.
124: * <p>
125: * Used by SVNKit internals to construct and initialize an
126: * <b>SVNEvent</b> object. It's not intended for users (from an API point of view).
127: *
128: * @param errorMessage the message describing the operation fault
129: */
130: public SVNEvent(SVNErrorMessage errorMessage) {
131: myErrorMessage = errorMessage;
132: }
133:
134: /**
135: * Constructs an <b>SVNEvent</b> object.
136: * <p>
137: * Used by SVNKit internals to construct and initialize an
138: * <b>SVNEvent</b> object. It's not intended for users (from an API point of view).
139: *
140: * @param info admin info
141: * @param adminArea admin area the item belongs to
142: * @param name the item's name
143: * @param action the type of action the item is exposed to
144: * @param expectedAction the action type that was expected
145: * @param kind the item's node kind
146: * @param revision a revision number
147: * @param mimetype the item's MIME type
148: * @param cstatus the item's contents status
149: * @param pstatus the item's properties status
150: * @param lstatus the item's lock status
151: * @param lock the item's lock
152: * @param error an error message
153: */
154: public SVNEvent(SVNAdminAreaInfo info, SVNAdminArea adminArea,
155: String name, SVNEventAction action,
156: SVNEventAction expectedAction, SVNNodeKind kind,
157: long revision, String mimetype, SVNStatusType cstatus,
158: SVNStatusType pstatus, SVNStatusType lstatus, SVNLock lock,
159: SVNErrorMessage error) {
160: myMimeType = mimetype;
161: myErrorMessage = error;
162: myExpectedAction = expectedAction != null ? expectedAction
163: : action;
164: myAction = action;
165: myNodeKind = kind == null ? SVNNodeKind.UNKNOWN : kind;
166: myRevision = revision;
167: myContentsStatus = cstatus == null ? SVNStatusType.INAPPLICABLE
168: : cstatus;
169: myPropertiesStatus = pstatus == null ? SVNStatusType.INAPPLICABLE
170: : pstatus;
171: myLockStatus = lstatus == null ? SVNStatusType.INAPPLICABLE
172: : lstatus;
173: myLock = lock;
174: myAdminAreaInfo = info;
175: myRoot = adminArea != null ? adminArea.getRoot() : null;
176: myName = name;
177: }
178:
179: /**
180: * Constructs an <b>SVNEvent</b> object.
181: * <p>
182: * Used by SVNKit internals to construct and initialize an
183: * <b>SVNEvent</b> object. It's not intended for users (from an API point of view).
184: *
185: * @param info admin info
186: * @param adminArea admin area the item belongs to
187: * @param name the item's name
188: * @param action the type of action the item is exposed to
189: * @param kind the item's node kind
190: * @param revision a revision number
191: * @param mimetype the item's MIME type
192: * @param cstatus the item's contents status
193: * @param pstatus the item's properties status
194: * @param lstatus the item's lock status
195: * @param lock the item's lock
196: * @param error an error message
197: */
198: public SVNEvent(SVNAdminAreaInfo info, SVNAdminArea adminArea,
199: String name, SVNEventAction action, SVNNodeKind kind,
200: long revision, String mimetype, SVNStatusType cstatus,
201: SVNStatusType pstatus, SVNStatusType lstatus, SVNLock lock,
202: SVNErrorMessage error) {
203: this (info, adminArea, name, action, null, kind, revision,
204: mimetype, cstatus, pstatus, lstatus, lock, error);
205: }
206:
207: /**
208: * Constructs an <b>SVNEvent</b> object filling it with informational
209: * details most of that would be retrieved and analized by an
210: * <b>ISVNEventHandler</b> implementation.
211: *
212: * <p>
213: * Used by SVNKit internals to construct and initialize an
214: * <b>SVNEvent</b> object. It's not intended for users (from an API point of view).
215: *
216: * <p>
217: * If <code>action</code> is {@link SVNEventAction#SKIP} (i.e. operation is skipped)
218: * then the expected action (that would have occurred if the operation hadn't been skipped)
219: * is provided in <code>expected</code>.
220: *
221: * @param rootFile the item's root directory
222: * @param file the item's path itself
223: * @param action the type of action the item is exposed to
224: * @param expected the action that is expected to happen, but may
225: * be skipped in real for some reason
226: * @param kind the item's node kind
227: * @param revision a revision number
228: * @param mimetype the item's MIME type
229: * @param cstatus the item's contents status
230: * @param pstatus the item's properties status
231: * @param lstatus the item's lock status
232: * @param lock the item's lock
233: * @param error an error message
234: */
235: public SVNEvent(File rootFile, File file, SVNEventAction action,
236: SVNEventAction expected, SVNNodeKind kind, long revision,
237: String mimetype, SVNStatusType cstatus,
238: SVNStatusType pstatus, SVNStatusType lstatus, SVNLock lock,
239: SVNErrorMessage error) {
240: myMimeType = mimetype;
241: myExpectedAction = expected != null ? expected : action;
242: myErrorMessage = error;
243: myAction = action;
244: myNodeKind = kind == null ? SVNNodeKind.UNKNOWN : kind;
245: myRevision = revision;
246: myContentsStatus = cstatus == null ? SVNStatusType.INAPPLICABLE
247: : cstatus;
248: myPropertiesStatus = pstatus == null ? SVNStatusType.INAPPLICABLE
249: : pstatus;
250: myLockStatus = lstatus == null ? SVNStatusType.INAPPLICABLE
251: : lstatus;
252: myLock = lock;
253:
254: myRoot = file != null ? file.getParentFile() : null;
255: myRootFile = rootFile;
256: myName = file != null ? file.getName() : "";
257: }
258:
259: /**
260: * Constructs an <b>SVNEvent</b> object filling it with informational
261: * details most of that would be retrieved and analized by an
262: * <b>ISVNEventHandler</b> implementation.
263: *
264: * <p>
265: * Used by SVNKit internals to construct and initialize an
266: * <b>SVNEvent</b> object. It's not intended for users (from an API point of view).
267: *
268: * @param rootFile the item's root directory
269: * @param file the item's path itself
270: * @param action the type of action the item is exposed to
271: * @param kind the item's node kind
272: * @param revision a revision number
273: * @param mimetype the item's MIME type
274: * @param cstatus the item's contents status
275: * @param pstatus the item's properties status
276: * @param lstatus the item's lock status
277: * @param lock the item's lock
278: * @param error an error message
279: */
280: public SVNEvent(File rootFile, File file, SVNEventAction action,
281: SVNNodeKind kind, long revision, String mimetype,
282: SVNStatusType cstatus, SVNStatusType pstatus,
283: SVNStatusType lstatus, SVNLock lock, SVNErrorMessage error) {
284: this (rootFile, file, action, null, kind, revision, mimetype,
285: cstatus, pstatus, lstatus, lock, error);
286: }
287:
288: /**
289: * Gets the item's path relative to the Working Copy root directory.
290: *
291: * @return a string representation of the item's path
292: */
293: public String getPath() {
294: if (myPath != null) {
295: return myPath;
296: }
297: if (myAdminAreaInfo == null && myRootFile == null) {
298: return myName;
299: }
300: File file = getFile();
301: File root = myAdminAreaInfo != null ? myAdminAreaInfo
302: .getAnchor().getRoot() : myRootFile;
303: String rootPath = root.getAbsolutePath().replace(
304: File.separatorChar, '/');
305: String filePath = file.getAbsolutePath().replace(
306: File.separatorChar, '/');
307: myPath = filePath.substring(rootPath.length());
308: if (myPath.startsWith("/")) {
309: myPath = myPath.substring(1);
310: }
311: return myPath;
312: }
313:
314: /**
315: * Gets a java.io.File representation of the item's path.
316: *
317: * @return the item's path
318: */
319: public File getFile() {
320: if (myRoot != null) {
321: return ("".equals(myName) || ".".equals(myName)) ? myRoot
322: : new File(myRoot, myName);
323: } else if (myAdminAreaInfo != null && getPath() != null) {
324: return new File(myAdminAreaInfo.getAnchor().getRoot(),
325: getPath());
326: }
327: return null;
328: }
329:
330: /**
331: * Gets the type of an action performed upon the item. An action is
332: * one of predefined <b>SVNEventAction</b> constants that are specific for
333: * each kind of operation, such as update actions, commit actions, etc.
334: *
335: * @return the current action
336: */
337: public SVNEventAction getAction() {
338: return myAction;
339: }
340:
341: /**
342: * Returns the expected action. It is always the same as
343: * the action returned by {@link #getAction()} except those cases
344: * when {@link #getAction()} returns {@link SVNEventAction#SKIP} (i.e.
345: * when the expected operation is skipped).
346: *
347: * @return the expected action
348: */
349: public SVNEventAction getExpectedAction() {
350: return myExpectedAction;
351: }
352:
353: /**
354: * Gets the status type of either file or directory contents.
355: * Use predefined <b>SVNStatusType</b> constants to examine the
356: * item's status. For a directory contents are its entries.
357: *
358: * @return the item's status type
359: */
360: public SVNStatusType getContentsStatus() {
361: return myContentsStatus;
362: }
363:
364: /**
365: * Gets the error message that (if it's an error situation and
366: * therefore the string is not <span class="javakeyword">null</span>)
367: * points to some fault.
368: *
369: * @return an error message (in case of an error occured) or
370: * <span class="javakeyword">null</span> if everything
371: * is OK
372: */
373: public SVNErrorMessage getErrorMessage() {
374: return myErrorMessage;
375: }
376:
377: public void setErrorMessage(SVNErrorMessage errorMessage) {
378: myErrorMessage = errorMessage;
379: }
380:
381: /**
382: * Gets the file item's lock information (if any) represented by an
383: * <b>SVNLock</b> object.
384: *
385: * @return the file item's lock info if the file is locked; otherwise
386: * <span class="javakeyword">null</span>
387: */
388: public SVNLock getLock() {
389: return myLock;
390: }
391:
392: /**
393: * Gets the file item's lock status. The value of
394: * <b>SVNStatusType.<i>LOCK_INAPPLICABLE</i></b> means
395: * the lock status is irrelevant during the current event action.
396: *
397: * @return the lock status of the file item
398: */
399: public SVNStatusType getLockStatus() {
400: return myLockStatus;
401: }
402:
403: /**
404: * Gets the MIME type of the item relying upon the special
405: * SVN's <i>'svn:mime-type'</i> property.
406: *
407: * <p>
408: * You can use {@link org.tmatesoft.svn.core.SVNProperty}'s metods to
409: * find out whether it's a text MIME type or a binary:
410: * <pre class="javacode">
411: * <span class="javakeyword">import</span> org.tmatesoft.svn.core.SVNProperty;
412: * ...
413: *
414: * String mimeType = event.getMimeType();
415: * <span class="javakeyword">if</span>(SVNProperty.isBinaryMimeType(mimeType)){
416: * <span class="javacomment">//your processing</span>
417: * }</pre>
418: *
419: * @return the item's MIME type as a string or
420: * <span class="javakeyword">null</span> if the item has no
421: * <i>'svn:mime-type'</i> property set
422: */
423: public String getMimeType() {
424: return myMimeType;
425: }
426:
427: /**
428: * Gets the node kind of the item characterizing it as an entry -
429: * whether it's a directory, file, etc. The value of
430: * <b>SVNNodeKind.<i>NONE</i></b> may mean the node kind is
431: * inapplicable diring the current event action. The value of
432: * <b>SVNNodeKind.<i>UNKNOWN</i></b> may mean deleted entries.
433: *
434: * @return the item's node kind
435: */
436: public SVNNodeKind getNodeKind() {
437: return myNodeKind;
438: }
439:
440: /**
441: * Gets the status type of the item's properties.
442: * The value of <b>SVNStatusType.<i>INAPPLICABLE</i></b> may mean
443: * the item has no versioned properties or that properties status is
444: * irrelevant during the current event action.
445: *
446: * @return the status type of the item's properties
447: */
448: public SVNStatusType getPropertiesStatus() {
449: return myPropertiesStatus;
450: }
451:
452: /**
453: * Gets the revision number specific for the action context.
454: * It may be whether an update revision or a committed one or
455: * an inapplicable value when a revision number is irrelevant during
456: * the event action.
457: *
458: * @return a revision number
459: */
460: public long getRevision() {
461: return myRevision;
462: }
463:
464: /**
465: * Sets the item's path relative to the Working Copy root.
466: *
467: * @param path the item's relative path
468: */
469: public void setPath(String path) {
470: myPath = path;
471: }
472: }
|