Search in sources :

Example 6 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project pwm by pwm-project.

the class StoredConfigurationImpl method createOrGetSettingElement.

private static Element createOrGetSettingElement(final Document document, final PwmSetting setting, final String profileID) {
    final XPathExpression xp = XPathBuilder.xpathForSetting(setting, profileID);
    final Element existingSettingElement = (Element) xp.evaluateFirst(document);
    if (existingSettingElement != null) {
        return existingSettingElement;
    }
    final Element settingElement = new Element(XML_ELEMENT_SETTING);
    settingElement.setAttribute(XML_ATTRIBUTE_KEY, setting.getKey());
    settingElement.setAttribute(XML_ATTRIBUTE_SYNTAX, setting.getSyntax().toString());
    if (profileID != null && profileID.length() > 0) {
        settingElement.setAttribute(XML_ATTRIBUTE_PROFILE, profileID);
    }
    Element settingsElement = document.getRootElement().getChild(XML_ELEMENT_SETTINGS);
    if (settingsElement == null) {
        settingsElement = new Element(XML_ELEMENT_SETTINGS);
        document.getRootElement().addContent(settingsElement);
    }
    settingsElement.addContent(settingElement);
    return settingElement;
}
Also used : XPathExpression(org.jdom2.xpath.XPathExpression) Element(org.jdom2.Element)

Example 7 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project pwm by pwm-project.

the class StoredConfigurationImpl method resetLocaleBundleMap.

public void resetLocaleBundleMap(final String bundleName, final String keyName) {
    preModifyActions();
    domModifyLock.writeLock().lock();
    try {
        final XPathExpression xp = XPathBuilder.xpathForLocaleBundleSetting(bundleName, keyName);
        final List<Element> oldBundleElements = xp.evaluate(document);
        if (oldBundleElements != null) {
            for (final Element element : oldBundleElements) {
                element.detach();
            }
        }
    } finally {
        domModifyLock.writeLock().unlock();
    }
}
Also used : XPathExpression(org.jdom2.xpath.XPathExpression) Element(org.jdom2.Element)

Example 8 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project pwm by pwm-project.

the class PwmSettingXml method readCategoryXml.

static Element readCategoryXml(final PwmSettingCategory category) {
    final XPathFactory xpfac = XPathFactory.instance();
    final XPathExpression xp = xpfac.compile("/settings/category[@key=\"" + category.toString() + "\"]");
    return (Element) xp.evaluateFirst(readXml());
}
Also used : XPathExpression(org.jdom2.xpath.XPathExpression) XPathFactory(org.jdom2.xpath.XPathFactory) Element(org.jdom2.Element)

Example 9 with XPathExpression

use of org.jdom2.xpath.XPathExpression in project pwm by pwm-project.

the class PwmSettingXml method readTemplateXml.

static Element readTemplateXml(final PwmSettingTemplate template) {
    final XPathFactory xpfac = XPathFactory.instance();
    final XPathExpression xp = xpfac.compile("/settings/template[@key=\"" + template.toString() + "\"]");
    return (Element) xp.evaluateFirst(readXml());
}
Also used : XPathExpression(org.jdom2.xpath.XPathExpression) XPathFactory(org.jdom2.xpath.XPathFactory) Element(org.jdom2.Element)

Example 10 with XPathExpression

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

the class MCRMigrationCommands method fixDerivateLinks.

@MCRCommand(syntax = "fix invalid derivate links {0} for {1}", help = "Fixes the paths of all derivate links " + "({0} -> xpath -> e.g. /mycoreobject/metadata/derivateLinks/derivateLink) for object {1}. (MCR-1267)", order = 15)
public static void fixDerivateLinks(String xpath, String id) throws IOException, JDOMException, SAXException {
    // get mcr object
    MCRObjectID objectID = MCRObjectID.getInstance(id);
    // find derivate links
    Document xml = MCRXMLMetadataManager.instance().retrieveXML(objectID);
    Element mcrObjectXML = xml.getRootElement();
    XPathExpression<Element> expression = XPathFactory.instance().compile(xpath, Filters.element());
    List<Element> derivateLinkElements = expression.evaluate(mcrObjectXML);
    // check them
    boolean changedObject = false;
    for (Element derivateLinkElement : derivateLinkElements) {
        String href = derivateLinkElement.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
        MCRMetaDerivateLink link = new MCRMetaDerivateLink();
        link.setReference(href, null, null);
        String owner = link.getOwner();
        try {
            String path = link.getPath();
            MCRPath mcrPath = MCRPath.getPath(owner, path);
            if (!Files.exists(mcrPath)) {
                // -> e.g. a?c.tif -> path (a), query (c.tif) which is obvious wrong
                if (tryRawPath(objectID, derivateLinkElement, href, link, owner)) {
                    changedObject = true;
                } else {
                    LOGGER.warn("{} of {}cannot be found on file system. This is most likly a dead link.", href, objectID);
                }
            }
        } catch (URISyntaxException uriExc) {
            // not encoded properly
            if (tryRawPath(objectID, derivateLinkElement, href, link, owner)) {
                changedObject = true;
            } else {
                LOGGER.warn("{} of {} isn't URI encoded and cannot be found on file system. This is most likly a dead link.", href, objectID);
            }
        }
    }
    // store the mcr object if its changed
    if (changedObject) {
        // we use MCRXMLMetadataMananger because we don't want to validate the old mcr object
        MCRXMLMetadataManager.instance().update(objectID, xml, new Date());
        // manually fire update event
        MCRObject newObject = MCRMetadataManager.retrieveMCRObject(objectID);
        newObject.setImportMode(true);
        MCRMetadataManager.fireUpdateEvent(newObject);
    }
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) Element(org.jdom2.Element) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) URISyntaxException(java.net.URISyntaxException) Document(org.jdom2.Document) MCRPath(org.mycore.datamodel.niofs.MCRPath) Date(java.util.Date) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

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