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:com.persistent.cloudninja.scheduler.DeploymentMonitor.java

/**
 * Parses the response received by making call to REST API.
 * It parses and gets the total no. roles and their instances.
 * //from w  w  w.  jav  a  2  s  . c o m
 * @param roleInfo
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws XPathExpressionException
 * @throws ParseException
 */
private void parseRoleInfo(StringBuffer roleInfo) throws ParserConfigurationException, SAXException,
        IOException, XPathExpressionException, ParseException {
    DocumentBuilder docBuilder = null;
    Document doc = null;
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setIgnoringElementContentWhitespace(true);
    docBuilder = docBuilderFactory.newDocumentBuilder();
    doc = docBuilder.parse(new InputSource(new StringReader(roleInfo.toString())));

    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();
    NodeList roleList = (NodeList) xPath.evaluate("/Deployment/RoleList/Role/RoleName", doc,
            XPathConstants.NODESET);
    //get all the roles
    String roleName = null;
    List<String> listRoleNames = new ArrayList<String>();
    for (int i = 0; i < roleList.getLength(); i++) {
        Element element = (Element) roleList.item(i);
        roleName = element.getTextContent();
        RoleEntity roleEntity = roleDao.findByRoleName(roleName);
        if (roleEntity == null) {
            roleEntity = new RoleEntity();
            roleEntity.setName(roleName);
            roleDao.add(roleEntity);
        }
        listRoleNames.add(roleName);
    }

    xPathFactory = XPathFactory.newInstance();
    xPath = xPathFactory.newXPath();
    RoleInstancesEntity roleInstancesEntity = null;
    RoleInstancesId roleInstancesId = null;
    for (String name : listRoleNames) {
        roleList = (NodeList) xPath.evaluate(
                "/Deployment/RoleInstanceList/RoleInstance[RoleName='" + name + "']", doc,
                XPathConstants.NODESET);
        //get no. of instances for WorkerRole
        int noOfInstances = roleList.getLength();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        String date = dateFormat.format(calendar.getTime());
        roleInstancesId = new RoleInstancesId(dateFormat.parse(date), name);
        roleInstancesEntity = new RoleInstancesEntity(roleInstancesId, noOfInstances, "UPDATE");
        roleInstancesDao.add(roleInstancesEntity);
    }

}

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

protected List<Record> search(String query, String arxivid, int max_result) throws IOException, HttpException {
    List<Record> results = new ArrayList<Record>();
    HttpGet method = null;/*from w  w w  .j  av a2 s . co  m*/
    try {
        HttpClient client = new DefaultHttpClient();
        HttpParams params = client.getParams();
        params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);

        try {
            URIBuilder uriBuilder = new URIBuilder("http://export.arxiv.org/api/query");
            uriBuilder.addParameter("id_list", arxivid);
            uriBuilder.addParameter("search_query", query);
            uriBuilder.addParameter("max_results", String.valueOf(max_result));
            method = new HttpGet(uriBuilder.build());
        } catch (URISyntaxException ex) {
            throw new HttpException(ex.getMessage());
        }

        // Execute the method.
        HttpResponse response = client.execute(method);
        StatusLine responseStatus = response.getStatusLine();
        int statusCode = responseStatus.getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {
            if (statusCode == HttpStatus.SC_BAD_REQUEST)
                throw new RuntimeException("arXiv query is not valid");
            else
                throw new RuntimeException("Http call failed: " + responseStatus);
        }

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

            DocumentBuilder db = factory.newDocumentBuilder();
            Document inDoc = db.parse(response.getEntity().getContent());

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

            for (Element dataRoot : dataRoots) {
                Record crossitem = ArxivUtils.convertArxixDomToRecord(dataRoot);
                if (crossitem != null) {
                    results.add(crossitem);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("ArXiv identifier is not valid or not exist");
        }
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }

    return results;
}

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

