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

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Invokes the delegate's close() method.

Usage

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

/**
 * Append to each dmdSec in a Mets-File one accessCondition-Element and save it.
 * /* w  w w.  ja  v  a2  s.co m*/
 * @param targetMetsFile
 * @param licenseHref
 * @param displayLabel
 * @param text
 * @throws IOException
 * @throws JDOMException
 */
public void appendAccessCondition(File targetMetsFile, String licenseHref, String displayLabel, String text)
        throws IOException, JDOMException {
    SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder();

    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> dmdSections = metsDoc.getRootElement().getChildren("dmdSec", C.METS_NS);

    for (int i = 0; i < dmdSections.size(); i++) {
        Element newAccessConditionE = generateAccessCondition(licenseHref, displayLabel, text);
        logger.debug("Append to Mets new LicenseElement: " + newAccessConditionE.toString());
        Element dmdSecElement = (Element) dmdSections.get(i);
        Element modsXmlData = MetsParser.getModsXmlData(dmdSecElement);
        modsXmlData.addContent(newAccessConditionE);
    }
    fileInputStream.close();
    bomInputStream.close();
    reader.close();

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

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

public EadMetsMetadataStructure(Path workPath, File metadataFile, List<de.uzk.hki.da.model.Document> documents)
        throws JDOMException, IOException, ParserConfigurationException, SAXException {
    super(workPath, metadataFile, documents);

    eadFile = metadataFile;//from  w  w w  .ja v  a 2  s.com

    SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder();
    FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, eadFile.getPath()));
    BOMInputStream bomInputStream = new BOMInputStream(fileInputStream);
    Reader reader = new InputStreamReader(bomInputStream, "UTF-8");
    InputSource is = new InputSource(reader);
    is.setEncoding("UTF-8");
    eadDoc = builder.build(is);
    EAD_NS = eadDoc.getRootElement().getNamespace();
    eadParser = new EadParser(eadDoc);

    metsReferencesInEAD = eadParser.getReferences();
    metsFiles = getReferencedFiles(eadFile, metsReferencesInEAD, documents);

    mmsList = new ArrayList<MetsMetadataStructure>();
    for (File metsFile : metsFiles) {
        MetsMetadataStructure mms = new MetsMetadataStructure(workPath, metsFile, documents);
        mmsList.add(mms);
    }
    fileInputStream.close();
    bomInputStream.close();
    reader.close();
}

From source file:de.uzk.hki.da.cb.CreateDCAction.java

/**
 * @param packageType//from  w  w  w .  j a v  a  2  s. c  o m
 * @param metadataFile
 */
