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.josso.tooling.gshell.install.installer.VFSInstaller.java

protected Document readContentAsDom(FileObject file, boolean nameSpaceAware) throws Exception {
    InputStream is = null;//w ww.j av a  2  s  .c  om

    try {
        is = file.getContent().getInputStream();

        DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
        parserFactory.setValidating(false);
        parserFactory.setNamespaceAware(nameSpaceAware);
        parserFactory.setIgnoringElementContentWhitespace(false);
        parserFactory.setIgnoringComments(false);

        DocumentBuilder builder = parserFactory.newDocumentBuilder();

        boolean dtdNotFound = false;
        Document doc = null;
        try {
            doc = builder.parse(is);
        } catch (FileNotFoundException e) {
            dtdNotFound = true;
        }

        // if dtd doesn't exist parse the document again without trying to load dtd
        if (dtdNotFound) {
            is = file.getContent().getInputStream();
            // disable dtd loading
            parserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            builder = parserFactory.newDocumentBuilder();
            doc = builder.parse(is);
        }

        DocumentType docType = doc.getDoctype();

        if (log.isDebugEnabled() && docType != null) {
            log.debug("docType.getPublicId()=" + docType.getPublicId());
            log.debug("docType.getSystemId()=" + docType.getSystemId());
        }

        return doc;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw e;
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
                /**/}
    }

}

From source file:org.kmallan.azureus.rssfeed.Scheduler.java

