Example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringComments

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

Introduction

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

Prototype


public void setIgnoringComments(boolean ignoreComments) 

Source Link

Document

Specifies that the parser produced by this code will ignore comments.

Usage

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   w  w w  .  j a  v  a  2s. c om
    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.mule.modules.sugarcrm.automation.unit.TransformerXmlToCxfTest.java

@Test
public void validTransformationXmlFromSugar() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from  w  w w  . j  av a2 s  .co m*/
    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.opendatakit.aggregate.externalservice.GoogleMapsEngine.java

public static String parseGmeAssetId(IForm form, CallingContext cc) throws ODKDatastoreException,
        UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException {
    String gmeAssetId = null;/*w w  w. ja va2s.c  om*/

    String formXml = form.getFormXml(cc);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setIgnoringComments(true);
    factory.setCoalescing(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new ByteArrayInputStream(formXml.getBytes(HtmlConsts.UTF8_ENCODE)));

    Element rootXml = doc.getDocumentElement();
    NodeList instance = rootXml.getElementsByTagName("instance");

    for (int i = 0; i < instance.getLength(); ++i) {
        Node possibleInstanceNode = instance.item(i);
        NodeList possibleFormRootValues = possibleInstanceNode.getChildNodes();
        for (int j = 0; j < possibleFormRootValues.getLength(); ++j) {
            Node node = possibleFormRootValues.item(j);
            if (node instanceof Element) {
                Element possibleFormRoot = (Element) node;
                // extract form id
                String formId = possibleFormRoot.getAttribute("id");
                // if odk id is not present use namespace
                if (formId.equalsIgnoreCase(BasicConsts.EMPTY_STRING)) {
                    String schema = possibleFormRoot.getAttribute("xmlns");
                    if (!formId.equalsIgnoreCase(BasicConsts.EMPTY_STRING)) {
                        formId = schema;
                    }

                }

                // if form id is present correct node, therefore extract gme asset id
                if (form.getFormId().equals(formId)) {
                    gmeAssetId = possibleFormRoot.getAttribute(GME_ASSET_ID);
                }
            }
        }

    }
    return gmeAssetId;
}

From source file:org.opendatakit.aggregate.parser.SubmissionParser.java

/**
 * Helper Constructor an ODK submission by processing XML submission to
 * extract values/*from  w ww  . ja  va  2  s .c  om*/
 * 
 * @param inputStreamXML
 *          xml submission input stream
 * @param isIncomplete
 * 
 * @throws IOException
 * @throws ODKFormNotFoundException
 *           thrown if a form is not found with a matching ODK ID
 * @throws ODKParseException
 * @throws ODKIncompleteSubmissionData
 * @throws ODKConversionException
 * @throws ODKDatastoreException
 * @throws ODKFormSubmissionsDisabledException
 */
