Example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace

List of usage examples for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace.

Prototype


public void setIgnoringElementContentWhitespace(boolean whitespace) 

Source Link

Document

Specifies that the parsers created by this factory must eliminate whitespace in element content (sometimes known loosely as 'ignorable whitespace') when parsing XML documents (see XML Rec 2.10).

Usage

From source file:org.dspace.app.util.SubmissionConfigReader.java

/**
 * Parse an XML encoded item submission configuration file.
 * <P>/* ww  w.j a  v a  2 s.  c o m*/
 * Creates two main hashmaps:
 * <ul>
 * <li>Hashmap of Collection to Submission definition mappings -
 * defines which Submission process a particular collection uses
 * <li>Hashmap of all Submission definitions.  List of all valid
 * Submision Processes by name.
 * </ul>
 */
private void buildInputs(String fileName) throws SubmissionConfigReaderException {
    collectionToSubmissionConfig = new HashMap<String, String>();
    submitDefns = new HashMap<String, List<Map<String, String>>>();

    String uri = "file:" + new File(fileName).getAbsolutePath();

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);

        DocumentBuilder db = factory.newDocumentBuilder();
        Document doc = db.parse(uri);
        doNodes(doc);
    } catch (FactoryConfigurationError fe) {
        throw new SubmissionConfigReaderException("Cannot create Item Submission Configuration parser", fe);
    } catch (Exception e) {
        throw new SubmissionConfigReaderException("Error creating Item Submission Configuration: " + e);
    }
}

From source file:org.dspace.submit.lookup.ArXivFileDataLoader.java

@Override
public RecordSet getRecords() throws MalformedSourceException {

    RecordSet recordSet = new RecordSet();

    try {//  ww  w  . ja  va2 s .c om
        InputStream inputStream = new FileInputStream(new File(filename));

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);

        DocumentBuilder db = factory.newDocumentBuilder();
        Document inDoc = db.parse(inputStream);

        Element xmlRoot = inDoc.getDocumentElement();
        List<Element> dataRoots = XMLUtils.getElementList(xmlRoot, "entry");

        for (Element dataRoot : dataRoots) {
            Record record = ArxivUtils.convertArxixDomToRecord(dataRoot);
            if (record != null) {
                recordSet.addRecord(convertFields(record));
            }
        }
    } catch (FileNotFoundException e) {
        log.error(e.getMessage(), e);
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
    } catch (SAXException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    return recordSet;
}

From source file:org.dspace.submit.lookup.CiNiiFileDataLoader.java

@Override
public RecordSet getRecords() throws MalformedSourceException {

    RecordSet recordSet = new RecordSet();

    try {/*  ww w .  ja va  2s. com*/
        InputStream inputStream = new FileInputStream(new File(filename));

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);

        DocumentBuilder db = factory.newDocumentBuilder();
        Document inDoc = db.parse(inputStream);

        Element xmlRoot = inDoc.getDocumentElement();

        // There is no element to represent an record, so we can not process 
        // multi records at once.
        Record record = CiNiiUtils.convertCiNiiDomToRecord(xmlRoot);
        if (record != null) {
            recordSet.addRecord(convertFields(record));
        }
    } catch (FileNotFoundException e) {
        log.error(e.getMessage(), e);
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
    } catch (SAXException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    return recordSet;
}

From source file:org.dspace.submit.lookup.CiNiiService.java

/**
 * Get metadata by searching CiNii RDF API with CiNii NAID
 *
 *///from  www  .j a va2 s  .com
private Record search(String id, String appId) throws IOException, HttpException {
    GetMethod method = null;
    try {
        HttpClient client = new HttpClient();
        client.setTimeout(timeout);
        method = new GetMethod("http://ci.nii.ac.jp/naid/" + id + ".rdf?appid=" + appId);
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            if (statusCode == HttpStatus.SC_BAD_REQUEST)
                throw new RuntimeException("CiNii RDF is not valid");
            else
                throw new RuntimeException("CiNii RDF Http call failed: " + method.getStatusLine());
        }

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setIgnoringComments(true);
            factory.setIgnoringElementContentWhitespace(true);

            DocumentBuilder db = factory.newDocumentBuilder();
            Document inDoc = db.parse(method.getResponseBodyAsStream());

            Element xmlRoot = inDoc.getDocumentElement();

            return CiNiiUtils.convertCiNiiDomToRecord(xmlRoot);
        } catch (Exception e) {
            throw new RuntimeException("CiNii RDF identifier is not valid or not exist");
        }
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:org.dspace.submit.lookup.CiNiiService.java

/**
 * Get CiNii NAIDs by searching CiNii OpenURL API with title, author and year
 *
 */// w w w .  jav a 2s  . c o  m