public synchronized void runFeed(final UrlBean urlBean) {
    String url = urlBean.getLocation();
    String title, link, description;

    ListGroup listBeans = urlBean.getGroup();

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setIgnoringComments(true);
    docFactory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder docBuild;/*from   ww w  .ja  v  a  2s  .  com*/
    Document feed;

    File xmlTmp = null;
    try {
        docBuild = docFactory.newDocumentBuilder();
        Downloader downloader = new Downloader();
        downloader.addListener(new DownloaderListener() {
            public boolean completed = false, error = false;

            public void downloaderUpdate(int state, int percent, int amount, String err) {
                if (completed || error)
                    return;
                String status = new String("Pending");
                switch (state) {
                case Downloader.DOWNLOADER_NON_INIT:
                    status = "Pending";
                    break;
                case Downloader.DOWNLOADER_INIT:
                    status = "Connecting";
                    break;
                case Downloader.DOWNLOADER_START:
                    status = "Download Starting";
                    break;
                case Downloader.DOWNLOADER_DOWNLOADING:
                    status = "Downloading";
                    break;
                case Downloader.DOWNLOADER_FINISHED:
                    status = "Download Finished";
                    completed = true;
                    break;
                case Downloader.DOWNLOADER_NOTMODIFIED:
                    status = "Not modified";
                    completed = true;
                    break;
                case Downloader.DOWNLOADER_ERROR:
                    status = "Error";
                    error = true;
                    break;
                }
                urlBean.setStatus(status);
                if (percent > 0)
                    urlBean.setPercent(percent);
                if (amount > 0)
                    urlBean.setAmount(amount);
                if (!err.equalsIgnoreCase(""))
                    urlBean.setError(err);

                if (view.isOpen() && view.display != null && !view.display.isDisposed())
                    view.display.asyncExec(new Runnable() {
                        public void run() {
                            if (view.listTable == null || view.listTable.isDisposed())
                                return;
                            ListTreeItem listGroup = view.treeViewManager.getItem(urlBean);
                            listGroup.setText(1, urlBean.getStatus() + " " + (!urlBean.getError()
                                    .equalsIgnoreCase("") && urlBean.getStatus() == "Error"
                                            ? "- " + urlBean.getError()
                                            : (urlBean.getStatus() == "Downloading" ? (urlBean.getPercent() > 0
                                                    ? Integer.toString(urlBean.getPercent()) + "%"
                                                    : (urlBean.getAmount() > 0 ? Double.toString(Math.floor(
                                                            new Integer(urlBean.getAmount()).doubleValue()
                                                                    / (double) 1024 * (double) 100)
                                                            / (double) 100) + "KB" : ""))
                                                    : "")));
                            if (!urlBean.getError().equalsIgnoreCase(""))
                                listGroup.setForeground(new Color(view.display, 255, 0, 0));
                            else
                                listGroup.resetForeground();
                        }
                    });
            }
        });
        downloader.init(url, "text/xml, text/html, text/plain, application/x-httpd-php", null,
                (urlBean.getUseCookie() ? urlBean.getCookie() : null), urlBean.getLastModifed(),
                urlBean.getLastEtag());

        if (downloader.getState() == Downloader.DOWNLOADER_ERROR)
            return;

        if (downloader.getState() == Downloader.DOWNLOADER_NOTMODIFIED) {
            // no change, add the old items again
            for (Iterator iter = listBeans.getPreviousItems().iterator(); iter.hasNext();) {
                addTableElement(urlBean, listBeans, (ListBean) iter.next());
            }
            addBacklogElements(urlBean);
            downloader.notModified();
            // use the last seen TTL value if available
            if (urlBean.getObeyTTL() && listBeans.getPreviousDelay() > 0)
                listBeans.setDelay(listBeans.getPreviousDelay());
            return;
        }

        Plugin.debugOut(
                urlBean.getName() + " Last-Modified: " + downloader.lastModified + " ETag: " + downloader.etag);

        urlBean.setLastModifed(downloader.lastModified);
        urlBean.setLastEtag(downloader.etag);

        xmlTmp = new File(Plugin.getPluginDirectoryName(), "tmp-" + urlBean.getID() + ".xml");
        xmlTmp.createNewFile();
        FileOutputStream fileout = new FileOutputStream(xmlTmp, false);

        byte[] buf = new byte[2048];
        int read = 0;
        do {
            if (downloader.getState() == Downloader.DOWNLOADER_CANCELED)
                break;
            read = downloader.read(buf);
            if (read > 0) {
                System.err.print(".");
                fileout.write(buf, 0, read);
            } else if (read == 0) {
                System.err.print("?");
                try {
                    long numMillisecondsToSleep = 100;
                    Thread.sleep(numMillisecondsToSleep);
                } catch (InterruptedException e) {
                }
            }
        } while (read >= 0);

        fileout.flush();
        fileout.close();

        docBuild.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId) {
                // System.out.println( publicId + ", " + systemId );

                // handle bad DTD external refs

                if (Plugin.getProxyOption() == Plugin.PROXY_TRY_PLUGIN) {

                    return new InputSource(
                            new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
                }

                try {
                    URL url = new URL(systemId);

                    String host = url.getHost();

                    InetAddress.getByName(host);

                    // try connecting too as connection-refused will also bork XML parsing

                    InputStream is = null;

                    try {
                        URLConnection con = url.openConnection();

                        con.setConnectTimeout(15 * 1000);
                        con.setReadTimeout(15 * 1000);

                        is = con.getInputStream();

                        byte[] buffer = new byte[32];

                        int pos = 0;

                        while (pos < buffer.length) {

                            int len = is.read(buffer, pos, buffer.length - pos);

                            if (len <= 0) {

                                break;
                            }

                            pos += len;
                        }

                        String str = new String(buffer, "UTF-8").trim().toLowerCase(Locale.US);

                        if (!str.contains("<?xml")) {

                            // not straightforward to check for naked DTDs, could be lots of <!-- commentry preamble which of course can occur
                            // in HTML too

                            buffer = new byte[32000];

                            pos = 0;

                            while (pos < buffer.length) {

                                int len = is.read(buffer, pos, buffer.length - pos);

                                if (len <= 0) {

                                    break;
                                }

                                pos += len;
                            }

                            str += new String(buffer, "UTF-8").trim().toLowerCase(Locale.US);

                            if (str.contains("<html") && str.contains("<head")) {

                                throw (new Exception("Bad DTD"));
                            }
                        }
                    } catch (Throwable e) {

                        return new InputSource(
                                new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));

                    } finally {

                        if (is != null) {

                            try {
                                is.close();

                            } catch (Throwable e) {

                            }
                        }
                    }
                    return (null);

                } catch (UnknownHostException e) {

                    return new InputSource(
                            new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));

                } catch (Throwable e) {

                    return (null);
                }
            }
        });

        try {
            feed = docBuild.parse(xmlTmp);

        } catch (Exception e) {

            feed = null;

            String msg = Debug.getNestedExceptionMessage(e);

            if ((msg.contains("entity") && msg.contains("was referenced"))
                    || msg.contains("entity reference")) {

                FileInputStream fis = new FileInputStream(xmlTmp);

                try {

                    feed = docBuild.parse(new EntityFudger(fis));

                } catch (Throwable f) {

                } finally {

                    fis.close();
                }
            }

            if (feed == null) {
                if (e instanceof ParserConfigurationException) {
                    throw ((ParserConfigurationException) e);
                } else if (e instanceof SAXException) {
                    throw ((SAXException) e);
                } else if (e instanceof IOException) {
                    throw ((IOException) e);
                } else {
                    throw (new IOException(msg));
                }
            }
        }

        xmlTmp.delete();
        downloader.done();

        if (downloader.getState() == Downloader.DOWNLOADER_ERROR)
            return;
    } catch (ParserConfigurationException e) {
        if (xmlTmp != null)
            xmlTmp.delete();
        urlBean.setError("Malformed RSS XML: " + e.getMessage());
        return;
    } catch (SAXException e) {
        if (xmlTmp != null)
            xmlTmp.delete();
        urlBean.setError("Malformed RSS XML: " + e.getMessage());
        return;
    } catch (IOException e) {
        if (xmlTmp != null)
            xmlTmp.delete();
        urlBean.setError("IO Exception: " + e.getMessage());
        return;
    }

    if (urlBean.getObeyTTL()) {
        NodeList feedTTL = feed.getElementsByTagName("ttl");
        if (feedTTL.getLength() == 1) {
            int newDelay = Integer.parseInt(getText(feedTTL.item(0))) * 60;
            if (newDelay > 0)
                urlBean.getGroup().setDelay(newDelay, true);
        }
    }

    // Parse the channel's "item"s
    NodeList feedItems = feed.getElementsByTagName("item");
    int feedItemLen = feedItems.getLength();
    for (int iLoop = 0; iLoop < feedItemLen; iLoop++) {
        Node item = feedItems.item(iLoop);
        NodeList params = item.getChildNodes();
        int paramsLen = params.getLength();

        title = link = description = "";

        for (int i = 0; i < paramsLen; i++) {
            Node param = params.item(i);
            if (param.getNodeType() == Node.ELEMENT_NODE) {
                if (param.getNodeName().equalsIgnoreCase("title")) {
                    title = getText(param);
                } else if (param.getNodeName().equalsIgnoreCase("enclosure") && param.hasAttributes()) {
                    if ((((param.getAttributes()).getNamedItem("type")).getNodeValue())
                            .equalsIgnoreCase("application/x-bittorrent")) {
                        link = ((param.getAttributes()).getNamedItem("url")).getNodeValue();
                    }
                } else if (param.getNodeName().equalsIgnoreCase("link") && link.length() == 0) {
                    link = getText(param);
                } else if (param.getNodeName().equalsIgnoreCase("description")) {
                    description = getText(param);
                    if (description != null && description.trim().startsWith("<")) {
                        // strip html tags and entity references from description
                        HtmlAnalyzer parser = new HtmlAnalyzer();
                        try {
                            new ParserDelegator().parse(new StringReader(description), parser, true);
                            description = parser.getPlainText();
                        } catch (IOException e) {
                        }
                    }
                    description += "\n";
                }
            }
        }

        if (link.length() == 0)
            continue;
        if (link.indexOf("://") < 0 && !link.toLowerCase().startsWith("magnet")) {
            try {
                link = HtmlAnalyzer.resolveRelativeURL(urlBean.getLocation(), link);
            } catch (MalformedURLException e) {
                Plugin.debugOut("Bad link URL: " + link + " -> " + e.getMessage());
                continue;
            }
        }

        int state = ListBean.NO_DOWNLOAD;

        String titleTest = title.toLowerCase();
        String linkTest = link.toLowerCase();

        FilterBean curFilter = null;
        for (int i = 0; i < view.rssfeedConfig.getFilterCount(); i++) {
            curFilter = view.rssfeedConfig.getFilter(i);
            if (curFilter == null)
                continue;
            if (curFilter.matches(urlBean.getID(), titleTest, linkTest)) {
                if (curFilter.getMode().equalsIgnoreCase("Pass")) {
                    state = ListBean.DOWNLOAD_INCL;
                } else {
                    state = ListBean.DOWNLOAD_EXCL;
                }
                break;
            }
        }
        Episode e = null;
        Movie m = null;
        final FilterBean filterBean = curFilter;
        if (filterBean != null) {
            if ("TVShow".equalsIgnoreCase(filterBean.getType())) {
                try {
                    e = FilterBean.getSeason(titleTest);
                } catch (Exception ee) {
                }
                try {
                    if (e == null) {
                        e = FilterBean.getSeason(linkTest);
                    }
                } catch (Exception ee) {
                }
            } else if ("Movie".equalsIgnoreCase(filterBean.getType())) {
                m = FilterBean.getMovie(titleTest);
                if (m == null) {
                    m = FilterBean.getMovie(linkTest);
                }
                Plugin.debugOut("Download is a movie: " + m);
            }
        }

        if (state == ListBean.DOWNLOAD_INCL) {
            Plugin.debugOut("testing for download: " + linkTest);
            if (filterBean.getUseSmartHistory()) {
                for (int i = 0; i < view.rssfeedConfig.getHistoryCount(); i++) {
                    HistoryBean histBean = view.rssfeedConfig.getHistory(i);
                    if (linkTest.equalsIgnoreCase(histBean.getLocation())) {
                        Plugin.debugOut("found location match: " + histBean);
                        state = ListBean.DOWNLOAD_HIST;
                        break;
                    }

                    if (e != null && histBean.getSeasonStart() >= 0 && filterBean.getUseSmartHistory()) {
                        final String showTitle = histBean.getTitle();

                        // Old history beans may not have set showTitle so keep using the old way of matching
                        if (showTitle == null ? (histBean.getFiltID() == filterBean.getID())
                                : showTitle.equalsIgnoreCase(e.showTitle)) {
                            // "Proper" episode is not skipped unless history is also proper
                            if (histBean.isProper() || !e.isProper()) {
                                int seasonStart = histBean.getSeasonStart();
                                int episodeStart = histBean.getEpisodeStart();
                                int seasonEnd = histBean.getSeasonEnd();
                                int episodeEnd = histBean.getEpisodeEnd();
                                Plugin.debugOut(e + " vs s" + seasonStart + "e" + episodeStart + " - s"
                                        + seasonEnd + "e" + episodeEnd);
                                if (e.inRange(seasonStart, episodeStart, seasonEnd, episodeEnd)) {
                                    Plugin.debugOut("found filter and episode match: " + e);
                                    state = ListBean.DOWNLOAD_HIST;
                                    break;
                                }
                            }
                        }
                    } else if (m != null && m.getTitle().equals(histBean.getTitle())
                            && m.getYear() == histBean.getYear()) {
                        if (histBean.isProper() || !m.isProper()) {
                            Plugin.debugOut("found movie match: " + m);
                            state = ListBean.DOWNLOAD_HIST;
                        }
                    }
                }
            } else
                Plugin.debugOut("Filter doesn't use smart history: " + filterBean);
        }

        final ListBean listBean = addTableElement(urlBean, listBeans, title, link, description, state);

        if (state == ListBean.DOWNLOAD_INCL) {
            // Add the feed
            final String curLink = link;
            boolean success = view.torrentDownloader.addTorrent(curLink, urlBean, filterBean, listBean);
            if (success && filterBean.getType().equalsIgnoreCase("Other") && filterBean.getDisableAfter())
                filterBean.setEnabled(false);

            if (view.isOpen() && view.display != null && !view.display.isDisposed())
                view.display.asyncExec(new Runnable() {
                    public void run() {
                        ListTreeItem listItem = view.treeViewManager.getItem(listBean);
                        if (listItem != null)
                            listItem.update();
                    }
                });
        }
    }
}