private void constructorHelper(InputStream inputStreamXML, boolean isIncomplete, CallingContext cc)
        throws IOException, ODKFormNotFoundException, ODKParseException, ODKIncompleteSubmissionData,
        ODKConversionException, ODKDatastoreException, ODKFormSubmissionsDisabledException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setIgnoringComments(true);
        factory.setCoalescing(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(inputStreamXML);
        root = doc.getDocumentElement();
        // if debugging: printNode(root);

        // check for odk id
        formId = root.getAttribute(ParserConsts.FORM_ID_ATTRIBUTE_NAME);

        // if odk id is not present use namespace
        if (formId.equalsIgnoreCase(BasicConsts.EMPTY_STRING)) {
            String schema = root.getAttribute(ParserConsts.NAMESPACE_ATTRIBUTE);

            // TODO: move this into FormDefinition?
            if (schema == null) {
                throw new ODKIncompleteSubmissionData(Reason.ID_MISSING);
            }

            formId = schema;
        }

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

    // need to escape all slashes... for xpath processing...
    formId = formId.replaceAll(ParserConsts.FORWARD_SLASH, ParserConsts.FORWARD_SLASH_SUBSTITUTION);

    String fullyQualifiedId = FormFactory.extractWellFormedFormId(formId);

    form = FormFactory.retrieveFormByFormId(fullyQualifiedId, cc);
    if (!form.getSubmissionEnabled()) {
        throw new ODKFormSubmissionsDisabledException();
    }

    String modelVersionString = root.getAttribute(ParserConsts.MODEL_VERSION_ATTRIBUTE_NAME);
    String uiVersionString = root.getAttribute(ParserConsts.UI_VERSION_ATTRIBUTE_NAME);
    Long modelVersion = null;
    Long uiVersion = null;
    if (modelVersionString != null && modelVersionString.length() > 0) {
        modelVersion = Long.valueOf(modelVersionString);
    }
    if (uiVersionString != null && uiVersionString.length() > 0) {
        uiVersion = Long.valueOf(uiVersionString);
    }

    String instanceId = getOpenRosaInstanceId();
    if (instanceId == null) {
        instanceId = root.getAttribute(ParserConsts.INSTANCE_ID_ATTRIBUTE_NAME);
        if (instanceId == null || instanceId.length() == 0) {
            instanceId = CommonFieldsBase.newUri();
        }
    }

    Date submissionDate = new Date();
    String submissionDateString = root.getAttribute(ParserConsts.SUBMISSION_DATE_ATTRIBUTE_NAME);
    if (submissionDateString != null && submissionDateString.length() != 0) {
        submissionDate = WebUtils.parseDate(submissionDateString);
    }

    Date markedAsCompleteDate = new Date();
    String markedAsCompleteDateString = root.getAttribute(ParserConsts.MARKED_AS_COMPLETE_DATE_ATTRIBUTE_NAME);
    if (markedAsCompleteDateString != null && markedAsCompleteDateString.length() != 0) {
        markedAsCompleteDate = WebUtils.parseDate(markedAsCompleteDateString);
    }

    // retrieve the record with this instanceId from the database or
    // create a new one. This supports submissions having more than
    // 10MB of attachments. In that case, ODK Collect will post the
    // submission in multiple parts and Aggregate needs to be able to
    // merge the parts together. This SHOULD NOT be used to 'update'
    // an existing submission, only to attach additional binary content
    // to an already-uploaded submission.
    boolean preExisting = false;
    try {
        Datastore ds = cc.getDatastore();
        User user = cc.getCurrentUser();
        TopLevelInstanceData fi = (TopLevelInstanceData) ds.getEntity(
                form.getTopLevelGroupElement().getFormDataModel().getBackingObjectPrototype(), instanceId,
                user);
        try {
            submission = new Submission(fi, form, cc);
        } catch (ODKDatastoreException e) {
            Log logger = LogFactory.getLog(Submission.class);
            e.printStackTrace();
            logger.error("Unable to reconstruct submission for " + fi.getSchemaName() + "." + fi.getTableName()
                    + " uri " + fi.getUri());
            if ((e instanceof ODKEntityNotFoundException) || (e instanceof ODKEnumeratedElementException)) {
                // this is a malformed submission...
                // try to clean this up...
                DeleteHelper.deleteDamagedSubmission(fi, form.getAllBackingObjects(), cc);
            }
            throw e;
        }
        preExisting = true;
        preExistingComplete = submission.isComplete();
    } catch (ODKEntityNotFoundException e) {
        submission = new Submission(modelVersion, uiVersion, instanceId, form, submissionDate, cc);
    }

    topLevelTableKey = submission.getKey();

    Map<String, Integer> repeatGroupIndices = new HashMap<String, Integer>();
    FormElementModel formRoot = form.getTopLevelGroupElement();
    // if the submission is pre-existing in the datastore, ONLY update binaries
    boolean uploadAllBinaries = processSubmissionElement(formRoot, root, submission, repeatGroupIndices,
            preExisting, cc);
    submission.setIsComplete(uploadAllBinaries);
    if (uploadAllBinaries) {
        submission.setMarkedAsCompleteDate(markedAsCompleteDate);
    }
    // save the elements inserted into the top-level submission
    try {
        submission.persist(cc);
    } catch (Exception e) {
        List<EntityKey> keys = new ArrayList<EntityKey>();
        submission.recursivelyAddEntityKeysForDeletion(keys, cc);
        keys.add(submission.getKey());
        try {
            DeleteHelper.deleteEntities(keys, cc);
        } catch (Exception ex) {
            // ignore... we are rolling back...
        }
        throw new ODKDatastoreException("Unable to persist data", e);
    }
}

