org.jdom2.filter.Filters Java Examples

The following examples show how to use org.jdom2.filter.Filters. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MCRRestAPIObjectsHelper.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private static void createXMLForSubdirectories(MCRPath mcrPath, Element currentElement, int currentDepth,
    int maxDepth) {
    if (currentDepth < maxDepth) {
        XPathExpression<Element> xp = XPathFactory.instance().compile("./children/child[@type='directory']",
            Filters.element());
        for (Element e : xp.evaluate(currentElement)) {
            String name = e.getChildTextNormalize("name");
            try {
                MCRPath pChild = (MCRPath) mcrPath.resolve(name);
                Document doc = MCRPathXML.getDirectoryXML(pChild);
                Element eChildren = doc.getRootElement().getChild("children");
                if (eChildren != null) {
                    e.addContent(eChildren.detach());
                    createXMLForSubdirectories(pChild, e, currentDepth + 1, maxDepth);
                }
            } catch (IOException ex) {
                //ignore
            }

        }
    }
}
 
Example #2
Source File: ApiXmlParseResult.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Execute parse request.
 * 
 * @param properties Properties defining request.
 * @return Parsed text.
 * @throws APIException Exception thrown by the API.
 */
@Override
public String executeParse(
    Map<String, String> properties)
        throws APIException {
  try {
    XPathExpression<Element> xpaText = XPathFactory.instance().compile(
        "/api/parse/text", Filters.element());
    Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
    Element text = xpaText.evaluateFirst(root);
    return (text != null) ? text.getText() : null;
  } catch (JDOMException e) {
    log.error("Error expanding templates", e);
    throw new APIException("Error parsing XML", e);
  }
}
 
Example #3
Source File: ApiXmlExpandResult.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Execute expand templates request.
 * 
 * @param properties Properties defining request.
 * @return Expanded text.
 * @throws APIException Exception thrown by the API.
 */
@Override
public String executeExpandTemplates(
    Map<String, String> properties)
        throws APIException {
  try {
    XPathExpression<Element> xpaText = XPathFactory.instance().compile(
        "/api/expandtemplates/wikitext", Filters.element());
    Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);
    Element text = xpaText.evaluateFirst(root);
    return (text != null) ? text.getText() : null;
  } catch (JDOMException e) {
    log.error("Error expanding templates", e);
    throw new APIException("Error parsing XML", e);
  }
}
 
Example #4
Source File: ISBNRange.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Analyze RangeMessage.xml file Rules.
 * 
 * @param node Current node.
 * @param rangeElement Range element.
 * @throws JDOMException
 */
private static void analyzeRules(Element node, Range rangeElement) throws JDOMException {
  XPathExpression<Element> xpa = XPathFactory.instance().compile(
      "./Rules/Rule", Filters.element());
  List<Element> results = xpa.evaluate(node);
  Iterator<Element> iter = results.iterator();
  while (iter.hasNext()) {
    Element ruleNode = iter.next();
    Element rangeNode = ruleNode.getChild("Range");
    String range = (rangeNode != null) ? rangeNode.getValue() : null;
    Element lengthNode = ruleNode.getChild("Length");
    String length = (lengthNode != null) ? lengthNode.getValue() : null;
    if ((range != null) && (length != null)) {
      String[] rangeElements = range.split("\\-");
      if ((rangeElements != null) && (rangeElements.length == 2)) {
        Rule rule = new Rule(rangeElements[0], rangeElements[1], Integer.parseInt(length));
        rangeElement.addRule(rule);
      }
    }
  }
}
 
Example #5
Source File: ApiXmlPropertiesResult.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve information about page title normalization.
 * 
 * @param root Root element.
 * @param normalization Map containing information about title normalization (key=From, value=To).
 * @throws JDOMException Exception thrown due to the DOM.
 */
