Example usage for com.liferay.portal.kernel.util FileUtil write

List of usage examples for com.liferay.portal.kernel.util FileUtil write

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util FileUtil write.

Prototype

public static void write(String pathName, String fileName, String s, boolean lazy) throws IOException 

Source Link

Usage

From source file:com.fmdp.webform.portlet.WebFormPortlet.java

License:Open Source License

protected boolean saveFile(Map<String, String> fieldsMap, String fileName) {
    StringBundler sb = new StringBundler();

    for (String fieldLabel : fieldsMap.keySet()) {
        String fieldValue = fieldsMap.get(fieldLabel);

        sb.append(getCSVFormattedValue(fieldValue));
        sb.append(PortletPropsValues.CSV_SEPARATOR);
    }//  ww w  .  j a v  a  2  s . c  o  m

    sb.setIndex(sb.index() - 1);

    sb.append(CharPool.NEW_LINE);

    try {
        FileUtil.write(fileName, sb.toString(), false, true);

        return true;
    } catch (Exception e) {
        _log.error("The web form data could not be saved to a file", e);

        return false;
    }
}

From source file:com.liferay.document.library.document.conversion.internal.DocumentConversionImpl.java

License:Open Source License

@Override
public File convert(String id, InputStream inputStream, String sourceExtension, String targetExtension)
        throws IOException {

    if (!isEnabled()) {
        return null;
    }/* ww w  .j  a v  a2 s  . c  om*/

    sourceExtension = _fixExtension(sourceExtension);
    targetExtension = _fixExtension(targetExtension);

    _validate(targetExtension, id);

    String fileName = getFilePath(id, targetExtension);

    File file = new File(fileName);

    if (_openOfficeConfiguration.cacheEnabled() && file.exists()) {
        return file;
    }

    DocumentFormatRegistry documentFormatRegistry = new DefaultDocumentFormatRegistry();

    DocumentFormat inputDocumentFormat = documentFormatRegistry.getFormatByFileExtension(sourceExtension);
    DocumentFormat outputDocumentFormat = documentFormatRegistry.getFormatByFileExtension(targetExtension);

    if (inputDocumentFormat == null) {
        throw new SystemException("Conversion is not supported from ." + sourceExtension);
    } else if (!inputDocumentFormat.isImportable()) {
        throw new SystemException("Conversion is not supported from " + inputDocumentFormat.getName());
    } else if (outputDocumentFormat == null) {
        throw new SystemException(StringBundler.concat("Conversion is not supported from ",
                inputDocumentFormat.getName(), " to .", targetExtension));
    } else if (!inputDocumentFormat.isExportableTo(outputDocumentFormat)) {
        throw new SystemException(StringBundler.concat("Conversion is not supported from ",
                inputDocumentFormat.getName(), " to ", outputDocumentFormat.getName()));
    }

    if (sourceExtension.equals("html")) {
        DocumentHTMLProcessor documentHTMLProcessor = new DocumentHTMLProcessor();

        inputStream = documentHTMLProcessor.process(inputStream);
    }

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    DocumentConverter documentConverter = _getDocumentConverter();

    documentConverter.convert(inputStream, inputDocumentFormat, unsyncByteArrayOutputStream,
            outputDocumentFormat);

    FileUtil.write(file, unsyncByteArrayOutputStream.unsafeGetByteArray(), 0,
            unsyncByteArrayOutputStream.size());

    inputStream.close();

    return file;
}

From source file:com.liferay.document.library.document.conversion.internal.DocumentHTMLProcessor.java

License:Open Source License

public InputStream process(InputStream inputStream) {
    File tempFile = null;// w w  w  .  j ava2s.  c om

    InputStream processedInputStream = null;

    Scanner scanner = null;

    String replacement = "";

    try {
        scanner = new Scanner(inputStream);

        scanner.useDelimiter(">");

        tempFile = FileUtil.createTempFile();

        long userId = PrincipalThreadLocal.getUserId();

        String imageRequestToken = ImageRequestTokenUtil.createToken(userId);

        while (scanner.hasNext()) {
            String token = scanner.next();

            if (Validator.isNotNull(token)) {
                token += ">";

                replacement = token.replaceAll(_DOCUMENTS_REGEX, "$1&auth_token=" + imageRequestToken + "$3");

                replacement = replacement.replaceAll(_IMAGE_REGEX, "$1&auth_token=" + imageRequestToken + "$3");

                replacement = replacement.replaceAll(_PORTLET_FILE_ENTRY_REGEX,
                        "$1?auth_token=" + imageRequestToken + "$3");

                replacement = replacement.replaceAll(_WIKI_PAGE_ATTACHMENT_REGEX,
                        "$1$3&auth_token=" + imageRequestToken + "$6");

                FileUtil.write(tempFile, replacement, true, true);
            }
        }

        processedInputStream = new AutoDeleteFileInputStream(tempFile);
    } catch (Exception e) {
        _log.error(e, e);
    } finally {
        scanner.close();
    }

    return processedInputStream;
}

From source file:com.liferay.portlet.documentlibrary.util.DocumentConversionUtil.java

License:Open Source License

private File _convert(String id, InputStream inputStream, String sourceExtension, String targetExtension)
        throws IOException, SystemException {

    if (!isEnabled()) {
        return null;
    }// ww  w  .j a  v a 2s . c  om

    sourceExtension = _fixExtension(sourceExtension);
    targetExtension = _fixExtension(targetExtension);

    String fileName = getFilePath(id, targetExtension);

    File file = new File(fileName);

    if (!PropsValues.OPENOFFICE_CACHE_ENABLED || !file.exists()) {
        DocumentFormatRegistry documentFormatRegistry = new DefaultDocumentFormatRegistry();

        DocumentFormat inputDocumentFormat = documentFormatRegistry.getFormatByFileExtension(sourceExtension);
        DocumentFormat outputDocumentFormat = documentFormatRegistry.getFormatByFileExtension(targetExtension);

        if (!inputDocumentFormat.isImportable()) {
            throw new SystemException("Conversion is not supported from " + inputDocumentFormat.getName());
        } else if (!inputDocumentFormat.isExportableTo(outputDocumentFormat)) {

            throw new SystemException("Conversion is not supported from " + inputDocumentFormat.getName()
                    + " to " + outputDocumentFormat.getName());
        }

        UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

        DocumentConverter documentConverter = _getDocumentConverter();

        documentConverter.convert(inputStream, inputDocumentFormat, unsyncByteArrayOutputStream,
                outputDocumentFormat);

        FileUtil.write(file, unsyncByteArrayOutputStream.unsafeGetByteArray(), 0,
                unsyncByteArrayOutputStream.size());
    }

    return file;
}

From source file:org.gfbio.ContactFormToHelpdeskPortlet.java

License:Open Source License

protected boolean saveFile(Map<String, String> fieldsMap, String fileName) {

    // Save the file as a standard Excel CSV format. Use ; as a delimiter,
    // quote each entry with double quotes, and escape double quotes in
    // values a two double quotes.

    StringBuilder sb = new StringBuilder();

    for (String fieldLabel : fieldsMap.keySet()) {
        String fieldValue = fieldsMap.get(fieldLabel);

        sb.append("\"");
        sb.append(StringUtil.replace(fieldValue, "\"", "\"\""));
        sb.append("\";");
    }/*from   w ww .  j a v a  2s. co  m*/

    String s = sb.substring(0, sb.length() - 1) + "\n";

    try {
        FileUtil.write(fileName, s, false, true);

        return true;
    } catch (Exception e) {
        _log.error("The web form data could not be saved to a file", e);

        return false;
    }
}