KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > ant > CopyPoliciesTask


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: CopyPoliciesTask.java 42616 2004-03-03 12:56:33Z gregor $ */
19
20 package org.apache.lenya.cms.ant;
21
22 import java.io.File JavaDoc;
23 import java.io.FileFilter JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import org.apache.avalon.excalibur.io.FileUtil;
27 import org.apache.lenya.cms.publication.Publication;
28 import org.apache.lenya.cms.publication.SiteTreeNode;
29 import org.apache.tools.ant.BuildException;
30
31 /**
32  * Ant task to copy the policies of a document
33  */

34 public class CopyPoliciesTask extends TwoDocumentsOperationTask {
35     private String JavaDoc policiesDir;
36
37     /**
38      *
39      */

40     public CopyPoliciesTask() {
41         super();
42     }
43
44     /**
45      * @return string The policies directory.
46      */

47     public String JavaDoc getPoliciesDir() {
48         return policiesDir;
49     }
50     /**
51      * @param string The policies directory
52      */

53     public void setPoliciesDir(String JavaDoc string) {
54         policiesDir = string;
55     }
56
57     /**
58      * Get all files in a given directory, that are not directories.
59      * If the given directory doesn't exist, return null.
60      * @param directory The directory
61      * @return List of files
62      */

63     public File JavaDoc[] getFiles(File JavaDoc directory) {
64         FileFilter JavaDoc filter = new FileFilter JavaDoc() {
65
66             public boolean accept(File JavaDoc file) {
67                 return file.isFile();
68             }
69         };
70         if (directory.exists() && directory.isDirectory()) {
71             return directory.listFiles(filter);
72         }
73         return null;
74     }
75
76     /**
77      * Copies the policies file
78      * @param srcDir The source directory of the policies files.
79      * @param destDir The destination directory of the policies files.
80      */

81     public void copyPolicies(File JavaDoc srcDir, File JavaDoc destDir) {
82         File JavaDoc[] authoringPolicies = this.getFiles(srcDir);
83         if (authoringPolicies == null) {
84             return;
85         }
86         for (int i = 0; i < authoringPolicies.length; i++) {
87             String JavaDoc srcPath;
88             try {
89                 srcPath = authoringPolicies[i].getCanonicalPath();
90                 String JavaDoc policyPath = srcPath.substring(srcDir.getCanonicalPath().length());
91                 File JavaDoc destFile = new File JavaDoc(destDir, policyPath);
92                 FileUtil.copyFile(authoringPolicies[i], destFile);
93             } catch (IOException JavaDoc e) {
94                 throw new BuildException(e);
95             }
96         }
97     }
98
99     /** (non-Javadoc)
100      * @see org.apache.lenya.cms.ant.DocumentOperationTask#visitSiteTreeNode(org.apache.lenya.cms.publication.SiteTreeNode)
101      */

102     public void visitSiteTreeNode(SiteTreeNode node) {
103         String JavaDoc srcArea = this.getFirstarea();
104         String JavaDoc destArea = this.getSecarea();
105
106         String JavaDoc srcDocumentid = node.getAbsoluteId();
107         String JavaDoc destDocumentid =
108             srcDocumentid.replaceFirst(
109                 getFirstdocumentid(),
110                 getSecdocumentid());
111
112         if (srcArea.equals(Publication.AUTHORING_AREA)) {
113             if (destArea.equals(Publication.AUTHORING_AREA)) {
114                 File JavaDoc srcDir =
115                     new File JavaDoc(
116                         policiesDir,
117                         this.getFirstarea() + File.separator + srcDocumentid);
118                 File JavaDoc destDir =
119                     new File JavaDoc(
120                         policiesDir,
121                         this.getSecarea() + File.separator + destDocumentid);
122                 copyPolicies(srcDir, destDir);
123
124                 File JavaDoc srcLiveDir =
125                     new File JavaDoc(
126                         policiesDir,
127                         Publication.LIVE_AREA + File.separator + srcDocumentid);
128                 File JavaDoc destLiveDir =
129                     new File JavaDoc(
130                         policiesDir,
131                         Publication.LIVE_AREA
132                             + File.separator
133                             + destDocumentid);
134
135                 copyPolicies(srcLiveDir, destLiveDir);
136
137             } else if (
138                 destArea.equals(Publication.ARCHIVE_AREA)
139                     | destArea.equals(Publication.TRASH_AREA)) {
140                 File JavaDoc srcDir =
141                     new File JavaDoc(
142                         policiesDir,
143                         this.getFirstarea() + File.separator + srcDocumentid);
144                 File JavaDoc destDir =
145                     new File JavaDoc(
146                         policiesDir,
147                         this.getSecarea()
148                             + File.separator
149                             + this.getFirstarea()
150                             + File.separator
151                             + destDocumentid);
152                 copyPolicies(srcDir, destDir);
153
154                 File JavaDoc srcLiveDir =
155                     new File JavaDoc(
156                         policiesDir,
157                         Publication.LIVE_AREA + File.separator + srcDocumentid);
158                 File JavaDoc destLiveDir =
159                     new File JavaDoc(
160                         policiesDir,
161                         this.getSecarea()
162                             + File.separator
163                             + Publication.LIVE_AREA
164                             + File.separator
165                             + destDocumentid);
166
167                 copyPolicies(srcLiveDir, destLiveDir);
168
169             }
170         } else if (
171             srcArea.equals(Publication.ARCHIVE_AREA)
172                 | srcArea.equals(Publication.TRASH_AREA)) {
173             if (destArea.equals(Publication.AUTHORING_AREA)) {
174                 File JavaDoc srcDir =
175                     new File JavaDoc(
176                         policiesDir,
177                         this.getFirstarea()
178                             + File.separator
179                             + this.getSecarea()
180                             + File.separator
181                             + srcDocumentid);
182                 File JavaDoc destDir =
183                     new File JavaDoc(
184                         policiesDir,
185                         this.getSecarea() + File.separator + destDocumentid);
186                 copyPolicies(srcDir, destDir);
187
188                 File JavaDoc srcLiveDir =
189                     new File JavaDoc(
190                         policiesDir,
191                         this.getFirstarea()
192                             + File.separator
193                             + Publication.LIVE_AREA
194                             + File.separator
195                             + srcDocumentid);
196                 File JavaDoc destLiveDir =
197                     new File JavaDoc(
198                         policiesDir,
199                         Publication.LIVE_AREA
200                             + File.separator
201                             + destDocumentid);
202
203                 copyPolicies(srcLiveDir, destLiveDir);
204
205             }
206         }
207     }
208 }
209
Popular Tags