KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > repository > filebased > solution > FileSolutionFile


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12 */

13 package org.pentaho.repository.filebased.solution;
14
15 import java.io.File JavaDoc;
16
17 import org.pentaho.core.solution.ISolutionFile;
18
19 public class FileSolutionFile implements ISolutionFile {
20     String JavaDoc solutionName = ""; //$NON-NLS-1$
21

22     String JavaDoc pathName = ""; //$NON-NLS-1$
23

24     String JavaDoc fileName = ""; //$NON-NLS-1$
25

26     int solutionAbsoluteStart = 0;
27
28     File JavaDoc file = null;
29
30     private FileSolutionFile(File JavaDoc file, int solutionAbsoluteStart, int solutionNameLength) {
31         // Keep track of where the solution root is
32
this.solutionAbsoluteStart = solutionAbsoluteStart;
33
34         // Chop off the path info before the root dir to make it start at
35
// solution root
36
String JavaDoc fullName = file.getAbsolutePath().substring(solutionAbsoluteStart);
37
38         // windows \ characters in the path gets messy in urls and xml, so
39
// switch them to /
40
fullName = fullName.replace('\\', '/');
41
42         solutionName = fullName.substring(0, solutionNameLength);
43
44         fileName = file.getName();
45         if (file.isDirectory()) {
46             this.file = file; // Only need to save the file object if it's a
47
// directory
48
if (fullName.length() > solutionNameLength) {
49                 pathName = fullName.substring(solutionNameLength + 1);
50             }
51         } else {
52             if (fullName.length() > (solutionNameLength + fileName.length() + 1)) {
53                 pathName = fullName.substring(solutionNameLength + 1, fullName.length() - fileName.length() - 1);
54             }
55         }
56     }
57
58     public FileSolutionFile(File JavaDoc file, File JavaDoc solutionRoot) {
59         this(file, (solutionRoot == null) ? 0 : solutionRoot.getAbsolutePath().length() - solutionRoot.getName().length(), (solutionRoot == null) ? 0 : solutionRoot.getName().length());
60     }
61
62     public boolean isDirectory() {
63         return (file != null);
64     }
65
66     public String JavaDoc getFileName() {
67         return (fileName);
68     }
69
70     public String JavaDoc getSolutionPath() {
71         return (pathName);
72     }
73
74     public String JavaDoc getSolution() {
75         return (solutionName);
76     }
77
78     public String JavaDoc getFullPath() {
79         String JavaDoc fullName = solutionName;
80         if (pathName.length() > 0) {
81             fullName += "/" + pathName; //$NON-NLS-1$
82
}
83         if (!isDirectory() && (fileName.length() > 0)) {
84             fullName += "/" + fileName; //$NON-NLS-1$
85
}
86         return (fullName);
87     }
88
89     public String JavaDoc getFileType() {
90         int dotIndex = fileName.lastIndexOf('.');
91         return ((dotIndex < 0) ? "" : fileName.substring(dotIndex)); //$NON-NLS-1$
92
}
93
94     public ISolutionFile[] listFiles() {
95         if (file == null) {
96             return (null);
97         }
98
99         File JavaDoc files[] = file.listFiles();
100         if (files == null) {
101             return (null);
102         }
103
104         ISolutionFile solFiles[] = new ISolutionFile[files.length];
105         FileSolutionFile solFile;
106         for (int i = 0; i < files.length; ++i) {
107             solFile = new FileSolutionFile(files[i], solutionAbsoluteStart, solutionName.length());
108             solFiles[i] = solFile;
109         }
110
111         return (solFiles);
112     }
113
114     public String JavaDoc toString() {
115         return (getSolution() + " : " + getSolutionPath() + " : " + getFileName() + " : " + getFileType()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
116
}
117
118     public static void main(String JavaDoc[] args) {
119         File JavaDoc f = new File JavaDoc("E:/eclipse/workspace/pentaho-samples/solutions/test-solution"); //$NON-NLS-1$
120
printRecursive(new FileSolutionFile(f, f));
121     }
122
123     private static void printRecursive(ISolutionFile sFile) {
124         if (sFile.isDirectory()) {
125             ISolutionFile sFiles[] = sFile.listFiles();
126             for (int i = 0; i < sFiles.length; ++i) {
127                 printRecursive(sFiles[i]);
128             }
129         }
130     }
131
132     public boolean isRoot() {
133         // TODO Auto-generated method stub
134
return false;
135     }
136
137 }
138
Popular Tags