org.jdom2.Parent Java Examples

The following examples show how to use org.jdom2.Parent. 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: OpenSearchModuleParser.java    From rome with Apache License 2.0 6 votes vote down vote up
/**
 * Use xml:base attributes at feed and entry level to resolve relative links
 */
private static String resolveURI(final URL baseURI, final Parent parent, String url) {
    url = url.equals(".") || url.equals("./") ? "" : url;
    if (isRelativeURI(url) && parent != null && parent instanceof Element) {
        final Attribute baseAtt = ((Element) parent).getAttribute("base", Namespace.XML_NAMESPACE);
        String xmlBase = baseAtt == null ? "" : baseAtt.getValue();
        if (!isRelativeURI(xmlBase) && !xmlBase.endsWith("/")) {
            xmlBase = xmlBase.substring(0, xmlBase.lastIndexOf("/") + 1);
        }
        return resolveURI(baseURI, parent.getParent(), xmlBase + url);
    } else if (isRelativeURI(url) && parent == null) {
        return baseURI + url;
    } else if (baseURI != null && url.startsWith("/")) {
        String hostURI = baseURI.getProtocol() + "://" + baseURI.getHost();
        if (baseURI.getPort() != baseURI.getDefaultPort()) {
            hostURI = hostURI + ":" + baseURI.getPort();
        }
        return hostURI + url;
    }
    return url;
}
 
Example #2
Source File: DesktopData.java    From Zettelkasten with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method deletes the selected entry from the desktop.
 *
 * @param timestamp the timestamp of the to the selected entry.
 */
public void deleteBullet(String timestamp) {
    // retrieve the entry that should be deleted
    Element bullet = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    if (bullet != null) {
        // retrieve all timestamps of those entries which are children of the to be
        // deleted bullet
        String[] timestamps = retrieveBulletTimestamps(bullet);
        // get the entry's parent
        Parent p = bullet.getParent();
        // remove entry from parent
        p.removeContent(bullet);
        // now remove all possible modified entries of that list
        if (timestamps != null && timestamps.length > 0) {
            // iterate all timestamps
            for (String singlets : timestamps) {
                // delete modified entries that already have been deleted due
                // to the removal of the bullet-point
                deleteModifiedEntry(singlets);
            }
        }
        // change modified state
        setModified(true);
    }
}
 
Example #3
Source File: DesktopData.java    From Zettelkasten with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method deletes the selected entry from the desktop.
 *
 * @param timestamp the timestamp of the to be deleted entry
 */
public void deleteEntry(String timestamp) {
    // retrieve the entry that should be deleted
    Element entry = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    if (entry != null) {
        // check whether we have any modified entry. if so, delete it, since
        // we no longer need it...
        deleteModifiedEntry(timestamp);
        // get the entry's parent
        Parent p = entry.getParent();
        // remove entry from parent
        p.removeContent(entry);
        // change modified state
        setModified(true);
    }
}
 
Example #4
Source File: DesktopData.java    From Zettelkasten with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method retrieves a bullet element from the given path {@code tp} and
 * copies it into a global Element-variable, so this element is stored for
 * later use. Furthermore, the element retrieve from the treepath {@code tp}
 * is removed from the XML-document
 *
 * @param timestamp the timestamp of the bullet that should be copied to the
 * "clipboard" and deleted afterwards (cut-operation)
 */
public void cutBulletToClip(String timestamp) {
    // retrieve the entry that should be deleted
    Element bullet = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    if (bullet != null) {
        // get the entry's parent
        Parent p = bullet.getParent();
        // get the index from "bullet"
        int index = p.indexOf(bullet);
        // if we have a valid index, go on
        if (index != -1) {
            // remove the content and save it to the clipboard-element
            clipbullet = (Element) p.removeContent(index);
            // change modified state
            setModified(true);
        }
    }
}
 