public List<Record> search(Context context, Set<String> dois, String apiKey)
        throws HttpException, IOException, JDOMException, ParserConfigurationException, SAXException {
    List<Record> results = new ArrayList<Record>();
    if (dois != null && dois.size() > 0) {
        for (String record : dois) {
            try {
                HttpGet method = null;// w w w .j  av  a 2  s  .  c o  m
                try {
                    HttpClient client = new DefaultHttpClient();
                    client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);

                    try {
                        URIBuilder uriBuilder = new URIBuilder("http://www.crossref.org/openurl/");
                        uriBuilder.addParameter("pid", apiKey);
                        uriBuilder.addParameter("noredirect", "true");
                        uriBuilder.addParameter("id", record);
                        method = new HttpGet(uriBuilder.build());
                    } catch (URISyntaxException ex) {
                        throw new HttpException("Request not sent", ex);
                    }

                    // Execute the method.
                    HttpResponse response = client.execute(method);
                    StatusLine statusLine = response.getStatusLine();
                    int statusCode = statusLine.getStatusCode();

                    if (statusCode != HttpStatus.SC_OK) {
                        throw new RuntimeException("Http call failed: " + statusLine);
                    }

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

                        DocumentBuilder db = factory.newDocumentBuilder();
                        Document inDoc = db.parse(response.getEntity().getContent());

                        Element xmlRoot = inDoc.getDocumentElement();
                        Element queryResult = XMLUtils.getSingleElement(xmlRoot, "query_result");
                        Element body = XMLUtils.getSingleElement(queryResult, "body");
                        Element dataRoot = XMLUtils.getSingleElement(body, "query");

                        crossitem = CrossRefUtils.convertCrossRefDomToRecord(dataRoot);
                        results.add(crossitem);
                    } catch (Exception e) {
                        log.warn(LogManager.getHeader(context, "retrieveRecordDOI",
                                record + " DOI is not valid or not exist: " + e.getMessage()));
                    }
                } finally {
                    if (method != null) {
                        method.releaseConnection();
                    }
                }
            } catch (RuntimeException rt) {
                log.error(rt.getMessage(), rt);
            }
        }
    }
    return results;
}

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

public List<Record> getByPubmedIDs(List<String> pubmedIDs)
        throws HttpException, IOException, ParserConfigurationException, SAXException {
    List<Record> results = new ArrayList<Record>();
    HttpGet method = null;/*  ww w .  jav a 2  s . c o  m*/
    try {
        HttpClient client = new DefaultHttpClient();
        client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5 * timeout);

        try {
            URIBuilder uriBuilder = new URIBuilder("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi");
            uriBuilder.addParameter("db", "pubmed");
            uriBuilder.addParameter("retmode", "xml");
            uriBuilder.addParameter("rettype", "full");
            uriBuilder.addParameter("id", StringUtils.join(pubmedIDs.iterator(), ","));
            method = new HttpGet(uriBuilder.build());
        } catch (URISyntaxException ex) {
            throw new RuntimeException("Request not sent", ex);
        }

        // Execute the method.
        HttpResponse response = client.execute(method);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {
            throw new RuntimeException("WS call failed: " + statusLine);
        }

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

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document inDoc = builder.parse(response.getEntity().getContent());

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

        for (Element xmlArticle : pubArticles) {
            Record pubmedItem = null;
            try {
                pubmedItem = PubmedUtils.convertPubmedDomToRecord(xmlArticle);
                results.add(pubmedItem);
            } catch (Exception e) {
                throw new RuntimeException("PubmedID is not valid or not exist: " + e.getMessage(), e);
            }
        }

        return results;
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:io.fabric8.forge.ipaas.repository.NexusConnectionRepository.java

protected void indexNexus() throws Exception {
    // must have q parameter so use connector to find all connectors
    String query = nexusUrl + "?q=connector";
    URL url = new URL(query);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//  w w w  .j  a va  2  s  .  c  om
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);

    DocumentBuilder documentBuilder = factory.newDocumentBuilder();

    InputStream is = url.openStream();
    Document dom = documentBuilder.parse(is);

    XPathFactory xpFactory = XPathFactory.newInstance();
    XPath exp = xpFactory.newXPath();
    NodeList list = (NodeList) exp.evaluate("//classifier[text() = '" + CLASSIFIER + "']", dom,
            XPathConstants.NODESET);

    Set<NexusArtifactDto> newArtifacts = new LinkedHashSet<>();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        Node parent = node.getParentNode();

        String g = getNodeText(parent.getChildNodes(), "groupId");
        String a = getNodeText(parent.getChildNodes(), "artifactId");
        String v = getNodeText(parent.getChildNodes(), "version");
        String l = getNodeText(parent.getChildNodes(), "artifactLink");

        if (g != null & a != null & v != null & l != null) {
            NexusArtifactDto dto = new NexusArtifactDto();
            dto.setGroupId(g);
            dto.setArtifactId(a);
            dto.setVersion(v);
            dto.setArtifactLink(l);

            System.out.println("Found connector: " + dto.getGroupId() + ":" + dto.getArtifactId() + ":"
                    + dto.getVersion());

            // is it a new artifact
            boolean newArtifact = true;
            for (NexusArtifactDto existing : indexedArtifacts) {
                if (existing.getGroupId().equals(dto.getGroupId())
                        && existing.getArtifactId().equals(dto.getArtifactId())
                        && existing.getVersion().equals(dto.getVersion())) {
                    newArtifact = false;
                    break;
                }
            }
            if (newArtifact) {
                newArtifacts.add(dto);
            }
        }
    }

    // now download the new artifact JARs and look inside to find more details
    for (NexusArtifactDto dto : newArtifacts) {
        try {
            // download using url classloader reader
            URL jarUrl = new URL(dto.getArtifactLink());
            String json = loadCamelConnectorJSonSchema(jarUrl);

            ObjectMapper mapper = new ObjectMapper();
            ConnectionCatalogDto cat = mapper.readerFor(ConnectionCatalogDto.class).readValue(json);

            indexedArtifacts.add(dto);
            connectors.putIfAbsent(dto, cat);
            System.out.println("Added connector: " + dto.getGroupId() + ":" + dto.getArtifactId() + ":"
                    + dto.getVersion());
        } catch (Exception e) {
            System.err.println("Error downloading connector JAR " + dto.getArtifactLink()
                    + ". This exception is ignored. " + e.getMessage());
        }
    }

    IOHelpers.close(is);
}

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

