Search in sources :

Example 1 with Element

use of org.jdom2.Content.CType.Element in project wpcleaner by WPCleaner.

the class ApiXmlCategoriesResult method executeCategories.

/**
 * Execute categories request.
 *
 * @param properties Properties defining request.
 * @param page Page.
 * @param list List to be filled with categories.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeCategories(Map<String, String> properties, Page page, List<Page> list) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve back links
        XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/query/pages/page/categories/cl", Filters.element());
        List<Element> listCategories = xpa.evaluate(root);
        Iterator<Element> itCategory = listCategories.iterator();
        while (itCategory.hasNext()) {
            Element currentCategory = itCategory.next();
            String pageId = currentCategory.getAttributeValue("pageid");
            String ns = currentCategory.getAttributeValue("ns");
            String title = currentCategory.getAttributeValue("title");
            Page category = DataManager.getPage(getWiki(), title, null, null, null);
            category.setNamespace(ns);
            category.setPageId(pageId);
            if (currentCategory.getAttribute("missing") != null) {
                category.setExisting(Boolean.FALSE);
            }
            if (!list.contains(category)) {
                list.add(category);
            }
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/categories", properties);
    } catch (JDOMException e) {
        log.error("Error loading templates", 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 2 with Element

use of org.jdom2.Content.CType.Element in project wpcleaner by WPCleaner.

the class ApiXmlLinksResult method executeLinks.

/**
 * Execute links request.
 *
 * @param properties Properties defining request.
 * @param lists Map of lists to be filled with links.
 * @param normalization Map containing information about title normalization (key=From, value=To).
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeLinks(Map<String, String> properties, Map<String, List<Page>> lists, Map<String, String> normalization) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve normalization information
        retrieveNormalization(root, normalization);
        // Retrieve back links
        XPathExpression<Element> xpaPages = XPathFactory.instance().compile("/api/query/pages/page", Filters.element());
        List<Element> listPages = xpaPages.evaluate(root);
        Iterator<Element> itPage = listPages.iterator();
        XPathExpression<Element> xpaLinks = XPathFactory.instance().compile("links/pl", Filters.element());
        while (itPage.hasNext()) {
            Element pageNode = itPage.next();
            String pageTitle = pageNode.getAttributeValue("title");
            List<Page> links = lists.get(pageTitle);
            if (links == null) {
                links = new ArrayList<>();
                lists.put(pageTitle, links);
            }
            List<Element> listLinks = xpaLinks.evaluate(pageNode);
            Iterator<Element> itLinks = listLinks.iterator();
            while (itLinks.hasNext()) {
                Element linkNode = itLinks.next();
                Page link = DataManager.getPage(getWiki(), linkNode.getAttributeValue("title"), null, null, null);
                link.setNamespace(linkNode.getAttributeValue("ns"));
                links.add(link);
            }
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/links", properties);
    } catch (JDOMException e) {
        log.error("Error loading links", 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 3 with Element

use of org.jdom2.Content.CType.Element in project wpcleaner by WPCleaner.

the class ApiXmlLinksResult method executeLinks.

/**
 * Execute links request.
 *
 * @param properties Properties defining request.
 * @param links List to be filled with links.
 * @param knownPages Already known pages.
 * @param normalization Map containing information about title normalization (key=From, value=To).
 * @param redirects List of redirects filled by the method.
 * @param useDisambig Flag indicating if disambiguation property should be used.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeLinks(Map<String, String> properties, List<Page> links, List<Page> knownPages, Map<String, String> normalization, List<Page> redirects, boolean useDisambig) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Retrieve normalization information
        retrieveNormalization(root, normalization);
        // Retrieve back links
        XPathExpression<Element> xpaPages = XPathFactory.instance().compile("/api/query/pages/page", Filters.element());
        List<Element> listLinks = xpaPages.evaluate(root);
        Iterator<Element> itLinks = listLinks.iterator();
        while (itLinks.hasNext()) {
            Element linkNode = itLinks.next();
            Page link = getPage(getWiki(), linkNode, knownPages, useDisambig);
            if ((redirects != null) && (link.getRedirects().isRedirect())) {
                redirects.add(link);
            }
            links.add(link);
        }
        // Retrieve continue
        return shouldContinue(root, "/api/query-continue/links", properties);
    } catch (JDOMException e) {
        log.error("Error loading links", 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 4 with Element

use of org.jdom2.Content.CType.Element in project wpcleaner by WPCleaner.

the class ApiXmlPagePropsResult method setDiambiguationStatus.

/**
 * Set disambiguation status of a list of pages.
 *
 * @param properties Properties defining request.
 * @param pages List of pages for which disambiguation status needs to be set.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean setDiambiguationStatus(Map<String, String> properties, Collection<Page> pages) throws APIException {
    try {
        Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
        // Manage redirects and missing pages
        updateRedirect(root, pages);
        // Set disambiguation status
        XPathExpression<Element> xpa = XPathFactory.instance().compile("/api/query/pages/page", Filters.element());
        List<Element> results = xpa.evaluate(root);
        Iterator<Element> iter = results.iterator();
        List<Page> tmpPages = new ArrayList<>();
        while (iter.hasNext()) {
            Element currentNode = iter.next();
            String title = currentNode.getAttributeValue("title");
            for (Page p : pages) {
                tmpPages.clear();
                Iterator<Page> it = p.getRedirects().getIteratorWithPage();
                while (it.hasNext()) {
                    Page p2 = it.next();
                    tmpPages.add(p2);
                    if ((p2.getTitle() != null) && (Page.areSameTitle(p2.getTitle(), title))) {
                        Boolean disambig = Boolean.FALSE;
                        Element pageProps = currentNode.getChild("pageprops");
                        if ((pageProps != null) && (pageProps.getAttribute("disambiguation") != null)) {
                            disambig = Boolean.TRUE;
                        }
                        for (Page p3 : tmpPages) {
                            p3.setDisambiguationPage(disambig);
                        }
                    }
                }
            }
        }
        return false;
    } catch (JDOMException e) {
        log.error("Error updating disambiguation status", e);
        throw new APIException("Error parsing XML", e);
    }
}
Also used : APIException(org.wikipediacleaner.api.APIException) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Page(org.wikipediacleaner.api.data.Page) JDOMException(org.jdom2.JDOMException)

Example 5 with Element

use of org.jdom2.Content.CType.Element in project wpcleaner by WPCleaner.

the class ApiXmlPropertiesResult method updatePageInformation.

/**
 * Update page information.
 *
 * @param node Element for the page.
 * @param page Page.
 * @throws JDOMException Exception from due to the DOM.
 */