Example #5
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Object buildNameStep(NameStep nameStep, String value, Parent parent) throws JaxenException {
    String name = nameStep.getLocalName();
    String prefix = nameStep.getPrefix();
    Namespace ns = prefix.isEmpty() ? Namespace.NO_NAMESPACE : MCRConstants.getStandardNamespace(prefix);

    if (nameStep.getAxis() == Axis.CHILD) {
        if (parent instanceof Document) {
            return buildPredicates(nameStep.getPredicates(), ((Document) parent).getRootElement());
        } else {
            return buildPredicates(nameStep.getPredicates(), buildElement(ns, name, value, (Element) parent));
        }
    } else if (nameStep.getAxis() == Axis.ATTRIBUTE) {
        return buildAttribute(ns, name, value, (Element) parent);
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("ignoring axis, can not be built: {} {}{}", nameStep.getAxis(),
                prefix.isEmpty() ? "" : prefix + ":", name);
        }
        return null;
    }
}
 
Example #6
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Resolves the first match for the given XPath and returns its value as a String 
 * 
 * @param xPath the XPath expression
 * @param parent the context element or document 
 * @return the value of the element or attribute as a String
 */
public String getValueOf(String xPath, Parent parent) {
    Object result = evaluateFirst(xPath, parent);

    if (result instanceof String) {
        return (String) result;
    } else if (result instanceof Element) {
        return ((Element) result).getText();
    } else if (result instanceof Attribute) {
        return ((Attribute) result).getValue();
    } else {
        return null;
    }
}
 
Example #7
Source File: BaseWireFeedGenerator.java    From rome with Apache License 2.0 5 votes vote down vote up
protected void generateForeignMarkup(final Element element, final List<Element> foreignElements) {
    if (foreignElements != null) {
        for (final Element foreignElement : foreignElements) {
            final Parent parent = foreignElement.getParent();
            if (parent != null) {
                parent.removeContent(foreignElement);
            }
            element.addContent(foreignElement);
        }
    }
}
 
Example #8
Source File: MCRXMLHelper.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private static Element canonicalElement(Parent e) throws IOException, SAXParseException {
    XMLOutputter xout = new XMLOutputter(Format.getCompactFormat());
    MCRByteArrayOutputStream bout = new MCRByteArrayOutputStream();
    if (e instanceof Element) {
        xout.output((Element) e, bout);
    } else {
        xout.output((Document) e, bout);
    }
    Document xml = MCRXMLParserFactory.getNonValidatingParser()
        .parseXML(new MCRByteContent(bout.getBuffer(), 0, bout.size()));
    return xml.getRootElement();
}
 
Example #9
Source File: MCRXPathBuilder.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private static String buildPositionPredicate(Element element) {
    Parent parent = element.getParent();
    if ((parent instanceof Document) || (parent == null)) {
        return "";
    }

    Element parentElement = (Element) parent;
    int pos = parentElement.getChildren(element.getName(), element.getNamespace()).indexOf(element);
    return (pos == 0 ? "" : "[" + ++pos + "]");
}
 
Example #10
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private Object assignLiteral(Expr expression, String literal, Parent parent, String xPath) throws JaxenException {
    Object result = evaluateFirst(xPath, parent);

    if ((result instanceof Element) || (result instanceof Attribute)) {
        return result;
    } else {
        xPath = simplify(expression.getText()) + "[9999]";
        return buildNode(xPath, literal, parent);
    }
}
 
Example #11
Source File: Node.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
private static boolean equals(Parent a, Parent b) {
    if(a == b) return true;
    if(a instanceof Element && b instanceof Element) {
        return equals((Element) a, (Element) b);
    }
    return a.equals(b);
}
 
Example #12
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private Object buildEqualityExpression(EqualityExpr ee, Parent parent) throws JaxenException {
    if (ee.getOperator().equals("=")) {
        if ((ee.getLHS() instanceof LocationPath) && (ee.getRHS() instanceof LiteralExpr)) {
            return assignLiteral(ee.getLHS(), (LiteralExpr) (ee.getRHS()), parent);
        } else if ((ee.getRHS() instanceof LocationPath) && (ee.getLHS() instanceof LiteralExpr)) {
            return assignLiteral(ee.getRHS(), (LiteralExpr) (ee.getLHS()), parent);
        } else if (ee.getLHS() instanceof LocationPath) {
            String value = getValueOf(ee.getRHS().getText(), parent);
            if (value != null) {
                return assignLiteral(ee.getLHS(), value, parent);
            }
        }
    }
    return canNotBuild(ee);
}
 
