Example usage for org.apache.commons.io.input BOMInputStream BOMInputStream

List of usage examples for org.apache.commons.io.input BOMInputStream BOMInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io.input BOMInputStream BOMInputStream.

Prototype

public BOMInputStream(InputStream delegate) 

Source Link

Document

Constructs a new BOM InputStream that excludes a ByteOrderMark#UTF_8 BOM.

Usage

From source file:org.nuxeo.ecm.csv.core.CSVImporterWork.java

/**
 * @since 7.3//from   w  w  w. java2 s. c  o m
 */
protected BufferedReader newReader(Blob blob) throws IOException {
    return new BufferedReader(new InputStreamReader(new BOMInputStream(blob.getStream())));
}

From source file:org.owasp.dependencycheck.xml.pom.PomParser.java

/**
 * Parses the given XML file and returns a Model object containing only the
 * fields dependency-check requires.//w  w w . j av  a  2s.com
 *
 * @param inputStream an InputStream containing suppression rues
 * @return a list of suppression rules
 * @throws PomParseException if the XML cannot be parsed
 */
public Model parse(InputStream inputStream) throws PomParseException {
    try {
        final PomHandler handler = new PomHandler();
        final SAXParser saxParser = XmlUtils.buildSecureSaxParser();
        final XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(handler);

        final BOMInputStream bomStream = new BOMInputStream(new XmlInputStream(inputStream));
        final ByteOrderMark bom = bomStream.getBOM();
        final String defaultEncoding = "UTF-8";
        final String charsetName = bom == null ? defaultEncoding : bom.getCharsetName();
        final Reader reader = new InputStreamReader(bomStream, charsetName);
        final InputSource in = new InputSource(reader);
        xmlReader.parse(in);
        return handler.getModel();
    } catch (ParserConfigurationException | SAXException | FileNotFoundException ex) {
        LOGGER.debug("", ex);
        throw new PomParseException(ex);
    } catch (IOException ex) {
        LOGGER.debug("", ex);
        throw new PomParseException(ex);
    }
}

From source file:org.paxml.table.csv.CsvTable.java

public CsvTable(Resource res, CsvOptions opt, ITableRange range) {
    if (opt == null) {
        opt = new CsvOptions();
    }//from   ww w . j a v a2s  .  com
    this.res = res;
    this.opt = opt;

    CsvPreference pref = opt.toPreference();

    if (res.exists()) {
        try {
            reader = new CsvListReader(
                    new InputStreamReader(new BOMInputStream(res.getInputStream()), opt.getEncoding()), pref);
        } catch (IOException e) {
            throw new PaxmlRuntimeException("Cannot read csv file: " + PaxmlUtils.getResourceFile(res), e);
        }
    } else {
        reader = null;
    }
    List<String> cols = opt.getColumns();
    if (cols == null) {
        // detect headers from file
        headers = new LinkedHashMap<String, IColumn>();
        if (reader != null) {
            String[] h;
            try {
                h = reader.getHeader(true);
            } catch (IOException e) {
                throw new PaxmlRuntimeException(
                        "Cannot read csv file header: " + PaxmlUtils.getResourceFile(res), e);
            }
            if (h != null) {
                for (String s : h) {
                    addColumn(s);
                }
            }
        }
    } else if (cols.isEmpty()) {
        headers = null;
    } else {
        // given header, skip existing header too
        if (reader != null) {
            try {
                reader.getHeader(true);
            } catch (IOException e) {
                throw new PaxmlRuntimeException(
                        "Cannot read csv file header: " + PaxmlUtils.getResourceFile(res), e);
            }
        }
        headers = new LinkedHashMap<String, IColumn>();
        for (String col : cols) {
            addColumn(col);
        }
    }
    CsvListWriter w = null;
    if (!opt.isReadOnly()) {
        writerFile = getWriterFile(res);
        if (writerFile != null) {

            try {
                w = new CsvListWriter(new FileWriter(reader == null ? res.getFile() : writerFile), pref);
                if (headers != null && !headers.isEmpty()) {
                    w.writeHeader(headers.keySet().toArray(new String[headers.size()]));
                }
            } catch (IOException e) {
                // do nothing, because this means writer cannot write to the
                // file.
            }

        }
    } else {
        writerFile = null;
    }
    writer = w;

    if (reader == null && writer == null) {
        throw new PaxmlRuntimeException(
                "Can neither read from nor write to csv file: " + PaxmlUtils.getResourceFile(res));
    }

    setRange(range);
}