From source file:org.opennms.protocols.xml.collector.AbstractXmlCollectionHandler.java

/**
 * Gets the XML document.//from   ww w .j  a va  2s.c o  m
 *
 * @param is the input stream
 * @param request the request
 * @return the XML document
 * @throws Exception the exception
 */
protected Document getXmlDocument(InputStream is, Request request) throws Exception {
    is = preProcessHtml(request, is);
    is = applyXsltTransformation(request, is);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    String contents = writer.toString();
    Document doc = builder.parse(IOUtils.toInputStream(contents, "UTF-8"));
    // Ugly hack to deal with DOM & XPath 1.0's battle royale 
    // over handling namespaces without a prefix. 
    if (doc.getNamespaceURI() != null && doc.getPrefix() == null) {
        factory.setNamespaceAware(false);
        builder = factory.newDocumentBuilder();
        doc = builder.parse(IOUtils.toInputStream(contents, "UTF-8"));
    }
    return doc;
}

From source file:org.openossad.util.core.xml.XMLHandler.java

/**
 * Load a file into an XML document/*w ww. ja  v  a 2s .co  m*/
 * @param filename The filename to load into a document
 * @param systemId Provide a base for resolving relative URIs.
 * @param ignoreEntities Ignores external entities and returns an empty dummy.
 * @param namespaceAware support XML namespaces.
 * @return the Document if all went well, null if an error occured!
 */
public static final Document loadXMLFile(FileObject fileObject, String systemID, boolean ignoreEntities,
        boolean namespaceAware) throws OpenDESIGNERXMLException {
    DocumentBuilderFactory dbf;
    DocumentBuilder db;
    Document doc;

    try {
        // Check and open XML document
        //
        dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringComments(true);
        dbf.setNamespaceAware(namespaceAware);
        db = dbf.newDocumentBuilder();

        // even dbf.setValidating(false) will the parser NOT prevent from checking the existance of the DTD
        // thus we need to give the BaseURI (systemID) below to have a chance to get it
        // or return empty dummy documents for all external entities (sources)
        //
        if (ignoreEntities) {
            db.setEntityResolver(new DTDIgnoringEntityResolver());
        }

        InputStream inputStream = null;
        try {
            if (Const.isEmpty(systemID)) {
                // Normal parsing
                //
                inputStream = OpenDESIGNERVFS.getInputStream(fileObject);
                doc = db.parse(inputStream);
            } else {
                // Do extra verifications
                //
                String systemIDwithEndingSlash = systemID.trim();

                //make sure we have an ending slash, otherwise the last part will be ignored
                //
                if (!systemIDwithEndingSlash.endsWith("/") && !systemIDwithEndingSlash.endsWith("\\")) {
                    systemIDwithEndingSlash = systemIDwithEndingSlash.concat("/");
                }
                inputStream = OpenDESIGNERVFS.getInputStream(fileObject);
                doc = db.parse(inputStream, systemIDwithEndingSlash);
            }
        } catch (FileNotFoundException ef) {
            throw new OpenDESIGNERXMLException(ef);
        } finally {
            if (inputStream != null)
                inputStream.close();
        }

        return doc;
    } catch (Exception e) {
        throw new OpenDESIGNERXMLException("Error reading information from file", e);
    }
}

From source file:org.pentaho.di.core.xml.XMLHandler.java