public void retrieveNormalization(
    Element root,
    Map<String, String> normalization) throws JDOMException {
  if (normalization == null) {
    return;
  }
  XPathExpression<Element> xpaNormalized = XPathFactory.instance().compile(
      "/api/query/normalized/n", Filters.element());
  List<Element> listNormalized = xpaNormalized.evaluate(root);
  if ((listNormalized == null) || (listNormalized.isEmpty())) {
    return;
  }
  Iterator<Element> itNormalized = listNormalized.iterator();
  while (itNormalized.hasNext()) {
    Element normalized = itNormalized.next();
    String from = normalized.getAttributeValue("from");
    String to = normalized.getAttributeValue("to");
    if ((from != null) && (to != null)) {
      normalization.put(from, to);
    }
  }
}
 
Example #6
Source File: MCRChangeTrackerTest.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testRemoveChangeTracking() throws JaxenException, JDOMException {
    String template = "document[titles[title][title[2]]][authors/author[first='John'][last='Doe']]";
    Document doc = new Document(new MCRNodeBuilder().buildElement(template, null, null));

    MCRChangeTracker tracker = new MCRChangeTracker();

    Element titles = (Element) (new MCRBinding("document/titles", true, new MCRBinding(doc)).getBoundNode());
    Element title = new Element("title").setAttribute("type", "alternative");
    titles.addContent(2, title);
    tracker.track(MCRAddedElement.added(title));

    Attribute lang = new Attribute("lang", "de");
    doc.getRootElement().setAttribute(lang);
    tracker.track(MCRAddedAttribute.added(lang));

    Element author = (Element) (new MCRBinding("document/authors/author", true, new MCRBinding(doc))
        .getBoundNode());
    tracker.track(MCRRemoveElement.remove(author));

    doc = MCRChangeTracker.removeChangeTracking(doc);
    assertFalse(doc.getDescendants(Filters.processinginstruction()).iterator().hasNext());
}
 
Example #7
Source File: TestWmsServer.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCapabilites() throws IOException, JDOMException {
  String endpoint = TestOnLocalServer.withHttpPath(
      "/wms/scanCdmUnitTests/conventions/coards/sst.mnmean.nc?service=WMS&version=1.3.0&request=GetCapabilities");
  byte[] result = TestOnLocalServer.getContent(endpoint, 200, ContentType.xmlwms);
  Reader in = new StringReader(new String(result, StandardCharsets.UTF_8));
  SAXBuilder sb = new SAXBuilder();
  Document doc = sb.build(in);

  XPathExpression<Element> xpath = XPathFactory.instance().compile("//wms:Capability/wms:Layer/wms:Layer/wms:Layer",
      Filters.element(), null, NS_WMS);
  List<Element> elements = xpath.evaluate(doc);
  assertEquals(1, elements.size());

  XPathExpression<Element> xpath2 = XPathFactory.instance()
      .compile("//wms:Capability/wms:Layer/wms:Layer/wms:Layer/wms:Name", Filters.element(), null, NS_WMS);
  Element emt = xpath2.evaluateFirst(doc);
  assertEquals("sst", emt.getTextTrim());
}
 
Example #8
Source File: ConsistentDatesTest.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void checkNCSSDates() throws JDOMException, IOException {
  String endpoint = TestOnLocalServer.withHttpPath(
      "/ncss/grid/cdmUnitTest/ncss/climatology/PF5_SST_Climatology_Monthly_1985_2001.nc?var=sst&latitude=45&longitude=-20&temporal=all&accept=xml");
  byte[] result = TestOnLocalServer.getContent(endpoint, 200, ContentType.xml);
  String results = new String(result, StandardCharsets.UTF_8);
  if (show)
    System.out.printf("checkNCSSDates%n%s%n", results);
  Reader in = new StringReader(results);
  SAXBuilder sb = new SAXBuilder();
  Document doc = sb.build(in);

  XPathExpression<Element> xpath =
      XPathFactory.instance().compile("/stationFeatureCollection/stationFeature", Filters.element());
  List<Element> dataTimeNodes = xpath.evaluate(doc);

  List<String> timePositionDateTime = new ArrayList<>();
  for (Element e : dataTimeNodes) {
    CalendarDate cd = CalendarDate.parseISOformat(null, e.getAttributeValue("date"));
    System.out.printf(" extract date= %s%n", cd);
    timePositionDateTime.add(cd.toString());;
  }

  assertEquals(expectedDatesAsDateTime, timePositionDateTime);
}
 
