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:de.uzk.hki.da.model.RightsSectionURNMetsXmlReader.java

/**
 * Read urn.//  w w w.ja  va2  s . co  m
 *
 * @param file the file
 * @return The URN specified in the METS file or null if the METS file doesn't specify an URN
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws ParseException the parse exception
 * @author Thomas Kleinke
 */
public String readURN(File file) throws IOException, ParseException {

    FileInputStream fileInputStream = new FileInputStream(file);
    BOMInputStream bomInputStream = new BOMInputStream(fileInputStream);

    XMLReader xmlReader = null;
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {
        xmlReader = spf.newSAXParser().getXMLReader();
    } catch (Exception e) {
        fileInputStream.close();
        bomInputStream.close();
        throw new IOException("Error creating SAX parser", e);
    }
    xmlReader.setErrorHandler(err);
    NodeFactory nodeFactory = new PremisXmlReaderNodeFactory();
    Builder parser = new Builder(xmlReader, false, nodeFactory);
    logger.trace("Successfully built builder and XML reader");

    try {
        String urn = null;

        Document doc = parser.build(bomInputStream);
        Element root = doc.getRootElement();

        Element dmdSecEl = root.getFirstChildElement("dmdSec", METS_NS);
        if (dmdSecEl == null)
            return null;

        Element mdWrapEl = dmdSecEl.getFirstChildElement("mdWrap", METS_NS);
        if (mdWrapEl == null)
            return null;

        Element xmlDataEl = mdWrapEl.getFirstChildElement("xmlData", METS_NS);
        if (xmlDataEl == null)
            return null;

        Element modsEl = xmlDataEl.getFirstChildElement("mods", MODS_NS);
        if (modsEl == null)
            return null;

        Elements identifierEls = modsEl.getChildElements("identifier", MODS_NS);
        for (int i = 0; i < identifierEls.size(); i++) {
            Element element = identifierEls.get(i);
            Attribute attribute = element.getAttribute("type");
            if (attribute.getValue().toLowerCase().equals("urn"))
                urn = element.getValue();
        }

        if (urn != null && urn.equals(""))
            urn = null;

        return urn;
    } catch (ValidityException ve) {
        throw new IOException(ve);
    } catch (ParsingException pe) {
        throw new IOException(pe);
    } catch (IOException ie) {
        throw new IOException(ie);
    } finally {
        fileInputStream.close();
        bomInputStream.close();
    }
}

From source file:com.itemanalysis.jmetrik.data.JmetrikFileImporterTest.java

