KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > taskdef > DefaultXmlcUtilsImpl


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  * $Id: DefaultXmlcUtilsImpl.java,v 1.2 2005/01/26 08:28:45 jkjome Exp $
22  */

23 package org.enhydra.xml.xmlc.taskdef;
24
25 import java.io.File JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29
30 /**
31  * Default implementation of the XmlcUtils class
32  *
33  * @author Robert Leftwich
34  * @version 1.0
35  */

36 public class DefaultXmlcUtilsImpl extends XmlcUtils {
37
38   //===========================================================================
39
// CONSTRUCTORS
40
//===========================================================================
41

42   /**
43    * Construct default DefaultXmlcUtilsImpl (protected to force the use
44    * of XmlcUtils.create() factory method).
45    */

46   protected DefaultXmlcUtilsImpl() {
47     super();
48   }
49
50   //===========================================================================
51
// QUERY METHODS
52
//===========================================================================
53

54   /**
55    * Get a list of options files for the specified directory.
56    * This method will search for the default options file name
57    * (options.xmlc) in the specified directory and the user.dir
58    * directory as well. It will return them in that order.
59    *
60    * @param theDirectory
61    * The directory to look in for the options file.
62    * @return An array of options files with absolute paths.
63    * An empty array if none found.
64    */

65   public String JavaDoc[] getOptionFiles(File JavaDoc theDirectory) {
66     return this.getOptionFiles(theDirectory,(String JavaDoc)null);
67   }
68
69   /**
70    * Get a list of options files for the specified directory,
71    * having the specified name. This method will search for the
72    * specified options file name and the default options file name
73    * (options.xmlc) in the specified directory and the user.dir
74    * directory as well. It will return them in that order.
75    *
76    * @param theDirectory
77    * The directory to look in for the options file.
78    * @param theOptionsFileName
79    * The name of the options file to look for.
80    * @return An array of options files with absolute paths.
81    * An empty array if none found.
82    */

83   public String JavaDoc[] getOptionFiles(File JavaDoc theDirectory, String JavaDoc theOptionsFileName) {
84     File JavaDoc[] dirs = { theDirectory };
85     return this.getOptionFiles(dirs, theOptionsFileName);
86   }
87
88   /**
89    * Get a list of options files for the specified directory,
90    * having the specified names. This method will search for the
91    * specified options file names and the default options file name
92    * (options.xmlc) in the specified directory and the user.dir
93    * directory as well. It will return them in that order.
94    *
95    * @param theDirectory
96    * The directory to look in for the options file.
97    * @param theOptionsFileNames
98    * The list of names of the options file to look for.
99    * @return An array of options files with absolute paths.
100    * An empty array if none found.
101    */

102   public String JavaDoc[] getOptionFiles(File JavaDoc theDirectory, String JavaDoc[] theOptionsFileNames) {
103     File JavaDoc[] dirs = { theDirectory };
104     return this.getOptionFiles(dirs, theOptionsFileNames);
105   }
106
107   /**
108    * Get a list of options files for the specified directories,
109    * having the specified name. This method will search for the
110    * specified options file name and the default options file name
111    * (options.xmlc) in the specified directories and the user.dir
112    * directory as well. It will return them in that order.
113    *
114    * @param theDirectories
115    * The list of directories to look in for the options files.
116    * @param theOptionsFileName
117    * The name of the options file to look for.
118    * @return An array of options files with absolute paths.
119    * An empty array if none found.
120    */

121   public String JavaDoc[] getOptionFiles(File JavaDoc[] theDirectories, String JavaDoc theOptionsFileName) {
122     String JavaDoc[] files = { theOptionsFileName };
123     return this.getOptionFiles(theDirectories, files);
124   }
125
126   /**
127    * Get a list of options files for the specified directories,
128    * having the specified names. This method will search for the
129    * specified options file names and the default options file name
130    * (options.xmlc) in the specified directories and the user.dir
131    * directory as well. It will return them in that order.
132    *
133    * @param theDirectories
134    * The list of directories to look in for the options files.
135    * @param theOptionsFileNames
136    * The list of names of the options file to look for.
137    * @return An array of options files with absolute paths.
138    * An empty array if none found.
139    */

140   public String JavaDoc[] getOptionFiles(File JavaDoc[] theDirectories, String JavaDoc[] theOptionsFileNames) {
141
142     String JavaDoc[] optionsFileNames = theOptionsFileNames;
143
144     if (null != optionsFileNames && optionsFileNames.length > 0) {
145       for (int i = 0; i < optionsFileNames.length; i++) {
146         String JavaDoc currFileName = optionsFileNames[i];
147         if (null == currFileName || currFileName.length() == 0) {
148           optionsFileNames[i] = DEFAULT_OPTIONS_FILENAME;
149         }
150         if (!optionsFileNames[i].endsWith(DEFAULT_OPTIONS_FILETYPE)) {
151           File JavaDoc tmpFile = new File JavaDoc(optionsFileNames[i]);
152           String JavaDoc tmpName = tmpFile.getName();
153           int idx = tmpName.lastIndexOf(".");
154           if (-1 == idx) {
155             idx = tmpName.length();
156           }
157           StringBuffer JavaDoc newFileName = new StringBuffer JavaDoc(tmpName.substring(0,idx));
158           newFileName.append(DEFAULT_OPTIONS_FILETYPE);
159           optionsFileNames[i] = newFileName.toString();
160         }
161       }
162     } else {
163       String JavaDoc[] defaultOptionsFileNames = {DEFAULT_OPTIONS_FILENAME};
164       optionsFileNames = defaultOptionsFileNames;
165     }
166
167     ArrayList JavaDoc allOptionsFiles = new ArrayList JavaDoc();
168
169     for (int fileNameIdx = 0; fileNameIdx < optionsFileNames.length; fileNameIdx++) {
170       String JavaDoc currFileName = optionsFileNames[fileNameIdx];
171       for (int dirIdx = 0; dirIdx < theDirectories.length; dirIdx++) {
172         File JavaDoc currDir = theDirectories[dirIdx];
173         this.addFileIfExists(new File JavaDoc(currDir, currFileName),
174                               allOptionsFiles);
175         this.addFileIfExists(new File JavaDoc(currDir, DEFAULT_OPTIONS_FILENAME),
176                               allOptionsFiles);
177       }
178       // look in user.dir as well
179
this.addFileIfExists(new File JavaDoc(currFileName),
180                             allOptionsFiles);
181     }
182     this.addFileIfExists(new File JavaDoc(DEFAULT_OPTIONS_FILENAME),
183                           allOptionsFiles);
184
185     String JavaDoc[] retVal = new String JavaDoc[ allOptionsFiles.size() ];
186     for (int i = 0; i < allOptionsFiles.size(); i++) {
187       retVal[i] = (String JavaDoc)allOptionsFiles.get(i);
188       //theirLogger.debug("Found options file: " + retVal[i]);
189
}
190
191     return retVal;
192   }
193
194   /**
195    * Build a full base file name (i.e. with no file type) out of
196    * the specified components.
197    *
198    * Note that any directory prefix on the theBaseFileName file is stripped before
199    * thePackageDir is applied. So that the following call :
200    *
201    * buildFullBaseFileName("a.b.c", "foo/resources", "config/test.html");
202    *
203    * will return a string of "a/b/c/foo/resources/test"
204    *
205    * and this call :
206    *
207    * buildFullBaseFileName("a.b.c", "", "config/test.html");
208    *
209    * will return a string of "a/b/c/config/test"
210    *
211    * @param thePackageName
212    * The name of the package to prepend to the file name.
213    * This should be specified using the Java naming convention
214    * i.e. org.enhydra.xmlc
215    * @param thePackageDir
216    * The name of the directory to prepend to the file name.
217    * This is a directory path that can be specified using either '/' or '\'
218    * i.e. foo/resources
219    * @param theBaseFileName
220    * The base file name that the preceding strings are prepended to.
221    * @return A string representing the full path resulting from the specified components
222    * with all separators set to '/'
223    */

224   public String JavaDoc buildFullBaseFileName(String JavaDoc thePackageName,
225                                        String JavaDoc thePackageDir,
226                                        String JavaDoc theBaseFileName) {
227     File JavaDoc baseFile;
228     int dot = theBaseFileName.indexOf(".");
229     if (dot != -1)
230       baseFile = new File JavaDoc(theBaseFileName.substring(0, dot));
231     else
232       baseFile = new File JavaDoc(theBaseFileName);
233     if (thePackageDir != null && thePackageDir.length() > 0)
234     {
235       baseFile = new File JavaDoc(thePackageDir,
236                            baseFile.getName());
237     }
238     if (thePackageName != null && thePackageName.length() > 0)
239     {
240       baseFile = new File JavaDoc(thePackageName.replace('.', File.separatorChar),
241                            baseFile.getPath());
242     }
243     return baseFile.getPath().replace(File.separatorChar, '/');
244   }
245
246   /**
247    * Build a Java class name out of the specified components.
248    *
249    * @param theFullBaseFileName
250    * The full base file name (specified with '/' separators)
251    * and no file type. e.g. "org.enhydra.test"
252    * @param theModifier
253    * The file modifier to append to the class name
254    * e.g. HTML
255    * @return A Java class name string
256    */

257   public String JavaDoc buildClassName(String JavaDoc theFullBaseFileName,
258                                 String JavaDoc theModifier) {
259     // Synthesise class name based on path and filename.
260
String JavaDoc className = theFullBaseFileName.replace('/', '.');
261     className = className.replace(File.separatorChar, '.') + theModifier;
262     return className;
263   }
264
265   //===========================================================================
266
// MUTATOR METHODS
267
//===========================================================================
268

269   //===========================================================================
270
// HELPER METHODS
271
//===========================================================================
272

273   /**
274    * Helper function to add a file to a list of files if the file
275    * is a file, is readable and is not in the list already.
276    *
277    * @param theFile The file to check
278    * @param theFileNameList
279    * The list to add the file to
280    */

281   private void addFileIfExists(File JavaDoc theFile, List JavaDoc theFileNameList) {
282     if (theFile.exists() && theFile.isFile()) {
283       String JavaDoc path = theFile.getAbsolutePath();
284       if (!theFileNameList.contains(path)) {
285         theFileNameList.add (path);
286       }
287     }
288   }
289
290 }
291
Popular Tags