Example #13
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private Object buildStep(Step step, String value, Parent parent) throws JaxenException {
    if (step instanceof NameStep) {
        return buildNameStep((NameStep) step, value, parent);
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("ignoring step, can not be built: {} {}", step.getClass().getName(),
                simplify(step.getText()));
        }
        return null;
    }
}
 
Example #14
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private Object buildExpression(Expr expression, String value, Parent parent) throws JaxenException {
    if (expression instanceof EqualityExpr) {
        return buildEqualityExpression((EqualityExpr) expression, parent);
    } else if (expression instanceof LocationPath) {
        return buildLocationPath((LocationPath) expression, value, parent);
    } else {
        return canNotBuild(expression);
    }
}
 
Example #15
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public Object buildNode(String xPath, String value, Parent parent) throws JaxenException {
    BaseXPath baseXPath = new BaseXPath(xPath, new DocumentNavigator());
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("start building {} relative to {}", simplify(xPath), MCRXPathBuilder.buildXPath(parent));
    }
    return buildExpression(baseXPath.getRootExpr(), value, parent);
}
 
Example #16
Source File: MCRXEditorTransformer.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public MCRXPathEvaluator getXPathEvaluator() {
    if (currentBinding != null) {
        return currentBinding.getXPathEvaluator();
    } else {
        return new MCRXPathEvaluator(editorSession.getVariables(), (Parent) null);
    }
}
 
Example #17
Source File: MCRBinding.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private void bind(String xPath, boolean buildIfNotExists, String initialValue) throws JaxenException {
    this.xPath = xPath;

    Map<String, Object> variables = buildXPathVariables();

    XPathExpression<Object> xPathExpr = XPathFactory.instance().compile(xPath, Filters.fpassthrough(), variables,
        MCRConstants.getStandardNamespaces());

    boundNodes.addAll(xPathExpr.evaluate(parent.getBoundNodes()));

    for (Object boundNode : boundNodes) {
        if (!(boundNode instanceof Element || boundNode instanceof Attribute || boundNode instanceof Document)) {
            throw new TypeNotPresentException(
                "XPath MUST only bind either element, attribute or document nodes: " + xPath, null);
        }
    }

    LOGGER.debug("Bind to {} selected {} node(s)", xPath, boundNodes.size());

    if (boundNodes.isEmpty() && buildIfNotExists) {
        MCRNodeBuilder builder = new MCRNodeBuilder(variables);
        Object built = builder.buildNode(xPath, initialValue, (Parent) (parent.getBoundNode()));
        LOGGER.debug("Bind to {} generated node {}", xPath, MCRXPathBuilder.buildXPath(built));
        boundNodes.add(built);
        trackNodeCreated(builder.getFirstNodeBuilt());
    }
}
 
Example #18
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
private Object assignLiteral(Expr expression, LiteralExpr literal, Parent parent) throws JaxenException {
    String xPath = simplify(expression.getText()) + "[.=" + literal.getText() + "]";
    return assignLiteral(expression, literal.getLiteral(), parent, xPath);
}
 
Example #19
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
private Object assignLiteral(Expr expression, String literal, Parent parent) throws JaxenException {
    String delimiter = literal.contains("'") ? "\"" : "'";
    String xPath = simplify(expression.getText()) + "[.=" + delimiter + literal + delimiter + "]";
    return assignLiteral(expression, literal, parent, xPath);
}
 
Example #20
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
private Object evaluateFirst(String xPath, Parent parent) {
    return new MCRXPathEvaluator(variables, parent).evaluateFirst(xPath);
}
 
Example #21
Source File: MCRXPathEvaluator.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
public MCRXPathEvaluator(Map<String, Object> variables, Parent context) {
    this.variables = variables;
    this.context = new ArrayList<>();
    this.context.add(context);
}
 
Example #22
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
public Attribute buildAttribute(String xPath, String value, Parent parent) throws JaxenException {
    return (Attribute) buildNode(xPath, value, parent);
}
 
Example #23
Source File: MCRNodeBuilder.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
public Element buildElement(String xPath, String value, Parent parent) throws JaxenException {
    return (Element) buildNode(xPath, value, parent);
}