Example #9
Source File: ApiXmlRandomPagesResult.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Execute random pages request.
 * 
 * @param properties Properties defining request.
 * @param list List to be filled with random pages.
 * @throws APIException Exception thrown by the API.
 */
@Override
public void executeRandomList(
    Map<String, String> properties,
    List<Page> list) throws APIException {
  try {
    Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);

    // Get random list
    XPathExpression<Element> xpa = XPathFactory.instance().compile(
        "/api/query/random/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"));
      page.setPageId(currentNode.getAttributeValue("pageid"));
      list.add(page);
    }
  } catch (JDOMException e) {
    log.error("Error loading random list", e);
    throw new APIException("Error parsing XML", e);
  }
}
 
Example #10
Source File: MCRMetsIIIFModsMetadataExtractor.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<MCRIIIFMetadata> extractModsMetadata(Element xmlData) {
    Map<String, String> elementLabelMap = new HashMap<>();

    elementLabelMap.put("title", "mods:mods/mods:titleInfo/mods:title/text()");
    elementLabelMap.put("genre", "mods:mods/mods:genre/text()");
    // TODO: add some more metadata

    return elementLabelMap.entrySet().stream().map(entry -> {
        XPathExpression<Text> pathExpression = XPathFactory.instance().compile(entry.getValue(), Filters.text(),
            null, MCRConstants.MODS_NAMESPACE);
        List<Text> texts = pathExpression.evaluate(xmlData);
        if (texts.size() == 0) {
            return null;
        }
        return new MCRIIIFMetadata(entry.getKey(),
            texts.stream().map(Text::getText).collect(Collectors.joining(", ")));
    }).filter(Objects::nonNull)
        .collect(Collectors.toList());
}
 
Example #11
Source File: ConsistentDatesTest.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void checkWCSDates() throws JDOMException, IOException {
  String endpoint = TestOnLocalServer.withHttpPath(
      "/wcs/cdmUnitTest/ncss/climatology/PF5_SST_Climatology_Monthly_1985_2001.nc?service=WCS&version=1.0.0&request=DescribeCoverage&coverage=sst");
  byte[] result = TestOnLocalServer.getContent(endpoint, 200, ContentType.xml);
  Reader in = new StringReader(new String(result, StandardCharsets.UTF_8));
  SAXBuilder sb = new SAXBuilder();
  Document doc = sb.build(in);

  Namespace wcs = Namespace.getNamespace("wcs", doc.getRootElement().getNamespaceURI());
  Namespace gml = Namespace.getNamespace("gml", "http://www.opengis.net/gml");
  XPathExpression<Element> xpath =
      XPathFactory.instance().compile("//wcs:temporalDomain/gml:timePosition", Filters.element(), null, wcs, gml);
  List<Element> timePositionNodes = xpath.evaluate(doc);

  List<String> timePositionDateTime = new ArrayList<>();
  for (Element e : timePositionNodes) {
    System.out.printf("Date= %s%n", e.getText());
    CalendarDate cd = CalendarDate.parseISOformat(null, e.getText());
    timePositionDateTime.add(cd.toString());
  }

  assertEquals(expectedDatesAsDateTime, timePositionDateTime);
}
 
Example #12
Source File: MCRRestAPIClassifications.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Output xml
 * @param eRoot - the root element
 * @param lang - the language which should be filtered or null for no filter
 * @return a string representation of the XML
 * @throws IOException
 */
private static String writeXML(Element eRoot, String lang) throws IOException {
    StringWriter sw = new StringWriter();
    if (lang != null) {
        // <label xml:lang="en" text="part" />
        XPathExpression<Element> xpE = XPathFactory.instance().compile("//label[@xml:lang!='" + lang + "']",
            Filters.element(), null, Namespace.XML_NAMESPACE);
        for (Element e : xpE.evaluate(eRoot)) {
            e.getParentElement().removeContent(e);
        }
    }
    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
    Document docOut = new Document(eRoot.detach());
    xout.output(docOut, sw);
    return sw.toString();
}
 
