List of usage examples for org.apache.commons.jxpath JXPathContext iteratePointers
public abstract Iterator iteratePointers(String xpath);
From source file:org.onecmdb.core.utils.xpath.commands.AbstractPathCommand.java
public Iterator<Pointer> getPathPointers() { JXPathContext context = getXPathContext(); return ((Iterator<Pointer>) context.iteratePointers(getPath())); }
From source file:org.onecmdb.core.utils.xpath.commands.AbstractPathCommand.java
public Iterator<Pointer> getRelativePointers(Pointer p, String outputAttribute) { JXPathContext relContext = getXPathContext().getRelativeContext(p); return ((Iterator<Pointer>) relContext.iteratePointers(outputAttribute)); }
From source file:org.onecmdb.core.utils.xpath.commands.UpdateCommand.java
protected Object getValues(String outputAttribute) { // Parse if not already done parseInputAttributes();/*from w ww .j a v a 2s . co m*/ // Fetch the values strings, un parsed if expression. String value = attributeMap.get(outputAttribute); if (value == null) { return (null); } Object parsedValue = null; // Check if it's an expression if (value.startsWith("[")) { if (!value.endsWith("]")) { throw new IllegalArgumentException("ERROR: Value expression must be ended with a ']' "); } String path = value.substring(1); path = path.substring(0, path.length() - 1); JXPathContext context = getXPathContext(); Iterator<Pointer> pointers = (Iterator<Pointer>) context.iteratePointers(path); while (pointers.hasNext()) { Pointer pointer = pointers.next(); parsedValue = pointer.getValue(); // Migth warn here if there exists multiple. } } else { // No need to parse this, will be a String representaion of the value. parsedValue = value; } return (parsedValue); }
From source file:org.onecmdb.core.utils.xpath.generator.PropertyContentGenerator.java
public void transfer(OutputStream out) { log.debug("Debug Query path <" + cmd.getPath() + ">"); PrintWriter text = new PrintWriter(new OutputStreamWriter(out), false); Iterator<Pointer> iter = cmd.getPathPointers(); // Need to peek inside the iter. Pointer p = null;/*from w w w. ja va2 s . c o m*/ if (iter.hasNext()) { p = (Pointer) iter.next(); } boolean first = true; String[] outputAttributes = cmd.getOutputAttributeAsArray(); if (outputAttributes.length == 1 && outputAttributes[0].equals("*")) { // Expand all. if (p != null) { Object v = p.getValue(); if (v instanceof IDynamicHandler) { IDynamicHandler dynBean = (IDynamicHandler) v; outputAttributes = dynBean.getProperties(); } } } if (p != null) { do { if (p == null) { p = iter.next(); } Object rootObject = p.getValue(); if (!(rootObject instanceof InstanceContext || rootObject instanceof TemplateContext)) { throw new IllegalArgumentException( "Illegal path '" + cmd.getPath() + "'. Must point to a ICi."); } if (outputAttributes.length == 0) { outln(text, getPropertyName(p, null) + "=" + p.getValue().toString()); } else { for (int i = 0; i < outputAttributes.length; i++) { String outputAttribute = outputAttributes[i]; JXPathContext context = cmd.getRelativeContext(p); if (true) { Iterator iterPointer = context.iteratePointers(outputAttribute); int count = 0; String uniqueStr = null; while (iterPointer.hasNext()) { Pointer valuePointer = (Pointer) iterPointer.next(); Object o = valuePointer.getValue(); if (iterPointer.hasNext() || uniqueStr != null) { // More than one. uniqueStr = "[" + count + "]"; } count++; String property = getPropertyName(rootObject, outputAttribute); if (uniqueStr != null) { property = property + uniqueStr; } if (o instanceof List) { int index = 0; for (Object v : (List) o) { outln(text, property + "[" + index + "]=" + v.toString()); index++; } } else { outln(text, property + "=" + (o == null ? "" : o.toString())); } } } } } p = null; } while (iter.hasNext()); } text.flush(); }
From source file:org.opennms.protocols.json.collector.AbstractJsonCollectionHandler.java
/** * Fill collection set.//from w w w.j av a2 s .co m * * @param agent the agent * @param collectionSet the collection set * @param source the source * @param json the JSON Object * @throws ParseException the parse exception */ @SuppressWarnings("unchecked") protected void fillCollectionSet(CollectionAgent agent, XmlCollectionSet collectionSet, XmlSource source, JSONObject json) throws ParseException { XmlCollectionResource nodeResource = new XmlSingleInstanceCollectionResource(agent); JXPathContext context = JXPathContext.newContext(json); for (XmlGroup group : source.getXmlGroups()) { LOG.debug("fillCollectionSet: getting resources for XML group {} using XPATH {}", group.getName(), group.getResourceXpath()); Date timestamp = getTimeStamp(context, group); Iterator<Pointer> itr = context.iteratePointers(group.getResourceXpath()); while (itr.hasNext()) { JXPathContext relativeContext = context.getRelativeContext(itr.next()); String resourceName = getResourceName(relativeContext, group); LOG.debug("fillCollectionSet: processing XML resource {} of type {}", resourceName, group.getResourceType()); XmlCollectionResource collectionResource; if (group.getResourceType().equalsIgnoreCase(CollectionResource.RESOURCE_TYPE_NODE)) { collectionResource = nodeResource; } else { collectionResource = getCollectionResource(agent, resourceName, group.getResourceType(), timestamp); } LOG.debug("fillCollectionSet: processing resource {}", collectionResource); AttributeGroupType attribGroupType = new AttributeGroupType(group.getName(), group.getIfType()); for (XmlObject object : group.getXmlObjects()) { try { Object obj = relativeContext.getValue(object.getXpath()); if (obj != null) { String value = obj.toString(); XmlCollectionAttributeType attribType = new XmlCollectionAttributeType(object, attribGroupType); collectionResource.setAttributeValue(attribType, value); } } catch (JXPathException ex) { LOG.warn("Unable to get value for {}: {}", object.getXpath(), ex.getMessage()); } } processXmlResource(collectionResource, attribGroupType); collectionSet.getCollectionResources().add(collectionResource); } } }
From source file:org.opennms.protocols.json.collector.JsonCollectorSolarisZonesIT.java
/** * Test to verify XPath content.//from www . j a va 2 s . com * * @throws Exception the exception */ @Test @SuppressWarnings("unchecked") public void testXpath() throws Exception { JSONObject json = MockDocumentBuilder.getJSONDocument(); JXPathContext context = JXPathContext.newContext(json); Iterator<Pointer> itr = context.iteratePointers("/zones/zone"); while (itr.hasNext()) { Pointer resPtr = itr.next(); JXPathContext relativeContext = context.getRelativeContext(resPtr); String resourceName = (String) relativeContext.getValue("@name"); Assert.assertNotNull(resourceName); String value = (String) relativeContext.getValue("parameter[@key='nproc']/@value"); Assert.assertNotNull(Integer.valueOf(value)); } }
From source file:pt.webdetails.cdf.dd.datasources.CdaDataSourceReader.java
protected static List<CdaDataSource> getCdaDataSources(JXPathContext docContext) { ArrayList<CdaDataSource> dataSources = new ArrayList<CdaDataSource>(); //external//from w ww . ja v a2 s . co m @SuppressWarnings("unchecked") Iterator<Pointer> extDataSources = docContext .iteratePointers("/datasources/rows[properties/name='dataAccessId']"); while (extDataSources.hasNext()) { Pointer source = extDataSources.next(); if (!(source instanceof NullPointer)) { dataSources.add(new CdaDataSource(docContext.getRelativeContext(source))); } } @SuppressWarnings("unchecked") Iterator<Pointer> builtInDataSources = docContext.iteratePointers("/datasources/rows[meta='CDA']"); if (builtInDataSources.hasNext()) { //built-in String fileName = XPathUtils.getStringValue(docContext, "/filename"); if (StringUtils.endsWith(fileName, ".wcdf")) { fileName = StringUtils.replace(fileName, ".wcdf", ".cda"); } else { fileName = StringUtils.replace(fileName, ".cdfde", ".cda"); } //just add cda name dataSources.add(new CdaDataSource(fileName, null)); } return dataSources; }
From source file:pt.webdetails.cdf.dd.datasources.DataSourceReader.java
private static List<CdaDataSource> getCdaDataSources(JXPathContext docContext) { ArrayList<CdaDataSource> dataSources = new ArrayList<CdaDataSource>(); //external/* w w w .j ava 2 s.com*/ @SuppressWarnings("unchecked") Iterator<Pointer> extDataSources = docContext .iteratePointers("/datasources/rows[properties/name='dataAccessId']"); while (extDataSources.hasNext()) { Pointer source = extDataSources.next(); if (!(source instanceof NullPointer)) { dataSources.add(new CdaDataSource(docContext.getRelativeContext(source))); } } @SuppressWarnings("unchecked") Iterator<Pointer> builtInDataSources = docContext.iteratePointers("/datasources/rows[meta='CDA']"); if (builtInDataSources.hasNext()) { //built-in String fileName = XPathUtils.getStringValue(docContext, "/filename"); String toReplace = ".cdfde"; String replaceWith = ".cda"; if (StringUtils.endsWith(fileName, ".wcdf")) { toReplace = ".wcdf"; } fileName = StringUtils.replace(fileName, toReplace, replaceWith); //just add cda name dataSources.add(new CdaDataSource(fileName, null)); // // // while(builtInDataSources.hasNext()){ // Pointer source = builtInDataSources.next(); // if(!(source instanceof NullPointer)){ // dataSources.add(new CdaDataSource(docContext.getRelativeContext(source), fileName)); // } // } } return dataSources; }
From source file:pt.webdetails.cdf.dd.model.inst.reader.cdfdejs.CdfdeJsDashboardReader.java
public void read(Dashboard.Builder builder, CdfdeJsReadContext context, JXPathContext source, String sourcePath) throws ThingReadException { builder.setMeta(DashboardType.getInstance()); // 0. File path (sourcePath and filename should be the same - they may differ on non-canonicalization or solution // relative vs system absolute?) builder.setSourcePath(sourcePath); //XPathUtils.getStringValue(source, "/filename")); // 1. WCDF/* w w w .j av a 2 s. com*/ builder.setWcdf(context.getWcdf()); // 2. REGULAR readKind(builder, KnownThingKind.Component, source, source.iteratePointers("/components/rows"), context, sourcePath); // 3. DATASOURCE readKind(builder, KnownThingKind.Component, source, source.iteratePointers("/datasources/rows"), context, sourcePath); // 4. LAYOUT //JXPathContext layoutXP = source.getRelativeContext(source.getPointer("/layout")); // HACK: 'layout' key for getting the reader IThingReader reader; try { reader = context.getFactory().getReader(KnownThingKind.Component, "layout", null); // TOTO: HACK: Until layout is handled the right way, we need to detect // a null reader, returned when there is an error building the layout inside // the factory :-( if (reader == null) { return; } } catch (UnsupportedThingException ex) { logger.error("While rendering dashboard. " + ex); return; } LayoutComponent.Builder compBuilder = (LayoutComponent.Builder) reader.read(context, source, sourcePath); builder.addComponent(compBuilder); }
From source file:pt.webdetails.cdf.dd.model.meta.reader.datasources.DataSourcesModelReader.java
public void read(MetaModel.Builder model, JSONObject cdaDefs, String sourcePath) throws ThingReadException { assert model != null; logger.info("Loading data source components of '" + sourcePath + "'"); final JXPathContext doc; try {/*from www .ja v a 2s . c om*/ doc = JsonUtils.toJXPathContext(cdaDefs); } catch (JSONException e) { throw new ThingReadException("Couldn't get JXPathContext from json", e); } @SuppressWarnings("unchecked") Iterator<Pointer> pointers = doc.iteratePointers("*"); while (pointers.hasNext()) { Pointer pointer = pointers.next(); this.readDataSourceComponent(model, pointer, sourcePath); } }