KickJava   Java API By Example, From Geeks To Geeks.

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


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: CopyResourcesTask.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.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.avalon.excalibur.io.FileUtil;
29 import org.apache.lenya.cms.publication.Document;
30 import org.apache.lenya.cms.publication.DocumentBuildException;
31 import org.apache.lenya.cms.publication.DocumentBuilder;
32 import org.apache.lenya.cms.publication.Label;
33 import org.apache.lenya.cms.publication.Publication;
34 import org.apache.lenya.cms.publication.ResourcesManager;
35 import org.apache.lenya.cms.publication.SiteTreeNode;
36 import org.apache.tools.ant.BuildException;
37
38 /**
39  * Ant task, which implements the SiteTreeNodeVisitor for the operation copy the resources.
40  * (Visitor pattern)
41  */

42 public class CopyResourcesTask extends TwoDocumentsOperationTask {
43
44     /**
45      *
46      */

47     public CopyResourcesTask() {
48         super();
49     }
50
51     /**
52      * Copy the resources files belongs to the documents corresponding to this node
53      *
54      * @see org.apache.lenya.cms.publication.SiteTreeNodeVisitor#visitSiteTreeNode(org.apache.lenya.cms.publication.SiteTreeNode)
55      */

56     public void visitSiteTreeNode(SiteTreeNode node) {
57         Publication publication = getPublication();
58         DocumentBuilder builder = publication.getDocumentBuilder();
59
60         String JavaDoc srcDocumentid = node.getAbsoluteId();
61         String JavaDoc destDocumentid =
62             srcDocumentid.replaceFirst(
63                 getFirstdocumentid(),
64                 getSecdocumentid());
65
66         Label[] labels = node.getLabels();
67
68         // FIXME: if the resources differ for different languages, so iterate
69
// on all languages
70

71         String JavaDoc language = labels[0].getLanguage();
72         String JavaDoc srcUrl =
73             builder.buildCanonicalUrl(
74                 publication,
75                 getFirstarea(),
76                 srcDocumentid,
77                 language);
78         Document srcDoc;
79         try {
80             srcDoc = builder.buildDocument(publication, srcUrl);
81         } catch (DocumentBuildException e) {
82             throw new BuildException(e);
83         }
84         ResourcesManager resourcesMgr = new ResourcesManager(srcDoc);
85         List JavaDoc resources = new ArrayList JavaDoc(Arrays.asList(resourcesMgr.getResources()));
86         resources.addAll(Arrays.asList(resourcesMgr.getMetaFiles()));
87         File JavaDoc[] srcFiles =
88             (File JavaDoc[]) resources.toArray(new File JavaDoc[resources.size()]);
89
90         if (srcFiles == null) {
91             log(
92                 "There are no resources for the document "
93                     + getFirstdocumentid());
94             return;
95         }
96
97         String JavaDoc destUrl =
98             builder.buildCanonicalUrl(
99                 publication,
100                 getSecarea(),
101                 destDocumentid,
102                 language);
103         Document destDoc;
104         try {
105             destDoc = builder.buildDocument(publication, destUrl);
106         } catch (DocumentBuildException e) {
107             throw new BuildException(e);
108         }
109         resourcesMgr = new ResourcesManager(destDoc);
110
111         for (int i = 0; i < srcFiles.length; i++) {
112
113             try {
114                 log(
115                     "copy file "
116                         + srcFiles[i].getAbsolutePath()
117                         + "to file "
118                         + resourcesMgr.getPath().getCanonicalPath());
119                 FileUtil.copyFileToDirectory(
120                     srcFiles[i],
121                     resourcesMgr.getPath());
122             } catch (IOException JavaDoc e) {
123                 throw new BuildException(e);
124             }
125         }
126
127     }
128
129 }
130
Popular Tags