Search in sources :

Example 11 with Element

use of org.hl7.elm.r1.Element in project wpcleaner by WPCleaner.

the class ApiXmlPagesWithPropResult method executePagesWithProp.

/**
 * Execute pages with property request.
 *
 * @param properties Properties defining request.
 * @param list List to be filled with protected titles.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executePagesWithProp(Map<String, String> properties, List<Page> list) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve embedding pages
        XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/query/pageswithprop/page", Filters.element());
        List<Element> results = xpa.evaluate(root);
        Iterator<Element> iter = results.iterator();
        while (iter.hasNext()) {
            Element currentNode = iter.next();
            Integer pageId = null;
            try {
                String tmp = currentNode.getAttributeValue("pageid");
                if (tmp != null) {
                    pageId = Integer.valueOf(tmp);
                }
            } catch (NumberFormatException e) {
            // 
            }
            Page page = DataManager.getPage(getWiki(), currentNode.getAttributeValue("title"), pageId, null, null);
            page.setNamespace(currentNode.getAttributeValue("ns"));
            list.add(page);
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/pageswithprop", properties);
    } catch (JDOMException e) {
        log.error("Error loading protected titles list", e);
        throw new APIException("Error parsing XML", e);
    }
}
Also used : APIException(org.wikipediacleaner.api.APIException) Element(org.jdom2.Element) Page(org.wikipediacleaner.api.data.Page) JDOMException(org.jdom2.JDOMException)

Example 12 with Element

use of org.hl7.elm.r1.Element in project wpcleaner by WPCleaner.

the class ApiXmlProtectedTitlesResult method executeProtectedTitles.

/**
 * Execute protected titles request.
 *
 * @param properties Properties defining request.
 * @param list List to be filled with protected titles.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeProtectedTitles(Map<String, String> properties, List<Page> list) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve embedding pages
        XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/query/protectedtitles/pt", Filters.element());
        List<Element> results = xpa.evaluate(root);
        Iterator<Element> iter = results.iterator();
        while (iter.hasNext()) {
            Element currentNode = iter.next();
            if ("infinity".equals(currentNode.getAttributeValue("expiry"))) {
                Page page = DataManager.getPage(getWiki(), currentNode.getAttributeValue("title"), null, null, null);
                page.setNamespace(currentNode.getAttributeValue("ns"));
                list.add(page);
            }
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/protectedtitles", properties);
    } catch (JDOMException e) {
        log.error("Error loading protected titles list", e);
        throw new APIException("Error parsing XML", e);
    }
}
Also used : APIException(org.wikipediacleaner.api.APIException) Element(org.jdom2.Element) Page(org.wikipediacleaner.api.data.Page) JDOMException(org.jdom2.JDOMException)

Example 13 with Element

use of org.hl7.elm.r1.Element in project wpcleaner by WPCleaner.

the class ApiXmlQueryPageResult method executeQueryPage.

/**
 * Execute query page request.
 *
 * @param properties Properties defining request.
 * @param list List to be filled with query pages.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeQueryPage(Map<String, String> properties, List<Page> list) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve query pages
        XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/query/querypage/results/page", Filters.element());
        List<Element> results = xpa.evaluate(root);
        Iterator<Element> iter = results.iterator();
        while (iter.hasNext()) {
            Element currentNode = iter.next();
            Page page = DataManager.getPage(getWiki(), currentNode.getAttributeValue("title"), null, null, null);
            page.setNamespace(currentNode.getAttributeValue("ns"));
            list.add(page);
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/querypage", properties);
    } catch (JDOMException e) {
        log.error("Error loading query page list", e);
        throw new APIException("Error parsing XML", e);
    }
}
Also used : APIException(org.wikipediacleaner.api.APIException) Element(org.jdom2.Element) Page(org.wikipediacleaner.api.data.Page) JDOMException(org.jdom2.JDOMException)

Example 14 with Element

use of org.hl7.elm.r1.Element in project wpcleaner by WPCleaner.

the class MediaWikiAPI method checkForError.

/**
 * Check for errors reported by the API.
 *
 * @param root Document root.
 * @throws APIException Exception thrown by the API.
 */
