List of usage examples for org.jdom2.xpath XPathFactory instance
public static final XPathFactory instance()
From source file:org.buddycloud.channelserver.XMPPAcceptanceTestHelper.java
License:Apache License
private Object getEl(Packet p, String xPath, boolean namespaceFeature) throws Exception { InputStream xmlStream = IOUtils.toInputStream(p.toXML()); SAXBuilder saxBuilder = new SAXBuilder(); saxBuilder.setFeature("http://xml.org/sax/features/namespaces", namespaceFeature); saxBuilder.setFeature("http://xml.org/sax/features/validation", false); saxBuilder.setFeature("http://xml.org/sax/features/namespace-prefixes", false); Document replyDoc = (Document) saxBuilder.build(xmlStream); return XPathFactory.instance().compile(xPath).evaluateFirst(replyDoc); }
From source file:org.goobi.production.export.ExportXmlLog.java
License:Open Source License
public List<Element> getMetsValues(String expression, Object element, List<Namespace> namespaces) throws JaxenException { XPathExpression<Element> xpath = XPathFactory.instance().compile(expression, Filters.element(), null, namespaces);/*from ww w . ja va 2 s. c om*/ return xpath.evaluate(element); }
From source file:org.jboss.hal.processor.mbui.MbuiViewProcessor.java
License:Apache License
protected void processType(final TypeElement type, final MbuiView mbuiView) { String subclass = TypeSimplifier.simpleNameOf(generatedClassName(type, "")); //NON-NLS String createMethod = verifyCreateMethod(type); MbuiViewContext context = new MbuiViewContext(TypeSimplifier.packageNameOf(type), TypeSimplifier.classNameOf(type), subclass, createMethod); // order is important! do not rearrange unless you know what you're doing! // parse and validate the MBUI XML xPathFactory = XPathFactory.instance(); Document document = parseXml(type, mbuiView); validateDocument(type, document);/*w w w .ja v a2 s .co m*/ // first process the metadata processMetadata(type, document, context); // processTabs(document, context); // then find and verify all @MbuiElement members processMbuiElements(type, document, context); processRoot(document, context); processCrossReferences(document, context); // init parameters and abstract properties processAbstractProperties(type, context); // find and verify all @PostConstruct methods processPostConstruct(type, context); // generate code code(TEMPLATE, context.getPackage(), context.getSubclass(), () -> ImmutableMap.of("context", context)); //NON-NLS info("Generated MBUI view implementation [%s] for [%s]", context.getSubclass(), context.getBase()); //NON-NLS }
From source file:org.jumpmind.metl.core.runtime.component.XmlFormatter.java
License:Open Source License
private void fillStackWithStaticParentElements(Stack<DocElement> parentStack, DocElement firstDocElement, Document generatedXml) {/*from w ww . ja va 2 s . c o m*/ Element elementToPutOnStack = null; Map<Element, Namespace> namespaces = null; // if the generatedXml doc is empty then start a new one and use it for // search if (!generatedXml.hasRootElement()) { Element newRootElement = templateDoc.getRootElement().clone(); generatedXml.setRootElement(newRootElement); namespaces = removeNamespaces(generatedXml); XPathExpression<Element> expression = XPathFactory.instance().compile(firstDocElement.xpath, Filters.element()); List<Element> matches = expression.evaluate(generatedXml.getRootElement()); if (matches.size() != 0) { elementToPutOnStack = matches.get(0).getParentElement(); } else { elementToPutOnStack = generatedXml.getRootElement(); } elementToPutOnStack.removeContent(); removeAllAttributes(elementToPutOnStack); parentStack.push(new DocElement(firstDocElement.level - 1, elementToPutOnStack, null, null)); restoreNamespaces(generatedXml, namespaces); } else { // we already have a genertedXml going, but need other static // elements from the template namespaces = removeNamespaces(templateDoc); XPathExpression<Element> expression = XPathFactory.instance().compile(firstDocElement.xpath, Filters.element()); List<Element> matches = expression.evaluate(templateDoc.getRootElement()); // TODO: do something here for when the attribute is more than one // level away from the entity if (matches.size() != 0) { elementToPutOnStack = matches.get(0).getParentElement().clone(); } else { // throw some exception here } elementToPutOnStack.removeContent(); removeAllAttributes(elementToPutOnStack); parentStack.push(new DocElement(firstDocElement.level - 1, elementToPutOnStack, null, null)); restoreNamespaces(templateDoc, namespaces); } }
From source file:org.jumpmind.metl.core.runtime.component.XmlFormatter.java
License:Open Source License
private void addModelAttributeXml(Stack<DocElement> parentStack, String attributeId, Object modelAttrValue, Document generatedXml, String entityId) { DocElement templateDocElement = entityAttributeDtls.get(attributeId); String value = modelAttrValue == null ? null : modelAttrValue.toString(); Element newElement = null;/*from w w w .j a v a2 s. c om*/ Element templateParentElement = null; String templateParentXPath = null; Attribute newAttribute = null; Map<Element, Namespace> generatedDocNamespaces = null; Map<Element, Namespace> templateNamespaces = null; Stack<Element> parentsToAdd = new Stack<Element>(); DocElement entityDocElement = entityAttributeDtls.get(entityId); generatedDocNamespaces = removeNamespaces(generatedXml); templateNamespaces = removeNamespaces(templateDoc); // we can be passed elements in the model that don't reside in the // template. If so, just ignore the field and do nothing if (templateDocElement != null) { // at this point, our stack should always currently have the entity // for this attribute as the top level of the stack // set up our new element or attribute to add if (templateDocElement.xmlElement != null) { // we have to add an element newElement = templateDocElement.xmlElement.clone(); newElement.removeContent(); removeAllAttributes(newElement); if (StringUtils.isEmpty(value)) { if (nullHandling.equalsIgnoreCase(NULL_HANDLING_XML_NIL)) { newElement.setAttribute("nil", "true", getXmlNamespace()); } } else { newElement.setText(value); } } else { // we have to add an attribute newAttribute = templateDocElement.xmlAttribute.clone(); if (value != null) { newAttribute.setValue(value); } } // in this case the attribute is one lower than the entity and // should simply be attached to the entity if (templateDocElement.level - 1 == parentStack.peek().level) { if (newElement != null) { applyAttributeXPath(generatedXml, templateDocElement.xpath, value); } else { parentStack.peek().xmlElement.setAttribute(newAttribute); } } else { // the attribute doesn't hang directly off the entity // we must find its parent in the existing doc or fill static // content as appropriate // first get the parent element for this model attribute, and // gets its xpath XPathExpression<Element> expression = XPathFactory.instance().compile(templateDocElement.xpath, Filters.element()); List<Element> matches = expression.evaluate(templateDoc.getRootElement()); if (matches.size() != 0) { templateParentElement = matches.get(0).getParentElement(); } else { // throw an exception, we should always find the element in // the template } // now look for parent elements in the generated xml until we // find one // or we hit the entity itself boolean parentFound = false; do { templateParentXPath = XPathHelper.getRelativePath(entityDocElement.xmlElement, templateParentElement); expression = XPathFactory.instance().compile(templateParentXPath, Filters.element()); matches = expression.evaluate(parentStack.peek().xmlElement); if (matches.size() == 0) { Element elementToAdd = templateParentElement.clone(); elementToAdd.removeContent(); removeAllAttributes(elementToAdd); parentsToAdd.push(elementToAdd); templateParentElement = templateParentElement.getParentElement(); } else { parentFound = true; } } while (parentFound == false); // add every parent we couldn't find up to the entity level Element elementToAddTo = matches.get(0); while (!parentsToAdd.isEmpty()) { elementToAddTo.addContent(0, parentsToAdd.peek()); elementToAddTo = parentsToAdd.pop(); } // add our model attribute to the latest level if (newElement != null) { applyAttributeXPath(generatedXml, templateDocElement.xpath, value); } else { elementToAddTo.setAttribute(newAttribute); } } } restoreNamespaces(templateDoc, templateNamespaces); restoreNamespaces(generatedXml, generatedDocNamespaces); }
From source file:org.jumpmind.metl.core.runtime.component.XmlFormatter.java
License:Open Source License
private void applyAttributeXPath(Document generatedXml, String xpath, String value) { List<Object> matches = XPathFactory.instance().compile(xpath).evaluate(generatedXml.getRootElement()); if (matches.size() == 0) { log(LogLevel.WARN, "XPath expression " + xpath + " did not find any matches"); return;//from w w w . j av a 2s.c o m } Object object = matches.get(0); if (object instanceof Element) { Element element = (Element) object; if (value != null) { element.setText(value.toString()); } else { if (nullHandling.equals(NULL_HANDLING_REMOVE)) { Element parent = element.getParentElement(); parent.removeContent(element); } else if (nullHandling.equalsIgnoreCase(NULL_HANDLING_XML_NIL)) { element.setAttribute("nil", "true", getXmlNamespace()); } } } else if (object instanceof Attribute) { Attribute attribute = (Attribute) object; if (value != null) { attribute.setValue(value); } } }
From source file:org.jumpmind.metl.core.runtime.component.XmlFormatter.java
License:Open Source License
private Map<String, DocElement> fillAttributeDetails(Document templateDoc) { Map<String, DocElement> attributeLevels = new HashMap<String, DocElement>(); Map<Element, Namespace> namespaces = removeNamespaces(templateDoc); for (ComponentAttributeSetting compAttributeSetting : getComponent().getAttributeSettings()) { if (compAttributeSetting.getName().equals(XML_FORMATTER_XPATH)) { XPathExpression<Object> expression = XPathFactory.instance() .compile(compAttributeSetting.getValue()); List<Object> matches = expression.evaluate(templateDoc.getRootElement()); if (matches.size() == 0) { log(LogLevel.WARN,/*from ww w .j a v a 2 s . c om*/ "XPath expression " + compAttributeSetting.getValue() + " did not find any matches"); } else { if (matches.get(0) instanceof Element) { Element element = (Element) matches.get(0); // a model attribute could never be the root element of // the doc int level = 1; Element elementToMatch = element.getParentElement(); while (!elementToMatch.getName().equalsIgnoreCase(templateDoc.getRootElement().getName())) { elementToMatch = elementToMatch.getParentElement(); level++; } attributeLevels.put(compAttributeSetting.getAttributeId(), new DocElement(level, element, null, compAttributeSetting.getValue())); } if (matches.get(0) instanceof Attribute) { Attribute attribute = (Attribute) matches.get(0); int level = 1; Element elementToMatch = attribute.getParent(); while (!elementToMatch.getName().equalsIgnoreCase(templateDoc.getRootElement().getName())) { elementToMatch = elementToMatch.getParentElement(); level++; } attributeLevels.put(compAttributeSetting.getAttributeId(), new DocElement(level, null, attribute, compAttributeSetting.getValue())); } } } } restoreNamespaces(templateDoc, namespaces); return attributeLevels; }
From source file:org.jumpmind.metl.core.runtime.component.XmlFormatter.java
License:Open Source License
private Map<String, DocElement> fillEntityDetails(Document templateDoc) { Map<String, DocElement> entityLevels = new HashMap<String, DocElement>(); Map<Element, Namespace> namespaces = removeNamespaces(templateDoc); for (ComponentEntitySetting compEntitySetting : getComponent().getEntitySettings()) { if (compEntitySetting.getName().equals(XML_FORMATTER_XPATH)) { XPathExpression<Element> expression = XPathFactory.instance().compile(compEntitySetting.getValue(), Filters.element());//ww w . j a v a2 s. c o m List<Element> matches = expression.evaluate(templateDoc.getRootElement()); if (matches.size() == 0) { log(LogLevel.WARN, "XPath expression " + compEntitySetting.getValue() + " did not find any matches"); } else { int level = 1; Element element = matches.get(0); if (!element.isRootElement()) { Element elementToMatch = element.getParentElement(); while (!elementToMatch.getName().equalsIgnoreCase(templateDoc.getRootElement().getName())) { elementToMatch = elementToMatch.getParentElement(); level++; } } entityLevels.put(compEntitySetting.getEntityId(), new DocElement(level, element, null, compEntitySetting.getValue())); } } } restoreNamespaces(templateDoc, namespaces); return entityLevels; }
From source file:org.jumpmind.metl.core.runtime.component.XmlParser.java
License:Open Source License
@Override protected void start() { super.start(); TypedProperties properties = getTypedProperties(); optimizeForSpeed = properties.is("optimize.for.speed"); rowsPerMessage = properties.getInt(ROWS_PER_MESSAGE); Model model = getComponent().getOutputModel(); if (model == null) { throw new IllegalStateException("The output model must be defined"); }/*from ww w .j av a2s . c o m*/ List<XmlFormatterEntitySetting> entitySettingsForPath; Component component = getComponent(); for (ComponentEntitySetting compEntitySetting : component.getEntitySettings()) { if (compEntitySetting.getName().equals(XML_FORMATTER_XPATH)) { XPathExpression<?> expression = XPathFactory.instance().compile(compEntitySetting.getValue()); XmlFormatterEntitySetting entitySetting = new XmlFormatterEntitySetting(compEntitySetting, expression); entitySettingsForPath = entitySettingsByPath.get(compEntitySetting.getValue()); if (entitySettingsForPath == null) { entitySettingsForPath = new ArrayList<XmlParser.XmlFormatterEntitySetting>(); entitySettingsByPath.put(compEntitySetting.getValue(), entitySettingsForPath); } entitySettingsForPath.add(entitySetting); entitySettings.add(entitySetting); List<ComponentAttributeSetting> attributeSettings = component .getAttributeSettingsFor(entitySetting.getSetting().getEntityId()); for (ComponentAttributeSetting componentAttributeSetting : attributeSettings) { if (componentAttributeSetting.getName().equals(XML_FORMATTER_XPATH)) { expression = XPathFactory.instance().compile(componentAttributeSetting.getValue()); entitySetting.getAttributeSettings() .add(new XmlFormatterAttributeSetting(componentAttributeSetting, expression)); } } } } if (entitySettings.size() == 0) { throw new MisconfiguredException("At least one XPATH setting must be provided."); } }
From source file:org.kisst.cordys.caas.util.XmlNode.java
License:Open Source License
/** * This method executes the XPath on the current element. * //from ww w . j a va2 s . c o m * @param xpath The XPath to execute. * @return The list of elements that match the given XPath. */ public List<XmlNode> xpath(String xpath, String[][] namespaces) { List<XmlNode> retVal = new ArrayList<XmlNode>(); try { // Parse the name spaces List<Namespace> ns = new ArrayList<Namespace>(); if (namespaces != null) { for (String[] nsDetails : namespaces) { ns.add(Namespace.getNamespace(nsDetails[0], nsDetails[1])); } } // Create the XPath XPathExpression<Element> p = XPathFactory.instance().compile(xpath, Filters.element(), null, ns); // Execute the xpath List<Element> nodes = p.evaluate(element); // Build up the response for (Object node : nodes) { retVal.add(new XmlNode((Element) node)); } } catch (Exception e) { e.printStackTrace(); } return retVal; }