0001: /*
0002: * Milin Radivoj
0003: * rmilin@gmail.com
0004: */
0005: package org.enhydra.snapperPreviewer.business;
0006:
0007: import java.io.BufferedReader;
0008: import java.io.ByteArrayOutputStream;
0009: import java.io.File;
0010: import java.io.FileInputStream;
0011: import java.io.FileOutputStream;
0012: import java.io.FileWriter;
0013: import java.io.IOException;
0014: import java.io.InputStream;
0015: import java.io.InputStreamReader;
0016: import java.io.OutputStream;
0017: import java.io.PrintWriter;
0018: import java.io.StringReader;
0019:
0020: import java.net.HttpURLConnection;
0021: import java.net.MalformedURLException;
0022: import java.net.URL;
0023: import java.net.URLEncoder;
0024: import java.util.Enumeration;
0025: import java.util.Properties;
0026: import java.util.StringTokenizer;
0027: import java.util.Vector;
0028:
0029: import org.enhydra.snapper.SnapperManager;
0030: import org.enhydra.snapper.api.Searcher;
0031: import org.enhydra.snapper.api.searching.ResultElement;
0032: import org.enhydra.snapper.api.searching.SearchResult;
0033: import org.enhydra.snapper.parsers.fileparsers.FileParserException;
0034: import org.enhydra.snapper.parsers.fileparsers.FileParserMaster;
0035: import org.enhydra.snapper.parsers.fileparsers.utils.ParserDataConstants;
0036: import org.enhydra.snapper.xml.tdv.SummaryDocument;
0037: import org.enhydra.snapper.xml.tdv.ContainerFileDocument.ContainerFile;
0038: import org.enhydra.snapper.xml.tdv.ContainerFileListDocument.ContainerFileList;
0039: import org.enhydra.snapper.xml.tdv.ParameterDocument.Parameter;
0040: import org.enhydra.snapper.xml.tdv.PreviewDocument.Preview;
0041: import org.enhydra.snapper.xml.tdv.SearchedParametersDocument.SearchedParameters;
0042: import org.enhydra.snapper.xml.tdv.SummaryDocument.Summary;
0043: import org.enhydra.snapperPreviewer.Previewer;
0044: import org.enhydra.snapperPreviewer.spec.DocumentGroup;
0045: import org.enhydra.snapperPreviewer.spec.Download;
0046: import org.enhydra.snapperPreviewer.spec.DownloadFactory;
0047: import org.enhydra.snapperPreviewer.spec.FileContainerManager;
0048: import org.enhydra.snapperPreviewer.spec.FileContainerManagerFactory;
0049: import org.enhydra.snapperPreviewer.spec.FileType;
0050: import org.enhydra.snapperPreviewer.spec.Path;
0051: import org.enhydra.snapperPreviewer.spec.Site;
0052: import org.enhydra.snapperPreviewer.spec.SiteList;
0053: import org.enhydra.snapperPreviewer.spec.SiteListFactory;
0054: import org.enhydra.snapperPreviewer.spec.Wrapper;
0055: import org.enhydra.snapperPreviewer.spec.WrapperFactory;
0056:
0057: import com.lutris.appserver.server.sql.DBTransaction;
0058:
0059: /**
0060: * @author Milin Radivoj
0061: *
0062: */
0063: public class PreviewBuilder implements
0064: org.enhydra.snapperPreviewer.spec.PreviewBuilder {
0065:
0066: // from indexer
0067: public String getDocument(String siteName, String id, String query,
0068: DBTransaction db, String langpair,
0069: boolean toShowcontainerFilesData, int showNumberOf,
0070: Properties pictureConversionRequestParm,
0071: Properties parserLimitProperties) {
0072: ResultElement retVal;
0073:
0074: String indexDir = "";
0075: String indexedDirectory = "false";
0076:
0077: SiteList sl = null;
0078:
0079: try {
0080: sl = SiteListFactory
0081: .getSiteList("org.enhydra.snapperPreviewer.business.SiteListImpl");
0082: Site site = sl.findSiteByName(siteName);
0083:
0084: if (site != null) {
0085: indexDir = site.getINDEXDIR();
0086: boolean directryI = site.getINDEXDIRECTORY();
0087:
0088: if (directryI)
0089: indexedDirectory = "true";
0090: }
0091:
0092: if (indexDir == null || indexDir.equals("")) {
0093: indexDir = Previewer.getIndexDir() + File.separator
0094: + siteName;
0095: }
0096:
0097: } catch (Exception exx) {
0098: Previewer.logError(" Problem in Previewer Builder : "
0099: + exx.getMessage());
0100: }
0101:
0102: Searcher searcher = SnapperManager.getInstance()
0103: .getSearcherFactory().newSearcher();
0104: retVal = searcher.getDocumentByID(siteName, indexDir,
0105: (new Integer(id)).intValue(), query);
0106:
0107: SummaryDocument sd = SummaryDocument.Factory.newInstance();
0108: Summary summary = sd.addNewSummary();
0109: SearchedParameters searchedParameters = summary
0110: .addNewSearchedParameters();
0111:
0112: Parameter perameter = searchedParameters.addNewParameter();
0113: perameter.setKey("siteName");
0114: perameter.setValue(BuilderUtil.filterAmp(siteName));
0115:
0116: Parameter perameter1 = searchedParameters.addNewParameter();
0117: perameter1.setKey("id");
0118: perameter1.setValue(id);
0119:
0120: Parameter perameter2 = searchedParameters.addNewParameter();
0121: perameter2.setKey("indexedDirectory");
0122: perameter2.setValue(indexedDirectory);
0123:
0124: Parameter perameter3 = searchedParameters.addNewParameter();
0125: perameter3.setKey("query");
0126: perameter3.setValue(BuilderUtil.filterAmp(query));
0127:
0128: Parameter perameter4 = searchedParameters.addNewParameter();
0129: perameter4.setKey("langpair");
0130: perameter4.setValue(langpair);
0131:
0132: addSearchParameters(searchedParameters,
0133: pictureConversionRequestParm, parserLimitProperties);
0134:
0135: //summary.setSearchedParameters(searchedParameters);
0136: Preview previewImp = summary.addNewPreview();
0137:
0138: String absolutFileName = retVal.getAbsolutFileName();
0139: String absolutFilePath = retVal.getAbsolutFilePath();
0140: String content = retVal.getContent();
0141: String fileType = retVal.getFileType();
0142: //String luceneID = retVal.getLuceneID();
0143: String metaData = retVal.getMetaData();
0144: //String modified = retVal.getModified();
0145: String properties = retVal.getProperties();
0146: String title = retVal.getTitle();
0147:
0148: try {
0149: content = BuilderUtil.clearContent(content, title,
0150: absolutFilePath, properties, metaData);
0151: String tempabsolutFilePath = absolutFilePath;
0152:
0153: title = BuilderUtil.filterAmp(title);
0154: absolutFileName = BuilderUtil.filterAmp(absolutFileName);
0155: absolutFilePath = BuilderUtil.filterAmp(absolutFilePath);
0156:
0157: previewImp.setTitle(title);
0158: previewImp.setFileType(fileType);
0159: previewImp.setAbsolutFileName(absolutFileName);
0160: String tempp = absolutFilePath;
0161:
0162: if (fileType.equalsIgnoreCase("directory")
0163: && !(tempp.endsWith("\\") || tempp.endsWith("/"))) {
0164: tempp = tempp + File.separator;
0165: }
0166:
0167: previewImp.setAbsolutFilePath(BuilderUtil
0168: .createAbsolutFilePath(tempp));
0169:
0170: int ind = absolutFilePath.indexOf('|');
0171: if (ind == -1)
0172: ind = absolutFilePath.length();
0173: // String remfp = absolutFilePath.substring(0, ind);
0174:
0175: previewImp.setSiteName(siteName);
0176:
0177: if (!langpair.equals("none")) {
0178: content = BuilderUtil
0179: .replaceAll(content, "\n", " # # ");
0180: content = googleTranslate(content, langpair);
0181: content = BuilderUtil.replaceAll(content, "# #", "\n");
0182: }
0183:
0184: //content je ok treba samo highlight pa filter
0185:
0186: if (langpair.equals("none"))
0187: content = searcher.hightlightDocumentByID(siteName,
0188: indexDir, query, content);
0189:
0190: content = BuilderUtil.filterAmp(content);
0191: content = BuilderUtil.filterNewLine(content);
0192: content = BuilderUtil.filterContentStartNewLine(content);
0193:
0194: metaData = BuilderUtil.filterAmp(metaData);
0195: metaData = BuilderUtil.filterNewLine(metaData);
0196:
0197: /* if(fileType.equalsIgnoreCase("xls"))
0198: content = BuilderUtil.filterXLSDocument(content);*/
0199:
0200: //pokushamo da nadjemo konvertovan, aplikacija parametar, toSearchForConverted
0201: if (Previewer.toReadConvertedFile()
0202: && Previewer.pathOfConvertedFiles() != null) {
0203: absolutFilePath = absolutFilePath.replaceAll(":", "_");
0204: char first = 0x7c;
0205: char second = 0x28;
0206: absolutFilePath = absolutFilePath
0207: .replace(first, second);
0208:
0209: String root = Previewer.pathOfConvertedFiles();
0210:
0211: if (!root.endsWith("/") && !root.endsWith("\\"))
0212: root = root + File.separator;
0213:
0214: String convertedFilePath = root + absolutFilePath;
0215:
0216: convertedFilePath = convertedFilePath + ".html";
0217:
0218: File convertedFile = new File(convertedFilePath);
0219: if (convertedFile != null && convertedFile.exists()) {
0220: ByteArrayOutputStream ba = null;
0221: FileInputStream fi = null;
0222: try {
0223: ba = new ByteArrayOutputStream();
0224: fi = new FileInputStream(convertedFile);
0225:
0226: byte[] buffer = new byte[4096];
0227: int no = fi.read(buffer);
0228: while (no > -1) {
0229: ba.write(buffer, 0, no);
0230: no = fi.read(buffer);
0231: }
0232: ba.flush();
0233: content = ba.toString();
0234: } catch (Exception e) {
0235: e.printStackTrace();
0236: } finally {
0237: if (ba != null) {
0238: try {
0239: ba.close();
0240: } catch (Exception e) {
0241: }
0242: ba = null;
0243: }
0244:
0245: if (fi != null) {
0246: try {
0247: fi.close();
0248: } catch (Exception e) {
0249: }
0250: fi = null;
0251: }
0252: }
0253:
0254: }
0255: }
0256:
0257: previewImp.setContent(content);
0258: previewImp.setMetaData(metaData);
0259:
0260: if (fileType.equalsIgnoreCase("msg")) {
0261: int numberOfAdded = 0;
0262: Properties sitelist = new Properties();
0263:
0264: Site[] sites = null;
0265:
0266: if (sl != null)
0267: sites = sl.getList();
0268:
0269: try {
0270: if (sites != null) {
0271: for (int i = 0; i < sites.length; i++) {
0272:
0273: if (siteName != null
0274: && siteName.equals(sites[i]
0275: .getNAME())) {
0276: String name = sites[i].getNAME();
0277: String value = sites[i].getINDEXDIR();
0278:
0279: if (value == null || value.equals(""))
0280: value = Previewer.getIndexDir()
0281: + "/" + name;
0282:
0283: sitelist.put(name, value);
0284:
0285: searcher = SnapperManager.getInstance()
0286: .getSearcherFactory()
0287: .newSearcher();
0288: searcher.setUpSearcher(sitelist);
0289: searcher
0290: .searchDocs(
0291: "path:\""
0292: + BuilderUtil
0293: .filterForQuery(tempabsolutFilePath)
0294: + "\"", "");
0295:
0296: SearchResult sR = null;
0297: if (showNumberOf <= 0)
0298: sR = searcher.getSearchResult();
0299: else
0300: sR = searcher.getSearchResult(0,
0301: showNumberOf + 1);
0302:
0303: ResultElement[] re = sR
0304: .getResultElements();
0305: searcher.close();
0306: searcher = null;
0307:
0308: if (!toShowcontainerFilesData) {
0309:
0310: if (re != null) {
0311:
0312: ContainerFileList atachedFileListO = null;
0313:
0314: for (int j = 0; j < re.length; j++) {
0315: String luceneIDS = re[j]
0316: .getLuceneID();
0317: //String fileName = re[j].getAbsolutFileName();
0318: String fullPath = re[j]
0319: .getAbsolutFilePath();
0320:
0321: String atachedFileName = "";
0322:
0323: if (fullPath
0324: .startsWith(tempabsolutFilePath
0325: + "|")) {
0326: atachedFileName = fullPath
0327: .substring(
0328: tempabsolutFilePath
0329: .length() + 1,
0330: fullPath
0331: .length());
0332: } else
0333: continue;
0334:
0335: if (!atachedFileName
0336: .equals("")) {
0337:
0338: /* if(atachedFileName.indexOf("|")==-1)
0339: {*/
0340: if (showNumberOf == 0
0341: || numberOfAdded < showNumberOf) {
0342: if (atachedFileListO == null)
0343: atachedFileListO = previewImp
0344: .addNewContainerFileList();
0345:
0346: ContainerFile atachFileO = atachedFileListO
0347: .addNewContainerFile();
0348: atachFileO
0349: .setLuceneID(luceneIDS);
0350: atachFileO
0351: .setInsideContainerFilePath(BuilderUtil
0352: .createInsideContainerFilePath(BuilderUtil
0353: .filterAmp(atachedFileName)));
0354: numberOfAdded++;
0355: } else {
0356: break;
0357: }
0358: }
0359: // }
0360:
0361: }
0362:
0363: }
0364: } else {
0365: BuilderUtil
0366: .createContainerFileList(
0367: previewImp
0368: .addNewContainerFileList(),
0369: re,
0370: tempabsolutFilePath,
0371: siteName, indexDir,
0372: query, sitelist,
0373: showNumberOf);
0374: }
0375: }
0376: }
0377: }
0378: } catch (Exception e) {
0379: }
0380: String pO = BuilderUtil.filterAmp(properties);
0381: previewImp.setProperties(pO);
0382: previewImp = BuilderUtil.prepareEmailFiles(previewImp);
0383:
0384: } else if (fileType.equalsIgnoreCase("directory")
0385: || fileType.equalsIgnoreCase("zip")
0386: || fileType.equalsIgnoreCase("tar")
0387: || fileType.equalsIgnoreCase("gz")
0388: || fileType.equalsIgnoreCase("bz2")
0389: || fileType.equalsIgnoreCase("pst")
0390: || fileType.equalsIgnoreCase("eml")) {
0391: int numberOfAdded = 0;
0392:
0393: Properties sitelist = new Properties();
0394: Site[] sites = null;
0395:
0396: if (sl != null)
0397: sites = sl.getList();
0398:
0399: try {
0400: if (sites != null) {
0401: for (int i = 0; i < sites.length; i++) {
0402:
0403: if (siteName != null
0404: && siteName.equals(sites[i]
0405: .getNAME())) {
0406: String name = sites[i].getNAME();
0407: String value = sites[i].getINDEXDIR();
0408:
0409: if (value == null || value.equals(""))
0410: value = Previewer.getIndexDir()
0411: + "/" + name;
0412:
0413: sitelist.put(name, value);
0414:
0415: searcher = SnapperManager.getInstance()
0416: .getSearcherFactory()
0417: .newSearcher();
0418: searcher.setUpSearcher(sitelist);
0419: searcher
0420: .searchDocs(
0421: "path:\""
0422: + BuilderUtil
0423: .filterForQuery(tempabsolutFilePath)
0424: + "\"", "");
0425:
0426: SearchResult sR = null;
0427: if (showNumberOf <= 0)
0428: sR = searcher.getSearchResult();
0429: else
0430: sR = searcher.getSearchResult(0,
0431: showNumberOf + 1);
0432:
0433: ResultElement[] re = sR
0434: .getResultElements();
0435: searcher.close();
0436: searcher = null;
0437:
0438: if (!toShowcontainerFilesData) {
0439:
0440: if (re != null) {
0441: ContainerFileList atachedFileListO = null;
0442: for (int j = 0; j < re.length; j++) {
0443: //String indexPath = re[j].getAbsolutFilePath();
0444: String luceneIDS = re[j]
0445: .getLuceneID();
0446: //String fileName = re[j].getAbsolutFileName();
0447: String fullPath = re[j]
0448: .getAbsolutFilePath();
0449: String insideFileType = re[j]
0450: .getFileType();
0451: String atachedFileName = "";
0452:
0453: if (fullPath
0454: .startsWith(tempabsolutFilePath
0455: + "|")
0456: && !fileType
0457: .equalsIgnoreCase("directory")) {
0458: atachedFileName = fullPath
0459: .substring(
0460: tempabsolutFilePath
0461: .length() + 1,
0462: fullPath
0463: .length());
0464: } else if (fileType
0465: .equalsIgnoreCase("directory")
0466: && fullPath
0467: .startsWith(tempabsolutFilePath)
0468: && !fullPath
0469: .equals(tempabsolutFilePath)) {
0470:
0471: if (tempabsolutFilePath
0472: .endsWith("/")
0473: || tempabsolutFilePath
0474: .endsWith("\\"))
0475: atachedFileName = fullPath
0476: .substring(
0477: tempabsolutFilePath
0478: .length(),
0479: fullPath
0480: .length());
0481: else
0482: atachedFileName = fullPath
0483: .substring(
0484: tempabsolutFilePath
0485: .length() + 1,
0486: fullPath
0487: .length());
0488:
0489: if (insideFileType
0490: .equalsIgnoreCase("directory")
0491: && !(atachedFileName
0492: .endsWith("\\") || atachedFileName
0493: .endsWith("/"))) {
0494: atachedFileName = atachedFileName
0495: + File.separator;
0496: }
0497:
0498: } else
0499: continue;
0500:
0501: if (!atachedFileName
0502: .equals("")) {
0503: /* if(atachedFileName.indexOf("|")==-1)
0504: {*/
0505:
0506: if (showNumberOf == 0
0507: || numberOfAdded < showNumberOf) {
0508:
0509: if (atachedFileListO == null) {
0510: atachedFileListO = previewImp
0511: .addNewContainerFileList();
0512: }
0513:
0514: ContainerFile atachFileO = atachedFileListO
0515: .addNewContainerFile();
0516: atachFileO
0517: .setLuceneID(luceneIDS);
0518: atachFileO
0519: .setInsideContainerFilePath(BuilderUtil
0520: .createInsideContainerFilePath(BuilderUtil
0521: .filterAmp(atachedFileName)));
0522: numberOfAdded++;
0523:
0524: } else {
0525: break;
0526: }
0527: }
0528: // }
0529: }
0530:
0531: }
0532: } else {
0533: BuilderUtil
0534: .createContainerFileList(
0535: previewImp
0536: .addNewContainerFileList(),
0537: re,
0538: tempabsolutFilePath,
0539: siteName, indexDir,
0540: query, sitelist,
0541: showNumberOf);
0542: }
0543:
0544: }
0545: }
0546: }
0547:
0548: } catch (Exception e) {
0549:
0550: }
0551: String pO = BuilderUtil.filterAmp(properties);
0552: previewImp.setProperties(pO);
0553: previewImp = BuilderUtil.prepareEmailFiles(previewImp);
0554:
0555: } else if (fileType.equalsIgnoreCase("g3f")
0556: || fileType.equalsIgnoreCase("tiff")
0557: || fileType.equalsIgnoreCase("tif")) {
0558: //int numberOfAdded = 0;
0559:
0560: String finalPath = retVal.getAbsolutFilePath();
0561: File picture = null;
0562: boolean isContainer = false;
0563:
0564: FileContainerManager manager = null;
0565:
0566: if (showNumberOf == 0 || showNumberOf > 0) {
0567:
0568: if (finalPath.indexOf("|") != -1) {
0569: try {
0570: manager = FileContainerManagerFactory
0571: .getFileContainerManager("org.enhydra.snapperPreviewer.business.FileContainerManagerImpl");
0572: manager.init();
0573: picture = manager.getContainerFile(
0574: finalPath, siteName);
0575: } catch (Exception e) {
0576: picture = null;
0577: }
0578: isContainer = true;
0579: } else {
0580: if (!retVal.getAbsolutFilePath().startsWith(
0581: "ftp")) {
0582: picture = new File(finalPath);
0583: } else {
0584: picture = downloadFtpFile(finalPath,
0585: fileType);
0586: }
0587: }
0588: }
0589:
0590: if (picture != null && picture.exists()) {
0591: BuilderUtil.createPictureContainerFileList(
0592: previewImp.addNewContainerFileList(),
0593: picture.getAbsolutePath(), showNumberOf);
0594: }
0595:
0596: if (isContainer && manager != null)
0597: manager.deleteAllTempFiles();
0598:
0599: } else if (fileType.equalsIgnoreCase("pst")
0600: || fileType.equalsIgnoreCase("eml")) {
0601: //pO.setValue(BuilderUtil.filterAmp(properties));
0602: String pO = BuilderUtil.filterAmp(properties);
0603: previewImp.setProperties(pO);
0604: previewImp = BuilderUtil.prepareEmailFiles(previewImp);
0605: } else if (fileType.equalsIgnoreCase("pdf")) {
0606: //int numberOfAdded = 0;
0607:
0608: String finalPdfPath = retVal.getAbsolutFilePath();
0609: File pdfFile = null;
0610: boolean isContainer = false;
0611: FileContainerManager manager = null;
0612:
0613: if (showNumberOf >= 0) {
0614: if (finalPdfPath.indexOf("|") != -1) {
0615: try {
0616: manager = FileContainerManagerFactory
0617: .getFileContainerManager("org.enhydra.snapperPreviewer.business.FileContainerManagerImpl");
0618: pdfFile = manager.getContainerFile(
0619: finalPdfPath, siteName);
0620: } catch (Exception e) {
0621: pdfFile = null;
0622: }
0623: isContainer = true;
0624: } else {
0625: if (!retVal.getAbsolutFilePath().startsWith(
0626: "ftp")) {
0627: pdfFile = new File(finalPdfPath);
0628: } else {
0629: pdfFile = downloadFtpFile(finalPdfPath,
0630: fileType);
0631: }
0632: }
0633:
0634: if (pdfFile != null && pdfFile.exists()) {
0635: BuilderUtil.createPDFContainerFileList(
0636: previewImp.addNewContainerFileList(),
0637: pdfFile, finalPdfPath, showNumberOf,
0638: toShowcontainerFilesData);
0639: } else {
0640:
0641: if (Previewer.isRelitive()) {
0642: try {
0643: SiteList sl1 = SiteListFactory
0644: .getSiteList("org.enhydra.snapperPreviewer.business.SiteListImpl");
0645:
0646: Site site = sl1
0647: .findSiteByName(siteName);
0648:
0649: Path[] pathList = site.getPathList();
0650:
0651: if (pathList != null
0652: && pathList.length == 1) {
0653: String mapRoot = pathList[0]
0654: .getMappingRoot();
0655: mapRoot = mapRoot + "/"
0656: + pdfFile.getPath();
0657: pdfFile = new File(mapRoot);
0658:
0659: }
0660: } catch (Exception e) {
0661: Previewer.logError(e.getMessage());
0662: }
0663: }
0664:
0665: BuilderUtil.createPDFContainerFileList(
0666: previewImp.addNewContainerFileList(),
0667: pdfFile, finalPdfPath, showNumberOf,
0668: toShowcontainerFilesData);
0669: }
0670:
0671: if (isContainer && manager != null)
0672: manager.deleteAllTempFiles();
0673: }
0674: properties = BuilderUtil.filterAmp(properties);
0675: properties = BuilderUtil.filterNewLine(properties);
0676: previewImp.setProperties(properties);
0677:
0678: } else {
0679: properties = BuilderUtil.filterAmp(properties);
0680: properties = BuilderUtil.filterNewLine(properties);
0681: previewImp.setProperties(properties);
0682: }
0683: summary.setPreview(previewImp);
0684:
0685: ByteArrayOutputStream tmpStream = null;
0686:
0687: try {
0688: tmpStream = new ByteArrayOutputStream();
0689: sd.save(tmpStream);
0690: return tmpStream.toString("UTF-8");
0691: } finally {
0692: if (tmpStream != null) {
0693: try {
0694: tmpStream.close();
0695: } catch (Exception e) {
0696: }
0697: tmpStream = null;
0698: }
0699: }
0700:
0701: } catch (Exception e) {
0702: Previewer.logError(" Problem in Previewer Builder : "
0703: + e.getMessage());
0704: }
0705: return "";
0706: }
0707:
0708: // parse on fly
0709: public String getDocument(String fullFilePath, DBTransaction t,
0710: String langpair, boolean toShowcontainerFilesData,
0711: int showNumberOf, Properties pictureConversionRequestParm,
0712: Properties parserLimitProperties) {
0713:
0714: try {
0715: File file = null;
0716: boolean isContainer = false;
0717: FileContainerManager manager = null;
0718: manager = FileContainerManagerFactory
0719: .getFileContainerManager("org.enhydra.snapperPreviewer.business.FileContainerManagerImpl");
0720: manager.init();
0721:
0722: String typeOfFile = "";
0723:
0724: boolean isPstContainer = false;
0725: boolean msgInMsg = false;
0726: String firstMsgPath = null;
0727:
0728: String rest = "";
0729:
0730: if (fullFilePath.indexOf("|") != -1) {
0731: try {
0732:
0733: if (fullFilePath.endsWith(".msg")
0734: || fullFilePath.endsWith(".MSG")) {
0735: boolean more = true;
0736: firstMsgPath = fullFilePath;
0737: do {
0738: int ind = firstMsgPath.lastIndexOf("|");
0739:
0740: if (ind != -1) {
0741: String temp = firstMsgPath.substring(0,
0742: ind);
0743:
0744: if (temp.endsWith(".msg")) {
0745: msgInMsg = true;
0746: firstMsgPath = temp;
0747:
0748: } else if (temp.endsWith(".MSG")) {
0749: msgInMsg = true;
0750: firstMsgPath = temp;
0751: } else {
0752: more = false;
0753: }
0754: } else
0755: more = false;
0756:
0757: } while (more);
0758: }
0759:
0760: if (msgInMsg) {
0761: manager.init();
0762: file = manager.getContainerFile(firstMsgPath,
0763: null);
0764: } else {
0765: file = manager.getContainerFile(fullFilePath,
0766: null);
0767: rest = manager.getRestOfFilePath();
0768: }
0769:
0770: } catch (Exception e) {
0771: file = null;
0772: }
0773: isContainer = true;
0774:
0775: String filePath = "";
0776: if (file != null)
0777: filePath = file.getAbsolutePath();
0778:
0779: int indexOfExtension = filePath.lastIndexOf(".");
0780: if (indexOfExtension != -1) {
0781: typeOfFile = filePath.substring(
0782: indexOfExtension + 1, filePath.length());
0783: }
0784:
0785: } else {
0786:
0787: int indexOfExtension = fullFilePath.lastIndexOf(".");
0788: if (indexOfExtension != -1) {
0789: typeOfFile = fullFilePath
0790: .substring(indexOfExtension + 1,
0791: fullFilePath.length());
0792: }
0793:
0794: if (!fullFilePath.startsWith("ftp")) {
0795: file = new File(fullFilePath);
0796: } else {
0797:
0798: file = downloadFtpFile(fullFilePath, typeOfFile);
0799: }
0800: }
0801:
0802: StringTokenizer st = new StringTokenizer(fullFilePath, "|");
0803: String pstMailNum = null;
0804:
0805: if (st.countTokens() == 2) {
0806: String root = st.nextToken();
0807: String temp = "";
0808: int indexOfExtension = root.lastIndexOf(".");
0809: if (indexOfExtension != -1) {
0810: temp = root.substring(indexOfExtension + 1, root
0811: .length());
0812: }
0813: if (temp.equalsIgnoreCase("pst")) {
0814: isPstContainer = true;
0815: file = new File(root);
0816: pstMailNum = st.nextToken();
0817: }
0818: }
0819:
0820: SummaryDocument sd = SummaryDocument.Factory.newInstance();
0821: Summary summary = sd.addNewSummary();
0822:
0823: SearchedParameters searchedParameters = summary
0824: .addNewSearchedParameters();
0825:
0826: Parameter perameter = searchedParameters.addNewParameter();
0827: perameter.setKey("fullFilePath");
0828: perameter.setValue(BuilderUtil.filterAmp(fullFilePath));
0829:
0830: Parameter perameter1 = searchedParameters.addNewParameter();
0831: perameter1.setKey("parseOnFly");
0832: perameter1.setValue("true");
0833: parserLimitProperties = SnapperManager.getInstance()
0834: .getParserLimitProperties();
0835:
0836: addSearchParameters(searchedParameters,
0837: pictureConversionRequestParm, parserLimitProperties);
0838:
0839: Preview previewImp = summary.addNewPreview();
0840:
0841: FileParserMaster parser = new FileParserMaster();
0842:
0843: Properties ft = new Properties();
0844:
0845: try {
0846:
0847: DocumentGroup group = SiteListImpl.forAllDocumentGroup;
0848:
0849: if (group != null) {
0850:
0851: Vector gropNames = (Vector) group
0852: .getListOfDocumentGroup();
0853:
0854: for (int i = 0; i < gropNames.size(); i++) {
0855:
0856: FileType[] groupName = group
0857: .getListOfFILETYPES((String) gropNames
0858: .elementAt(i));
0859:
0860: for (int j = 0; j < groupName.length; j++) {
0861: if (!ft.containsKey(groupName[j]
0862: .getEXTENSION()))
0863: ft.put(groupName[j].getEXTENSION(),
0864: groupName[j].getPARSER());
0865: }
0866: }
0867:
0868: }
0869:
0870: } catch (Exception e) {
0871: ft = SnapperManager.getInstance()
0872: .getFileTypeProperties();
0873: }
0874:
0875: Vector parsedData = new Vector();
0876:
0877: String currentTime = (new Long(System.currentTimeMillis()))
0878: .toString();
0879:
0880: if (file != null && !file.isDirectory()) {
0881: try {
0882: if (file != null && !isPstContainer && !msgInMsg) {
0883: parser.setIndexDirectory(true);
0884: parser.setIndexUnknownFileTypes(true);
0885: parser.setProp(ft);
0886: parser.setConversionTypes(Previewer
0887: .getConverterClassNames());
0888: parser.setIndexContent(true);
0889: parser.setLimit(parserLimitProperties);
0890: parser.setConversionPath(SnapperManager
0891: .getInstance().getTempDir()
0892: + File.separator + "conversion");
0893: parser.setSource(1);
0894: parser.setTimeLimit(Previewer.getTimeLimit());
0895:
0896: try {
0897: parser.setWord2007HTMLTransformer(Previewer
0898: .getWord2007HTMLTemplates()
0899: .newTransformer());
0900: parser.setWord2007TextTransformer(Previewer
0901: .getWord2007TextTemplates()
0902: .newTransformer());
0903: parser
0904: .setExcel2007HTMLTransformer(Previewer
0905: .getExcel2007HTMLTemplates()
0906: .newTransformer());
0907: parser
0908: .setExcel2007TextTransformer(Previewer
0909: .getExcel2007TextTemplates()
0910: .newTransformer());
0911: parser
0912: .setPowerpoint2007HTMLTransformer(Previewer
0913: .getPowerpoint2007HTMLTemplates()
0914: .newTransformer());
0915: parser
0916: .setPowerpoint2007TextTransformer(Previewer
0917: .getPowerpoint2007TextTemplates()
0918: .newTransformer());
0919: } catch (Exception e) {
0920: Previewer
0921: .logError("Unable to create Office 2007 transformers!!!");
0922: }
0923:
0924: parsedData = parser.parse(file, SnapperManager
0925: .getInstance().getTempDir()
0926: + File.separator + currentTime);
0927: } else if (file != null && isPstContainer
0928: && !msgInMsg) {
0929: Wrapper wrapper = WrapperFactory
0930: .getWrapper("org.enhydra.snapperPreviewer.business.WrapperImpl");
0931:
0932: if (wrapper != null)
0933: parsedData = wrapper.getPSTData(pstMailNum,
0934: parserLimitProperties, file);
0935:
0936: } else if (file != null && msgInMsg) {
0937: Wrapper wrapper = WrapperFactory
0938: .getWrapper("org.enhydra.snapperPreviewer.business.WrapperImpl");
0939:
0940: if (wrapper != null)
0941: parsedData = wrapper.getMsgData(
0942: fullFilePath,
0943: parserLimitProperties, file);
0944: } else {
0945: String title = "";
0946: int indexOfFileName = fullFilePath
0947: .lastIndexOf(File.separator);
0948:
0949: if (indexOfFileName != -1)
0950: title = fullFilePath
0951: .substring(indexOfFileName + 1);
0952:
0953: parsedData.add(
0954: ParserDataConstants.FILE_PATH_POSITION,
0955: fullFilePath);
0956: parsedData.add(
0957: ParserDataConstants.FILE_TYPE_POSITION,
0958: typeOfFile);
0959: parsedData.add(
0960: ParserDataConstants.TITLE_POSITION,
0961: title);
0962: parsedData
0963: .add(
0964: ParserDataConstants.LAST_MODIFIED_DATE_POSITION,
0965: "");
0966: parsedData
0967: .add(
0968: ParserDataConstants.PROPERTIES_POSITION,
0969: "");
0970: parsedData.add(
0971: ParserDataConstants.CONTENT_POSITION,
0972: "File : " + fullFilePath
0973: + " could not be parsed");
0974: }
0975: } catch (FileParserException e0) {
0976:
0977: throw new IOException("File : " + fullFilePath
0978: + " could not be parsed, "
0979: + e0.getMessage());
0980:
0981: } catch (Throwable e1) {
0982:
0983: Previewer.logError(" Problem in parser : "
0984: + e1.getMessage());
0985: parsedData.add(
0986: ParserDataConstants.FILE_PATH_POSITION,
0987: fullFilePath);
0988: parsedData.add(
0989: ParserDataConstants.FILE_TYPE_POSITION,
0990: typeOfFile);
0991: if (file != null)
0992: parsedData.add(
0993: ParserDataConstants.TITLE_POSITION,
0994: file.getName());
0995: else {
0996: String title = "";
0997: int indexOfFileName = fullFilePath
0998: .lastIndexOf(File.separator);
0999:
1000: if (indexOfFileName != -1)
1001: title = fullFilePath
1002: .substring(indexOfFileName + 1);
1003:
1004: parsedData.add(
1005: ParserDataConstants.TITLE_POSITION,
1006: title);
1007: }
1008: parsedData
1009: .add(
1010: ParserDataConstants.LAST_MODIFIED_DATE_POSITION,
1011: "");
1012: parsedData
1013: .add(
1014: ParserDataConstants.PROPERTIES_POSITION,
1015: "");
1016: parsedData.add(
1017: ParserDataConstants.CONTENT_POSITION,
1018: "File : " + fullFilePath
1019: + " could not be parsed");
1020: }
1021:
1022: } else {
1023: if (file != null) {
1024: if (!(fullFilePath.endsWith("\\") || fullFilePath
1025: .endsWith("/")))
1026: fullFilePath = fullFilePath + File.separator;
1027:
1028: parsedData.add(
1029: ParserDataConstants.FILE_PATH_POSITION,
1030: fullFilePath);
1031: parsedData.add(
1032: ParserDataConstants.FILE_TYPE_POSITION,
1033: "directory");
1034: parsedData.add(ParserDataConstants.TITLE_POSITION,
1035: file.getName());
1036: parsedData
1037: .add(
1038: ParserDataConstants.LAST_MODIFIED_DATE_POSITION,
1039: (new Long(file.lastModified())
1040: .toString()));
1041: parsedData
1042: .add(
1043: ParserDataConstants.PROPERTIES_POSITION,
1044: "");
1045: parsedData.add(
1046: ParserDataConstants.CONTENT_POSITION, "");
1047:
1048: } else {
1049: String title = "";
1050: int indexOfFileName = fullFilePath
1051: .lastIndexOf(File.separator);
1052:
1053: if (indexOfFileName != -1)
1054: title = fullFilePath
1055: .substring(indexOfFileName + 1);
1056:
1057: parsedData.add(
1058: ParserDataConstants.FILE_PATH_POSITION,
1059: fullFilePath);
1060: parsedData.add(
1061: ParserDataConstants.FILE_TYPE_POSITION,
1062: typeOfFile);
1063: parsedData.add(ParserDataConstants.TITLE_POSITION,
1064: title);
1065: parsedData
1066: .add(
1067: ParserDataConstants.LAST_MODIFIED_DATE_POSITION,
1068: "");
1069: parsedData
1070: .add(
1071: ParserDataConstants.PROPERTIES_POSITION,
1072: "");
1073: parsedData.add(
1074: ParserDataConstants.CONTENT_POSITION,
1075: "File : " + fullFilePath
1076: + " could not be parsed");
1077: }
1078:
1079: }
1080:
1081: String titleS = (String) parsedData
1082: .get(ParserDataConstants.TITLE_POSITION);
1083: String fileTypeS = (String) parsedData
1084: .get(ParserDataConstants.FILE_TYPE_POSITION);
1085:
1086: String ftype = "File";
1087:
1088: if (file != null) {
1089: String fileName1 = file.getName();
1090:
1091: if ((fileTypeS.equalsIgnoreCase("zip") || fileTypeS
1092: .equalsIgnoreCase("tar"))
1093: && !fullFilePath.endsWith(fileName1)) {
1094: ftype = "directory";
1095: } else {
1096: fileTypeS = BuilderUtil.filterAmp(fileTypeS);
1097: ftype = fileTypeS;
1098: }
1099:
1100: } else {
1101: fileTypeS = BuilderUtil.filterAmp(fileTypeS);
1102: ftype = fileTypeS;
1103: }
1104:
1105: titleS = BuilderUtil.filterAmp(titleS);
1106: titleS = BuilderUtil.filter(titleS, true);
1107:
1108: previewImp.setTitle(titleS);
1109: previewImp.setFileType(ftype);
1110:
1111: if (file != null) {
1112: previewImp.setAbsolutFileName(BuilderUtil
1113: .filterAmp(file.getName()));
1114: } else {
1115: String fileName = fullFilePath;
1116:
1117: int indexOfContainer = fullFilePath.lastIndexOf("|");
1118:
1119: if (indexOfContainer != -1)
1120: fileName = fullFilePath
1121: .substring(indexOfContainer + 1);
1122:
1123: int indexOfFileName = fileName
1124: .lastIndexOf(File.separator);
1125:
1126: if (indexOfFileName != -1)
1127: fileName = fileName.substring(indexOfFileName + 1);
1128:
1129: previewImp.setAbsolutFileName(BuilderUtil
1130: .filterAmp(fileName));
1131: }
1132:
1133: previewImp.setAbsolutFilePath(BuilderUtil
1134: .createAbsolutFilePath(BuilderUtil
1135: .filterAmp(fullFilePath)));
1136:
1137: String contentS = (String) parsedData
1138: .get(ParserDataConstants.CONTENT_POSITION);
1139:
1140: if ((fileTypeS.equalsIgnoreCase("doc") || (fileTypeS
1141: .equalsIgnoreCase("xls")
1142: || fileTypeS.equalsIgnoreCase("ppt") || fileTypeS
1143: .equalsIgnoreCase("pps")))
1144: && Previewer.getConverterClassNames() != null) {
1145: try {
1146: String convertedS = (String) parsedData
1147: .get(ParserDataConstants.CONVERTED_CONTENT_POSITION);
1148: String extens = (String) parsedData
1149: .get(ParserDataConstants.CONVERTED_CONTENT_EXTENSION_POSITION);
1150:
1151: if (extens != null
1152: && extens.equalsIgnoreCase("html")
1153: && convertedS != null
1154: && !convertedS.equalsIgnoreCase("")
1155: && langpair.equals("none")) {
1156: contentS = BuilderUtil
1157: .filter(convertedS, false);
1158: } else {
1159: contentS = BuilderUtil.filter(contentS, true);
1160: }
1161: } catch (Exception exx) {
1162: contentS = BuilderUtil.filter(contentS, true);
1163: }
1164: } else {
1165: contentS = BuilderUtil.filter(contentS, true);
1166: }
1167:
1168: if ((fileTypeS.equalsIgnoreCase("docx"))
1169: || (fileTypeS.equalsIgnoreCase("xlsx"))
1170: || (fileTypeS.equalsIgnoreCase("pptx"))) {
1171: try {
1172: String convertedS = (String) parsedData
1173: .get(ParserDataConstants.CONVERTED_CONTENT_POSITION);
1174: String extens = (String) parsedData
1175: .get(ParserDataConstants.CONVERTED_CONTENT_EXTENSION_POSITION);
1176:
1177: if (extens != null
1178: && extens.equalsIgnoreCase("html")
1179: && convertedS != null
1180: && !convertedS.equalsIgnoreCase("")
1181: && langpair.equals("none")) {
1182: contentS = BuilderUtil
1183: .filter(convertedS, false);
1184: } else {
1185: contentS = BuilderUtil.filter(contentS, true);
1186: }
1187: } catch (Exception exx) {
1188: contentS = BuilderUtil.filter(contentS, true);
1189: }
1190: }
1191:
1192: if (!langpair.equals("none")) {
1193: contentS = BuilderUtil.replaceAll(contentS, "\n",
1194: " # # ");
1195: //contentS = BuilderUtil.replaceAll(contentS, "<", " 0#8 ");
1196: //contentS = BuilderUtil.replaceAll(contentS, ">", " 0#9 ");
1197: contentS = googleTranslate(contentS, langpair);
1198: contentS = BuilderUtil
1199: .replaceAll(contentS, "# #", "\n");
1200: //contentS = BuilderUtil.replaceAll(contentS, " 0#8", "<");
1201: //contentS = BuilderUtil.replaceAll(contentS, "0#9 ", ">");
1202: previewImp.setContent(contentS);
1203: }
1204:
1205: contentS = BuilderUtil.filterAmp(contentS);
1206: contentS = BuilderUtil.filterNewLine(contentS);
1207: contentS = BuilderUtil.filterContentStartNewLine(contentS);
1208:
1209: String propertiesS = (String) parsedData
1210: .get(ParserDataConstants.PROPERTIES_POSITION);
1211: propertiesS = BuilderUtil.filter(propertiesS, true);
1212: previewImp.setContent(contentS);
1213:
1214: if ((fileTypeS.equalsIgnoreCase("msg") && !msgInMsg)
1215: || fileTypeS.equalsIgnoreCase("eml")) {
1216:
1217: BuilderUtil.createContainerFileList(previewImp
1218: .addNewContainerFileList(), parser,
1219: fullFilePath, "", toShowcontainerFilesData,
1220: showNumberOf);
1221:
1222: previewImp.setProperties(BuilderUtil
1223: .filterAmp(propertiesS));
1224: previewImp = BuilderUtil.prepareEmailFiles(previewImp);
1225: } else if (fileTypeS.equalsIgnoreCase("msg") && msgInMsg) {
1226: try {
1227: Vector temp = (Vector) parsedData.get(6);
1228:
1229: ContainerFileList atachedFileListO = null;
1230:
1231: if (temp.size() > 0) {
1232: atachedFileListO = previewImp
1233: .addNewContainerFileList();
1234:
1235: }
1236:
1237: if (!toShowcontainerFilesData) {
1238:
1239: for (int k = 0; k < temp.size(); k++) {
1240:
1241: ContainerFile cf = atachedFileListO
1242: .addNewContainerFile();
1243: cf
1244: .setInsideContainerFilePath(BuilderUtil
1245: .createInsideContainerFilePath((String) temp
1246: .get(k)));
1247: }
1248:
1249: } else {
1250: for (int k = 0; k < temp.size(); k++) {
1251: String insidePath = (String) temp.get(k);
1252:
1253: File fileToParse = null;
1254:
1255: if (insidePath.indexOf("|") == -1) {
1256: String newFilePathToParse = fullFilePath
1257: + "|" + insidePath;
1258:
1259: String ifMsgInMsgNewPath = null;
1260:
1261: // taj parsiramo jer je prvi novi, u suprotnom preskacemo
1262: try {
1263: manager = FileContainerManagerFactory
1264: .getFileContainerManager("org.enhydra.snapperPreviewer.business.FileContainerManagerImpl");
1265:
1266: if (newFilePathToParse
1267: .endsWith(".msg")
1268: || newFilePathToParse
1269: .endsWith(".MSG")) {
1270: boolean more = true;
1271: ifMsgInMsgNewPath = newFilePathToParse;
1272: do {
1273: int ind = ifMsgInMsgNewPath
1274: .lastIndexOf("|");
1275:
1276: if (ind != -1) {
1277: String temp1 = ifMsgInMsgNewPath
1278: .substring(0,
1279: ind);
1280:
1281: if (temp1
1282: .endsWith(".msg")) {
1283: msgInMsg = true;
1284: ifMsgInMsgNewPath = temp1;
1285:
1286: } else if (temp1
1287: .endsWith(".MSG")) {
1288: msgInMsg = true;
1289: ifMsgInMsgNewPath = temp1;
1290: } else {
1291: more = false;
1292: }
1293: } else
1294: more = false;
1295:
1296: } while (more);
1297:
1298: fileToParse = manager
1299: .getContainerFile(
1300: ifMsgInMsgNewPath,
1301: null);
1302:
1303: int ind = newFilePathToParse
1304: .lastIndexOf("|");
1305: String inside = newFilePathToParse
1306: .substring(
1307: ind + 1,
1308: newFilePathToParse
1309: .length());
1310:
1311: Wrapper wrapper = WrapperFactory
1312: .getWrapper("org.enhydra.snapperPreviewer.business.WrapperImpl");
1313:
1314: if (wrapper != null)
1315: parsedData = wrapper
1316: .getMsgData(
1317: newFilePathToParse,
1318: parserLimitProperties,
1319: fileToParse);
1320:
1321: String titleAtacmentS = (String) parsedData
1322: .get(ParserDataConstants.TITLE_POSITION);
1323: String fileAtacmentTypeS = (String) parsedData
1324: .get(ParserDataConstants.FILE_TYPE_POSITION);
1325: //String filePathAtacmentTypeS = (String) parsedData.get(ParserDataConstants.FILE_PATH_POSITION);
1326: String contentAtached = (String) parsedData
1327: .get(ParserDataConstants.CONTENT_POSITION);
1328: //Long lastModifiedL = (Long) parsedData.get(ParserDataConstants.LAST_MODIFIED_DATE_POSITION);
1329: String propertS = (String) parsedData
1330: .get(ParserDataConstants.PROPERTIES_POSITION);
1331:
1332: Vector temp1 = (Vector) parsedData
1333: .get(6);
1334:
1335: if (temp1.size() > 0) {
1336: atachedFileListO = previewImp
1337: .addNewContainerFileList();
1338: }
1339:
1340: for (int kk = 0; kk < temp1
1341: .size(); kk++) {
1342: ContainerFile cf = atachedFileListO
1343: .addNewContainerFile();
1344: cf
1345: .setInsideContainerFilePath(BuilderUtil
1346: .createInsideContainerFilePath((String) temp1
1347: .get(kk)));
1348:
1349: }
1350:
1351: ContainerFile containerFile = atachedFileListO
1352: .addNewContainerFile();
1353:
1354: Preview currentAttacmentPrev = BuilderUtil
1355: .createPreview(
1356: containerFile
1357: .addNewPreview(),
1358: BuilderUtil
1359: .filterAmp(titleAtacmentS),
1360: BuilderUtil
1361: .filterAmp(fileAtacmentTypeS),
1362: BuilderUtil
1363: .filterAmp(inside),
1364: BuilderUtil
1365: .filterAmp(newFilePathToParse),
1366: null,
1367: null,
1368: null,
1369: null,
1370: null,
1371: null,
1372: BuilderUtil
1373: .filterAmp(contentAtached),
1374: BuilderUtil
1375: .filterAmp(propertS),
1376: null, null);
1377:
1378: currentAttacmentPrev = BuilderUtil
1379: .prepareEmailFiles((Preview) currentAttacmentPrev);
1380: // containerFile.setPreview(currentAttacmentPrev);
1381:
1382: } else {
1383: fileToParse = manager
1384: .getContainerFile(
1385: newFilePathToParse,
1386: null);
1387:
1388: if (file != null) {
1389: parser
1390: .setIndexDirectory(true);
1391: parser
1392: .setIndexUnknownFileTypes(true);
1393: parser.setProp(ft);
1394: parser
1395: .setIndexContent(true);
1396: parser
1397: .setLimit(parserLimitProperties);
1398: parser.setSource(1);
1399: parser
1400: .setTimeLimit(Previewer
1401: .getTimeLimit());
1402:
1403: try {
1404: parser
1405: .setWord2007HTMLTransformer(Previewer
1406: .getWord2007HTMLTemplates()
1407: .newTransformer());
1408: parser
1409: .setWord2007HTMLTransformer(Previewer
1410: .getWord2007TextTemplates()
1411: .newTransformer());
1412: parser
1413: .setExcel2007HTMLTransformer(Previewer
1414: .getExcel2007HTMLTemplates()
1415: .newTransformer());
1416: parser
1417: .setExcel2007TextTransformer(Previewer
1418: .getExcel2007TextTemplates()
1419: .newTransformer());
1420: parser
1421: .setPowerpoint2007HTMLTransformer(Previewer
1422: .getPowerpoint2007HTMLTemplates()
1423: .newTransformer());
1424: parser
1425: .setPowerpoint2007TextTransformer(Previewer
1426: .getPowerpoint2007TextTemplates()
1427: .newTransformer());
1428: } catch (Exception e) {
1429: Previewer
1430: .logError("Unable to create Office 2007 transformers!!!");
1431: }
1432:
1433: parsedData = parser
1434: .parse(
1435: fileToParse,
1436: SnapperManager
1437: .getInstance()
1438: .getTempDir()
1439: + File.separator
1440: + currentTime);
1441: } else {
1442: String title1 = "";
1443: int indexOfFileName = newFilePathToParse
1444: .lastIndexOf(File.separator);
1445: int indexOfFileExt = newFilePathToParse
1446: .lastIndexOf(".");
1447:
1448: if (indexOfFileName != -1
1449: && indexOfFileExt != -1
1450: && indexOfFileName < indexOfFileExt)
1451: title1 = newFilePathToParse
1452: .substring(indexOfFileName + 1);
1453:
1454: parsedData
1455: .add(
1456: ParserDataConstants.FILE_PATH_POSITION,
1457: newFilePathToParse);
1458:
1459: if (indexOfFileExt != -1)
1460: parsedData
1461: .add(
1462: ParserDataConstants.FILE_TYPE_POSITION,
1463: newFilePathToParse
1464: .substring(indexOfFileExt + 1));
1465: else
1466: parsedData
1467: .add(
1468: ParserDataConstants.FILE_TYPE_POSITION,
1469: "File");
1470:
1471: parsedData
1472: .add(
1473: ParserDataConstants.TITLE_POSITION,
1474: title1);
1475: parsedData
1476: .add(
1477: ParserDataConstants.LAST_MODIFIED_DATE_POSITION,
1478: "");
1479: parsedData
1480: .add(
1481: ParserDataConstants.PROPERTIES_POSITION,
1482: "");
1483: parsedData
1484: .add(
1485: ParserDataConstants.CONTENT_POSITION,
1486: "File : "
1487: + newFilePathToParse
1488: + " could not be parsed");
1489: }
1490:
1491: String titleAtacmentS = (String) parsedData
1492: .get(ParserDataConstants.TITLE_POSITION);
1493: String fileAtacmentTypeS = (String) parsedData
1494: .get(ParserDataConstants.FILE_TYPE_POSITION);
1495: //String filePathAtacmentTypeS = (String) parsedData.get(ParserDataConstants.FILE_PATH_POSITION);
1496: String contentAtached = (String) parsedData
1497: .get(ParserDataConstants.CONTENT_POSITION);
1498: //Long lastModifiedL = (Long) parsedData.get(ParserDataConstants.LAST_MODIFIED_DATE_POSITION);
1499: String propertS = (String) parsedData
1500: .get(ParserDataConstants.PROPERTIES_POSITION);
1501:
1502: ContainerFile containerFile = atachedFileListO
1503: .addNewContainerFile();
1504:
1505: Preview currentAttacmentPrev = BuilderUtil
1506: .createPreview(
1507: containerFile
1508: .addNewPreview(),
1509: BuilderUtil
1510: .filterAmp(titleAtacmentS),
1511: BuilderUtil
1512: .filterAmp(fileAtacmentTypeS),
1513: BuilderUtil
1514: .filterAmp(insidePath),
1515: BuilderUtil
1516: .filterAmp(newFilePathToParse),
1517: null,
1518: null,
1519: null,
1520: null,
1521: null,
1522: null,
1523: BuilderUtil
1524: .filterAmp(contentAtached),
1525: BuilderUtil
1526: .filterAmp(propertS),
1527: null, null);
1528:
1529: BuilderUtil
1530: .createContainerFileList(
1531: currentAttacmentPrev
1532: .addNewContainerFileList(),
1533: parser,
1534: newFilePathToParse,
1535: "", false,
1536: showNumberOf);
1537:
1538: parser.close();
1539: }
1540:
1541: } catch (Exception e) {
1542: file = null;
1543: }
1544: }
1545: }
1546: }
1547: previewImp.setProperties(BuilderUtil
1548: .filterAmp(propertiesS));
1549: previewImp = BuilderUtil
1550: .prepareEmailFiles(previewImp);
1551: } catch (Exception e) {
1552: }
1553:
1554: } else if (fileTypeS.equalsIgnoreCase("pst")
1555: && isPstContainer) {
1556: try {
1557: Vector temp = (Vector) parsedData.get(6);
1558:
1559: ContainerFileList atachedFileListO = null;
1560:
1561: if (temp.size() > 0) {
1562: atachedFileListO = previewImp
1563: .addNewContainerFileList();
1564: }
1565:
1566: for (int k = 0; k < temp.size(); k++) {
1567: ContainerFile cf = atachedFileListO
1568: .addNewContainerFile();
1569:
1570: cf
1571: .setInsideContainerFilePath(BuilderUtil
1572: .createInsideContainerFilePath((String) temp
1573: .get(k)));
1574: }
1575:
1576: previewImp.setProperties(BuilderUtil
1577: .filterAmp(propertiesS));
1578: previewImp = BuilderUtil
1579: .prepareEmailFiles(previewImp);
1580: } catch (Exception e) {
1581: }
1582:
1583: } else if (fileTypeS.equalsIgnoreCase("pdf")) {
1584: File pdfFile = file;
1585:
1586: if (showNumberOf >= 0 && pdfFile != null
1587: && pdfFile.exists()) {
1588: BuilderUtil.createPDFContainerFileList(previewImp
1589: .addNewContainerFileList(), pdfFile,
1590: fullFilePath, showNumberOf,
1591: toShowcontainerFilesData);
1592: }
1593:
1594: propertiesS = BuilderUtil.filterAmp(propertiesS);
1595: propertiesS = BuilderUtil.filterNewLine(propertiesS);
1596: previewImp.setProperties(propertiesS);
1597:
1598: } else if (fileTypeS.equalsIgnoreCase("zip")
1599: || fileTypeS.equalsIgnoreCase("tar")
1600: || fileTypeS.equalsIgnoreCase("gz")
1601: || fileTypeS.equalsIgnoreCase("bz2")
1602: || fileTypeS.equalsIgnoreCase("pst")) {
1603: BuilderUtil.createContainerFileList(previewImp
1604: .addNewContainerFileList(), parser,
1605: fullFilePath, rest, toShowcontainerFilesData,
1606: showNumberOf);
1607: } else if (fileTypeS.equalsIgnoreCase("g3f")
1608: || fileTypeS.equalsIgnoreCase("tiff")
1609: || fileTypeS.equalsIgnoreCase("tif")) {
1610: BuilderUtil.createPictureContainerFileList(previewImp
1611: .addNewContainerFileList(), file
1612: .getAbsolutePath(), showNumberOf);
1613: } else if (fileTypeS.equalsIgnoreCase("directory")) {
1614:
1615: ContainerFileList atachedFileListO = null;
1616:
1617: File[] lista = file.listFiles();
1618:
1619: if (lista != null && lista.length > 0) {
1620: atachedFileListO = previewImp
1621: .addNewContainerFileList();
1622:
1623: for (int i = 0; i < lista.length; i++) {
1624:
1625: File f = lista[i];
1626: ContainerFile atachFileO = atachedFileListO
1627: .addNewContainerFile();
1628: String path = f.getName();
1629:
1630: if (f != null && f.isDirectory())
1631: path = path + File.separator;
1632:
1633: atachFileO
1634: .setInsideContainerFilePath(BuilderUtil
1635: .createInsideContainerFilePath(BuilderUtil
1636: .filterAmp(path)));
1637: }
1638: }
1639:
1640: } else {
1641: propertiesS = BuilderUtil.filterAmp(propertiesS);
1642: propertiesS = BuilderUtil.filterNewLine(propertiesS);
1643: previewImp.setProperties(propertiesS);
1644: }
1645:
1646: if (isContainer && manager != null)
1647: manager.deleteAllTempFiles();
1648:
1649: parser.close();
1650: try {
1651: File temp = new File(SnapperManager.getInstance()
1652: .getTempDir()
1653: + File.separator + currentTime);
1654: if (temp.exists() && temp.isDirectory()) {
1655: try {
1656: FinalDeleter.deleteDir(temp);
1657: } catch (Exception ex) {
1658: }
1659: }
1660: } catch (Exception e) {
1661: }
1662:
1663: ByteArrayOutputStream tmpStream = null;
1664:
1665: try {
1666: tmpStream = new ByteArrayOutputStream();
1667: summary.setPreview(previewImp);
1668: sd.save(tmpStream);
1669: return tmpStream.toString("UTF-8");
1670: } catch (Exception e) {
1671: throw new Exception();
1672: } finally {
1673: if (tmpStream != null) {
1674: try {
1675: tmpStream.close();
1676: } catch (Exception e) {
1677: }
1678: tmpStream = null;
1679: }
1680: }
1681:
1682: } catch (Exception e) {
1683:
1684: SummaryDocument sd = SummaryDocument.Factory.newInstance();
1685: Summary summary = sd.addNewSummary();
1686:
1687: SearchedParameters searchedParameters = summary
1688: .addNewSearchedParameters();
1689:
1690: summary.setSearchedParameters(searchedParameters);
1691: BuilderUtil.createPreview(summary.addNewPreview(), "", "",
1692: "", fullFilePath, "", null, null, null, null, null,
1693: e.getMessage(), null, null, null);
1694: ByteArrayOutputStream tmpStream = null;
1695: try {
1696: tmpStream = new ByteArrayOutputStream();
1697: sd.save(tmpStream);
1698: return tmpStream.toString("UTF-8");
1699: } catch (Exception exx) {
1700: Previewer
1701: .logError(" Unexpected problem in Previewer Builder : "
1702: + e.getStackTrace().toString());
1703: } finally {
1704: if (tmpStream != null) {
1705: try {
1706: tmpStream.close();
1707: } catch (Exception e1) {
1708: }
1709: tmpStream = null;
1710: }
1711: }
1712: return "";
1713:
1714: }
1715:
1716: }
1717:
1718: // find it in index
1719: public String getDocument(String fullFilePath, String query,
1720: DBTransaction t, String langpair,
1721: boolean toShowcontainerFilesData, int showNumberOf,
1722: Properties pictureConversionRequestParm,
1723: Properties parserLimitProperties) {
1724: Searcher searcher = SnapperManager.getInstance()
1725: .getSearcherFactory().newSearcher();
1726: try {
1727: SiteList sl = SiteListFactory
1728: .getSiteList("org.enhydra.snapper.snapperPreviewer.SiteListImpl");
1729:
1730: Site[] sites = sl.getList();
1731:
1732: Properties sitelist = new Properties();
1733: if (sites != null) {
1734: for (int i = 0; i < sites.length; i++) {
1735: String name = sites[i].getNAME();
1736: String value = sites[i].getINDEXDIR();
1737:
1738: if (value == null || value.equals(""))
1739: value = Previewer.getIndexDir() + "/" + name;
1740:
1741: sitelist.put(name, value);
1742: }
1743: }
1744:
1745: searcher.setUpSearcher(sitelist);
1746: try {
1747: searcher.setUpHighLighter(new Properties());
1748: } catch (IOException e) {
1749:
1750: }
1751: searcher.searchDocs("path:"
1752: + BuilderUtil.filterForQuery(fullFilePath), "");
1753: SearchResult sR = searcher.getSearchResult();
1754: ResultElement[] re = sR.getResultElements();
1755: searcher.close();
1756: searcher = null;
1757: for (int i = 0; i < re.length; i++) {
1758: String indexPath = re[i].getAbsolutFilePath();
1759: if (indexPath.equalsIgnoreCase(fullFilePath)) {
1760: return getDocument(re[i].getSiteName(), re[i]
1761: .getLuceneID(), query, t, langpair,
1762: toShowcontainerFilesData, showNumberOf,
1763: pictureConversionRequestParm,
1764: parserLimitProperties);
1765: }
1766: }
1767: } catch (Exception e) {
1768: if (searcher != null) {
1769: try {
1770: searcher.close();
1771: } catch (Exception e1) {
1772: }
1773: }
1774: Previewer.logError(" Problem in Previewer Builder : "
1775: + e.getCause());
1776: }
1777: return "";
1778: }
1779:
1780: public void writeResult(String result, OutputStream os) {
1781:
1782: StringReader is = null;
1783:
1784: try {
1785: is = new StringReader(result);
1786:
1787: SummaryDocument sd = null;
1788: if (is != null) {
1789: sd = SummaryDocument.Factory.parse(is);
1790: }
1791: sd.save(os);
1792: } catch (Exception ex) {
1793: Previewer.logError(" Problem in Previewer Builder : "
1794: + ex.getMessage());
1795: } finally {
1796: if (is != null) {
1797: try {
1798: is.close();
1799: } catch (Exception e) {
1800: }
1801: is = null;
1802: }
1803: }
1804: }
1805:
1806: private String googleTranslate(String text, String langpair) {
1807:
1808: HttpURLConnection conn = null;
1809: String result = null;
1810:
1811: try {
1812: conn = (HttpURLConnection) new URL(Previewer
1813: .getTranslatorConnectionString()).openConnection();
1814: conn.setRequestMethod("POST");
1815: conn.setDoInput(true);
1816: conn.setDoOutput(true);
1817: conn.setUseCaches(false);
1818: conn.setRequestProperty("Accept-Charset", "*");
1819: conn
1820: .setRequestProperty("User-Agent",
1821: "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
1822: PrintWriter pw = new PrintWriter(conn.getOutputStream());
1823: pw.print(Previewer.getTranslatorContentString()
1824: + URLEncoder.encode(text, "UTF-8") + "&"
1825: + Previewer.getTranslatorLangpairString()
1826: + langpair);
1827: pw.flush();
1828: pw.close();
1829: InputStream inputStream = conn.getInputStream();
1830: BufferedReader br = new BufferedReader(
1831: new InputStreamReader(inputStream, "UTF-8"));
1832: String line = null;
1833: StringBuffer googleResult = new StringBuffer();
1834: while ((line = br.readLine()) != null) {
1835: googleResult.append(line);
1836: }
1837: result = googleResult.toString();
1838:
1839: //hard code 47
1840: String temp = new String(Previewer
1841: .getTranslatorContentStartString());
1842: int beginIndex = result.indexOf(temp);
1843: int endIndex = result.lastIndexOf(Previewer
1844: .getTranslatorContentEndString());
1845:
1846: if (beginIndex != -1 && endIndex != -1) {
1847: beginIndex = beginIndex + temp.length();
1848: result = result.substring(beginIndex + 1, endIndex + 1);
1849: } else {
1850: result = "Problem ocured while translating text";
1851: }
1852:
1853: } catch (MalformedURLException murle) {
1854: Previewer.logError(" Problem in Previewer Builder : "
1855: + murle.getMessage());
1856: result = "Problem ocured while translating text";
1857:
1858: } catch (IOException ioe) {
1859: Previewer.logError(" Problem in Previewer Builder : "
1860: + ioe.getMessage());
1861: result = "Problem ocured while translating text";
1862:
1863: } finally {
1864: if (conn != null)
1865: conn.disconnect();
1866: }
1867:
1868: return result;
1869: }
1870:
1871: private File downloadFtpFile(String id, String type) {
1872: try {
1873: Download dw = DownloadFactory
1874: .getDownload("org.enhydra.snapperPreviewer.business.DownloadImpl");
1875: File retVal = dw.downloadFtpFile(SnapperManager
1876: .getInstance().getTempDir(), id, type);
1877: if (retVal != null) {
1878: return retVal;
1879: } else {
1880: Previewer.logError(" Error geting ftp file ");
1881: return null;
1882: }
1883: } catch (Exception e) {
1884: Previewer.logError(" Error geting ftp file ");
1885: Previewer.logError(e.getMessage());
1886: }
1887: return null;
1888: }
1889:
1890: private SearchedParameters addSearchParameters(
1891: SearchedParameters result, Properties pict, Properties limit) {
1892:
1893: for (Enumeration e = pict.propertyNames(); e.hasMoreElements();) {
1894: String name = (String) e.nextElement();
1895: String value = (String) pict.getProperty(name);
1896:
1897: Parameter perameter = result.addNewParameter();
1898: perameter.setKey(name);
1899: perameter.setValue(value);
1900: }
1901:
1902: for (Enumeration e = limit.propertyNames(); e.hasMoreElements();) {
1903: String name = (String) e.nextElement();
1904: String value = (String) limit.getProperty(name);
1905:
1906: Parameter perameter = result.addNewParameter();
1907: perameter.setKey(name);
1908: perameter.setValue(value);
1909: }
1910:
1911: return result;
1912: }
1913:
1914: }
|