private void copyDCdatastreamFromMetadata(String audience) {

    FileInputStream inputStream = null;
    BOMInputStream bomInputStream = null;
    FileOutputStream outputStream = null;

    String xsltFile = getDcMappings().get(o.getPackage_type());
    if (xsltFile == null) {
        throw new RuntimeException("No conversion available for package type '" + o.getPackage_type()
                + "'. DC can not be created.");
    }
    try {
        inputStream = new FileInputStream(wa.pipMetadataFile(audience, o.getPackage_type()));

        bomInputStream = new BOMInputStream(inputStream);
        XsltGenerator xsltGenerator = new XsltGenerator(xsltFile, bomInputStream);

        String result = xsltGenerator.generate();

        File file = wa.pipMetadataFile(audience, METADATA_STREAM_ID_DC);
        outputStream = new FileOutputStream(file);

        outputStream.write(result.getBytes(C.ENCODING_UTF_8.toLowerCase()));
        outputStream.flush();

    } catch (Exception e) {
        throw new RuntimeException("Unable to create DC file.", e);
    } finally {
        try {
            if (inputStream != null)
                inputStream.close();
            if (bomInputStream != null)
                bomInputStream.close();
            if (outputStream != null)
                outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

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

public void replaceMetsRefsInEad(File eadFile, HashMap<String, String> eadReplacements)
        throws JDOMException, IOException {

    File targetEadFile = eadFile;

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

    String namespaceUri = eadDoc.getRootElement().getNamespace().getURI();
    XPath xPath = XPath.newInstance(C.EAD_XPATH_EXPRESSION);

    //      Case of new DDB EAD with namespace xmlns="urn:isbn:1-931666-22-9"
    if (!namespaceUri.equals("")) {
        xPath = XPath.newInstance("//isbn:daoloc/@href");
        xPath.addNamespace("isbn", eadDoc.getRootElement().getNamespace().getURI());
    }/*from w w  w  .  j  a  va2 s.  c  o m*/

    @SuppressWarnings("rawtypes")
    List allNodes = xPath.selectNodes(currentEadDoc);

    for (Object node : allNodes) {
        Attribute attr = (Attribute) node;
        for (String replacement : eadReplacements.keySet()) {
            if (attr.getValue().equals(replacement)) {
                attr.setValue(eadReplacements.get(replacement));
            }
        }
    }

    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    outputter.output(currentEadDoc, new FileWriter(Path.makeFile(workPath, targetEadFile.getPath())));
    fileInputStream.close();
    bomInputStream.close();
    reader.close();
}

From source file:com.hangum.tadpole.importdb.core.dialog.importdb.sql.SQLToDBImportDialog.java

private void insert() throws IOException {
    int ret;//from   w  w  w.  java  2s .co m
    BOMInputStream bomInputStream = null;

    File[] arryFiles = receiver.getTargetFiles();
    if (arryFiles.length == 0) {
        MessageDialog.openError(null, Messages.CsvToRDBImportDialog_4, Messages.CsvToRDBImportDialog_21);
        return;
    }

    if (!MessageDialog.openConfirm(null, Messages.CsvToRDBImportDialog_4,
            Messages.SQLToDBImportDialog_UploadQuestion))
        return;
    bufferBatchResult = new StringBuffer();

    try {
        batchSize = Integer.valueOf(textBatchSize.getText());
    } catch (Exception e) {
        batchSize = 1000;
    }

    File userUploadFile = arryFiles[arryFiles.length - 1];
    try {
        // bom?  charset? ? ?.
        bomInputStream = new BOMInputStream(FileUtils.openInputStream(FileUtils.getFile(userUploadFile)));//`, false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);

        String charsetName = "utf-8";
        String strSQLData = "";
        if (bomInputStream.getBOM() == null) {
            strSQLData = FileUtils.readFileToString(userUploadFile, charsetName);
        } else {
            charsetName = bomInputStream.getBOMCharsetName();
            strSQLData = FileUtils.readFileToString(userUploadFile, charsetName).substring(1);
        }

        String[] strArrySQL = StringUtils.split(strSQLData, textSeprator.getText());
        ret = runSQLExecuteBatch(Arrays.asList(strArrySQL));

        if (ret == 0)
            MessageDialog.openInformation(null, "Confirm", Messages.SQLToDBImportDialog_StoreData); //$NON-NLS-1$
    } catch (IOException e) {
        logger.error(Messages.SQLToDBImportDialog_ReadError, e);
        MessageDialog.openError(null, Messages.CsvToRDBImportDialog_4,
                Messages.SQLToDBImportDialog_LoadException + e.getMessage());

    } catch (Exception e) {
        logger.error(Messages.SQLToDBImportDialog_ImportException, e);
        MessageDialog.openError(null, Messages.CsvToRDBImportDialog_4,
                Messages.SQLToDBImportDialog_LoadException + e.getMessage());
    } finally {
        if (bomInputStream != null)
            bomInputStream.close();
    }
}

From source file:com.hangum.tadpole.importexport.core.dialogs.SQLToDBImportDialog.java

private void insert() throws IOException {
    int ret;/*from ww  w  .  ja  v  a 2  s  . c o  m*/
    BOMInputStream bomInputStream = null;

    File[] arryFiles = receiver.getTargetFiles();
    if (arryFiles.length == 0) {
        MessageDialog.openWarning(null, Messages.get().Warning, Messages.get().CsvToRDBImportDialog_21);
        return;
    }

    if (!MessageDialog.openConfirm(null, Messages.get().Confirm,
            Messages.get().SQLToDBImportDialog_UploadQuestion))
        return;
    bufferBatchResult = new StringBuffer();

    try {
        batchSize = Integer.valueOf(textBatchSize.getText());
    } catch (Exception e) {
        batchSize = 1000;
    }

    File userUploadFile = arryFiles[arryFiles.length - 1];
    try {
        // bom?  charset? ? ?.
        bomInputStream = new BOMInputStream(FileUtils.openInputStream(FileUtils.getFile(userUploadFile)));//`, false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);

        String charsetName = "utf-8"; //$NON-NLS-1$
        String strSQLData = ""; //$NON-NLS-1$
        if (bomInputStream.getBOM() == null) {
            strSQLData = FileUtils.readFileToString(userUploadFile, charsetName);
        } else {
            charsetName = bomInputStream.getBOMCharsetName();
            strSQLData = FileUtils.readFileToString(userUploadFile, charsetName).substring(1);
        }

        String[] strArrySQL = StringUtils.split(strSQLData, textSeprator.getText());
        ret = runSQLExecuteBatch(Arrays.asList(strArrySQL));

        if (ret == 0)
            MessageDialog.openInformation(null, Messages.get().Confirm,
                    Messages.get().SQLToDBImportDialog_StoreData); //$NON-NLS-1$
    } catch (IOException e) {
        logger.error(Messages.get().SQLToDBImportDialog_ReadError, e);
        MessageDialog.openError(null, Messages.get().Confirm,
                Messages.get().SQLToDBImportDialog_LoadException + e.getMessage());

    } catch (Exception e) {
        logger.error(Messages.get().SQLToDBImportDialog_ImportException, e);
        MessageDialog.openError(null, Messages.get().Confirm,
                Messages.get().SQLToDBImportDialog_LoadException + e.getMessage());
    } finally {
        if (bomInputStream != null)
            bomInputStream.close();
    }
}

From source file:org.apache.flex.compiler.internal.embedding.transcoders.XMLTranscoder.java

private String getXMLString(Collection<ICompilerProblem> problems) {
    InputStream strm = getDataStream(problems);
    if (strm == null)
        return "";

    Reader reader = null;// ww w.j a  va2s  . c o m
    BOMInputStream bomStream = null;
    StringBuilder str = new StringBuilder();
    try {
        bomStream = new BOMInputStream(strm);
        String bomCharsetName = bomStream.getBOMCharsetName();
        if (bomCharsetName == null) {
            if (encoding == null || encoding.length() == 0) {
                bomCharsetName = System.getProperty("file.encoding");
            } else {
                bomCharsetName = encoding;
            }
        }

        reader = new InputStreamReader(bomStream, bomCharsetName);
        char[] line = new char[2048];
        int count = 0;
        while ((count = reader.read(line, 0, line.length)) >= 0) {
            str.append(line, 0, count);
        }
    } catch (IOException e) {
        problems.add(new EmbedSourceAttributeCouldNotBeReadProblem(source));
    } finally {
        if (bomStream != null) {
            try {
                bomStream.close();
            } catch (IOException e) {
            }
        }

        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }

    return str.toString();
}

From source file:org.jboss.elasticsearch.river.remote.sitemap.SiteMapParser.java

/**
 * Decompress the gzipped content and process the resulting XML Sitemap.
 * //from w ww.j  a v  a2s.  c  o m
 * @param url - URL of the gzipped content
 * @param response - Gzipped content
 * @throws MalformedURLException
 * @throws IOException
 * @throws UnknownFormatException
 */
private AbstractSiteMap processGzip(URL url, byte[] response)
        throws MalformedURLException, IOException, UnknownFormatException {

    logger.debug("Processing gzip");

    AbstractSiteMap smi;

    InputStream is = new ByteArrayInputStream(response);

    // Remove .gz ending
    String xmlUrl = url.toString().replaceFirst("\\.gz$", "");

    logger.debug("XML url = " + xmlUrl);

    BOMInputStream decompressed = new BOMInputStream(new GZIPInputStream(is));
    InputSource in = new InputSource(decompressed);
    in.setSystemId(xmlUrl);
    smi = processXml(url, in);
    decompressed.close();
    return smi;
}