KickJava   Java API By Example, From Geeks To Geeks.

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


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: DeleteContentTask.java 160149 2005-04-05 09:51:54Z michi $ */
19
20 package org.apache.lenya.cms.ant;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import org.apache.avalon.excalibur.io.FileUtil;
26 import org.apache.lenya.cms.publication.Document;
27 import org.apache.lenya.cms.publication.DocumentBuildException;
28 import org.apache.lenya.cms.publication.DocumentBuilder;
29 import org.apache.lenya.cms.publication.Label;
30 import org.apache.lenya.cms.publication.Publication;
31 import org.apache.lenya.cms.publication.SiteTree;
32 import org.apache.lenya.cms.publication.SiteTreeNode;
33 import org.apache.tools.ant.BuildException;
34
35 /**
36  * Ant task to delete the contents (xml files) of documents corresponding to a defined subtree
37  * Visitor of the defined subtree (visitor pattern). The subtree is reverse visited.
38  */

39 public class DeleteContentTask extends TwoDocumentsOperationTask {
40
41     /**
42      *
43      */

44     public DeleteContentTask() {
45         super();
46     }
47
48     /** (non-Javadoc)
49      * @see org.apache.lenya.cms.publication.SiteTreeNodeVisitor#visitSiteTreeNode(org.apache.lenya.cms.publication.SiteTreeNode)
50      */

51     public void visitSiteTreeNode(SiteTreeNode node) {
52         Publication publication = getPublication();
53         DocumentBuilder builder = publication.getDocumentBuilder();
54
55         String JavaDoc destDocumentid = node.getAbsoluteId();
56         String JavaDoc srcDocumentid =
57             destDocumentid.replaceFirst(
58                 getSecdocumentid(),
59                 getFirstdocumentid());
60
61         Label[] labels = node.getLabels();
62         for (int i = 0; i < labels.length; i++) {
63             String JavaDoc language = labels[i].getLanguage();
64             String JavaDoc url =
65                 builder.buildCanonicalUrl(
66                     publication,
67                     getFirstarea(),
68                     srcDocumentid,
69                     language);
70             Document doc;
71             try {
72                 doc = builder.buildDocument(publication, url);
73             } catch (DocumentBuildException e) {
74                 throw new BuildException(e);
75             }
76             File JavaDoc srcFile = doc.getFile();
77             if (!srcFile.exists()) {
78                 log("There are no file " + srcFile.getAbsolutePath());
79                 return;
80             }
81             File JavaDoc directory = srcFile.getParentFile();
82             try {
83                 FileUtil.forceDelete(srcFile);
84             } catch (IOException JavaDoc e) {
85                 //FIXME: catch Exception because of window's delete problem
86
log("exception " + e);
87             }
88             if (directory.exists()
89                 && directory.isDirectory()
90                 && directory.listFiles().length == 0) {
91                 try {
92                     FileUtil.forceDelete(directory);
93                 } catch (IOException JavaDoc e) {
94                     //FIXME: catch Exception because of window's delete problem
95
log("exception " + e);
96                 }
97             }
98         }
99
100     }
101
102     /**
103      * @see org.apache.tools.ant.Task#execute()
104      **/

105     public void execute() throws BuildException {
106         try {
107             log("document-id for the source :" + this.getFirstdocumentid());
108             log("area for the source :" + this.getFirstarea());
109             log("document-id for the destination :" + this.getSecdocumentid());
110             log("area for the destination :" + this.getSecarea());
111
112             Publication publication = getPublication();
113             SiteTree tree = publication.getTree(this.getSecarea());
114             SiteTreeNode node = tree.getNode(this.getSecdocumentid());
115             node.acceptReverseSubtree(this);
116         } catch (Exception JavaDoc e) {
117             throw new BuildException(e);
118         }
119     }
120 }
121
Popular Tags