From source file:org.kuali.kfs.module.purap.service.impl.ElectronicInvoiceHelperServiceImpl.java

protected byte[] addNamespaceDefinition(ElectronicInvoiceLoad eInvoiceLoad, File invoiceFile) {

    boolean result = true;

    if (LOG.isInfoEnabled()) {
        LOG.info("Adding namespace definition");
    }/*from   w  w  w  .  jav a  2 s.com*/

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setValidating(false); // It's not needed to validate here
    builderFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = null;
    try {
        builder = builderFactory.newDocumentBuilder(); // Create the parser
    } catch (ParserConfigurationException e) {
        LOG.error("Error getting document builder - " + e.getMessage());
        throw new RuntimeException(e);
    }

    Document xmlDoc = null;

    try {
        xmlDoc = builder.parse(invoiceFile);
    } catch (Exception e) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Error parsing the file - " + e.getMessage());
        }
        rejectElectronicInvoiceFile(eInvoiceLoad, UNKNOWN_DUNS_IDENTIFIER, invoiceFile, e.getMessage(),
                PurapConstants.ElectronicInvoice.FILE_FORMAT_INVALID);
        return null;
    }

    Node node = xmlDoc.getDocumentElement();
    Element element = (Element) node;

    String xmlnsValue = element.getAttribute("xmlns");
    String xmlnsXsiValue = element.getAttribute("xmlns:xsi");

    File namespaceAddedFile = getInvoiceFile(invoiceFile.getName());

    if (StringUtils.equals(xmlnsValue, "http://www.kuali.org/kfs/purap/electronicInvoice")
            && StringUtils.equals(xmlnsXsiValue, "http://www.w3.org/2001/XMLSchema-instance")) {
        if (LOG.isInfoEnabled()) {
            LOG.info("xmlns and xmlns:xsi attributes already exists in the invoice xml");
        }
    } else {
        element.setAttribute("xmlns", "http://www.kuali.org/kfs/purap/electronicInvoice");
        element.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    }

    OutputFormat outputFormat = new OutputFormat(xmlDoc);
    outputFormat.setOmitDocumentType(true);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLSerializer serializer = new XMLSerializer(out, outputFormat);
    try {
        serializer.asDOMSerializer();
        serializer.serialize(xmlDoc.getDocumentElement());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    if (LOG.isInfoEnabled()) {
        LOG.info("Namespace validation completed");
    }

    return out.toByteArray();

}

