Example usage for org.w3c.dom.ls LSInput setEncoding

List of usage examples for org.w3c.dom.ls LSInput setEncoding

Introduction

In this page you can find the example usage for org.w3c.dom.ls LSInput setEncoding.

Prototype

public void setEncoding(String encoding);

Source Link

Document

The character encoding, if known.

Usage

From source file:edu.ur.ir.ir_import.service.DefaultCollectionImportService.java

/**
 * Load the dspace collection information from the xml file.
 * @throws DuplicateNameException //from  ww  w . j a  v a 2 s.  c  o  m
 * 
 * @see edu.ur.dspace.load.CollectionImporter#getCollections(java.io.File)
 */
private void getCollections(File communityXmlFile, Repository repo, ZipFile zip)
        throws IOException, DuplicateNameException {
    if (log.isDebugEnabled()) {
        log.debug("get collections");
    }
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;

    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e);
    }

    DOMImplementation impl = builder.getDOMImplementation();
    DOMImplementationLS domLs = (DOMImplementationLS) impl.getFeature("LS", "3.0");
    LSInput lsIn = domLs.createLSInput();
    LSParser parser = domLs.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

    lsIn.setEncoding("UTF-8");

    FileInputStream fileInputStream;
    try {
        fileInputStream = new FileInputStream(communityXmlFile);
    } catch (FileNotFoundException e) {
        throw new IllegalStateException(e);
    }
    InputStreamReader inputStreamReader;
    try {
        inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException(e);
    }
    lsIn.setCharacterStream(inputStreamReader);

    Document doc = parser.parse(lsIn);
    Element root = doc.getDocumentElement();

    NodeList nodeList = root.getChildNodes();

    log.debug("node list length = " + nodeList.getLength());
    for (int index = 0; index < nodeList.getLength(); index++) {
        Node child = nodeList.item(index);
        importCollection(child, repo, zip);
    }
}