List of usage examples for org.apache.commons.jxpath Pointer getValue
Object getValue();
From source file:org.chiba.xml.xforms.xpath.test.ExtensionFunctionsTest.java
public void testInstance() throws Exception { Document inDocument = getXmlResource("instance-test.xml"); ChibaBean chibaBean = new ChibaBean(); chibaBean.setXMLContainer(inDocument); chibaBean.init();/*from w w w . j a v a 2 s .c o m*/ // XPathExtensionFunctions functions = new XPathExtensionFunctions(chibaBean.getContainer().getDefaultModel()); XPathExtensionFunctions functions = new XPathExtensionFunctions(); functions.setNamespaceContext(chibaBean.getContainer().getDefaultModel().getDefaultInstance().getElement()); JXPathContext context = chibaBean.getContainer().getDefaultModel().getDefaultInstance() .getInstanceContext(); context.setFunctions(functions); Object o = context.getValue("instance('first')/some-dummy"); assertTrue(o.equals("some dummy value")); o = context.getValue("xforms:instance('first')/some-dummy"); assertTrue(o.equals("some dummy value")); o = context.getValue("instance('second')/."); assertTrue(o.equals("another dummy value")); Pointer pointer = context.getPointer("instance('second')/."); assertEquals("another dummy value", pointer.getValue()); assertEquals("/another-dummy[1]", pointer.asPath()); o = context.getValue("xforms:instance('second')/."); assertTrue(o.equals("another dummy value")); }
From source file:org.chiba.xml.xforms.xpath.test.JXPathTest.java
/** * Tests calculation with a relative context. * * @throws Exception if any error occurred during the test. *///from www . j a va2 s .co m public void testRelativeCalculation() throws Exception { Pointer rootPointer = this.context.getPointer("//my:item[2]/my:discount"); JXPathContext relativeContext = this.context.getRelativeContext(rootPointer); Pointer relativePointer = relativeContext.getPointer("string(count(../../my:item))"); assertEquals("2", relativePointer.getValue()); }
From source file:org.chiba.xml.xforms.xpath.test.JXPathTest.java
/** * Tests relevance with a relative context. * * @throws Exception if any error occurred during the test. */// w w w . jav a2s . c o m public void testRelativeRelevance() throws Exception { Pointer rootPointer1 = this.context.getPointer("//my:item[1]/my:discount"); JXPathContext relativeContext1 = this.context.getRelativeContext(rootPointer1); Pointer relativePointer1 = relativeContext1.getPointer("boolean(../my:amount > 100)"); assertEquals(Boolean.TRUE, relativePointer1.getValue()); Pointer rootPointer2 = this.context.getPointer("//my:item[2]/my:discount"); JXPathContext relativeContext2 = this.context.getRelativeContext(rootPointer2); Pointer relativePointer2 = relativeContext2.getPointer("boolean(../my:amount > 100)"); assertEquals(Boolean.FALSE, relativePointer2.getValue()); Pointer rootPointer3 = this.context.getPointer("//my:item[2]/my:discount"); JXPathContext relativeContext3 = this.context.getRelativeContext(rootPointer3); Pointer relativePointer3 = relativeContext3.getPointer("boolean(../my:non-existing-node > 100)"); assertEquals(Boolean.FALSE, relativePointer3.getValue()); }
From source file:org.dcm4chee.xds2.registry.ws.XDSPersistenceWrapper.java
/** * Correct metadata parameters to conform to XDS specification. * Replace non-conformant ids to uuids, update references, check classification scemes, nodes, etc *///from www.ja v a 2 s. c o m void checkAndCorrectSubmitObjectsRequest(SubmitObjectsRequest req) throws XDSException { // TODO: DB_RESTRUCT - CODE INSPECTION - is it correct to replace ids with uuids for ALL identifiables, not only ROs? JXPathContext requestContext = JXPathContext.newContext(req); ////// Pre-process - move detached classifications from registryObjectList into corresponding objects Iterator<JAXBElement<? extends IdentifiableType>> objectListIterator = req.getRegistryObjectList() .getIdentifiable().iterator(); while (objectListIterator.hasNext()) { JAXBElement<? extends IdentifiableType> elem = objectListIterator.next(); /// filter Classifications only if (!ClassificationType.class.isAssignableFrom(elem.getValue().getClass())) continue; /// find referenced object and add the classification to the referenced object ClassificationType cl = (ClassificationType) elem.getValue(); // this xpath return all nodes in the tree with the specified id Iterator referencedObjs = (Iterator) requestContext .iteratePointers(String.format("//*[id = '%s']", cl.getClassifiedObject())); try { Object o = ((Pointer) referencedObjs.next()).getValue(); if (!RegistryObjectType.class.isAssignableFrom(o.getClass())) throw new XDSException(XDSException.XDS_ERR_REGISTRY_METADATA_ERROR, "Classification " + cl.getId() + " classifies object " + cl.getClassifiedObject() + " which is not a Registry Object", null); RegistryObjectType registryObj = (RegistryObjectType) o; // add this classification to the classification list registryObj.getClassification().add(cl); } catch (NoSuchElementException e) { throw new XDSException(XDSException.XDS_ERR_REGISTRY_METADATA_ERROR, "Classified object " + cl.getClassifiedObject() + " not found in the request (Classification " + cl.getId() + " )", null); } // there must be a single node with referenced id if (referencedObjs.hasNext()) throw new XDSException( XDSException.XDS_ERR_REGISTRY_METADATA_ERROR, "Classification " + cl.getId() + " references an object " + cl.getClassifiedObject() + " that is not unique", null); /// remove the detached classification from the list objectListIterator.remove(); } ////// First run - replace non-uuid IDs with UUIDs for all identifiables, included nested ones // Use //id xpath to find all id fields of identifiables in the request Iterator ids = (Iterator) requestContext.iteratePointers("//id"); while (ids.hasNext()) { Pointer p = (Pointer) ids.next(); String oldId = (String) p.getValue(); String newIdUUID = oldId; if (oldId == null) continue; // Replace non-UUID id with a generated UUID if (!oldId.startsWith("urn:")) { newIdUUID = "urn:uuid:" + UUID.randomUUID().toString(); p.setValue(newIdUUID); log.debug("Replacing id {} with uuid {}", oldId, newIdUUID); } newUUIDs.put(oldId, newIdUUID); } ////// Second run - perform check and correction recursively for (JAXBElement<? extends IdentifiableType> elem : req.getRegistryObjectList().getIdentifiable()) { // filter RegistryObjects only if (!RegistryObjectType.class.isAssignableFrom(elem.getValue().getClass())) continue; RegistryObjectType ro = (RegistryObjectType) elem.getValue(); checkAndCorrectMetadata(ro); } }
From source file:org.firesoa.common.jxpath.JXPathTestCase.java
protected void assertXPathCreatePath(JXPathContext ctx, String xpath, Object expectedValue, String expectedPath) {//w w w . j av a 2 s . c o m Pointer pointer = ctx.createPath(xpath); assertEquals("Creating path <" + xpath + ">", expectedPath, pointer.asPath()); assertEquals("Creating path (pointer value) <" + xpath + ">", expectedValue, pointer.getValue()); assertEquals("Creating path (context value) <" + xpath + ">", expectedValue, ctx.getValue(pointer.asPath())); }
From source file:org.firesoa.common.jxpath.JXPathTestCase.java
protected void assertXPathCreatePathAndSetValue(JXPathContext ctx, String xpath, Object value, String expectedPath) {// www . java 2 s . c o m Pointer pointer = ctx.createPathAndSetValue(xpath, value); assertEquals("Creating path <" + xpath + ">", expectedPath, pointer.asPath()); assertEquals("Creating path (pointer value) <" + xpath + ">", value, pointer.getValue()); assertEquals("Creating path (context value) <" + xpath + ">", value, ctx.getValue(pointer.asPath())); }
From source file:org.firesoa.common.jxpath.XMLModelTestCase.java
public void testNodes() { Pointer pointer = context.getPointer("/vendor[1]/contact[1]"); assertFalse(pointer.getNode().equals(pointer.getValue())); }
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. * /* w ww.j a v a2 s. co m*/ * @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();//from w ww .j a va2 s . com 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.onecmdb.core.utils.xpath.commands.DeleteCommand.java
/** * Transfer the content to the stream.//from w ww.ja v a2s . c om * Execute the update. * * @param out */ public void transfer(OutputStream out) { setupTX(); Iterator<Pointer> iter = getPathPointers(); while (iter.hasNext()) { Pointer p = (Pointer) iter.next(); Object value = p.getValue(); if (value instanceof ICmdbObjectDestruction) { ((ICmdbObjectDestruction) value).destory(); } else { throw new IllegalArgumentException("Not a valid item to delete"); } } // Process the TX. processTX(); }