Search in sources :

Example 16 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project mycore by MyCoRe-Org.

the class MCRURNObjectXPathMetadataManager method removeIdentifier.

@Override
public void removeIdentifier(MCRDNBURN identifier, MCRBase obj, String additional) {
    String xpath = getProperties().get("Xpath");
    Document xml = obj.createXML();
    XPathFactory xPathFactory = XPathFactory.instance();
    XPathExpression<Element> xp = xPathFactory.compile(xpath, Filters.element());
    List<Element> elements = xp.evaluate(xml);
    elements.stream().filter(element -> element.getTextTrim().equals(identifier.asString())).forEach(Element::detach);
}
Also used : MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) XPathFactory(org.jdom2.xpath.XPathFactory) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRException(org.mycore.common.MCRException) Text(org.jdom2.Text) XPathExpression(org.jdom2.xpath.XPathExpression) Document(org.jdom2.Document) List(java.util.List) MCRObject(org.mycore.datamodel.metadata.MCRObject) Optional(java.util.Optional) MCRPersistentIdentifierMetadataManager(org.mycore.pi.MCRPersistentIdentifierMetadataManager) Filters(org.jdom2.filter.Filters) MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) Element(org.jdom2.Element) XPathFactory(org.jdom2.xpath.XPathFactory) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 17 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project mycore by MyCoRe-Org.

the class MCRURNUtils method getDNBRegisterDate.

public static Date getDNBRegisterDate(String identifier) throws MCRIdentifierUnresolvableException, ParseException {
    Document document = MCRDNBPIDefProvider.get(identifier);
    XPathExpression<Element> xp = XPathFactory.instance().compile(".//pidef:created[contains(../pidef:identifier, '" + identifier + "')]", Filters.element(), null, MCRConstants.PIDEF_NAMESPACE);
    Element element = xp.evaluateFirst(document);
    if (element == null) {
        return null;
    }
    return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.GERMAN).parse(element.getText());
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) SimpleDateFormat(java.text.SimpleDateFormat)

Example 18 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project mycore by MyCoRe-Org.

the class MCRAclEditorResource method transform.

protected InputStream transform(String xmlFile) throws Exception {
    InputStream guiXML = getClass().getResourceAsStream(xmlFile);
    if (guiXML == null) {
        throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).build());
    }
    SAXBuilder saxBuilder = new SAXBuilder();
    Document webPage = saxBuilder.build(guiXML);
    XPathExpression<Object> xpath = XPathFactory.instance().compile("/MyCoReWebPage/section/div[@id='mycore-acl-editor2']");
    Object node = xpath.evaluateFirst(webPage);
    MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
    String lang = mcrSession.getCurrentLanguage();
    if (node != null) {
        Element mainDiv = (Element) node;
        mainDiv.setAttribute("lang", lang);
        String bsPath = CONFIG.getString("MCR.bootstrap.path", "");
        if (!bsPath.equals("")) {
            bsPath = MCRFrontendUtil.getBaseURL() + bsPath;
            Element item = new Element("link").setAttribute("href", bsPath).setAttribute("rel", "stylesheet").setAttribute("type", "text/css");
            mainDiv.addContent(0, item);
        }
    }
    MCRContent content = MCRJerseyUtil.transform(webPage, request);
    return content.getInputStream();
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) MCRSession(org.mycore.common.MCRSession) WebApplicationException(javax.ws.rs.WebApplicationException) InputStream(java.io.InputStream) Element(org.jdom2.Element) JsonObject(com.google.gson.JsonObject) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent)

Example 19 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project mycore by MyCoRe-Org.

the class MCRLayoutUtilities method getItem.

/**
 * 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;
}
Also used : Element(org.jdom2.Element)

Example 20 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project mycore by MyCoRe-Org.

the class MCRClassificationMappingEventHandler method createMapping.

private void createMapping(MCRObject obj) {
    MCRMetaElement mappings = obj.getMetadata().getMetadataElement("mappings");
    if (mappings != null) {
        oldMappings = mappings.clone();
        obj.getMetadata().removeMetadataElement("mappings");
    }
    Element currentClassElement = null;
    try {
        Document doc = new Document(obj.getMetadata().createXML().detach());
        XPathExpression<Element> classElementPath = XPathFactory.instance().compile("//*[@categid]", Filters.element());
        List<Element> classList = classElementPath.evaluate(doc);
        if (classList.size() > 0) {
            mappings = new MCRMetaElement();
            mappings.setTag("mappings");
            mappings.setClass(MCRMetaClassification.class);
            mappings.setHeritable(false);
            mappings.setNotInherit(true);
            obj.getMetadata().setMetadataElement(mappings);
        }
        for (Element classElement : classList) {
            currentClassElement = classElement;
            MCRCategory categ = DAO.getCategory(new MCRCategoryID(classElement.getAttributeValue("classid"), classElement.getAttributeValue("categid")), 0);
            addMappings(mappings, categ);
        }
    } catch (Exception je) {
        if (currentClassElement == null) {
            LOGGER.error("Error while finding classification elements", je);
        } else {
            LOGGER.error("Error while finding classification elements for {}", new XMLOutputter().outputString(currentClassElement), je);
        }
    } finally {
        if (mappings == null || mappings.size() == 0) {
            obj.getMetadata().removeMetadataElement("mappings");
        }
    }
}
Also used : MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) XMLOutputter(org.jdom2.output.XMLOutputter) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) Element(org.jdom2.Element) Document(org.jdom2.Document)

Aggregations

Element (org.jdom2.Element)43 Document (org.jdom2.Document)23 XPathExpression (org.jdom2.xpath.XPathExpression)13 IOException (java.io.IOException)9 XPathFactory (org.jdom2.xpath.XPathFactory)9 SAXBuilder (org.jdom2.input.SAXBuilder)7 Attribute (org.jdom2.Attribute)6 JDOMException (org.jdom2.JDOMException)5 Test (org.junit.Test)5 Text (org.jdom2.Text)4 XMLOutputter (org.jdom2.output.XMLOutputter)4 MCRObject (org.mycore.datamodel.metadata.MCRObject)4 MCRPath (org.mycore.datamodel.niofs.MCRPath)4 URISyntaxException (java.net.URISyntaxException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)2 MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)2