Example #13
Source File: MCRLayoutUtilities.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns all labels of the ancestor axis for the given item within
 * navigation.xml
 *
 * @param item a navigation item
 * @return Label as String, like "labelRoot &gt; labelChild &gt;
 *         labelChildOfChild"
 */
public static String getAncestorLabels(Element item) {
    StringBuilder label = new StringBuilder();
    String lang = MCRSessionMgr.getCurrentSession().getCurrentLanguage().trim();
    XPathExpression<Element> xpath;
    Element ic = null;
    xpath = XPATH_FACTORY.compile("//.[@href='" + getWebpageID(item) + "']", Filters.element());
    ic = xpath.evaluateFirst(getNavi());
    while (ic.getName().equals("item")) {
        ic = ic.getParentElement();
        String webpageID = getWebpageID(ic);
        Element labelEl = null;
        xpath = XPATH_FACTORY.compile("//.[@href='" + webpageID + "']/label[@xml:lang='" + lang + "']",
            Filters.element());
        labelEl = xpath.evaluateFirst(getNavi());
        if (labelEl != null) {
            if (label.length() == 0) {
                label = new StringBuilder(labelEl.getTextTrim());
            } else {
                label.insert(0, labelEl.getTextTrim() + " > ");
            }
        }
    }
    return label.toString();
}
 
Example #14
Source File: MCREditorOutValidator.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
/**
 * tries to generate a valid MCRObject as JDOM Document.
 *
 * @return MCRObject
 */
public Document generateValidMyCoReObject() throws JDOMException, SAXParseException, IOException {
    MCRObject obj;
    // load the JDOM object
    XPathFactory.instance()
        .compile("/mycoreobject/*/*/*/@editor.output", Filters.attribute())
        .evaluate(input)
        .forEach(Attribute::detach);
    try {
        byte[] xml = new MCRJDOMContent(input).asByteArray();
        obj = new MCRObject(xml, true);
    } catch (SAXParseException e) {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        LOGGER.warn("Failure while parsing document:\n{}", xout.outputString(input));
        throw e;
    }
    Date curTime = new Date();
    obj.getService().setDate("modifydate", curTime);

    // return the XML tree
    input = obj.createXML();
    return input;
}
 
Example #15
Source File: MCRXSLInfoServlet.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private void listTemplates() {
    List<Element> list = xsl.getChildren("template", MCRConstants.XSL_NAMESPACE);
    IteratorIterable<Element> callTemplateElements = xsl
        .getDescendants(Filters.element("call-template", MCRConstants.XSL_NAMESPACE));
    LinkedList<Element> templates = new LinkedList<>(list);
    HashSet<String> callNames = new HashSet<>();
    for (Element callTemplate : callTemplateElements) {
        String name = callTemplate.getAttributeValue("name");
        if (callNames.add(name)) {
            templates.add(callTemplate);
        }
    }
    for (Element template : templates) {
        Element copy = template.clone();
        copy.removeContent();
        this.templates.add(copy);
    }
}
 
Example #16
Source File: ApiXmlAllMessagesResult.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Execute message request.
 * 
 * @param properties Properties defining request.
 * @return Message.
 * @throws APIException Exception thrown by the API.
 */
@Override
public String executeMessage(
    Map<String, String> properties)
        throws APIException {
  try {
    Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);

    // Retrieve general information
    XPathExpression<Element> xpa = XPathFactory.instance().compile(
        "/api/query/allmessages/message", Filters.element());
    Element generalNode = xpa.evaluateFirst(root);
    if (generalNode != null) {
      return generalNode.getValue();
    }

    return null;
  } catch (JDOMException e) {
    log.error("Error loading messages", e);
    throw new APIException("Error parsing XML", e);
  }
}
 
