KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > ant > taskdefs > Copy


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/ant/src/java/org/apache/webdav/ant/taskdefs/Copy.java,v 1.2.2.1 2004/08/15 12:57:17 luetzkendorf Exp $
3  * $Revision: 1.2.2.1 $
4  * $Date: 2004/08/15 12:57:17 $
5  * ========================================================================
6  * Copyright 2004 The Apache Software Foundation
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================================================================
20  */

21 package org.apache.webdav.ant.taskdefs;
22
23 import java.io.IOException JavaDoc;
24
25 import org.apache.commons.httpclient.HttpURL;
26 import org.apache.commons.httpclient.URIException;
27
28 import org.apache.tools.ant.BuildException;
29 import org.apache.webdav.ant.Utils;
30 import org.apache.webdav.lib.methods.DepthSupport;
31
32 /**
33  * WebDAV task for copying resources and collections.
34  *
35  * @see <a HREF="../doc-files/tasks.htm#davcopy">Task documentation</a>
36  */

37 public class Copy extends WebdavMatchingTask {
38
39     private String JavaDoc destination;
40     private int depth = DepthSupport.DEPTH_INFINITY;
41     private boolean overwrite = false;
42     private HttpURL destinationURL;
43
44     /*
45      * @see org.apache.tools.ant.Task#execute()
46      */

47     public void execute() throws BuildException {
48         validate();
49         try {
50             // TODO assure that the colletion to which we copy does exist
51
log("Copying " + getUrl(), ifVerbose());
52             Utils.copyResource(
53                 getHttpClient(),
54                 getUrl(),
55                 this.destinationURL.getURI(),
56                 this.depth,
57                 this.overwrite
58             );
59         }
60         catch (IOException JavaDoc e) {
61             throw Utils.makeBuildException("Can't copy!", e);
62         }
63     }
64
65     public void setDestination(String JavaDoc destination) {
66         this.destination = destination;
67     }
68
69     public void setDepth(String JavaDoc value) {
70         if ("0".trim().equals(value)) {
71            this.depth = DepthSupport.DEPTH_0;
72         }
73         else if ("infinity".trim().toLowerCase().equals(value)) {
74            this.depth = DepthSupport.DEPTH_INFINITY;
75         }
76         else {
77             throw new BuildException("Invalid value of depth attribute."
78                  + " (One of '0' or 'infinity' expected)");
79         }
80     }
81
82     public void setOverwrite(boolean value) {
83         this.overwrite = value;
84     }
85
86     protected void validate() {
87         super.validate();
88         if (destination == null) {
89             throw new BuildException("Missing required attribute destination");
90         }
91         
92         try {
93             this.destinationURL = Utils.createHttpURL(getUrl(), this.destination);
94             this.destinationURL.setPath(removeDoubleSlashes(
95                     this.destinationURL.getPath()));
96         } catch (URIException e) {
97             throw new BuildException("Invalid destination uri!", e);
98         }
99     }
100
101 }
102
Popular Tags