Search in sources :

Example 6 with GXFile

use of com.genexus.util.GXFile in project JavaClasses by genexuslabs.

the class GxImageUtil method writeImage.

private static void writeImage(BufferedImage croppedImage, String destinationFilePathOrUrl) throws IOException {
    try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
        ImageIO.write(croppedImage, CommonUtil.getFileType(destinationFilePathOrUrl), outStream);
        try (ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray())) {
            GXFile file = getGXFile(destinationFilePathOrUrl);
            file.create(inStream, true);
            file.close();
        }
    }
}
Also used : GXFile(com.genexus.util.GXFile)

Example 7 with GXFile

use of com.genexus.util.GXFile in project JavaClasses by genexuslabs.

the class DocumentHandler method htmlPreview.

public static String htmlPreview(Object obj, String query, String textType, String preTag, String postTag, int fragmentSize, int maxNumFragments) {
    String text;
    try {
        if (obj instanceof GxSilentTrnSdt) {
            text = ((GxSilentTrnSdt) obj).getTransaction().toString();
        } else if (obj instanceof GXFile) {
            GXFile file = (GXFile) obj;
            text = DocumentHandler.getText(file.getAbsoluteName(), file.getExt());
        } else if (textType.toLowerCase().startsWith("htm")) {
            text = new JTidyHTMLHandler().getTextFromString(obj.toString());
        } else {
            text = obj.toString();
        }
        if (!query.equals("") && !text.equals("")) {
            QueryParser qp = new QueryParser(IndexRecord.CONTENTFIELD, Indexer.CreateAnalyzer());
            qp.setDefaultOperator(QueryParser.Operator.AND);
            Query unReWrittenQuery = qp.parse(query);
            Query q = unReWrittenQuery;
            try {
                if (reader == null) {
                    reader = Indexer.getReader();
                }
                if (queries.get(query) != null) {
                    q = (Query) queries.get(query);
                } else {
                    // required to expand search terms (for the usage of
                    q = unReWrittenQuery.rewrite(reader);
                    // highlighting with wildcards)
                    if (queries.size() == Integer.MAX_VALUE) {
                        queries.clear();
                    }
                    queries.put(query, q);
                }
            } catch (Exception ex) {
            }
            QueryScorer scorer = new QueryScorer(q);
            SimpleHTMLFormatter formatter = new SimpleHTMLFormatter(preTag, postTag);
            Highlighter highlighter = new Highlighter(formatter, scorer);
            Fragmenter fragmenter = new SimpleFragmenter(fragmentSize);
            highlighter.setTextFragmenter(fragmenter);
            TokenStream tokenStream = Indexer.CreateAnalyzer().tokenStream(IndexRecord.CONTENTFIELD, new StringReader(text));
            String result = highlighter.getBestFragments(tokenStream, text, maxNumFragments, "...");
            return result;
        } else {
            return text;
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
        ex.printStackTrace();
        return "";
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) Query(org.apache.lucene.search.Query) QueryScorer(org.apache.lucene.search.highlight.QueryScorer) GXFile(com.genexus.util.GXFile) GxSilentTrnSdt(com.genexus.GxSilentTrnSdt) FileNotFoundException(java.io.FileNotFoundException) QueryParser(org.apache.lucene.queryParser.QueryParser) SimpleFragmenter(org.apache.lucene.search.highlight.SimpleFragmenter) Fragmenter(org.apache.lucene.search.highlight.Fragmenter) SimpleFragmenter(org.apache.lucene.search.highlight.SimpleFragmenter) StringReader(java.io.StringReader) SimpleHTMLFormatter(org.apache.lucene.search.highlight.SimpleHTMLFormatter) Highlighter(org.apache.lucene.search.highlight.Highlighter)

Example 8 with GXFile

use of com.genexus.util.GXFile in project JavaClasses by genexuslabs.

the class Indexer method getIndexRecord.

private IndexRecord getIndexRecord(Object obj, GXContentInfo contentInfo) {
    if (contentInfo == null)
        contentInfo = new GXContentInfo();
    IndexRecord ir = new IndexRecord();
    if (obj instanceof GXFile) {
        GXFile file = (GXFile) obj;
        ir.setUri(file.getAbsoluteName());
        ir.setContent(DocumentHandler.getText(file.getAbsoluteName(), file.getExt()).toLowerCase());
        ir.setEntity(contentInfo.getType() == null ? file.getClass().getName() : contentInfo.getType());
        ir.setTitle(contentInfo.getTitle() == null ? file.getName() : contentInfo.getTitle());
        ir.setViewer(contentInfo.getViewer() == null ? file.getName() : contentInfo.getViewer());
        ir.setKeys(contentInfo.getKeys() == null || contentInfo.getKeys().size() == 0 ? new Vector<String>() : contentInfo.getKeys());
    } else if (obj instanceof GxSilentTrnSdt) {
        IGxSilentTrn bc = ((GxSilentTrnSdt) obj).getTransaction();
        GXContentInfo info = bc.getContentInfo();
        ir.setUri(info.getId());
        ir.setContent(IndexRecord.processContent(bc.toString()));
        ir.setEntity(contentInfo.getType() == null ? info.getType() : contentInfo.getType());
        ir.setTitle(contentInfo.getTitle() == null ? info.getTitle() : contentInfo.getTitle());
        ir.setViewer(contentInfo.getViewer() == null ? info.getViewer() : contentInfo.getViewer());
        ir.setKeys(contentInfo.getKeys() == null || contentInfo.getKeys().size() == 0 ? info.getKeys() : contentInfo.getKeys());
    } else if (obj instanceof String) {
        ir.setUri(contentInfo.getId() == null ? "" : contentInfo.getId());
        ir.setContent(IndexRecord.processContent((String) obj));
        ir.setEntity(contentInfo.getType() == null ? "" : contentInfo.getType());
        ir.setTitle(contentInfo.getTitle() == null ? "" : contentInfo.getTitle());
        ir.setViewer(contentInfo.getViewer() == null ? "" : contentInfo.getViewer());
        ir.setKeys(contentInfo.getKeys() == null || contentInfo.getKeys().size() == 0 ? new Vector<String>() : contentInfo.getKeys());
    } else {
        ir = null;
    }
    return ir;
}
Also used : GXFile(com.genexus.util.GXFile) Vector(java.util.Vector) GxSilentTrnSdt(com.genexus.GxSilentTrnSdt) IGxSilentTrn(com.genexus.IGxSilentTrn)

Example 9 with GXFile

use of com.genexus.util.GXFile in project JavaClasses by genexuslabs.

the class HttpContextWeb method getResourceRelative.

public String getResourceRelative(String path, boolean includeBasePath) {
    if (Application.getExternalProvider() != null && !path.isEmpty()) {
        if (CommonUtil.isAbsoluteURL(path))
            return path;
        GXFile gxFile = new GXFile(path);
        String pathURL = gxFile.getAbsolutePath();
        if (CommonUtil.isAbsoluteURL(pathURL))
            return pathURL;
    }
    try {
        java.io.File file = new java.io.File(path);
        if (file.getPath().compareTo(file.getAbsolutePath()) != 0)
            return path;
    } catch (Exception e) {
        return path;
    }
    if (path.length() == 0)
        return "";
    String ContextPath = null;
    try {
        ContextPath = request.getContextPath();
    } catch (Exception e) {
        // on submit (there is no http context) request.getContextPath throws a
        // java.lang.NullPointerException
        System.err.println(e.toString());
    }
    ;
    String Resource = path;
    String basePath = getDefaultPath();
    if (Resource.startsWith(basePath) && Resource.length() >= basePath.length())
        Resource = Resource.substring(basePath.length());
    if (ContextPath != null && !ContextPath.equals("") && Resource.startsWith(ContextPath))
        return Resource.replace('\\', '/');
    Resource = Resource.replace('\\', '/');
    if (includeBasePath) {
        if (Resource.startsWith("/"))
            Resource = ContextPath + Resource;
        else
            Resource = ContextPath + "/" + Resource;
    } else {
        if (Resource.startsWith("/"))
            Resource = Resource.substring(1);
    }
    String baseName = FilenameUtils.getBaseName(Resource);
    Resource = CommonUtil.replaceLast(Resource, baseName, PrivateUtilities.encodeFileName(baseName));
    return Resource;
}
Also used : GXFile(com.genexus.util.GXFile) GXFile(com.genexus.util.GXFile) File(java.io.File) File(java.io.File) JSONException(json.org.json.JSONException) IOException(java.io.IOException)

Example 10 with GXFile

use of com.genexus.util.GXFile in project JavaClasses by genexuslabs.

the class ExcelDocument method Open.

public short Open(String fileName) {
    resetError();
    if (fileName.indexOf('.') == -1) {
        fileName += ".xlsx";
    }
    try {
        if (!template.equals("")) {
            GXFile templateFile = new GXFile(template);
            if (templateFile.exists()) {
                workBook = new SXSSFWorkbook(new XSSFWorkbook(templateFile.getStream()));
            } else {
                // Invalid template
                errCod = 4;
                errDescription = "Invalid template.";
                return errCod;
            }
        } else {
            GXFile file = new GXFile("", fileName, Constants.EXTERNAL_UPLOAD_ACL, GxFileInfoSourceType.Unknown);
            if (file.exists()) {
                // System.out.println("Opening..");
                workBook = new SXSSFWorkbook(new XSSFWorkbook(file.getStream()));
            } else {
                // System.out.println("Creating..");
                workBook = new SXSSFWorkbook();
            }
        }
        this.selectFirstSheet();
        xlsFileName = fileName.toString();
        stylesCache = new StylesCache(workBook);
    } catch (Exception e) {
        // error creando xlsx file
        errCod = 10;
        errDescription = "Could not open file.";
        System.err.println("GXOffice Error: " + e.toString());
        return errCod;
    }
    return 0;
}
Also used : StylesCache(com.genexus.gxoffice.poi.xssf.StylesCache) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) GXFile(com.genexus.util.GXFile)

Aggregations

GXFile (com.genexus.util.GXFile)20 ByteArrayInputStream (java.io.ByteArrayInputStream)6 File (java.io.File)5 IOException (java.io.IOException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3 SQLException (java.sql.SQLException)3 GxSilentTrnSdt (com.genexus.GxSilentTrnSdt)2 HttpContext (com.genexus.internet.HttpContext)2 FileInputStream (java.io.FileInputStream)2 URL (java.net.URL)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 com.genexus (com.genexus)1 IGxSilentTrn (com.genexus.IGxSilentTrn)1 IExtensionGXExternalFileInfo (com.genexus.common.interfaces.IExtensionGXExternalFileInfo)1 ExternalProvider (com.genexus.db.driver.ExternalProvider)1 StylesCache (com.genexus.gxoffice.poi.xssf.StylesCache)1 ExcelException (com.genexus.msoffice.excel.exception.ExcelException)1 ExcelTemplateNotFoundException (com.genexus.msoffice.excel.exception.ExcelTemplateNotFoundException)1