private List<String> getCiNiiIDs(String title, String author, int year, int maxResults, String appId)
        throws IOException, HttpException {
    // Need at least one query term
    if (title == null && author == null && year == -1) {
        return null;
    }

    GetMethod method = null;
    List<String> ids = new ArrayList<String>();
    try {
        HttpClient client = new HttpClient();
        client.setTimeout(timeout);
        StringBuilder query = new StringBuilder();
        query.append("format=rss&appid=").append(appId).append("&count=").append(maxResults);
        if (title != null) {
            query.append("&title=").append(URLEncoder.encode(title, "UTF-8"));
        }
        if (author != null) {
            query.append("&author=").append(URLEncoder.encode(author, "UTF-8"));
        }
        if (year != -1) {
            query.append("&year_from=").append(String.valueOf(year));
            query.append("&year_to=").append(String.valueOf(year));
        }
        method = new GetMethod("http://ci.nii.ac.jp/opensearch/search?" + query.toString());
        // Execute the method.
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            if (statusCode == HttpStatus.SC_BAD_REQUEST)
                throw new RuntimeException("CiNii OpenSearch query is not valid");
            else
                throw new RuntimeException("CiNii OpenSearch call failed: " + method.getStatusLine());
        }

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setIgnoringComments(true);
            factory.setIgnoringElementContentWhitespace(true);

            DocumentBuilder db = factory.newDocumentBuilder();
            Document inDoc = db.parse(method.getResponseBodyAsStream());

            Element xmlRoot = inDoc.getDocumentElement();
            List<Element> items = XMLUtils.getElementList(xmlRoot, "item");

            int url_len = "http://ci.nii.ac.jp/naid/".length();
            for (Element item : items) {
                String about = item.getAttribute("rdf:about");
                if (about.length() > url_len) {
                    ids.add(about.substring(url_len));
                }
            }

            return ids;
        } catch (Exception e) {
            throw new RuntimeException("CiNii OpenSearch results is not valid or not exist");
        }
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:org.dspace.submit.lookup.CrossRefFileDataLoader.java

@Override
public RecordSet getRecords() throws MalformedSourceException {

    RecordSet recordSet = new RecordSet();

    try {/* w  ww. j  a  v  a 2s .  c o m*/
        InputStream inputStream = new FileInputStream(new File(filename));

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);

        DocumentBuilder db = factory.newDocumentBuilder();
        Document inDoc = db.parse(inputStream);

        Element xmlRoot = inDoc.getDocumentElement();
        Element queryResult = XMLUtils.getSingleElement(xmlRoot, "query_result");
        Element body = XMLUtils.getSingleElement(queryResult, "body");
        Element dataRoot = XMLUtils.getSingleElement(body, "query");
        Record record = CrossRefUtils.convertCrossRefDomToRecord(dataRoot);
        recordSet.addRecord(convertFields(record));

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return recordSet;

}

From source file:org.dspace.submit.lookup.PubmedFileDataLoader.java

@Override
public RecordSet getRecords() throws MalformedSourceException {

    RecordSet recordSet = new RecordSet();

    try {/*from ww  w .j  ava  2  s  .  co  m*/
        InputStream inputStream = new FileInputStream(new File(filename));

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document inDoc = builder.parse(inputStream);

        Element xmlRoot = inDoc.getDocumentElement();
        List<Element> pubArticles = XMLUtils.getElementList(xmlRoot, "PubmedArticle");

        for (Element xmlArticle : pubArticles) {
            Record record = null;
            try {
                record = PubmedUtils.convertPubmedDomToRecord(xmlArticle);
                recordSet.addRecord(convertFields(record));
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return recordSet;

}

From source file:org.fosstrak.epcis.repository.capture.CaptureOperationsModule.java

/**
 * Parses the input into a DOM. If a schema is given, the input is also
 * validated against this schema. The schema may be null.
 *///from   ww w  .  j ava  2s  .c o m
private Document parseInput(InputStream in, Schema schema)
        throws InternalBusinessException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setIgnoringComments(true);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setSchema(schema);
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {
            public void warning(SAXParseException e) throws SAXException {
                LOG.warn("warning while parsing XML input: " + e.getMessage());
            }

            public void fatalError(SAXParseException e) throws SAXException {
                LOG.error("non-recovarable error while parsing XML input: " + e.getMessage());
                throw e;
            }

            public void error(SAXParseException e) throws SAXException {
                LOG.error("error while parsing XML input: " + e.getMessage());
                throw e;
            }
        });
        Document document = builder.parse(in);
        LOG.debug("payload successfully parsed as XML document");
        if (LOG.isDebugEnabled()) {
            logDocument(document);
        }
        return document;
    } catch (ParserConfigurationException e) {
        throw new InternalBusinessException("unable to configure document builder to parse XML input", e);
    }
}

From source file:org.fusesource.cloudmix.agent.jbi.JBIInstallerAgent.java

private Document parse(String result) throws ParserConfigurationException, SAXException, IOException {

    LOGGER.debug("Parsing XML Document:\n" + result);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from w  w  w  .j  a  va  2  s  .  com*/
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(result)));
}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth2ConfService.java

/**
 * @param stream/*from  w w  w  . ja  v a2s .co m*/
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
public synchronized GluuErrorHandler validateMetadata(InputStream stream)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
    newFactory.setCoalescing(false);
    newFactory.setExpandEntityReferences(true);
    newFactory.setIgnoringComments(false);

    newFactory.setIgnoringElementContentWhitespace(false);
    newFactory.setNamespaceAware(true);
    newFactory.setValidating(false);
    DocumentBuilder xmlParser = newFactory.newDocumentBuilder();
    Document xmlDoc = xmlParser.parse(stream);
    String schemaDir = System.getProperty("catalina.home") + File.separator + "conf" + File.separator
            + "shibboleth2" + File.separator + "idp" + File.separator + "schema" + File.separator;
    Schema schema = SchemaBuilder.buildSchema(SchemaLanguage.XML, schemaDir);
    Validator validator = schema.newValidator();
    GluuErrorHandler handler = new GluuErrorHandler();
    validator.setErrorHandler(handler);
    validator.validate(new DOMSource(xmlDoc));

    return handler;

}