@Test
public void readJmetrikFileTest() {
    System.out.println("JmetrikFileImporterTest: Reading *.jmetrik file");
    CSVParser parser = null;/*from   w w  w  .  j a  v a  2  s . c  o  m*/
    Reader reader = null;

    try {
        File dataFile = FileUtils.toFile(this.getClass().getResource("/data/example-import-file.jmetrik"));
        reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8");

        parser = new CSVParser(reader, CSVFormat.DEFAULT.withCommentMarker('#'));
        Iterator<CSVRecord> iter = parser.iterator();
        CSVRecord temp = null;

        boolean readAttributes = false;
        boolean readData = false;
        int attCount = 0;

        while (iter.hasNext()) {
            temp = iter.next();

            if ("VERSION".equals(temp.getComment())) {
                System.out.println("VERSION: " + temp.get(0));

            } else if ("METADATA".equals(temp.getComment())) {
                System.out.println("CASES: " + temp.get(0));
            } else if ("ATTRIBUTES".equals(temp.getComment())) {
                readAttributes = true;
            } else if ("DATA".equals(temp.getComment())) {
                readAttributes = false;
                readData = true;
            }

            if (readAttributes) {
                System.out.print("ATTRIBUTE-" + attCount + ": ");
                Iterator<String> innerIter = temp.iterator();
                while (innerIter.hasNext()) {
                    System.out.print(innerIter.next());
                    if (innerIter.hasNext()) {
                        System.out.print(",");
                    }
                }
                System.out.println();
                attCount++;
            }

            if (readData) {
                Iterator<String> innerIter = temp.iterator();
                while (innerIter.hasNext()) {
                    System.out.print(innerIter.next());
                    if (innerIter.hasNext()) {
                        System.out.print(",");
                    }
                }
                System.out.println();
            }

        }

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            parser.close();
            reader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

From source file:com.itemanalysis.jmetrik.file.JmetrikFileImporter.java

/**
 * 1. Gets the file header or creates one.
 * 2. Sets the number of columns/*from w w w  . j  a  va  2  s .  c om*/
 * 3. Checks the type of data stored in each variable using the first rowsToScan rows.
 *    Variables are integers by default. This method will change the data type to either
 *    double or string.
 *
 */
private void setDataTypes() {
    CSVParser parser = null;
    Reader reader = null;

    try {
        reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8");

        //Get column names from variable attributes
        colNames = new String[variableAttributeMap.size()];
        int index = 0;
        Iterator<VariableName> iter = variableAttributeMap.keySet().iterator();
        VariableName tempName = null;
        while (iter.hasNext()) {
            colNames[index++] = iter.next().toString();
        }

        //Create a parser with variable names from the variable attributes
        if (hasHeader) {
            parser = new CSVParser(reader,
                    dataFileFormat.withHeader(colNames).withSkipHeaderRecord(true).withCommentMarker('#'));
        } else {
            parser = new CSVParser(reader, dataFileFormat.withHeader(colNames).withCommentMarker('#'));
        }

        //Check data types in each column.
        String value = "";
        Iterator<CSVRecord> csvIter = parser.iterator();
        CSVRecord csvRecord = null;
        double testValue = 0;
        nrow = 0;

        while (csvIter.hasNext()) {
            csvRecord = csvIter.next();

            iter = variableAttributeMap.keySet().iterator();
            while (iter.hasNext()) {
                tempName = iter.next();
                value = csvRecord.get(tempName.toString()).trim();

                //Check that string can be converted to double. If not, Change variable type.
                //Ignore missing data and other special codes
                try {
                    if (!"".equals(value) && !specialDataCodes.isMissing(value)) {
                        testValue = Double.parseDouble(value);
                        if (testValue != Math.floor(testValue)) {
                            //if any value is a double, the variable is a double
                            variableAttributeMap.get(tempName).setDataType(DataType.DOUBLE);
                        }
                    }
                } catch (NumberFormatException ex) {
                    //if any value is a String, the variable is a String
                    variableAttributeMap.get(tempName).setDataType(DataType.STRING);
                }
            }
            nrow++;
        }

    } catch (IOException ex) {
        theException = ex;
    } finally {
        try {
            if (parser != null)
                parser.close();
            if (reader != null)
                reader.close();
        } catch (IOException ex) {
            theException = ex;
            logger.fatal(ex);
        }
    }

}

From source file:cn.dreampie.resource.LessSource.java

private String loadResource(Resource resource, Charset charset) throws IOException {
    BOMInputStream inputStream = new BOMInputStream(resource.getInputStream());
    try {// w ww . j av  a2s  .c o m
        if (inputStream.hasBOM()) {
            logger.debug("BOM found %s", inputStream.getBOMCharsetName());
            return IOUtils.toString(inputStream, inputStream.getBOMCharsetName());
        } else {
            logger.debug("Using charset " + charset.name());
            return IOUtils.toString(inputStream, charset.name());
        }
    } finally {
        inputStream.close();
    }
}

From source file:de.uzk.hki.da.metadata.LidoMetadataStructure.java

/**
 * Append to each administrativeMetadata in a Lido-File one RightsResourceType-Element and save it.
 * /*w  w  w .  j a v a 2  s  . c o m*/
 * @param targetLidoFile
 * @param licenseHref
 * @param displayLabel
 * @param text
 * @throws IOException
 * @throws JDOMException
 */
public void appendRightsResource(File targetLidoFile, String licenseHref, String displayLabel)
        throws IOException, JDOMException {
    SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder();

    FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, targetLidoFile.getPath()));
    BOMInputStream bomInputStream = new BOMInputStream(fileInputStream);
    Reader reader = new InputStreamReader(bomInputStream, "UTF-8");
    InputSource is = new InputSource(reader);
    is.setEncoding("UTF-8");
    Document lidoDoc = builder.build(is);

    List<Element> lidoElems = lidoDoc.getRootElement().getChildren("lido", C.LIDO_NS);

    for (int i = 0; i < lidoElems.size(); i++) {
        appendRightsResourceToLido(lidoElems.get(i), licenseHref, displayLabel);
    }

    fileInputStream.close();
    bomInputStream.close();
    reader.close();

    writeDocumentToFile(lidoDoc, Path.makeFile(workPath, targetLidoFile.getPath()));
}