/**
 * Load a file into an XML document/*from  w  ww. j  a  v a 2  s  . com*/
 *
 * @param inputStream
 *          The stream to load a document from
 * @param systemId
 *          Provide a base for resolving relative URIs.
 * @param ignoreEntities
 *          Ignores external entities and returns an empty dummy.
 * @param namespaceAware
 *          support XML namespaces.
 * @return the Document if all went well, null if an error occured!
 */
public static final Document loadXMLFile(InputStream inputStream, String systemID, boolean ignoreEntities,
        boolean namespaceAware) throws KettleXMLException {
    DocumentBuilderFactory dbf;
    DocumentBuilder db;
    Document doc;

    try {
        // Check and open XML document
        //
        dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringComments(true);
        dbf.setNamespaceAware(namespaceAware);
        db = dbf.newDocumentBuilder();

        // even dbf.setValidating(false) will the parser NOT prevent from checking the existance of the DTD
        // thus we need to give the BaseURI (systemID) below to have a chance to get it
        // or return empty dummy documents for all external entities (sources)
        //
        if (ignoreEntities) {
            db.setEntityResolver(new DTDIgnoringEntityResolver());
        }

        try {
            if (Const.isEmpty(systemID)) {
                // Normal parsing
                //
                doc = db.parse(inputStream);
            } else {
                // Do extra verifications
                //
                String systemIDwithEndingSlash = systemID.trim();

                // make sure we have an ending slash, otherwise the last part will be ignored
                //
                if (!systemIDwithEndingSlash.endsWith("/") && !systemIDwithEndingSlash.endsWith("\\")) {
                    systemIDwithEndingSlash = systemIDwithEndingSlash.concat("/");
                }
                doc = db.parse(inputStream, systemIDwithEndingSlash);
            }
        } catch (FileNotFoundException ef) {
            throw new KettleXMLException(ef);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }

        return doc;
    } catch (Exception e) {
        throw new KettleXMLException("Error reading information from input stream", e);
    }
}

From source file:org.pepstock.jem.jbpm.JBpmFactory.java

/**
 * Validates the JCL content, calling the JBPM API 
 * @param jcl default JCL to store into job
 * @param content JCL content//  w  ww  .j a  va 2  s  .  c o  m
 * @throws JBpmException if any error occurs
 */