private void checkForError(Element root) throws APIException {
    if (root == null) {
        return;
    }
    // Check for errors
    XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/error", Filters.element());
    List<Element> listErrors = xpa.evaluate(root);
    if (listErrors != null) {
        Iterator<Element> iterErrors = listErrors.iterator();
        while (iterErrors.hasNext()) {
            Element currentNode = iterErrors.next();
            String text = "Error reported: " + currentNode.getAttributeValue("code") + " - " + currentNode.getAttributeValue("info");
            log.warn(text);
            throw new APIException(text, currentNode.getAttributeValue("code"));
        }
    }
    // Check for warnings
    xpa = XPathFactory.instance().compile("/api/warnings/*", Filters.element());
    List<Element> listWarnings = xpa.evaluate(root);
    if (listWarnings != null) {
        Iterator iterWarnings = listWarnings.iterator();
        while (iterWarnings.hasNext()) {
            Element currentNode = (Element) iterWarnings.next();
            log.warn("Warning reported: " + currentNode.getName() + " - " + currentNode.getValue());
        }
    }
}
Also used : APIException(org.wikipediacleaner.api.APIException) Element(org.jdom2.Element) Iterator(java.util.Iterator)

Example 15 with Element

use of org.hl7.elm.r1.Element in project wpcleaner by WPCleaner.

the class MediaWikiAPI method constructEdit.

/**
 * @param root Root element in MediaWiki answer.
 *
 * @param query Path to the answer.
 * @return Result of the query.
 * @throws APIException Exception thrown by the API.
 * @throws CaptchaException Captcha.
 */
private QueryResult constructEdit(Element root, String query) throws APIException, CaptchaException {
    XPathExpression<Element> xpa = XPathFactory.instance().compile(query, Filters.element());
    Element node = xpa.evaluateFirst(root);
    if (node != null) {
        String result = node.getAttributeValue("result");
        if ("Success".equalsIgnoreCase(result)) {
            Integer pageId = null;
            try {
                pageId = Integer.valueOf(node.getAttributeValue("pageid"));
            } catch (NumberFormatException e) {
            // 
            }
            Integer pageOldRevId = null;
            try {
                pageOldRevId = Integer.valueOf(node.getAttributeValue("oldrevid"));
            } catch (NumberFormatException e) {
            // 
            }
            Integer pageNewRevId = null;
            try {
                pageNewRevId = Integer.valueOf(node.getAttributeValue("newrevid"));
            } catch (NumberFormatException e) {
            // 
            }
            return QueryResult.createCorrectQuery(pageId, node.getAttributeValue("title"), pageOldRevId, pageNewRevId);
        } else if ("Failure".equalsIgnoreCase(result)) {
            XPathExpression<Element> xpaCaptcha = XPathFactory.instance().compile("./captcha", Filters.element());
            Element captcha = xpaCaptcha.evaluateFirst(node);
            if (captcha != null) {
                CaptchaException exception = new CaptchaException("Captcha", captcha.getAttributeValue("type"));
                exception.setMime(captcha.getAttributeValue("mime"));
                exception.setId(captcha.getAttributeValue("id"));
                exception.setURL(captcha.getAttributeValue("url"));
                throw exception;
            }
            String spamBlacklist = node.getAttributeValue("spamblacklist");
            if (spamBlacklist != null) {
                throw new APIException(GT._T("URL {0} is blacklisted", spamBlacklist));
            }
            throw new APIException(xmlOutputter.outputString(node));
        }
        return QueryResult.createErrorQuery(result, node.getAttributeValue("details"), node.getAttributeValue("wait"));
    }
    return QueryResult.createErrorQuery(null, null, null);
}
Also used : ConfigurationValueInteger(org.wikipediacleaner.utils.ConfigurationValueInteger) XPathExpression(org.jdom2.xpath.XPathExpression) APIException(org.wikipediacleaner.api.APIException) Element(org.jdom2.Element) CaptchaException(org.wikipediacleaner.api.CaptchaException)

Aggregations

Element (org.jdom2.Element)3601 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)659 ArrayList (java.util.ArrayList)616 Document (org.jdom2.Document)589 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)488 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)486 Test (org.junit.Test)474 IOException (java.io.IOException)414 JDOMException (org.jdom2.JDOMException)230 Attribute (org.jdom2.Attribute)217 List (java.util.List)189 SAXBuilder (org.jdom2.input.SAXBuilder)169 File (java.io.File)162 FHIRException (org.hl7.fhir.exceptions.FHIRException)162 HashMap (java.util.HashMap)159 Namespace (org.jdom2.Namespace)157 Test (org.junit.jupiter.api.Test)154 Element (org.osate.aadl2.Element)143 XMLOutputter (org.jdom2.output.XMLOutputter)136 XConfiguration (org.apache.oozie.util.XConfiguration)98