From source file:de.hybris.platform.acceleratorservices.process.strategies.impl.DefaultEmailTemplateTranslationStrategy.java

protected Map loadPropertyfile(final String path) {
    final Properties properties = new Properties();
    Reader reader = null;//  www.  j  a v a2 s  .  c  om
    try {
        final Resource propertyResource = getApplicationContext().getResource(path);
        if (propertyResource != null && (propertyResource.exists()) && (propertyResource.isReadable())) {
            reader = new InputStreamReader(new BOMInputStream(propertyResource.getInputStream()), "UTF-8");
            properties.load(reader);
        }
    } catch (final IOException e) {
        throw new RendererException("Problem during rendering", e);
    } finally {
        IOUtils.closeQuietly(reader);
    }

    return properties;
}

From source file:de.uzk.hki.da.metadata.MetsMetadataStructure.java

public void makeReplacementsHrefInMetsFile(File targetMetsFile, String currentHref, String targetHref,
        String mimetype, String loctype) throws IOException, JDOMException {
    SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder();
    logger.debug(":::" + workPath + ":::" + targetMetsFile.getPath());
    FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, targetMetsFile.getPath()));
    BOMInputStream bomInputStream = new BOMInputStream(fileInputStream);
    Reader reader = new InputStreamReader(bomInputStream, "UTF-8");
    InputSource is = new InputSource(reader);
    is.setEncoding("UTF-8");
    Document metsDoc = builder.build(is);

    List<Element> metsFileElements = metsParser.getFileElementsFromMetsDoc(metsDoc);

    for (int i = 0; i < metsFileElements.size(); i++) {
        Element fileElement = (Element) metsFileElements.get(i);
        if (metsParser.getHref(fileElement).equals(currentHref)) {
            setHref(fileElement, targetHref);
            setMimetype(fileElement, mimetype);
            setLoctype(fileElement, loctype);
        }/*from  w ww .  j  a v  a2  s . c om*/
    }

    fileInputStream.close();
    bomInputStream.close();
    reader.close();

    writeDocumentToFile(metsDoc, Path.makeFile(workPath, targetMetsFile.getPath()));
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.nettoExport.NettoCSVTransformerTest.java

@Test
/**//from  w w  w .j av a2  s. c o  m
 * Testcase creates 4 Informationsystemreleases with Text, Date and Numeric Attributes, which are transformed in CSV
 * The CSV output stream will be parsed again as CSV and the values are asserted
 */