From source file:org.kuali.ole.module.purap.service.impl.ElectronicInvoiceHelperServiceImpl.java

protected byte[] addNamespaceDefinition(ElectronicInvoiceLoad eInvoiceLoad, File invoiceFile) {

    boolean result = true;

    if (LOG.isInfoEnabled()) {
        LOG.info("Adding namespace definition");
    }//from   ww w .j a va2  s.co  m

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setValidating(false); // It's not needed to validate here
    builderFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = null;
    try {
        builder = builderFactory.newDocumentBuilder(); // Create the parser
    } catch (ParserConfigurationException e) {
        LOG.error("Error getting document builder - " + e.getMessage());
        throw new RuntimeException(e);
    }

    Document xmlDoc = null;

    try {
        xmlDoc = builder.parse(invoiceFile);
    } catch (Exception e) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Error parsing the file - " + e.getMessage());
        }
        rejectElectronicInvoiceFile(eInvoiceLoad, UNKNOWN_DUNS_IDENTIFIER, invoiceFile, e.getMessage(),
                PurapConstants.ElectronicInvoice.FILE_FORMAT_INVALID);
        return null;
    }

    Node node = xmlDoc.getDocumentElement();
    Element element = (Element) node;

    String xmlnsValue = element.getAttribute("xmlns");
    String xmlnsXsiValue = element.getAttribute("xmlns:xsi");

    File namespaceAddedFile = getInvoiceFile(invoiceFile.getName());

    if (StringUtils.equals(xmlnsValue, "http://www.kuali.org/ole/purap/electronicInvoice")
            && StringUtils.equals(xmlnsXsiValue, "http://www.w3.org/2001/XMLSchema-instance")) {
        if (LOG.isInfoEnabled()) {
            LOG.info("xmlns and xmlns:xsi attributes already exists in the invoice xml");
        }
    } else {
        element.setAttribute("xmlns", "http://www.kuali.org/ole/purap/electronicInvoice");
        element.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    }

    OutputFormat outputFormat = new OutputFormat(xmlDoc);
    outputFormat.setOmitDocumentType(true);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLSerializer serializer = new XMLSerializer(out, outputFormat);
    try {
        serializer.asDOMSerializer();
        serializer.serialize(xmlDoc.getDocumentElement());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    if (LOG.isInfoEnabled()) {
        LOG.info("Namespace validation completed");
    }

    return out.toByteArray();

}