public void updatePageInformation(Element node, Page page) throws JDOMException {
    // Retrieve basic page information
    Attribute attrPageId = node.getAttribute("pageid");
    if (attrPageId != null) {
        page.setPageId(attrPageId.getValue());
    }
    Attribute attrTitle = node.getAttribute("title");
    if (attrTitle != null) {
        page.setTitle(attrTitle.getValue());
    }
    Optional.ofNullable(node.getAttributeValue("starttimestamp")).ifPresent(timestamp -> page.setStartTimestamp(timestamp));
    Attribute attrRedirect = node.getAttribute("redirect");
    if (attrRedirect != null) {
        page.getRedirects().isRedirect(true);
    }
    Attribute attrMissing = node.getAttribute("missing");
    if (attrMissing != null) {
        page.setExisting(Boolean.FALSE);
    }
    // Retrieve protection information
    XPathExpression<Element> xpaProtection = XPathFactory.instance().compile("protection/pr[@type=\"edit\"]", Filters.element());
    List<Element> protectionNodes = xpaProtection.evaluate(node);
    for (Element protectionNode : protectionNodes) {
        if ("edit".equals(protectionNode.getAttributeValue("type"))) {
            page.setEditProtectionLevel(protectionNode.getAttributeValue("level"));
        }
    }
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element)

Aggregations

Element (org.jdom2.Element)3601 Document (org.jdom2.Document)648 Test (org.junit.Test)469 ArrayList (java.util.ArrayList)364 IOException (java.io.IOException)352 Attribute (org.jdom2.Attribute)305 JDOMException (org.jdom2.JDOMException)250 SAXBuilder (org.jdom2.input.SAXBuilder)206 Namespace (org.jdom2.Namespace)172 XMLOutputter (org.jdom2.output.XMLOutputter)165 File (java.io.File)147 Test (org.junit.jupiter.api.Test)134 List (java.util.List)133 HashMap (java.util.HashMap)129 XConfiguration (org.apache.oozie.util.XConfiguration)98 StringReader (java.io.StringReader)97 Configuration (org.apache.hadoop.conf.Configuration)96 Text (org.jdom2.Text)72 Iterator (java.util.Iterator)70 Map (java.util.Map)69