Example #17
Source File: ISBNRange.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Analyze RangeMessage.xml file for Ranges.
 * 
 * @param root Root of RangeMessage.xml file.
 * @param ranges Current list of ranges.
 * @param xpath XPath selector.
 * @throws JDOMException
 */
private static void analyzeRanges(Element root, List<Range> ranges, String xpath) throws JDOMException {
  XPathExpression<Element> xpa = XPathFactory.instance().compile(xpath, Filters.element());
  List<Element> results = xpa.evaluate(root);
  Iterator<Element> iter = results.iterator();
  while (iter.hasNext()) {
    Element node = iter.next();
    Element prefixNode = node.getChild("Prefix");
    String prefix = (prefixNode != null) ? prefixNode.getValue() : null;
    Element agencyNode = node.getChild("Agency");
    String agency = (agencyNode != null) ? agencyNode.getValue() : null;
    Range range = new Range(prefix, agency);
    analyzeRules(node, range);
    ranges.add(range);
  }
}
 
Example #18
Source File: MCRMODSLinkedMetadataTest.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testUpdate() throws IOException, URISyntaxException, MCRPersistenceException,
    MCRActiveLinkException, JDOMException, SAXException, MCRAccessException {
    MCRObject seriesNew = new MCRObject(getResourceAsURL(seriesID + "-updated.xml").toURI());
    MCRMetadataManager.update(seriesNew);
    Document bookNew = MCRXMLMetadataManager.instance().retrieveXML(bookID);
    XPathBuilder<Element> builder = new XPathBuilder<>(
        "/mycoreobject/metadata/def.modsContainer/modsContainer/mods:mods/mods:relatedItem/mods:titleInfo/mods:title",
        Filters.element());
    builder.setNamespace(MCRConstants.MODS_NAMESPACE);
    XPathExpression<Element> seriesTitlePath = builder.compileWith(XPathFactory.instance());
    Element titleElement = seriesTitlePath.evaluateFirst(bookNew);
    Assert.assertNotNull(
        "No title element in related item: " + new XMLOutputter(Format.getPrettyFormat()).outputString(bookNew),
        titleElement);
    Assert.assertEquals("Title update from series was not promoted to book of series.",
        "Updated series title", titleElement.getText());
}
 
Example #19
Source File: XmlUtil.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static List<Element> evaluateXPath(Document doc, String strXpath) {
  try {
    XPathExpression<Element> xpath = XPathFactory.instance().compile(strXpath, Filters.element());
    return xpath.evaluate(doc);
  } catch (IllegalStateException e) {
    e.printStackTrace();
  }
  return null;
}
 
Example #20
Source File: MCRLayoutUtilities.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a Element presentation of an item[@href=$webpageID]
 *
 * @param webpageID
 * @return Element
 */
private static Element getItem(String webpageID) {
    Element item = itemStore.get(webpageID);
    if (item == null) {
        XPathExpression<Element> xpath = XPATH_FACTORY.compile("//.[@href='" + webpageID + "']", Filters.element());
        item = xpath.evaluateFirst(getNavi());
        itemStore.put(webpageID, item);
    }
    return item;
}
 
Example #21
Source File: ApiXmlPropertiesResult.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
  }
  page.setStartTimestamp(node.getAttributeValue("starttimestamp"));
  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"));
    }
  }
}
 
Example #22
Source File: XsdTypeProvider.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Override
public RecordMeta getTypeFromParentType(RecordMeta parentMeta, String eventName) {
    Element parentElement = getComplexElement( parentMeta.getType());

    while (parentElement != null && eventName != null) {
        IteratorIterable<Element> res = parentElement.getDescendants(Filters.element(ELEMENT, XS_NAMESPACE));
        for (Element e : res) {
            if (e.getAttributeValue(NAME).equals(eventName)) {
                String elementType = e.getAttributeValue(TYPE);
                if (elementType == null) {
                    Element simple = e.getChild(SIMPLETYPE, XS_NAMESPACE);
                    elementType = getSimpleTypeRestrictionBase(simple);
                }

                return new RecordMetaImpl(eventName, elementType, shouldBeList(e, parentElement), parentMeta.getAction());
            }
        }

        IteratorIterable<Element> extensions = parentElement.getDescendants(Filters
                .element(EXTENSION, XS_NAMESPACE));

        if (extensions.hasNext()) {
            parentElement = getComplexElement(extensions.next().getAttributeValue(BASE));
        } else {
            parentElement = null;
        }
    }

    return null;
}
 
