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 RuntimeException( "XPath MUST only bind either element, attribute or document nodes: " + xPath); 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()); } }
@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; } }
/** * 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); } } }
/** * 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); } }
/** * 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); } }
/** Use xml:base attributes at feed and entry level to resolve relative links */ private static String resolveURI(URL baseURI, Parent parent, String url) { url = (url.equals(".") || url.equals("./")) ? "" : url; if (isRelativeURI(url) && parent != null && parent instanceof Element) { 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; }
/** * 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; }
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); }
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); }
@SuppressWarnings("unchecked") private Object buildLocationPath(LocationPath locationPath, String value, Parent parent) throws JaxenException { Object existingNode = null; List<Step> steps = locationPath.getSteps(); int i, indexOfLastStep = steps.size() - 1; for (i = indexOfLastStep; i >= 0; i--) { String xPath = buildXPath(steps.subList(0, i + 1)); existingNode = evaluateFirst(xPath, parent); if (existingNode instanceof Element) { if (LOGGER.isDebugEnabled()) LOGGER.debug("element already existing"); parent = (Element) existingNode; break; } else if (existingNode instanceof Attribute) { if (LOGGER.isDebugEnabled()) LOGGER.debug("attribute already existing"); break; } else if (LOGGER.isDebugEnabled()) LOGGER.debug("{} does not exist or is not a node, will try to build it", xPath); } if (i == indexOfLastStep) return existingNode; else return buildLocationSteps(steps.subList(i + 1, steps.size()), value, parent); }
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; } }
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); }
/** * 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; }
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); } }
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 + "]"); }
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(); }
protected void generateForeignMarkup(Element e, List foreignMarkup) { if (foreignMarkup != null) { Iterator elems = (Iterator) foreignMarkup.iterator(); while (elems.hasNext()) { Element elem = (Element) elems.next(); Parent parent = elem.getParent(); if (parent != null) { parent.removeContent(elem); } e.addContent(elem); } } }
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); } } }
public MCRXPathEvaluator getXPathEvaluator() { if (currentBinding != null) return currentBinding.getXPathEvaluator(); else return new MCRXPathEvaluator(editorSession.getVariables(), (Parent) null); }
public Element buildElement(String xPath, String value, Parent parent) throws JaxenException { return (Element) buildNode(xPath, value, parent); }
public Attribute buildAttribute(String xPath, String value, Parent parent) throws JaxenException { return (Attribute) buildNode(xPath, value, parent); }
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); }
private Object evaluateFirst(String xPath, Parent parent) { return new MCRXPathEvaluator(variables, parent).evaluateFirst(xPath); }
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); }
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); }
public MCRXPathEvaluator(Map<String, Object> variables, Parent context) { this.variables = variables; this.context = new ArrayList<>(); this.context.add(context); }
public static int indexOf(Parent p, Content child) { return p.getContent().indexOf(child); }