KickJava   Java API By Example, From Geeks To Geeks.

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


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: CopyContentTask.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.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25
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.SiteTreeNode;
32 import org.apache.lenya.util.FileUtil;
33 import org.apache.tools.ant.BuildException;
34
35 /**
36  * Ant task to copy the contents (xml files) belonging to a subtree defined by a given document id and a given area.
37  * Visitor of the defined subtree (visitor pattern)
38  */

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

44     public CopyContentTask() {
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 srcDocumentid = node.getAbsoluteId();
56         String JavaDoc destDocumentid =
57             srcDocumentid.replaceFirst(
58                 getFirstdocumentid(),
59                 getSecdocumentid());
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 srcUrl =
65                 builder.buildCanonicalUrl(
66                     publication,
67                     getFirstarea(),
68                     srcDocumentid,
69                     language);
70             Document srcDoc;
71             try {
72                 srcDoc = builder.buildDocument(publication, srcUrl);
73             } catch (DocumentBuildException e) {
74                 throw new BuildException(e);
75             }
76             File JavaDoc srcFile = srcDoc.getFile();
77             if (!srcFile.exists()) {
78                 log("There are no file " + srcFile.getAbsolutePath());
79                 return;
80             }
81             String JavaDoc destUrl =
82                 builder.buildCanonicalUrl(
83                     publication,
84                     getSecarea(),
85                     destDocumentid,
86                     language);
87             Document destDoc;
88             try {
89                 destDoc = builder.buildDocument(publication, destUrl);
90             } catch (DocumentBuildException e) {
91                 throw new BuildException(e);
92             }
93             File JavaDoc destFile = destDoc.getFile();
94
95             log(
96                 "copy file "
97                     + srcFile.getAbsolutePath()
98                     + "to file "
99                     + destFile.getAbsolutePath());
100             try {
101                 FileUtil.copy(
102                     srcFile.getAbsolutePath(),
103                     destFile.getAbsolutePath());
104             } catch (FileNotFoundException JavaDoc e) {
105                 throw new BuildException(e);
106             } catch (IOException JavaDoc e) {
107                 throw new BuildException(e);
108             }
109         }
110
111     }
112 }
113
Popular Tags