From source file:org.roda.core.common.RodaUtils.java

public static Reader applyMetadataStylesheet(Binary binary, String basePath, String metadataType,
        String metadataVersion, Map<String, String> parameters) throws GenericException {
    try (Reader descMetadataReader = new InputStreamReader(
            new BOMInputStream(binary.getContent().createInputStream()))) {

        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setEntityResolver(new RodaEntityResolver());
        InputSource source = new InputSource(descMetadataReader);
        Source text = new SAXSource(xmlReader, source);

        XsltExecutable xsltExecutable = CACHE.get(Triple.of(basePath, metadataType, metadataVersion));

        XsltTransformer transformer = xsltExecutable.load();
        CharArrayWriter transformerResult = new CharArrayWriter();

        transformer.setSource(text);/* w  w  w. j  av  a  2  s.co  m*/
        transformer.setDestination(PROCESSOR.newSerializer(transformerResult));

        for (Entry<String, String> parameter : parameters.entrySet()) {
            QName qName = new QName(parameter.getKey());
            XdmValue xdmValue = new XdmAtomicValue(parameter.getValue());
            transformer.setParameter(qName, xdmValue);
        }

        transformer.transform();

        return new CharArrayReader(transformerResult.toCharArray());

    } catch (IOException | SAXException | ExecutionException | SaxonApiException e) {
        throw new GenericException("Could not process descriptive metadata binary " + binary.getStoragePath()
                + " metadata type " + metadataType + " and version " + metadataVersion, e);
    }
}

From source file:org.roda.core.common.RodaUtils.java

public static Reader applyEventStylesheet(Binary binary, boolean onlyDetails, Map<String, String> translations,
        String path) throws GenericException {
    try (Reader descMetadataReader = new InputStreamReader(
            new BOMInputStream(binary.getContent().createInputStream()))) {

        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setEntityResolver(new RodaEntityResolver());
        InputSource source = new InputSource(descMetadataReader);
        Source text = new SAXSource(xmlReader, source);

        XsltExecutable xsltExecutable = EVENT_CACHE.get(path);

        XsltTransformer transformer = xsltExecutable.load();
        CharArrayWriter transformerResult = new CharArrayWriter();

        transformer.setSource(text);/* w  w w  .  j a  va2s  .c om*/
        transformer.setDestination(PROCESSOR.newSerializer(transformerResult));

        // send param to filter stylesheet work
        transformer.setParameter(new QName("onlyDetails"), new XdmAtomicValue(Boolean.toString(onlyDetails)));

        for (Entry<String, String> parameter : translations.entrySet()) {
            QName qName = new QName(parameter.getKey());
            XdmValue xdmValue = new XdmAtomicValue(parameter.getValue());
            transformer.setParameter(qName, xdmValue);
        }

        transformer.transform();
        return new CharArrayReader(transformerResult.toCharArray());
    } catch (IOException | SAXException | ExecutionException | SaxonApiException e) {
        LOGGER.error(e.getMessage(), e);
        throw new GenericException("Could not process event binary " + binary.getStoragePath(), e);
    }
}

From source file:org.roda.core.common.validation.ValidationUtils.java