private void validate(Jcl jcl, String content) throws JBpmException {
    // read the BPMN Metadata, loading in properties object
    Properties p = new Properties();

    // creates all readers
    StringReader reader = new StringReader(content);
    InputSource source = new InputSource(reader);
    SimpleRuntimeEnvironment jbpmEnvironment = null;
    String jobName = null;

    // SCANS JCL to get JOBNAME
    // JOBNAME = attribute ID of PROCESS element
    try {
        // creates DOM objects
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringComments(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(source);

        // scan XML to read comment
        NodeList nl = doc.getDocumentElement().getChildNodes();
        // scans all nodes
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node.getNodeType() == Element.ELEMENT_NODE) {
                Element el = (Element) node;

                String tagName = null;
                // checks if name space
                if (el.getTagName().contains(":")) {
                    tagName = StringUtils.substringAfter(el.getTagName(), ":");
                } else {
                    tagName = el.getTagName();
                }
                // checks only if is a PROCESS element
                if (tagName.equalsIgnoreCase(PROCESS_XML_ELEMENT)) {
                    // gets ID attribute to set JOBNAME
                    String jobNameAttr = el.getAttribute(ID_XML_ATTRIBUTE);
                    if (jobNameAttr != null) {
                        // puts on properties
                        p.setProperty(JBpmKeys.JBPM_JOB_NAME, jobNameAttr);
                    } else {
                        throw new JBpmException(JBpmMessage.JEMM031E);
                    }
                }
            }
        }

        // checks if project has attribute name, if yes, uses it as JOB NAME
        // otherwise get the JEM property to define it
        jobName = p.getProperty(JBpmKeys.JBPM_JOB_NAME);

        // Anyway job name can't be null. if yes, exception occurs
        if (jobName == null) {
            throw new JBpmException(JBpmMessage.JEMM031E);
        }
        // loads JCL jobname
        jcl.setJobName(jobName);

        // creates JBPM environment to check BPMN2 syntax
        SimpleRegisterableItemsFactory factory = new SimpleRegisterableItemsFactory();
        jbpmEnvironment = new SimpleRuntimeEnvironment(factory);
        jbpmEnvironment.setUsePersistence(false);
        Resource res = ResourceFactory.newReaderResource(new StringReader(content),
                CharSet.DEFAULT_CHARSET_NAME);
        jbpmEnvironment.addAsset(res, ResourceType.BPMN2);
        // gets process to read metadata
        org.kie.api.definition.process.Process process = jbpmEnvironment.getKieBase().getProcess(jobName);
        // if process is null, exception
        if (process == null) {
            throw new JBpmException(JBpmMessage.JEMM063E);
        }
        // loads all METADATA
        p.putAll(process.getMetaData());

        // check again because you could put the job name on metadata
        jcl.setJobName(p.getProperty(JBpmKeys.JBPM_JOB_NAME));

        // loads the JBPM process ID in a map to reuse when a JBPM task will be scheduled
        Map<String, Object> jclMap = new HashMap<String, Object>();
        jclMap.put(JBpmKeys.JBPM_JOB_NAME, jobName);
        jcl.setProperties(jclMap);

    } catch (DOMException e) {
        throw new JBpmException(JBpmMessage.JEMM047E, e, e.getMessage());
    } catch (ParserConfigurationException e) {
        throw new JBpmException(JBpmMessage.JEMM047E, e, e.getMessage());
    } catch (SAXException e) {
        throw new JBpmException(JBpmMessage.JEMM047E, e, e.getMessage());
    } catch (Exception e) {
        throw new JBpmException(JBpmMessage.JEMM047E, e, e.getMessage());
    } finally {
        // clean up of JBPM environment
        if (jbpmEnvironment != null && jobName != null) {
            try {
                jbpmEnvironment.getKieBase().removeProcess(jobName);
            } catch (Exception e) {
                LogAppl.getInstance().debug(e.getMessage(), e);
            }
        }
    }

    // if I'm here, JCL is correct

    // extracts the locking scope and checks if the value is correct
    // the value is not save in JCL because is not helpful to node but
    // at runtime so it will be read again from ANT listener
    String lockingScopeProperty = p.getProperty(JBpmKeys.JBPM_LOCKING_SCOPE);
    if (lockingScopeProperty != null && !lockingScopeProperty.equalsIgnoreCase(JBpmKeys.JBPM_JOB_SCOPE)
            && !lockingScopeProperty.equalsIgnoreCase(JBpmKeys.JBPM_STEP_SCOPE)
            && !lockingScopeProperty.equalsIgnoreCase(JBpmKeys.JBPM_TASK_SCOPE)) {
        throw new JBpmException(JBpmMessage.JEMM032E, JBpmKeys.JBPM_LOCKING_SCOPE, lockingScopeProperty);
    }

    // Extracts from ANT enviroment property
    String environment = p.getProperty(JBpmKeys.JBPM_ENVIRONMENT);
    // if null, uses current environment assigned to JEM NODE
    if (environment == null) {
        environment = Main.EXECUTION_ENVIRONMENT.getEnvironment();
    }

    // Extracts from ANT domain property
    String domain = p.getProperty(JBpmKeys.JBPM_DOMAIN);
    // if null, uses domain default
    if (domain == null) {
        domain = Jcl.DEFAULT_DOMAIN;
    }

    // Extracts from ANT email addresses notification property
    String emailAddresses = p.getProperty(JBpmKeys.JBPM_EMAILS_NOTIFICATION);
    if (null != emailAddresses) {
        jcl.setEmailNotificationAddresses(emailAddresses);
    }
    // Extracts from ANT affinity property
    String affinity = p.getProperty(JBpmKeys.JBPM_AFFINITY);
    // if null, uses affinity default
    if (affinity == null) {
        affinity = Jcl.DEFAULT_AFFINITY;
    }

    // Extracts from ANT user property
    String user = p.getProperty(JBpmKeys.JBPM_USER);
    if (null != user) {
        jcl.setUser(user);
    }

    // Extracts from ANT classpath property
    String classPath = p.getProperty(JBpmKeys.JBPM_CLASSPATH);
    // if classpath is not set, changes if some variables are in
    if (classPath != null) {
        jcl.setClassPath(super.resolvePathNames(classPath, ConfigKeys.JEM_CLASSPATH_PATH_NAME));
    }

    // Extracts from ANT prior classpath property
    String priorClassPath = p.getProperty(JBpmKeys.JBPM_PRIOR_CLASSPATH);
    // if classpath is not set, changes if some variables are in
    if (priorClassPath != null) {
        jcl.setPriorClassPath(super.resolvePathNames(priorClassPath, ConfigKeys.JEM_CLASSPATH_PATH_NAME));
    }

    // Extracts from ANT memory property. If missing, default is 256 
    int memory = Parser.parseInt(p.getProperty(JBpmKeys.JBPM_MEMORY), Jcl.DEFAULT_MEMORY);
    // Extracts from ANT hold property. If missing, default is FALSE
    boolean hold = Parser.parseBoolean(p.getProperty(JBpmKeys.JBPM_HOLD), false);
    // Extracts from ANT priority property. If missing, default is 10
    int priority = Parser.parseInt(p.getProperty(JBpmKeys.JBPM_PRIORITY), Jcl.DEFAULT_PRIORITY);

    // saves all info inside of JCL object for further computing

    jcl.setEnvironment(environment);
    jcl.setDomain(domain);
    jcl.setAffinity(affinity);
    jcl.setHold(hold);
    jcl.setPriority(priority);
    jcl.setMemory(memory);

}

