KickJava   Java API By Example, From Geeks To Geeks.

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


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 // Kelp imports
26
import org.enhydra.kelp.common.AbstractEchoBuilder;
27 import org.enhydra.kelp.common.PathUtil;
28 import org.enhydra.kelp.common.event.WriteListener;
29 import org.enhydra.kelp.common.node.OtterTemplateNode;
30 import org.enhydra.kelp.common.node.OtterFileNode;
31 import org.enhydra.kelp.common.node.OtterNode;
32 import org.enhydra.kelp.common.node.OtterNodeFactory;
33
34 // ToolBox imports
35
import org.enhydra.tool.common.FileUtil;
36 import org.enhydra.tool.common.PathHandle;
37 import org.enhydra.tool.common.Template;
38 import org.enhydra.tool.common.ToolException;
39 import org.enhydra.tool.configure.ConfigTool;
40
41 // Standard imports
42
import java.awt.Component JavaDoc;
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 InputBuilder extends AbstractEchoBuilder {
50     static ResourceBundle JavaDoc res =
51         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
52
private File JavaDoc[] builtTemplates = new File JavaDoc[0];
53     private File JavaDoc[] builtPassthrough = new File JavaDoc[0];
54     private OtterNode[] nodes = null;
55     private Component JavaDoc owner = null;
56
57     public InputBuilder(WriteListener listener) {
58         super(listener);
59     }
60
61     public File JavaDoc[] getBuiltTemplates() {
62         return builtTemplates;
63     }
64
65     public File JavaDoc[] getBuiltPassthrough() {
66         return builtPassthrough;
67     }
68
69     public void setNodes(OtterNode[] n) {
70         nodes = n;
71     }
72
73     public OtterNode[] getNodes() {
74         OtterNode[] n = new OtterTemplateNode[0];
75
76         if (nodes == null) {
77             n = getProject().getAllInput();
78         } else {
79             n = nodes;
80         }
81         return n;
82     }
83
84     protected void buildImpl() throws ToolException {
85         File JavaDoc[] out = new File JavaDoc[0];
86         ArrayList JavaDoc outList = new ArrayList JavaDoc();
87
88         if (isInputEnabled()) {
89             builtTemplates = makeTemplates();
90             if (!getProject().isDeployInputFilter()) {
91                 builtPassthrough = makePassthrough();
92             }
93             outList.addAll(Arrays.asList(builtTemplates));
94             outList.addAll(Arrays.asList(builtPassthrough));
95             out = (File JavaDoc[]) outList.toArray(out);
96             outList.clear();
97             addFilesToProject(out);
98         }
99     }
100
101     public void setOwner(Component JavaDoc c) {
102         owner = c;
103     }
104
105     public Component JavaDoc getOwner() {
106         return owner;
107     }
108
109     //
110
private boolean isInputEnabled() {
111         boolean enabled = false;
112
113         if (getNodes().length > 0) {
114             enabled = getNodes()[0].getProject().isDeployInput();
115         }
116         return enabled;
117     }
118
119     private void addFilesToProject(File JavaDoc[] files) {
120         OtterNodeFactory factory = null;
121
122         factory = getProject().getNodeFactory();
123         for (int i = 0; i < files.length; i++) {
124             if (!isFresh()) {
125                 break;
126             }
127             factory.importFile(getProject(), files[i]);
128         }
129
130     }
131
132     private File JavaDoc[] makePassthrough() throws ToolException {
133         File JavaDoc[] out = new File JavaDoc[0];
134         OtterFileNode[] pass = new OtterFileNode[0];
135         PathHandle outPath = null;
136         File JavaDoc inFile = null;
137
138         pass = PathUtil.getInputPassthrough(getProject());
139         for (int i = 0; i < pass.length; i++) {
140             if (pass[i].isSelected()) {
141                 outPath = getPassthroughPath(pass[i]);
142                 inFile = new File JavaDoc(pass[i].getFilePath());
143                 try {
144                     FileUtil.copy(inFile, outPath.getFile());
145                     echo(res.getString("Copying_input_to")
146                          + outPath.getPath());
147                 } catch (ToolException e) {
148                     echo(e);
149                     e.printStackTrace();
150                 }
151             }
152         }
153         return out;
154     }
155
156     private File JavaDoc[] makeTemplates() throws ToolException {
157         File JavaDoc[] output = new File JavaDoc[0];
158         ConfigTool tool = new ConfigTool();
159
160         tool.setOwner(getOwner());
161         tool.setEcho(isEcho());
162         tool.setEchoWriter(getEchoWriter());
163         tool.setTransformPath(true);
164         tool.setTemplates(getTemplates());
165         tool.setOverwrite(getProject().isDeployOverwrite());
166         tool.initReplacements(PathUtil.expandReplacementTable(getProject(),
167                 getProject().getReplacementTable()));
168         output = tool.createOutput();
169         return output;
170     }
171
172     private Template[] getTemplates() {
173         Template[] templates = new Template[0];
174         ArrayList JavaDoc list = new ArrayList JavaDoc();
175         OtterTemplateNode[] templateNodes = new OtterTemplateNode[0];
176
177         templateNodes = PathUtil.getInputTemplates(getProject());
178         for (int i = 0; i < templateNodes.length; i++) {
179             OtterTemplateNode cursor = null;
180             Template temp = null;
181             File JavaDoc outFile = null;
182             String JavaDoc outPath = new String JavaDoc();
183
184             cursor = templateNodes[i];
185             if (cursor.isSelected()) {
186                 temp = new Template(new File JavaDoc(cursor.getFilePath()),
187                                     getProject().getDeployInputPath());
188                 outPath = cursor.getOutputPath();
189                 outFile = new File JavaDoc(outPath);
190                 temp.setOutput(outFile);
191                 list.add(temp);
192             }
193             if (! isFresh()) {
194               break;
195             }
196         }
197         list.trimToSize();
198         templates = new Template[list.size()];
199         templates = (Template[]) list.toArray(templates);
200         list.clear();
201         return templates;
202     }
203
204     private PathHandle getPassthroughPath(OtterFileNode node) {
205         PathHandle inputPath = null;
206         PathHandle filePath = null;
207         PathHandle rootPath = null;
208         PathHandle passPath = null;
209         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
210         String JavaDoc suffix = new String JavaDoc();
211
212         inputPath =
213             PathHandle.createPathHandle(getProject().getDeployInputPath());
214         filePath = PathHandle.createPathHandle(node.getFilePath());
215         rootPath =
216             PathHandle.createPathHandle(getProject().getDeployRootPath());
217         buf.append(rootPath.getPath());
218         if (inputPath.parentOf(filePath)) {
219             suffix =
220                 filePath.getPath().substring(inputPath.getPath().length());
221         } else {
222             suffix = filePath.getFile().getName();
223         }
224         if ((suffix.length() == 0) || (suffix.charAt(0) != '/')) {
225             buf.append('/');
226         }
227         buf.append(suffix);
228         passPath = PathHandle.createPathHandle(buf.toString());
229         return passPath;
230     }
231
232 }
233
Popular Tags