KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > deployer > ContentBuilder


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common.deployer;
24
25 // Toolbox imports
26
import org.enhydra.tool.common.ToolException;
27 import org.enhydra.tool.common.PathHandle;
28 import org.enhydra.tool.common.FileUtil;
29
30 // Kelp imports
31
import org.enhydra.kelp.common.AbstractEchoBuilder;
32 import org.enhydra.kelp.common.PathUtil;
33 import org.enhydra.kelp.common.event.WriteListener;
34 import org.enhydra.kelp.common.map.Mapper;
35 import org.enhydra.kelp.common.node.OtterImageFileNode;
36 import org.enhydra.kelp.common.node.OtterTextFileNode;
37 import org.enhydra.kelp.common.node.OtterDocumentNode;
38 import org.enhydra.kelp.common.node.OtterFileNode;
39 import org.enhydra.kelp.common.node.OtterNode;
40 import org.enhydra.kelp.common.node.OtterProject;
41
42 //
43
import java.io.File JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Arrays JavaDoc;
46 import java.util.ResourceBundle JavaDoc;
47
48 //
49
public class ContentBuilder extends AbstractEchoBuilder {
50     static ResourceBundle JavaDoc res =
51         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
52
private Mapper mapper = null;
53     private PathHandle resourcePath = null;
54     private String JavaDoc copyingTo = res.getString("Copying_to");
55
56     public ContentBuilder(WriteListener listener) {
57         super(listener);
58     }
59
60     protected void buildImpl() {
61         makeStaticDocuments();
62         makeNonDocumentContent();
63         makeDeploymentDesc();
64     }
65
66     public void setProject(OtterProject p) {
67         super.setProject(p);
68         mapper = new Mapper(getProject());
69         resourcePath =
70             PathHandle.createPathHandle(getProject().getDeployResourcePath());
71     }
72
73     private void makeStaticDocuments() {
74
75         // Find all images in project and copy using
76
// the mapping table.
77
OtterDocumentNode[] docs = getProject().getAllDocuments();
78         File JavaDoc source = null;
79         File JavaDoc dest = null;
80         String JavaDoc destPath = new String JavaDoc();
81
82         for (int i = 0; i < docs.length; i++) {
83             if (docs[i].isStatic()) {
84                 destPath = getResourceDestPath(docs[i]);
85                 dest = new File JavaDoc(destPath);
86                 source = new File JavaDoc(docs[i].getFilePath());
87                 try {
88                     FileUtil.copy(source, dest);
89                     echo(copyingTo + destPath);
90                 } catch (ToolException e) {
91                     echo(e);
92                     e.printStackTrace();
93                 }
94             }
95         }
96     }
97
98     /**
99      * Method declaration
100      *
101      */

102     private void makeNonDocumentContent() {
103
104         // Find all images in project and copy using
105
// the mapping table.
106
String JavaDoc[] types = new String JavaDoc[0];
107         OtterFileNode[] content = new OtterFileNode[0];
108         PathHandle cursorPath = null;
109
110         types = getProject().getNonDocumentContentTypes();
111         content = getProject().findFileNodesByType(types);
112         File JavaDoc source = null;
113         File JavaDoc dest = null;
114         String JavaDoc destPath = new String JavaDoc();
115
116         for (int i = 0; i < content.length; i++) {
117             cursorPath =
118                 PathHandle.createPathHandle(content[i].getFilePath());
119             if (resourcePath.parentOf(cursorPath)) {
120                 destPath = getResourceDestPath(content[i]);
121                 dest = new File JavaDoc(destPath);
122                 source = cursorPath.getFile();
123                 try {
124                     FileUtil.copy(source, dest);
125                     echo(copyingTo + destPath);
126                 } catch (ToolException e) {
127                     echo(e);
128                     e.printStackTrace();
129                 }
130             }
131         }
132     }
133
134     private void makeDeploymentDesc() {
135
136         // Find all images in project and copy using
137
// the mapping table.
138
OtterTextFileNode[] descs = getProject().getAllDeploymentDescs();
139         File JavaDoc source = null;
140         File JavaDoc dest = null;
141         String JavaDoc sourcePath = null;
142         String JavaDoc rawFile = null;
143         StringBuffer JavaDoc outPath = new StringBuffer JavaDoc();
144
145         for (int i = 0; i < descs.length; i++) {
146             source = new File JavaDoc(descs[i].getFilePath());
147             sourcePath = getProject().getSourcePathOf(descs[i]);
148             rawFile = (new File JavaDoc(descs[i].getFilePath())).getAbsolutePath();
149             outPath.append(PathUtil.getDeployContentPath(getProject()));
150             outPath.append(rawFile.substring(sourcePath.length()));
151             dest = new File JavaDoc(outPath.toString());
152             try {
153                 FileUtil.copy(source, dest);
154                 echo(copyingTo + PathHandle.createPathString(dest));
155             } catch (ToolException e) {
156                 echo(e);
157                 e.printStackTrace();
158             }
159         }
160     }
161
162     /**
163      * Method declaration
164      *
165      * @param resource
166      *
167      * @return
168      */

169     private String JavaDoc getResourceDestPath(OtterFileNode resource) {
170         StringBuffer JavaDoc destPath = new StringBuffer JavaDoc();
171         PathHandle sourceFilePath = null;
172         int type = OtterProject.TYPE_WEBAPP;
173
174         type = getProject().getDeployType();
175         sourceFilePath = PathHandle.createPathHandle(resource.getFilePath());
176         if (type == OtterProject.TYPE_EN3APP) {
177             destPath.append(mapper.getMappedOutputPath(resource));
178         } else {
179             String JavaDoc suffix = new String JavaDoc();
180
181             destPath.append(PathUtil.getDeployContentPath(getProject()));
182             suffix =
183                 sourceFilePath.getPath().substring(resourcePath.getPath().length());
184             if ((suffix.length() == 0) || (suffix.charAt(0) != '/')) {
185                 destPath.append('/');
186             }
187             destPath.append(suffix);
188         }
189         return PathHandle.createPathString(destPath.toString());
190     }
191
192 }
193
Popular Tags