public static ValidationReport isXMLValid(ContentPayload xmlPayload) {
    ValidationReport ret = new ValidationReport();

    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(false);//w w w .j a  v  a  2  s.com
    factory.setNamespaceAware(true);

    RodaErrorHandler errorHandler = new RodaErrorHandler();

    try (Reader reader = new InputStreamReader(new BOMInputStream(xmlPayload.createInputStream()))) {
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setEntityResolver(new RodaEntityResolver());
        InputSource inputSource = new InputSource(reader);

        xmlReader.setErrorHandler(errorHandler);
        xmlReader.parse(inputSource);
        ret.setValid(errorHandler.getErrors().isEmpty());
        for (SAXParseException saxParseException : errorHandler.getErrors()) {
            ret.addIssue(convertSAXParseException(saxParseException));
        }
    } catch (SAXException e) {
        ret.setValid(false);
        for (SAXParseException saxParseException : errorHandler.getErrors()) {
            ret.addIssue(convertSAXParseException(saxParseException));
        }
    } catch (IOException e) {
        ret.setValid(false);
        ret.setMessage(e.getMessage());
    }
    return ret;
}

From source file:org.roda.core.common.validation.ValidationUtils.java

/**
 * Validates descriptive medatada (e.g. against its schema, but other
 * strategies may be used)/*from w  w w  .java  2s.co m*/
 * 
 * @param descriptiveMetadataType
 * 
 * @param failIfNoSchema
 * @throws ValidationException
 */
