1 17 18 19 20 package org.apache.lenya.cms.ant; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.List ; 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 42 public class CopyResourcesTask extends TwoDocumentsOperationTask { 43 44 47 public CopyResourcesTask() { 48 super(); 49 } 50 51 56 public void visitSiteTreeNode(SiteTreeNode node) { 57 Publication publication = getPublication(); 58 DocumentBuilder builder = publication.getDocumentBuilder(); 59 60 String srcDocumentid = node.getAbsoluteId(); 61 String destDocumentid = 62 srcDocumentid.replaceFirst( 63 getFirstdocumentid(), 64 getSecdocumentid()); 65 66 Label[] labels = node.getLabels(); 67 68 71 String language = labels[0].getLanguage(); 72 String 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 resources = new ArrayList (Arrays.asList(resourcesMgr.getResources())); 86 resources.addAll(Arrays.asList(resourcesMgr.getMetaFiles())); 87 File [] srcFiles = 88 (File []) resources.toArray(new File [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 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 e) { 123 throw new BuildException(e); 124 } 125 } 126 127 } 128 129 } 130 | Popular Tags |