001: /*
002: * Created on Jan 19, 2006
003: * Milin Radivoj
004: * rmilin@gmail.com
005: */
006: package org.enhydra.snapperPreviewer.business;
007:
008: import java.io.File;
009: import java.io.IOException;
010: import java.util.Enumeration;
011: import java.util.StringTokenizer;
012: import java.util.Vector;
013:
014: import org.enhydra.snapper.SnapperManager;
015: import org.enhydra.snapper.parsers.fileparsers.FileParserMaster;
016: import org.enhydra.snapperPreviewer.Previewer;
017: import org.enhydra.snapperPreviewer.spec.Download;
018: import org.enhydra.snapperPreviewer.spec.DownloadFactory;
019: import org.enhydra.snapperPreviewer.spec.FileContainerManager;
020: import org.enhydra.snapperPreviewer.spec.PDFPictureExtractor;
021: import org.enhydra.snapperPreviewer.spec.PDFPictureExtractorFactory;
022: import org.enhydra.snapperPreviewer.spec.Path;
023: import org.enhydra.snapperPreviewer.spec.Site;
024: import org.enhydra.snapperPreviewer.spec.SiteList;
025: import org.enhydra.snapperPreviewer.spec.SiteListFactory;
026:
027: /**
028: * @author Milin Radivoj
029: *
030: */
031: public class FileContainerManagerImpl implements FileContainerManager {
032:
033: Vector parsedPath = new Vector();
034: Vector createdFileList = new Vector();
035: String rest = "";
036: PDFPictureExtractor pdfExtractor;
037:
038: public FileContainerManagerImpl() {
039: try {
040: pdfExtractor = PDFPictureExtractorFactory
041: .getPDFPictureExtractor("org.enhydra.snapperPreviewer.business.PDFPictureExtractorImpl");
042:
043: } catch (Exception ex) {
044: pdfExtractor = null;
045: }
046: }
047:
048: public void init() throws IOException {
049: if (pdfExtractor != null)
050: pdfExtractor.init();
051: }
052:
053: public String getRestOfFilePath() {
054: return rest;
055: }
056:
057: public File getContainerFile(String path, String siteName)
058: throws Exception {
059:
060: if (siteName != null) {
061: File tF = new File(path);
062:
063: if (tF != null && Previewer.isRelitive()) {
064: try {
065: SiteList sl1 = SiteListFactory
066: .getSiteList("org.enhydra.snapperPreviewer.business.SiteListImpl");
067:
068: Site site = sl1.findSiteByName(siteName);
069: if (site != null) {
070: Path[] pathList = site.getPathList();
071:
072: if (pathList != null && pathList.length == 1) {
073: String mapRoot = pathList[0].getRoot();
074: if (!path.startsWith(mapRoot))
075: path = mapRoot + "/" + path;
076: }
077:
078: }
079: } catch (Exception e) {
080: e.printStackTrace();
081: Previewer.logError(e.getMessage());
082: }
083: }
084: }
085:
086: StringTokenizer st = new StringTokenizer(path, "|");
087: if (st.countTokens() > 1) {
088: parsedPath.add(st.nextToken());
089: while (st.hasMoreTokens()) {
090: parsedPath.add(st.nextToken());
091: }
092: } else {
093: parsedPath.add(path);
094: }
095:
096: String originalFilePath = (String) parsedPath.elementAt(0);
097: parsedPath.remove(0);
098:
099: int indexOfExtension = originalFilePath.lastIndexOf(".");
100: String typeOfFile = null;
101:
102: if (indexOfExtension != -1) {
103: typeOfFile = originalFilePath.substring(
104: indexOfExtension + 1, originalFilePath.length());
105: } else {
106: throw new Exception(" Missing file extension : "
107: + originalFilePath);
108: }
109:
110: File originalFile = null;
111:
112: if (originalFilePath.startsWith("ftp")) {
113: originalFile = downloadFtpFile(originalFilePath,
114: typeOfFile, siteName);
115: if (originalFile == null) {
116: throw new Exception(" Error geting ftp file ");
117: } else {
118: createdFileList.add(originalFile.getAbsolutePath());
119: }
120: } else if (originalFilePath.startsWith("http")) {
121: originalFile = downloadWabDAVFile(originalFilePath,
122: typeOfFile, siteName);
123: if (originalFile == null) {
124: throw new Exception(" Error geting WebDAV file ");
125: } else {
126: createdFileList.add(originalFile.getAbsolutePath());
127: }
128: } else {
129: originalFile = new File(originalFilePath);
130: }
131:
132: if (!originalFile.exists()) {
133: throw new Exception("File not exists : "
134: + originalFile.getAbsolutePath());
135: }
136:
137: if (typeOfFile.equalsIgnoreCase("tiff")
138: || typeOfFile.equalsIgnoreCase("tif")
139: || typeOfFile.equalsIgnoreCase("g3f"))
140: return originalFile;
141:
142: File inside = originalFile;
143: String lastTypeOfTheFile = typeOfFile;
144:
145: Enumeration paths = parsedPath.elements();
146:
147: for (Enumeration e = paths; e.hasMoreElements();) {
148: String insidePath = (String) e.nextElement();
149: int indexOfExt = insidePath.lastIndexOf(".");
150: String typeOfFileInside = "File";
151:
152: if (indexOfExt != -1) {
153: typeOfFileInside = insidePath.substring(indexOfExt + 1,
154: insidePath.length());
155: } else if (lastTypeOfTheFile.equalsIgnoreCase("pst")) {
156: typeOfFileInside = "";
157: } else {
158: rest = insidePath;
159: return inside;
160: }
161:
162: if (!lastTypeOfTheFile.equalsIgnoreCase("pdf")) {
163:
164: FileParserMaster parser = new FileParserMaster();
165:
166: if (lastTypeOfTheFile.equalsIgnoreCase("pst")) {
167: if (!e.hasMoreElements()) {
168: throw new Exception(
169: " Mising attached file name : "
170: + originalFilePath);
171: }
172:
173: String attachedFile = (String) e.nextElement();
174: int indexOfAttachedFileExt = attachedFile
175: .lastIndexOf(".");
176:
177: if (indexOfAttachedFileExt == -1) {
178: throw new Exception(
179: " Mising attached file extension : "
180: + originalFilePath);
181: }
182:
183: typeOfFileInside = attachedFile.substring(
184: indexOfAttachedFileExt + 1, attachedFile
185: .length());
186: inside = parser.getContainerFile(inside, insidePath
187: + "|" + attachedFile, SnapperManager
188: .getInstance().getTempDir()
189: + System.currentTimeMillis(),
190: SnapperManager.getInstance()
191: .getFileTypeProperties());
192: } else
193: inside = parser.getContainerFile(inside,
194: insidePath, SnapperManager.getInstance()
195: .getTempDir()
196: + System.currentTimeMillis(),
197: SnapperManager.getInstance()
198: .getFileTypeProperties());
199:
200: createdFileList.add(inside.getAbsolutePath());
201: lastTypeOfTheFile = typeOfFileInside;
202: } else {
203:
204: if (pdfExtractor == null)
205: return null;
206:
207: inside = pdfExtractor.getPicture(inside, insidePath,
208: SnapperManager.getInstance().getTempDir()
209: + System.currentTimeMillis());
210: createdFileList.add(inside.getAbsolutePath());
211:
212: }
213:
214: if (typeOfFileInside.equalsIgnoreCase("tiff")
215: || typeOfFileInside.equalsIgnoreCase("tif")
216: || typeOfFileInside.equalsIgnoreCase("g3f")) {
217: return inside;
218: }
219: }
220:
221: if (inside != null) {
222: return inside;
223: } else {
224: throw new Exception(
225: " Problem ocured while extracting file from container "
226: + path);
227: }
228:
229: }
230:
231: public void deleteAllTempFiles() {
232: Enumeration filesToDelete = createdFileList.elements();
233:
234: for (Enumeration e = filesToDelete; e.hasMoreElements();) {
235: String filePath = (String) e.nextElement();
236: if (filePath != null) {
237: File temp = new File(filePath);
238: if (temp.exists()) {
239: try {
240: File parentCurentTime = temp.getParentFile();
241: FileDeleter.delete(temp);
242: FileDeleter.delete(parentCurentTime);
243: } catch (Exception ex) {
244: ex.printStackTrace();
245: }
246: }
247: }
248:
249: }
250: }
251:
252: private File downloadFtpFile(String id, String type, String siteName) {
253: try {
254: Download dw = DownloadFactory
255: .getDownload("org.enhydra.snapperPreviewer.business.DownloadImpl");
256: File retVal = dw.downloadFtpFile(id, type, siteName);
257: if (retVal != null) {
258: return retVal;
259: } else {
260: return null;
261: }
262: } catch (Exception e) {
263: Previewer.logError(e.getMessage());
264: }
265: return null;
266: }
267:
268: private File downloadWabDAVFile(String id, String type,
269: String siteName) {
270: try {
271: Download dw = DownloadFactory
272: .getDownload("org.enhydra.snapperPreviewer.business.DownloadImpl");
273: File retVal = dw.downloadWebDavFile(id, type, siteName);
274: if (retVal != null) {
275: return retVal;
276: } else {
277: return null;
278: }
279: } catch (Exception e) {
280: Previewer.logError(e.getMessage());
281: }
282: return null;
283: }
284:
285: }
|