public List<Record> search(String query) throws IOException, HttpException {
    List<Record> results = new ArrayList<>();
    if (!ConfigurationManager.getBooleanProperty(SubmissionLookupService.CFG_MODULE, "remoteservice.demo")) {
        HttpGet method = null;//from   w  w w .  jav  a2 s  . c om
        try {
            HttpClient client = new DefaultHttpClient();
            client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);

            URIBuilder uriBuilder = new URIBuilder("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi");
            uriBuilder.addParameter("db", "pubmed");
            uriBuilder.addParameter("datetype", "edat");
            uriBuilder.addParameter("retmax", "10");
            uriBuilder.addParameter("term", query);
            method = new HttpGet(uriBuilder.build());

            // Execute the method.
            HttpResponse response = client.execute(method);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            if (statusCode != HttpStatus.SC_OK) {
                throw new RuntimeException("WS call failed: " + statusLine);
            }

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

            DocumentBuilder builder;
            try {
                builder = factory.newDocumentBuilder();

                Document inDoc = builder.parse(response.getEntity().getContent());

                Element xmlRoot = inDoc.getDocumentElement();
                Element idList = XMLUtils.getSingleElement(xmlRoot, "IdList");
                List<String> pubmedIDs = XMLUtils.getElementValueList(idList, "Id");
                results = getByPubmedIDs(pubmedIDs);
            } catch (ParserConfigurationException e1) {
                log.error(e1.getMessage(), e1);
            } catch (SAXException e1) {
                log.error(e1.getMessage(), e1);
            }
        } catch (Exception e1) {
            log.error(e1.getMessage(), e1);
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }
    } else {
        InputStream stream = null;
        try {
            File file = new File(ConfigurationManager.getProperty("dspace.dir")
                    + "/config/crosswalks/demo/pubmed-search.xml");
            stream = new FileInputStream(file);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setIgnoringComments(true);
            factory.setIgnoringElementContentWhitespace(true);

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

            Element xmlRoot = inDoc.getDocumentElement();
            Element idList = XMLUtils.getSingleElement(xmlRoot, "IdList");
            List<String> pubmedIDs = XMLUtils.getElementValueList(idList, "Id");
            results = getByPubmedIDs(pubmedIDs);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return results;
}

From source file:XMLReader.java

/** Parses XML file and returns XML document.
 * @param fileName XML file to parse/*www  . jav  a2 s  . c o  m*/
 * @return XML document or <B>null</B> if error occured
 */
private Document parseFile(String fileName) {
    System.out.println("Parsing XML file... " + fileName);
    DocumentBuilder docBuilder;
    Document doc = null;
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setIgnoringElementContentWhitespace(true);

    boolean success = true;

    try {
        docBuilder = docBuilderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        System.err.println("XMLReader::parseFile - Wrong parser configuration: " + e.getMessage());
        success = false;
        return null;
    }
    File sourceFile = new File(fileName);
    try {
        doc = docBuilder.parse(sourceFile);
    } catch (SAXException e) {
        System.err.println("XMLReader::parseFile - Wrong XML file structure: " + e.getMessage());
        success = false;
        return null;
    } catch (FileNotFoundException catcher) {
        System.err.println("AgencyTable::initNames - AgencyInfo.skc not Found: " + catcher.getMessage());
        System.out.println("Expected XML file to be in following location: " + System.getProperty("user.dir")
                + "\\" + fileName + ".");
    } catch (IOException e) {
        System.err.println("XMLReader::parseFile - Could not read source file: " + e.getMessage());
        success = false;
    }
    System.out.println("XML file parsed" + (success ? "" : " - but with errors"));

    if (!success) {
        System.out.println("Error reading XML file.  Refer to the log file for more information.");
    }

    return doc;
}

From source file:com.mediaworx.xmlutils.XmlHelper.java

/**
 * Creates and returns a document builder that is configured with the following options:
 * <ul>//from   w  w  w  . j  av a  2  s  .  c  om
 *     <li>don't validate</li>
 *     <li>ignore comments</li>
 *     <li>ignore content whitespace</li>
 *     <li>convert CDATA nodes to text nodes</li>
 *     <li>don't perform namespace processing</li>
 *     <li>ignore DTDs</li>
 * </ul>
 * @return the DocumentBuilder
 * @throws ParserConfigurationException if for some reason the DocumentBuilder used to parse the XML can't be
 *                                      initialized
 */
private DocumentBuilder getNonValidatingDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setValidating(false);
    documentBuilderFactory.setIgnoringComments(true);
    documentBuilderFactory.setIgnoringElementContentWhitespace(true);
    documentBuilderFactory.setCoalescing(true);
    documentBuilderFactory.setFeature("http://xml.org/sax/features/namespaces", false);
    documentBuilderFactory.setFeature("http://xml.org/sax/features/validation", false);
    documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    return documentBuilderFactory.newDocumentBuilder();
}