Example #23
Source File: MCRXPathEvaluator.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public Object evaluateFirst(String xPathExpression) {
    try {
        XPathExpression<Object> xPath = XPATH_FACTORY.compile(xPathExpression, Filters.fpassthrough(), variables,
            MCRConstants.getStandardNamespaces());
        return xPath.evaluateFirst(context);
    } catch (Exception ex) {
        LOGGER.warn("unable to evaluate XPath: {}", xPathExpression);
        LOGGER.warn("XPath factory used is {} {}", XPATH_FACTORY.getClass().getCanonicalName(),
            MCRConfiguration2.getString("MCR.XPathFactory.Class").orElse(null));
        LOGGER.warn(ex);
        return null;
    }
}
 
Example #24
Source File: MCRDefaultAltoChangeApplier.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void applyChange(MCRAltoChangeSet changeSet) {
    String derivateID = changeSet.getDerivateID();

    changeSet.getWordChanges().stream().forEach(change -> {
        List<MCRAltoWordChange> list = fileChangeMap.computeIfAbsent(change.getFile(), (k) -> new ArrayList<>());
        list.add(change);
    });

    fileChangeMap.keySet().forEach(file -> {
        LOGGER.info("Open file {} to apply changes!", file);
        MCRPath altoFilePath = MCRPath.getPath(derivateID, file);

        if (!Files.exists(altoFilePath)) {
            LOGGER.warn("Could not find file {} which was referenced by alto change!", altoFilePath);
            throw new MCRException(new IOException("Alto-File " + altoFilePath + " does not exist"));
        }

        Document altoDocument = readALTO(altoFilePath);
        List<MCRAltoWordChange> wordChangesInThisFile = fileChangeMap.get(file);
        wordChangesInThisFile.stream().forEach(wordChange -> {
            String xpath = String
                .format(Locale.ROOT, "//alto:String[number(@HPOS)=number('%d') and number(@VPOS)=number('%d')]",
                    wordChange.getHpos(), wordChange.getVpos());
            List<Element> wordToChange = XPathFactory.instance()
                .compile(xpath, Filters.element(), null, MCRConstants.ALTO_NAMESPACE).evaluate(altoDocument);

            if (wordToChange.size() != 1) {
                LOGGER.warn("Found {} words to change.", wordToChange.size());
            }
            wordToChange.forEach(word -> {
                word.setAttribute("CONTENT", wordChange.getTo());
                word.setAttribute("WC", "1");
            });
        });
        storeALTO(altoFilePath, altoDocument);
    });
}
 
Example #25
Source File: XPathExtractor.java    From web-data-extractor with Apache License 2.0 5 votes vote down vote up
private XPathExpression createXpathExpression() {
    XPathFactory xpfac = XPathFactory.instance();
    XPathExpression xp = null;
    if (namespaces.isEmpty()) {
        xp = xpfac.compile(xpath, Filters.fpassthrough());
    } else {
        xp = xpfac.compile(xpath, Filters.fpassthrough(), new LinkedHashMap<String, Object>(), namespaces.toArray(new Namespace[namespaces.size()]));
    }
    return xp;
}
 
Example #26
Source File: ApiXmlTokensResult.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * Execute tokens request.
 * 
 * @param properties Properties defining request.
 * @throws APIException Exception thrown by the API.
 */
