static Element buildElement(String xPath, String content, Element parent) { try { return new MCRNodeBuilder().buildElement(xPath, content, parent); } catch (JaxenException ex) { throw new MCRException("Unable to build field " + xPath, ex); } } }
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()); } }
public void insert(int pos) throws JaxenException { if ("build".equals(method)) { Element parentElement = getParentElement(); Element precedingElement = (Element) (getBoundNodes().get(pos - 1)); int posOfPrecedingInParent = parentElement.indexOf(precedingElement); int targetPos = posOfPrecedingInParent + 1; String pathToBuild = getElementNameWithPredicates(); Element newElement = (Element) (new MCRNodeBuilder().buildNode(pathToBuild, null, null)); parentElement.addContent(targetPos, newElement); boundNodes.add(pos, newElement); track(MCRAddedElement.added(newElement)); } else { cloneBoundElement(pos - 1); } } }
@SuppressWarnings("unchecked") public String getElementNameWithPredicates() throws JaxenException { BaseXPath baseXPath = new BaseXPath(xPath, new DocumentNavigator()); Expr rootExpr = baseXPath.getRootExpr(); LocationPath locationPath = (LocationPath) rootExpr; List<Step> steps = locationPath.getSteps(); Step lastStep = steps.get(steps.size() - 1); return MCRNodeBuilder.simplify(lastStep.getText()); }
/** * Builds the XML representation of this identifier within MODS. */ void buildElement(Element parent) { MCRNodeBuilder builder = new MCRNodeBuilder(); try { LOGGER.info("building new identifier " + this); builder.buildElement(type.getXPath(), value, parent); } catch (JaxenException ex) { throw new MCRException(ex); } } }
@Override public void insertIdentifier(MCRPersistentIdentifier identifier, MCRBase obj, String additional) throws MCRPersistentIdentifierException { String xpath = getProperties().get("Xpath"); Document xml = obj.createXML(); MCRNodeBuilder nb = new MCRNodeBuilder(); try { nb.buildElement(xpath, identifier.asString(), xml); if (obj instanceof MCRObject) { final Element metadata = xml.getRootElement().getChild("metadata"); ((MCRObject) obj).getMetadata().setFromDOM(metadata); } else { throw new MCRPersistentIdentifierException(obj.getId() + " is no MCRObject!", new OperationNotSupportedException(getClass().getName() + " only supports " + MCRObject.class.getName() + "!")); } } catch (Exception e) { throw new MCRException("Error while inscribing PI to " + obj.getId(), e); } }