From source file:org.kuali.rice.core.api.util.xml.XmlHelper.java

public static org.w3c.dom.Document trimXml(InputStream input)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document oldDocument = builder.parse(input);
    org.w3c.dom.Element naviElement = oldDocument.getDocumentElement();
    trimElement(naviElement);//from w  ww. j a v a 2s  .  c om
    return oldDocument;
}

From source file:org.mule.modules.sugarcrm.automation.unit.TransformerXmlToCxfTest.java

@Test
public void validTransformationXmlFromSugar() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from   w ww  .j  a va2s.c om*/
    dbf.setCoalescing(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    DocumentBuilder db = dbf.newDocumentBuilder();

    String xml = IOUtils.getResourceAsString("response-searchByModule.xml", getClass());
    String xmlTransform = new XmlToCxfTransformer().transform(xml);

    Document doc1 = db.parse(org.apache.commons.io.IOUtils.toInputStream(xmlTransform));
    doc1.normalizeDocument();

    Document doc2 = db.parse(IOUtils.getResourceAsStream("response-searchByModule-ok.xml", getClass()));
    doc2.normalizeDocument();

    Assert.assertTrue(doc1.isEqualNode(doc2));
}

From source file:org.openfact.common.converts.DocumentUtils.java

/**
 * Convert an inputStream to a Document Object
 *
 * @param inputStream//from  w w w . j a  v a2 s  .  co m
 *            The inputstream to convert
 * @return a Document Object
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
public static Document getInputStreamToDocument(InputStream inputStream)
        throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setAttribute("http://xml.org/sax/features/namespaces",
    // Boolean.TRUE);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Reader reader = new InputStreamReader(inputStream, "ISO8859_1");
    InputSource is = new InputSource(reader);
    is.setEncoding("ISO-8859-1");
    Document doc = db.parse(is);
    return doc;
}

From source file:org.rimudb.configuration.AbstractXmlLoader.java

private Document loadXML(InputStream is) throws Exception {
    // Load document
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*ww  w.  j  av a 2 s  .co  m*/

    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    factory.setValidating(false); // Don't use DTD validation

    DocumentBuilder docBuilder = factory.newDocumentBuilder();

    ErrorHandler eh = new StrictErrorHandler();
    docBuilder.setErrorHandler(eh);

    InputSource inputSource = new InputSource(is);
    inputSource.setPublicId(RimuDBNamespace.URI);
    inputSource.setSystemId(RimuDBNamespace.URI);

    Document document = docBuilder.parse(is);
    is.close();

    // Determine the XML schema version from the XML document without validating
    Element root = document.getDocumentElement();
    setDocumentSchema(lookupSchemaVersion(root));

    // Validate the XML document and determine the XML Schema version
    if (isValidateXML()) {
        if (getDocumentSchema() != null) {
            // Validate the document against the schema found in the document 
            SAXParseException saxParseException = validate(document, getDocumentSchema());
            if (saxParseException != null) {
                throw saxParseException;
            }
        } else {
            setDocumentSchema(lookupSchemaByValidation(document));
        }
    }

    return document;
}