@Override
public void executeTokens(
    Map<String, String> properties)
        throws APIException {
  try {
    Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);

    // Get recent changes list
    XPathExpression<Element> xpa = XPathFactory.instance().compile(
        "/api/tokens", Filters.element());
    List<Element> results = xpa.evaluate(root);
    Iterator<Element> iter = results.iterator();
    while (iter.hasNext()) {
      Element currentNode = iter.next();
      String deleteToken = currentNode.getAttributeValue("deletetoken");
      if (deleteToken != null) {
        getWiki().getConnection().setDeleteToken(deleteToken);
      }
      String editToken = currentNode.getAttributeValue("edittoken");
      if (editToken != null) {
        getWiki().getConnection().setEditToken(editToken);
      }
    }
  } catch (JDOMException e) {
    log.error("Error retrieving tokens", e);
    throw new APIException("Error parsing XML", e);
  }
}
 
Example #27
Source File: ApiXmlAllMessagesResult.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * Execute messages request.
 * 
 * @param properties Properties defining request.
 * @param messages Map of messages to be filled with the results.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeMessages(
    Map<String, String> properties,
    Map<String, String> messages) throws APIException {
  try {
    Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);

    // Retrieve general information
    XPathExpression<Element> xpa = XPathFactory.instance().compile(
        "/api/query/allmessages/message", Filters.element());
    List<Element> listMessages = xpa.evaluate(root);
    Iterator<Element> itMessages = listMessages.iterator();
    while (itMessages.hasNext()) {
      Element message = itMessages.next();
      String name = message.getAttributeValue("name");
      String text = message.getText().trim();
      messages.put(name, text);
    }

    // Retrieve continue
    return shouldContinue(
        root, "/api/query-continue/allmessages",
        properties);
  } catch (JDOMException e) {
    log.error("Error loading messages", e);
    throw new APIException("Error parsing XML", e);
  }
}
 
Example #28
Source File: ApiXmlLanguageLinksResult.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * Get language links of a page.
 * 
 * @param properties Properties defining request.
 * @param languageLinks Map of language links to be set.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean getLanguageLinks(
    Map<String, String> properties,
    Map<String, String> languageLinks) throws APIException {
  try {
    Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);

    // Set disambiguation status
    XPathExpression<Element> xpa = XPathFactory.instance().compile(
        "/api/query/pages/page/langlinks/ll", Filters.element());
    List<Element> results = xpa.evaluate(root);
    Iterator<Element> iter = results.iterator();
    while (iter.hasNext()) {
      Element currentNode = iter.next();
      String title = currentNode.getText();
      String lang = currentNode.getAttributeValue("lang");
      if ((title != null) && (title.trim().length() > 0)) {
        languageLinks.put(lang, title);
      }
    }

    // Retrieve continue
    return shouldContinue(
        root, "/api/query-continue/langlinks",
        properties);
  } catch (JDOMException e) {
    log.error("Error updating disambiguation status", e);
    throw new APIException("Error parsing XML", e);
  }
}
 
Example #29
Source File: ApiXmlAbuseLogResult.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * Execute abuse log request.
 * 
 * @param properties Properties defining request.
 * @param list List to be filled with abuse logs.
 * @return True if request should be continued.
 * @throws APIException Exception thrown by the API.
 */
@Override
public boolean executeAbuseLog(
    Map<String, String> properties,
    List<Page> list) throws APIException {
  try {
    Element root = getRoot(properties, ApiRequest.MAX_ATTEMPTS);

    // Retrieve category members
    XPathExpression<Element> xpa = XPathFactory.instance().compile(
        "/api/query/abuselog/item", Filters.element());
    List<Element> results = xpa.evaluate(root);
    Iterator<Element> iter = results.iterator();
    while (iter.hasNext()) {
      Element currentNode = iter.next();
      String title = currentNode.getAttributeValue("title");
      Page page = DataManager.getPage(getWiki(), title, null, null, null);
      list.add(page);
    }

    // Retrieve continue
    return false; // Not continuing
    /*return shouldContinue(
        root, "/api/query-continue/abuselog",
        properties);*/
  } catch (JDOMException e) {
    log.error("Error loading abuse filters list", e);
    throw new APIException("Error parsing XML", e);
  }
}
 
Example #30
Source File: ApiXmlLinksResult.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
  }
}