From source file:userinterface.graph.Graph.java

/**
 * Method to load a PRISM 'gra' file into the application.
 * @param file Name of the file to load.
 * @return The model of the graph contained in the file.
 * @throws GraphException if I/O errors have occurred.
 *//*from  ww w . j  av  a  2s  . c  o  m*/
public static Graph load(File file) throws GraphException {

    Graph graph = new Graph();

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        factory.setValidating(true);
        factory.setIgnoringElementContentWhitespace(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(graph);
        Document doc = builder.parse(file);
        Element chartFormat = doc.getDocumentElement();

        graph.setTitle(chartFormat.getAttribute("graphTitle"));

        String titleFontName = chartFormat.getAttribute("titleFontName");
        String titleFontSize = chartFormat.getAttribute("titleFontSize");
        String titleFontStyle = chartFormat.getAttribute("titleFontStyle");

        Font titleFont = parseFont(titleFontName, titleFontStyle, titleFontSize);

        String titleFontColourR = chartFormat.getAttribute("titleFontColourR");
        String titleFontColourG = chartFormat.getAttribute("titleFontColourG");
        String titleFontColourB = chartFormat.getAttribute("titleFontColourB");
        Color titleFontColour = parseColor(titleFontColourR, titleFontColourG, titleFontColourB);

        graph.setTitleFont(new FontColorPair(titleFont, titleFontColour));
        graph.setLegendVisible(parseBoolean(chartFormat.getAttribute("legendVisible")));

        String legendPosition = chartFormat.getAttribute("legendPosition");

        // Facilitate for bugs export in previous prism versions.
        if (chartFormat.getAttribute("versionString").equals(""))
            graph.setLegendPosition(RIGHT);
        else {
            if (legendPosition.equals("left"))
                graph.setLegendPosition(LEFT);
            else if (legendPosition.equals("right"))
                graph.setLegendPosition(RIGHT);
            else if (legendPosition.equals("bottom"))
                graph.setLegendPosition(BOTTOM);
            else if (legendPosition.equals("top"))
                graph.setLegendPosition(TOP);
            else // Probably was manual, now depricated
                graph.setLegendPosition(RIGHT);
        }

        //Get the nodes used to describe the various parts of the graph
        NodeList rootChildren = chartFormat.getChildNodes();

        // Element layout is depricated for now. 
        Element layout = (Element) rootChildren.item(0);
        Element xAxis = (Element) rootChildren.item(1);
        Element yAxis = (Element) rootChildren.item(2);

        graph.getXAxisSettings().load(xAxis);
        graph.getYAxisSettings().load(yAxis);

        //Read the headings and widths for each series 
        for (int i = 3; i < rootChildren.getLength(); i++) {
            Element series = (Element) rootChildren.item(i);
            SeriesKey key = graph.addSeries(series.getAttribute("seriesHeading"));

            synchronized (graph.getSeriesLock()) {
                SeriesSettings seriesSettings = graph.getGraphSeries(key);
                seriesSettings.load(series);

                NodeList graphChildren = series.getChildNodes();

                //Read each series out of the file and add its points to the graph
                for (int j = 0; j < graphChildren.getLength(); j++) {
                    Element point = (Element) graphChildren.item(j);
                    graph.addPointToSeries(key, new XYDataItem(parseDouble(point.getAttribute("x")),
                            parseDouble(point.getAttribute("y"))));
                }
            }
        }

        //Return the model of the graph 
        return graph;
    } catch (Exception e) {
        throw new GraphException("Error in loading chart: " + e);
    }
}