All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.bliki.api.XMLCategoryMembersParser Maven / Gradle / Ivy

The newest version!
package info.bliki.api;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * Reads PageInfo data from an XML file generated by the Wikimedia API through the
 * categorymembers query.
 */
public class XMLCategoryMembersParser extends AbstractXMLParser {
    private static final String CM_TAG = "cm";
    private static final String CATEGORYMEMBERS_TAG = "categorymembers";
    private static final String CMCONTINUE_ID = "cmcontinue";

    private PageInfo fPage;

    private List pagesList;

    private String cmContinue;

    public XMLCategoryMembersParser(String xmlText) throws SAXException {
        super(xmlText);
        this.pagesList = new ArrayList<>();
        this.cmContinue = "";
    }

    @Override
    public void endElement(String uri, String name, String qName) {
        try {
            if (CM_TAG.equals(qName)) {// ||
                // CATEGORY_ID.equals(qName))
                // {
                if (fPage != null) {
                    pagesList.add(fPage);
                }
                // System.out.println(getString());
            }

            fData = null;
            fAttributes = null;

        } catch (RuntimeException re) {
            re.printStackTrace();
        }
    }

    /**
     * @return the cmContinue
     */
    public String getCmContinue() {
        if (cmContinue == null) {
            return "";
        }
        return cmContinue;
    }

    public List getPagesList() {
        return pagesList;
    }

    @Override
    public void startElement(String namespaceURI, String localName,
            String qName, Attributes atts) {
        fAttributes = atts;

        if (CM_TAG.equals(qName)) {
            fPage = new PageInfo();
            fPage.setPageid(fAttributes.getValue(AbstractXMLParser.PAGE_ID));
            fPage.setNs(fAttributes.getValue(AbstractXMLParser.NS_ID));
            fPage.setTitle(fAttributes.getValue(AbstractXMLParser.TITLE_ID));
        } else if (CATEGORYMEMBERS_TAG.equals(qName)) {
            String value = fAttributes.getValue(CMCONTINUE_ID);
            if (value != null) {
                cmContinue = value;
            }
        }
        fData = null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy