001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.repository.filebased.solution;
014:
015: import java.io.File;
016: import java.io.IOException;
017: import org.pentaho.core.solution.ISolutionFile;
018: import org.pentaho.util.FileHelper;
019:
020: public class FileSolutionFile implements ISolutionFile {
021: String solutionName = ""; //$NON-NLS-1$
022:
023: String pathName = ""; //$NON-NLS-1$
024:
025: String fileName = ""; //$NON-NLS-1$
026:
027: int solutionAbsoluteStart = 0;
028:
029: File file = null;
030:
031: File solutionRoot = null;
032:
033: public FileSolutionFile(File inFile, File inSolutionRoot) {
034: file = inFile;
035: solutionRoot = inSolutionRoot;
036: solutionAbsoluteStart = (solutionRoot == null) ? 0
037: : solutionRoot.getAbsolutePath().length()
038: - solutionRoot.getName().length();
039: int solutionNameLength = (solutionRoot == null) ? 0
040: : solutionRoot.getName().length();
041: // Keep track of where the solution root is
042: // Chop off the path info before the root dir to make it start at
043: // solution root
044: String fullName = file.getAbsolutePath().substring(
045: solutionAbsoluteStart);
046: // windows \ characters in the path gets messy in urls and xml, so
047: // switch them to /
048: fullName = fullName.replace('\\', '/');
049: solutionName = fullName.substring(0, solutionNameLength);
050: fileName = file.getName();
051: if (file.isDirectory()) {
052: if (fullName.length() > solutionNameLength) {
053: pathName = fullName.substring(solutionNameLength + 1);
054: }
055: } else {
056: if (solutionNameLength > 0
057: && fullName.length() > (solutionNameLength
058: + fileName.length() + 1)) {
059: pathName = fullName.substring(solutionNameLength + 1,
060: fullName.length() - fileName.length() - 1);
061: } else {
062: pathName = fullName.substring(0, fullName.length()
063: - fileName.length() - 1);
064: }
065: if (pathName.equals(solutionName)) {
066: pathName = ""; //$NON-NLS-1$
067: }
068: }
069: }
070:
071: public boolean isDirectory() {
072: return (file != null) && (file.isDirectory());
073: }
074:
075: public String getFileName() {
076: return (fileName);
077: }
078:
079: public String getSolutionPath() {
080: return (pathName);
081: }
082:
083: public String getSolution() {
084: return (solutionName);
085: }
086:
087: public String getFullPath() {
088: String fullName = "/" + solutionName;//$NON-NLS-1$
089: if (pathName.length() > 0) {
090: fullName += "/" + pathName; //$NON-NLS-1$
091: }
092: if (!isDirectory() && (fileName.length() > 0)) {
093: fullName += "/" + fileName; //$NON-NLS-1$
094: }
095: return (fullName);
096: }
097:
098: public String getFileType() {
099: int dotIndex = fileName.lastIndexOf('.');
100: return ((dotIndex < 0) ? "" : fileName.substring(dotIndex)); //$NON-NLS-1$
101: }
102:
103: public ISolutionFile[] listFiles() {
104: if (file == null) {
105: return (null);
106: }
107: File files[] = file.listFiles();
108: if (files == null) {
109: return (null);
110: }
111: ISolutionFile solFiles[] = new ISolutionFile[files.length];
112: FileSolutionFile solFile;
113: for (int i = 0; i < files.length; ++i) {
114: solFile = new FileSolutionFile(files[i], solutionRoot);
115: solFiles[i] = solFile;
116: }
117: return (solFiles);
118: }
119:
120: public String toString() {
121: return (getSolution()
122: + " : " + getSolutionPath() + " : " + getFileName() + " : " + getFileType()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
123: }
124:
125: public static void main(String[] args) {
126: File f = new File(
127: "E:/eclipse/workspace/pentaho-samples/solutions/test-solution"); //$NON-NLS-1$
128: printRecursive(new FileSolutionFile(f, f));
129: }
130:
131: private static void printRecursive(ISolutionFile sFile) {
132: if (sFile.isDirectory()) {
133: ISolutionFile sFiles[] = sFile.listFiles();
134: for (int i = 0; i < sFiles.length; ++i) {
135: printRecursive(sFiles[i]);
136: }
137: }
138: }
139:
140: public boolean isRoot() {
141: if (solutionRoot != null && file != null) {
142: try {
143: return (solutionRoot.getCanonicalPath().equals(file
144: .getCanonicalPath()));
145: } catch (IOException e) {
146: }
147: } else if (file != null) {
148: return (file.getParentFile() == null);
149: }
150: return false;
151: }
152:
153: public byte[] getData() {
154: try {
155: return FileHelper.getBytesFromFile(file);
156: } catch (IOException e) {
157: return null;
158: }
159: }
160:
161: public ISolutionFile retrieveParent() {
162: return new FileSolutionFile(file.getParentFile(), solutionRoot);
163: }
164:
165: public boolean exists() {
166: return file.exists();
167: }
168:
169: public File getFile() {
170: return file;
171: }
172:
173: }
|