public void testCSVTransformerForSpreadsheetReport() {

    NettoTransformer csvTransformer = NettoCSVTransformer
            .newInstance(createSimpleSpreadsheetReportTableStructure());

    assertNotNull("Can't create netto transformer for overview page table structure for csv", csvTransformer);

    //Create output stream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    csvTransformer.transform(sourceList, out, TypeOfBuildingBlock.INFORMATIONSYSTEMRELEASE);

    //assert that output stream
    InputStream in = new ByteArrayInputStream(out.toByteArray());
    CSVReader reader;
    try {

        //BOMInputStream is necessary, because of leading BOM in Stream
        //Otherwise assertion would fail
        BOMInputStream bOMInputStream = new BOMInputStream(in);
        ByteOrderMark bom = bOMInputStream.getBOM();
        String charsetName = bom == null ? "UTF-8" : bom.getCharsetName();

        reader = new CSVReader(new InputStreamReader(new BufferedInputStream(bOMInputStream), charsetName),
                ';');

        List<String[]> allLines = reader.readAll();

        int index = 0;
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN);

        for (String[] line : allLines) {

            String name = line[0];
            String desc = line[1];
            String start = line[2];
            String end = line[3];
            String statusString = line[4];
            String numString = line[5];

            //get the status enum first
            //we onlny need planned and current in this test
            TypeOfStatus status = null;

            if ("Plan".equals(statusString)) {
                status = TypeOfStatus.PLANNED;
            } else if ("Ist".equals(statusString)) {
                status = TypeOfStatus.CURRENT;
            }

            if (index == 0) {
                //assert the headers of CSV file
                assertEquals("Name und Version", name.trim());
                assertEquals("Beschreibung", desc);
                assertEquals("von", start);
                assertEquals("bis", end);
                assertEquals("Status", statusString);
                assertEquals("Complexity", numString);
            } else {

                //format the number
                NumberFormat nf = new DecimalFormat("0.00");
                String number = nf.format(Double.valueOf(numString));

                assertEquals(isArray[index - 1].getName(), name);
                assertEquals(isArray[index - 1].getDescription(), desc);
                assertEquals(df.format(isArray[index - 1].getRuntimePeriod().getStart()), start);
                assertEquals(df.format(isArray[index - 1].getRuntimePeriod().getEnd()), end);
                assertEquals(isArray[index - 1].getTypeOfStatus(), status);
                assertEquals(isArray[index - 1].getAttributeValue(numberAT.getName(), Locale.GERMAN), number);
            }

            index++;

        }

        reader.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:br.gov.lexml.parser.documentoarticulado.LexMLParserFromTextTest.java

private String sampleDocx(String resourceName) {
    String content = null;/*from www. j av a 2 s .  c o  m*/
    try {
        InputStream input = new BOMInputStream(TestUtil.class.getResourceAsStream(resourceName));
        XWPFDocument document = new XWPFDocument(OPCPackage.open(input));
        @SuppressWarnings("resource")
        XWPFWordExtractor wordExtractor = new XWPFWordExtractor(document);
        content = wordExtractor.getText();
    } catch (Exception exep) {
        exep.printStackTrace();
    }
    return content;
}

From source file:com.hangum.tadpole.commons.dialogs.fileupload.SingleFileuploadDialog.java

private boolean insert() throws Exception {
    BOMInputStream bomInputStream;//from  www .jav  a 2s  .c  o m

    File[] arryFiles = receiver.getTargetFiles();
    if (arryFiles.length == 0) {
        throw new Exception(Messages.get().SingleFileuploadDialog_5);
    }

    File userUploadFile = arryFiles[arryFiles.length - 1];
    try {
        // bom?  charset? ? ?.
        bomInputStream = new BOMInputStream(FileUtils.openInputStream(FileUtils.getFile(userUploadFile)));
        ByteOrderMark bom = bomInputStream.getBOM();
        String charsetName = bom == null ? "CP949" : bom.getCharsetName(); //$NON-NLS-1$

        strTxtFile = FileUtils.readFileToString(userUploadFile, charsetName);
        if (bom != null) {
            // ByteOrderMark.UTF_8.equals(strTxtFile.getBytes()[0]);
            //??? ??  BOM? .
            strTxtFile = strTxtFile.substring(1);
        }

    } catch (Exception e) {
        logger.error("file read error", e); //$NON-NLS-1$
        throw new Exception(Messages.get().SingleFileuploadDialog_7 + e.getMessage());
    }

    return true;
}