List of usage examples for org.apache.commons.jxpath JXPathContext getPointer
public abstract Pointer getPointer(String xpath);
From source file:org.firesoa.common.jxpath.XMLModelTestCase.java
protected void assertXMLSignature(JXPathContext context, String path, String signature, boolean elements, boolean attributes, boolean text, boolean pi) { Object node = context.getPointer(path).getNode(); String sig = getXMLSignature(node, elements, attributes, text, pi); assertEquals("XML Signature mismatch: ", signature, sig); }
From source file:org.firesoa.common.jxpath.XMLModelTestCase.java
public void testID() { context.setIdentityManager(new IdentityManager() { public Pointer getPointerByID(JXPathContext context, String id) { NodePointer ptr = (NodePointer) context.getPointer("/"); ptr = ptr.getValuePointer(); // Unwrap the container return ptr.getPointerByID(context, id); }/* ww w . j av a 2 s. c om*/ }); assertXPathValueAndPointer(context, "id(101)//street", "Tangerine Drive", "id('101')/address[1]/street[1]"); assertXPathPointerLenient(context, "id(105)/address/street", "id(105)/address/street"); }
From source file:org.mitre.ptmatchadapter.format.SimplePatientCsvFormat.java
/** * Returns supported Patient properties as a string of comma-separated values. * Values of String fields are enclosed by double-quotes. * //from w w w.j a va2 s . c om * @param patient * the patient resource to serialize to a CSV string * @return * comma-delimited values of fields associated with the Patient */ public String toCsv(Patient patient) { final StringBuilder sb = new StringBuilder(INITIAL_ROW_LENGTH); JXPathContext patientCtx = JXPathContext.newContext(patient); try { // resource id (logical id only) sb.append(patient.getIdElement().getIdPart()); } catch (NullPointerException e) { // check for null by exception since it is unexpected. // Privacy concern keeps me from logging anything about this patient. LOG.error("Patient has null identifier element. This is unexpected!"); } sb.append(COMMA); // identifiers of interest for (String sysName : identifierSystems) { Pointer ptr = patientCtx.getPointer("identifier[system='" + sysName + "']"); Identifier id = (Identifier) ptr.getValue(); if (id != null) { sb.append(id.getValue()); } sb.append(COMMA); } // Extract Name Parts of interest for (String use : nameUses) { Pointer ptr; if (use.isEmpty()) { ptr = patientCtx.getPointer("name[not(use)]"); } else { ptr = patientCtx.getPointer("name[use='" + use + "']"); } HumanName name = (HumanName) ptr.getValue(); if (name != null) { JXPathContext nameCtx = JXPathContext.newContext(ptr.getValue()); for (String part : nameParts) { sb.append(DOUBLE_QUOTE); if (TEXT_NAME_PART.equals(part)) { Object val = nameCtx.getValue(part); if (val != null) { sb.append(val.toString()); } } else { // other supported parts return lists of string types Object namePart = nameCtx.getValue(part); if (namePart instanceof List<?>) { List<StringType> partList = (List<StringType>) namePart; if (partList.size() > 0) { sb.append(partList.get(0).getValue()); } } } sb.append(DOUBLE_QUOTE); sb.append(COMMA); } } else { // add blank sections for the name parts for (int i = 0; i < nameParts.length; i++) { sb.append(COMMA); } } } // Gender sb.append(patient.getGender().toString()); sb.append(COMMA); // Date of Birth Date dob = patient.getBirthDate(); if (dob != null) { sb.append(dateFormat.format(dob)); } sb.append(COMMA); sb.append(buidContactInfo(patient)); return sb.toString(); }
From source file:org.mitre.ptmatchadapter.format.SimplePatientCsvFormatTest.java
@Test public void testToCsv() { final SimplePatientCsvFormat fmt = new SimplePatientCsvFormat(); Patient patient = newPatient();/*w ww.j a v a 2 s . c om*/ JXPathContext patientCtx = JXPathContext.newContext(patient); Pointer ptr = patientCtx.getPointer("identifier[system='SSN']"); assertNotNull(ptr); Identifier id = (Identifier) ptr.getValue(); if (id != null) { assertNotNull("ssn", id.getValue()); } Object obj1 = ptr.getNode(); ptr = patientCtx.getPointer("name[use='official']"); assertNotNull(ptr); Object obj2 = ptr.getValue(); ptr = patientCtx.getPointer("name[not(use)]"); assertNotNull(ptr); obj2 = ptr.getValue(); String str = fmt.toCsv(patient); LOG.info("CSV: {}", str); assertTrue(str.length() > 0); // Ensure the literal, null doesn't appear assertTrue(!str.contains("null")); // Ensure there is no sign of array delimiters assertTrue(!str.contains("[")); // Ensure line doesn't end with a comma assertTrue(!str.endsWith(",")); assertTrue(str.contains("Smith")); List<ContactPoint> matches = ContactPointUtil.find(patient.getTelecom(), ContactPointSystem.PHONE, ContactPointUse.WORK); assertNotNull(matches); assertNotNull(matches.get(0)); }
From source file:org.openvpms.component.business.domain.im.archetype.descriptor.NodeDescriptor.java
/** * Derive the node value for the specified {@link NodeDescriptor}. If the * node does not support derived value or the value cannot be derived then * raise an exception./*from w ww. ja v a 2s . co m*/ * * @param imobj the {@link IMObject} * @throws FailedToDeriveValueException if the node is not declared to support dervied value or * the value cannot be derived correctly. */ public void deriveValue(IMObject imobj) { if (!isDerived()) { throw new FailedToDeriveValueException(FailedToDeriveValueException.ErrorCode.DerivedValueUnsupported, new Object[] { getName() }); } // attempt to derive the value try { JXPathContext context = JXPathHelper.newContext(imobj); Object value = context.getValue(this.getDerivedValue()); context.getPointer(this.getPath()).setValue(value); } catch (Exception exception) { throw new FailedToDeriveValueException(FailedToDeriveValueException.ErrorCode.FailedToDeriveValue, new Object[] { getName(), getPath(), getDerivedValue() }, exception); } }
From source file:org.openvpms.component.business.service.archetype.ArchetypeService.java
/** * Iterate through the {@link NodeDescriptor}s and set all derived values. * Do it recursively./* w w w . ja v a 2 s.co m*/ * * @param context the context object * @param nodes a list of node descriptors * @throws ArchetypeServiceException */ private void deriveValues(JXPathContext context, Map<String, NodeDescriptor> nodes) { for (NodeDescriptor node : nodes.values()) { if (node.isDerived()) { try { Object value = context.getValue(node.getDerivedValue()); context.getPointer(node.getPath()).setValue(value); } catch (Exception exception) { throw new ArchetypeServiceException(ArchetypeServiceException.ErrorCode.FailedToDeriveValue, exception, node.getName(), node.getPath()); } } // if this node contains other nodes then make a recursive call if (node.getNodeDescriptors().size() > 0) { deriveValues(context, node.getNodeDescriptors()); } } }
From source file:org.openvpms.component.business.service.archetype.IMObjectValidator.java
/** * Validates a node.//from w w w . jav a2 s .c om * * @param parent the parent object * @param context holds the object to be validated * @param archetype the archetype descriptor * @param node the node to validate * @param errors the list to add validation errors to */ protected void validateNode(IMObject parent, JXPathContext context, ArchetypeDescriptor archetype, NodeDescriptor node, List<ValidationError> errors) { Object value; try { value = node.getValue(context); } catch (Exception exception) { addError(errors, parent, node, "Failed to get value"); log.error("Failed to get value for " + node.getName(), exception); return; } // first check whether the value for this node is derived and if it // is then set the derived value if (node.isDerived()) { try { context.getPointer(node.getPath()).setValue(value); } catch (Exception exception) { addError(errors, parent, node, "Cannot derive value"); log.error("Failed to derive value for " + node.getName(), exception); return; } } if (node.isCollection()) { checkCollection(parent, node, value, errors); } else { checkSimpleValue(parent, node, value, errors); } if (value != null) { // only check the assertions for non-null values. Null values are handled by minCardinality checks. checkAssertions(parent, node, value, errors); } // validate any child nodes if (!node.getNodeDescriptors().isEmpty()) { validateNodes(parent, context, archetype, node.getNodeDescriptors(), errors); } }
From source file:org.talend.esb.sam.common.filter.impl.JxPathFilter.java
@Override public boolean filter(Event event) { JXPathContext context = JXPathContext.newContext(event); Pointer pointer = context.getPointer(expression); return (Boolean) pointer.getValue(); }
From source file:org.xchain.framework.jxpath.JXPathContextTest.java
@Test public void testRelativeContext() throws Exception { Node root = new Node("root"); Node child = new Node("child"); root.getChild().add(child);/* www . j a v a 2 s . c o m*/ JXPathContext rootContext = JXPathContext.newContext(root); JXPathContext childContext = rootContext.getRelativeContext(rootContext.getPointer("/child")); String rootName = (String) childContext.getValue("/name"); assertEquals("The root node has the wrong name.", "root", rootName); String childName = (String) childContext.getValue("name"); assertEquals("The context node has the wrong name.", "child", childName); }