From source file:org.silverpeas.util.xml.transform.XPathTransformer.java

public void transform(XmlConfiguration configuration) {
    InputStream in = null;/*  w  ww.ja v a 2 s.  com*/
    Document doc = null;
    String xmlFile = configuration.getFileName();
    try {
        console.printMessage(xmlFile);
        in = new BufferedInputStream(new FileInputStream(xmlFile));
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setValidating(false);
        docFactory.setIgnoringElementContentWhitespace(false);
        docFactory.setIgnoringComments(false);
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        docBuilder.setEntityResolver(new ClasspathEntityResolver(null));
        doc = docBuilder.parse(in);
        applyTransformation(configuration, doc);
    } catch (SAXException ex) {
        Logger.getLogger(XPathTransformer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParserConfigurationException ex) {
        Logger.getLogger(XPathTransformer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ioex) {
        Logger.getLogger(XPathTransformer.class.getName()).log(Level.SEVERE, null, ioex);
    } finally {
        IOUtils.closeQuietly(in);
    }
    if (doc != null) {
        saveDoc(xmlFile, doc);
    }
}

From source file:org.soa4all.dashboard.gwt.module.wsmolite.server.WsmoLiteDataServiceImpl.java

private Document parseXML(String rawData) throws Exception {
    Document doc = null;/*from w  ww . j  a v a  2s .c  o  m*/
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setIgnoringElementContentWhitespace(true);

        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(rawData)));
    } catch (Exception exc) {
        throw new Exception(exc.getClass().getSimpleName() + " : " + exc.getMessage());
    }
    return doc;
}