From source file:org.pepstock.jem.jbpm.XmlParser.java

/**
 * Loads all task defined as workitem based on JEM, to load all info about data description, data sources and locks.
 * @param jclFile BPMN file to parse//from  w w  w  .ja  v a2  s . com
 * @return list of all tasks of JCL
 * @throws ParserConfigurationException if any exception occurs parsing XML
 * @throws SAXException if any exception occurs parsing XML
 * @throws IOException if any exception occurs reading file
 */
public static final List<TaskDescription> getTaskDescription(String jclFile)
        throws ParserConfigurationException, SAXException, IOException {
    // creates an input soure
    InputSource source = new InputSource(new FileInputStream(jclFile));

    // DOM document and parsing
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringComments(false);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(source);

    // scan XML to read comment
    NodeList firstLevel = doc.getDocumentElement().getChildNodes();
    for (int i = 0; i < firstLevel.getLength(); i++) {
        Node node = firstLevel.item(i);
        // gets tag name 
        String tagName = getElementName(node);
        // if is a process, starts scanning nodes
        if (tagName != null && PROCESS_ELEMENT.equalsIgnoreCase(tagName)) {
            Element element = (Element) node;
            if (element.hasChildNodes()) {
                // scans children only if the element has got children
                return getTasks(element.getChildNodes());
            }
        }
    }
    return Collections.emptyList();
}

From source file:org.rhq.core.clientapi.agent.metadata.i18n.PropertiesGenerator.java

public void generateI18NProperties() {
    try {//from  www  .j  av  a  2s  . com
        if (update) {
            // First load into properties we can check for existence
            previousProperties = new Properties();
            FileInputStream is = new FileInputStream(propertiesFile);
            try {
                previousProperties.load(is);
            } finally {
                is.close();
            }
            this.contentWriter.println("\n\n# Contents added " + new Date() + "\n\n");
        }

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);

        FileOutputStream fos = new FileOutputStream(propertiesFile, this.update);
        this.contentWriter = new PrintWriter(fos);
        generateNode(doc.getDocumentElement(), "");
    } catch (Exception e) {
        log.error("Failed to generate i18n properties file.", e);
    } finally {
        if (this.contentWriter != null) {
            this.contentWriter.close();
        }
    }
}