KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > Backward


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  *
21  */

22 package org.enhydra.kelp.common;
23
24 // ToolBox imports
25
import org.enhydra.tool.ToolBoxInfo;
26 import org.enhydra.tool.common.PathHandle;
27 import org.enhydra.tool.common.FileUtil;
28 import org.enhydra.tool.common.ExtensionFilter;
29 import org.enhydra.tool.configure.ConfigTool;
30
31 // Kelp imports
32
import org.enhydra.kelp.common.Constants;
33 import org.enhydra.kelp.common.importer.JavaFilter;
34 import org.enhydra.kelp.common.node.OtterTemplateNode;
35 import org.enhydra.kelp.common.node.OtterProject;
36
37 // Standard imports
38
import java.io.BufferedReader JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.FileFilter JavaDoc;
42 import java.io.FileNotFoundException JavaDoc;
43 import java.io.FileReader JavaDoc;
44 import java.util.Vector JavaDoc;
45
46 //
47
public class Backward {
48
49     // strings not to be resourced
50
private final static String JavaDoc RELATIVE_CLASSES = "= ../classes";
51
52     //
53
private Vector JavaDoc phVector = new Vector JavaDoc();
54
55     public static boolean createIsWebApp(String JavaDoc[] sourcePaths) {
56         boolean webApp = true;
57         PresentationFilter filter = new PresentationFilter();
58
59         for (int i = 0; i < sourcePaths.length; i++) {
60             File JavaDoc found = FileUtil.findFirst(filter, sourcePaths[i]);
61
62             if (found != null) {
63                 webApp = filter.isWebApp();
64                 break;
65             }
66         }
67         return webApp;
68     }
69
70     public static String JavaDoc createDefaultInputPath(String JavaDoc in,
71                                                 String JavaDoc[] sourcePaths) {
72         PathHandle handle = null;
73         File JavaDoc file = null;
74         String JavaDoc out = new String JavaDoc(in);
75
76         file = new File JavaDoc(out);
77         if (file.isDirectory()) {
78
79             // done
80
} else if (file.getParentFile().exists()) {
81             file = new File JavaDoc(file.getParentFile(), Constants.DIR_INPUT);
82             if (file.exists()) {
83                 out = file.getAbsolutePath();
84
85                 // done
86
} else {
87                 ExtensionFilter filter = new ExtensionFilter();
88                 StringBuffer JavaDoc mkIn = new StringBuffer JavaDoc();
89
90                 mkIn.append('.');
91                 mkIn.append(Constants.TYPE_MK);
92                 mkIn.append('.');
93                 mkIn.append(Constants.TYPE_IN);
94                 filter.setDirectoryValid(false);
95                 filter.addExtension(Constants.TYPE_IN);
96                 filter.addExclusion(mkIn.toString());
97                 for (int i = 0; i < sourcePaths.length; i++) {
98                     file = FileUtil.findFirst(filter, sourcePaths[i]);
99                     if (file != null) {
100                         out = file.getParent();
101                         break;
102                     }
103                 }
104             }
105         }
106         out = PathHandle.createPathString(out);
107         return out;
108     }
109
110     public static String JavaDoc createDefaultBootstrapPath(String JavaDoc in,
111             OtterProject project) {
112         String JavaDoc out = new String JavaDoc(in);
113         String JavaDoc deployPath = new String JavaDoc();
114         String JavaDoc inputPath = new String JavaDoc();
115         File JavaDoc file = new File JavaDoc(out);
116         BootstrapFilter filter = null;
117
118         if (file.isFile()) {
119             out = PathHandle.createPathString(file);
120
121             // done
122
} else {
123             deployPath = project.getDeployRootPath();
124             filter = new BootstrapFilter(false);
125             file = FileUtil.findFirst(filter, deployPath);
126             if (file != null && file.isFile()) {
127                 out = PathHandle.createPathString(file);
128
129                 // done
130
} else {
131                 inputPath = project.getDeployInputPath();
132                 filter = new BootstrapFilter(true);
133                 file = FileUtil.findFirst(filter, inputPath);
134                 if (file != null && file.isFile()) {
135                     out = PathHandle.createPathString(file);
136                     out = deployPath
137                           + out.substring(inputPath.length(),
138                                           out.length() - 3);
139                 }
140             }
141         }
142         return out;
143     }
144
145     public static String JavaDoc[][] createReplacementTable(String JavaDoc[][] tableIn,
146             OtterTemplateNode[] templates) {
147         String JavaDoc[][] tableOut = new String JavaDoc[0][2];
148         String JavaDoc[] placeholders = new String JavaDoc[0];
149         Backward back = null;
150
151         back = new Backward();
152         placeholders = back.findPlaceholders(templates);
153         if (placeholders.length == 0) {
154             tableOut = tableIn;
155         } else {
156             tableOut = new String JavaDoc[tableIn.length + placeholders.length][2];
157             for (int i = 0; i < placeholders.length; i++) {
158                 tableOut[i][0] = placeholders[i];
159
160                 // strings not to be resourced
161
if (placeholders[i].equals(Backward.RELATIVE_CLASSES)) {
162                     tableOut[i][1] = "= @JAVA_DEPLOY_PATH@/../classes";
163                 } else if (placeholders[i].equals("@CLASSES@")) {
164                     tableOut[i][1] = "@JAVA_DEPLOY_PATH@/../classes";
165                 } else if (placeholders[i].equals("@SERVLET_CONF_DIR@")) {
166                     tableOut[i][1] = "@JAVA_DEPLOY_PATH@/servlet";
167                 } else if (placeholders[i].equals("@OUTPUT@")) {
168                     tableOut[i][1] = "@JAVA_DEPLOY_PATH@";
169                 } else {
170                     tableOut[i][1] = placeholders[i];
171                 }
172             }
173             for (int i = 0; i < tableIn.length; i++) {
174                 tableOut[i + placeholders.length][0] = tableIn[i][0];
175                 tableOut[i + placeholders.length][1] = tableIn[i][1];
176             }
177         }
178         return tableOut;
179     }
180
181     private String JavaDoc[] findPlaceholders(OtterTemplateNode[] templates) {
182         String JavaDoc placeholders[] = new String JavaDoc[0];
183
184         phVector.clear();
185         for (int i = 0; i < templates.length; i++) {
186             findPlaceholdersInFile(templates[i].getFilePath());
187         }
188         placeholders = new String JavaDoc[phVector.size()];
189         placeholders = (String JavaDoc[]) phVector.toArray(placeholders);
190         phVector.clear();
191         return placeholders;
192     }
193
194     private void findPlaceholdersInFile(String JavaDoc path) {
195         BufferedReader JavaDoc reader = null;
196         File JavaDoc file = null;
197         String JavaDoc line = null;
198
199         file = new File JavaDoc(path);
200         if (file.isFile()) {
201             try {
202                 reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
203                 line = reader.readLine();
204                 while (line != null) {
205                     findRelativeClasses(line);
206                     findPlaceholdersInLine(line);
207                     line = reader.readLine();
208                 }
209                 reader.close();
210             } catch (FileNotFoundException JavaDoc e) {
211                 e.printStackTrace();
212             } catch (IOException JavaDoc e) {
213                 e.printStackTrace();
214             }
215         }
216     }
217
218     private void findRelativeClasses(String JavaDoc line) {
219         int index = line.indexOf(Backward.RELATIVE_CLASSES);
220
221         if (index > 0) {
222             if (!phVector.contains(Backward.RELATIVE_CLASSES)) {
223                 phVector.insertElementAt(Backward.RELATIVE_CLASSES, 0);
224             }
225         }
226     }
227
228     private void findPlaceholdersInLine(String JavaDoc line) {
229         String JavaDoc search = new String JavaDoc(line);
230         String JavaDoc[] exclude = ConfigTool.getSuffixArray();
231         StringBuffer JavaDoc ph = new StringBuffer JavaDoc();
232         int index = -1;
233         boolean start = false;
234         boolean include = true;
235
236         index = search.indexOf('@');
237         while (index > -1) {
238             if (start) {
239                 ph.setLength(0);
240                 ph.append('@');
241                 ph.append(search.substring(0, index));
242                 ph.append('@');
243                 if (!phVector.contains(ph.toString())) {
244                     include = true;
245                     for (int i = 0; i < exclude.length; i++) {
246                         if (ph.toString().endsWith(exclude[i])) {
247                             include = false;
248                             break;
249                         }
250                     }
251                     if (include) {
252                         phVector.addElement(ph.toString());
253                     }
254                 }
255             }
256             if (search.length() > index) {
257                 search = search.substring(index + 1);
258             } else {
259                 search = new String JavaDoc();
260             }
261             start = (!start);
262             index = search.indexOf('@');
263         }
264     }
265
266 }
267
268 //
269
class BootstrapFilter implements FileFilter JavaDoc {
270     private boolean template = false;
271
272     public BootstrapFilter(boolean t) {
273         template = true;
274     }
275
276     public boolean accept(File JavaDoc file) {
277         boolean accept = false;
278         String JavaDoc path = file.getAbsolutePath().toLowerCase();
279         StringBuffer JavaDoc multiserverBuf = new StringBuffer JavaDoc();
280         StringBuffer JavaDoc servletBuf = new StringBuffer JavaDoc();
281         StringBuffer JavaDoc kernelBuf = new StringBuffer JavaDoc();
282         StringBuffer JavaDoc bootstrapBuf = new StringBuffer JavaDoc();
283         StringBuffer JavaDoc bootstrap4Buf = new StringBuffer JavaDoc();
284
285         //
286
multiserverBuf.append(Constants.FILE_MULTISERVER_CONF);
287         servletBuf.append(Constants.FILE_SERVLET_CONF);
288         kernelBuf.append(Constants.FILE_KERNEL_CONF);
289         bootstrapBuf.append(Constants.FILE_BOOTSTRAP_CONF);
290         bootstrap4Buf.append(Constants.FILE_BOOTSTRAP4_CONF);
291         if (template) {
292             multiserverBuf.append('.');
293             multiserverBuf.append(Constants.TYPE_IN);
294             servletBuf.append('.');
295             servletBuf.append(Constants.TYPE_IN);
296             kernelBuf.append('.');
297             kernelBuf.append(Constants.TYPE_IN);
298             bootstrapBuf.append('.');
299             bootstrapBuf.append(Constants.TYPE_IN);
300             bootstrap4Buf.append('.');
301             bootstrap4Buf.append(Constants.TYPE_IN);
302         }
303         if (file.isFile()) {
304             if (ToolBoxInfo.isEnhydra3()) {
305                 if (path.endsWith(multiserverBuf.toString())
306                         || path.endsWith(servletBuf.toString())) {
307                     accept = true;
308                 }
309             } else {
310                 if (path.endsWith(kernelBuf.toString())
311                         || path.endsWith(bootstrapBuf.toString())
312                         || path.endsWith(bootstrap4Buf.toString())) {
313                     accept = true;
314                 }
315             }
316         }
317         return accept;
318     }
319
320 }
321
322 //
323
class PresentationFilter extends JavaFilter {
324
325     // strings not to be resourced
326
private final static String JavaDoc HTTP_PRESENTATION = "HttpPresentation";
327     private final static String JavaDoc HTTP_SERVLET = "HttpServlet";
328
329     //
330
private boolean webApp = true;
331
332     public boolean accept(File JavaDoc f) {
333         boolean accept = super.accept(f);
334
335         if (accept && f.canRead()) {
336             accept = false;
337             BufferedReader JavaDoc reader = null;
338             String JavaDoc line = new String JavaDoc();
339
340             try {
341                 reader = new BufferedReader JavaDoc(new FileReader JavaDoc(f));
342                 line = reader.readLine();
343                 while (line != null) {
344                     if (line.indexOf(HTTP_PRESENTATION) > -1) {
345                         webApp = false;
346                         accept = true;
347                         break;
348                     } else if (line.indexOf(HTTP_SERVLET) > -1) {
349                         webApp = true;
350                         accept = true;
351                         break;
352                     }
353                     line = reader.readLine();
354                 }
355                 reader.close();
356             } catch (IOException JavaDoc e) {
357                 e.printStackTrace();
358                 accept = false;
359             }
360         }
361         return accept;
362     }
363
364     protected boolean isWebApp() {
365         return webApp;
366     }
367
368 }
369
Popular Tags