public static ValidationReport validateDescriptiveBinary(ContentPayload descriptiveMetadataPayload,
        String descriptiveMetadataType, String descriptiveMetadataVersion, boolean failIfNoSchema) {
    ValidationReport ret = new ValidationReport();
    InputStream inputStream = null;
    Optional<Schema> xmlSchema = RodaCoreFactory.getRodaSchema(descriptiveMetadataType,
            descriptiveMetadataVersion);
    try {
        if (xmlSchema.isPresent()) {
            RodaErrorHandler errorHandler = new RodaErrorHandler();

            try (InputStreamReader inputStreamReader = new InputStreamReader(
                    new BOMInputStream(descriptiveMetadataPayload.createInputStream()))) {

                XMLReader xmlReader = XMLReaderFactory.createXMLReader();
                xmlReader.setEntityResolver(new RodaEntityResolver());
                InputSource inputSource = new InputSource(inputStreamReader);
                Source source = new SAXSource(xmlReader, inputSource);

                Validator validator = xmlSchema.get().newValidator();

                validator.setErrorHandler(errorHandler);

                validator.validate(source);
                ret.setValid(errorHandler.getErrors().isEmpty());
                for (SAXParseException saxParseException : errorHandler.getErrors()) {
                    ret.addIssue(convertSAXParseException(saxParseException));
                }
            } catch (SAXException e) {
                LOGGER.debug("Error validating descriptive binary " + descriptiveMetadataType, e);
                ret.setValid(false);
                for (SAXParseException saxParseException : errorHandler.getErrors()) {
                    ret.addIssue(convertSAXParseException(saxParseException));
                }
            }
        } else {
            if (failIfNoSchema) {
                LOGGER.error(
                        "Will fail validating descriptive metadata with type '{}' and version '{}' because couldn't find its schema",
                        descriptiveMetadataType, descriptiveMetadataVersion);
                ret.setValid(false);
                ret.setMessage("No schema to validate " + descriptiveMetadataType);
            } else {
                LOGGER.debug(
                        "Found no schema do validate descriptive metadata but will try to validate XML syntax...");
                ret = isXMLValid(descriptiveMetadataPayload);
            }
        }
    } catch (IOException e) {
        LOGGER.error("Error validating descriptive metadata", e);
        ret.setValid(false);
        ret.setMessage(e.getMessage());
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

    return ret;

}

From source file:org.sakaiproject.citation.tool.CitationHelperAction.java

/**
 *
 * @param data/*  w  ww. ja va  2  s . com*/
 */
public void doImport(RunData data) {
    logger.debug("doImport called.");

    // get the state object
    SessionState state = ((JetspeedRunData) data).getPortletSessionState(((JetspeedRunData) data).getJs_peid());
    ParameterParser params = data.getParameters();

    int requestStateId = params.getInt("requestStateId", 0);
    restoreRequestState(state,
            new String[] { CitationHelper.RESOURCES_REQUEST_PREFIX, CitationHelper.CITATION_PREFIX },
            requestStateId);

    Iterator iter = params.getNames();

    String param = null;

    while (iter.hasNext()) {
        param = (String) iter.next();
        if (logger.isDebugEnabled()) {
            logger.debug("param = " + param);
            logger.debug(param + " value = " + params.get(param));
        }
    }

    String citationCollectionId = params.getString("citationCollectionId");

    if (citationCollectionId == null) {
        citationCollectionId = (String) state.getAttribute(STATE_CITATION_COLLECTION_ID);
    }

    CitationCollection collection = null;
    collection = getCitationService().getUnnestedCitationCollection(citationCollectionId);

    String ristext = params.get("ristext");

    // We're going to read the RIS file from the submitted form's textarea first. If that's
    // empty we'll read it from the uploaded file.  We'll crate a BufferedReader in either
    // circumstance so that the parsing code need not know where the ris text came from.
    java.io.BufferedReader bread = null;

    if (ristext.trim().length() > 0) // form has text in the risimport textarea
    {
        java.io.StringReader risStringReader = new java.io.StringReader(ristext);
        bread = new java.io.BufferedReader(risStringReader);
        logger.debug("String buffered reader ready");

    } // end RIS text is in the textarea
    else // textarea empty, set the read of the import from the file
    {
        String upload = params.get("risupload");
        if (logger.isDebugEnabled()) {
            logger.debug("Upload String = " + upload);
        }

        FileItem risImport = params.getFileItem("risupload");

        if (risImport == null) {
            logger.debug("risImport is null.");
            return;
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Filename = " + risImport.getFileName());
        }

        InputStream risImportStream = risImport.getInputStream();

        // Attempt to detect the encoding of the file.
        BOMInputStream irs = new BOMInputStream(risImportStream);

        // below is needed if UTF-8 above is commented out
        Reader isr = null;
        String bomCharsetName = null;
        try {
            bomCharsetName = irs.getBOMCharsetName();
            if (bomCharsetName != null) {
                isr = new InputStreamReader(risImportStream, bomCharsetName);
            }
        } catch (UnsupportedEncodingException uee) {
            // Something strange as the JRE should support all the formats.
            if (logger.isInfoEnabled()) {
                logger.info("Problem using character set when importing RIS: " + bomCharsetName);
            }
        } catch (IOException ioe) {
            // Probably won't get any further, but may as well try.
            if (logger.isDebugEnabled()) {
                logger.debug("Problem reading the character set from RIS import: " + ioe.getMessage());
            }
        }
        // Fallback to platform default
        if (isr == null) {
            isr = new InputStreamReader(irs);
        }

        bread = new java.io.BufferedReader(isr);
    } // end set the read of the import from the uploaded file.

    // The below code is a major work in progress.
    // This code is for demonstration purposes only. No gambling or production use!

    StringBuilder fileString = new StringBuilder();
    String importLine = null;
    java.util.List importList = new java.util.ArrayList();

    // Read the BufferedReader and populate the importList. Each entry in the list
    // is a line in the RIS import "file".
    try {
        while ((importLine = bread.readLine()) != null) {
            importLine = importLine.trim();

            if (importLine != null && importLine.length() > 2) {
                importList.add(importLine);
                if (logger.isDebugEnabled()) {
                    fileString.append("\n");
                    fileString.append(importLine);
                }
            }

        } // end while
    } // end try
    catch (Exception e) {
        logger.debug("ISR error = " + e);
    } // end catch
    finally {
        if (bread != null) {
            try {
                bread.close();
            } catch (IOException e) {
                // tried
            }
        }
    }

    if (logger.isDebugEnabled()) {
        logger.debug("fileString = \n" + fileString.toString());
    }

    // tempList holds the entries read in to make a citation up to and
    // including the ER entry from importList
    List tempList = new java.util.ArrayList();

    Citation importCitation = getCitationService().getTemporaryCitation();
    CitationCollection importCollection = getCitationService().getTemporaryCollection();

    int sucessfullyReadCitations = 0;
    int totalNumberCitations = 0;

    // Read each entry in the RIS List and build a citation
    for (int i = 0; i < importList.size(); i++) {
        String importEntryString = (String) importList.get(i);
        //         logger.debug("Import line (#1) = " + importEntryString);
        //         logger.debug("Substring is = " + importEntryString.substring(0, 2));
        tempList.add(importEntryString);

        // make sure importEntryString can be tested for "ER" existence. It could
        // be a dinky invalid line less than 2 characters.
        if (importEntryString != null && importEntryString.length() > 1
                && importEntryString.substring(0, 2).equalsIgnoreCase("ER")) {
            // end of citation (signaled by ER).

            totalNumberCitations++;
            if (logger.isDebugEnabled()) {
                logger.debug("------> Trying to add citation " + totalNumberCitations);
            }
            if (importCitation.importFromRisList(tempList)) // import went well
            {
                importCollection.add(importCitation);
                sucessfullyReadCitations++;
            }
            tempList.clear();
            importCitation = getCitationService().getTemporaryCitation();
        }
    } // end for

    if (logger.isDebugEnabled()) {
        logger.debug(
                "Done reading in " + sucessfullyReadCitations + " / " + totalNumberCitations + " citations.");
    }

    collection.addAll(importCollection);
    getCitationService().save(collection);

    // remove collection from state
    state.removeAttribute(STATE_CITATION_COLLECTION);

    //setMode(state, Mode.LIST);
    setMode(state, Mode.NEW_RESOURCE);
}

From source file:org.sonar.batch.source.CodeColorizers.java

@CheckForNull
public void toSyntaxHighlighting(File file, Charset charset, String language, NewHighlighting highlighting) {
    CodeColorizerFormat format = byLang.get(language);
    List<Tokenizer> tokenizers;
    if (format == null) {
        // Workaround for Java test code since Java plugin only provides highlighting for main source and no colorizer
        // TODO can be dropped when Java plugin embed its own CodeColorizerFormat of (better) provides highlighting for tests
        // See SONARJAVA-830
        if ("java".equals(language)) {
            tokenizers = JavaTokenizers.forHtml();
        } else {/*from  w w  w.  j a  va  2s  .com*/
            return;
        }
    } else {
        tokenizers = format.getTokenizers();
    }
    try (Reader reader = new BufferedReader(
            new InputStreamReader(new BOMInputStream(new FileInputStream(file)), charset))) {
        new HighlightingRenderer().render(reader, tokenizers, highlighting);
    } catch (Exception e) {
        LOG.warn("Unable to perform colorization of file " + file, e);
    }
}

From source file:org.sonar.plugins.resharper.customseverities.FileCustomSeverities.java

/**
 * Create inputsource for a file/*  ww w .  j ava  2 s .  co m*/
 * Creates inputstream, removes BOM
 * @return null in case file could not be opened, otherwise inputsource for the file
 */
@Override
InputSource createInputSource(String path) {
    try {
        LOG.trace("Using " + path);
        InputStream fileReader = new FileInputStream(path);
        InputStream inputStream = new BOMInputStream(fileReader);
        return new InputSource(inputStream);
    } catch (FileNotFoundException e) {
        LOG.error("could not open " + path + "defined in " + ReSharperPlugin.CUSTOM_SEVERITIES_PATH_PROPERTY_KEY
                + " reason:", e);
    }
    return null;
}