From 2ca5fff61b0217cc593697fcaa09552c9ca2aa49 Mon Sep 17 00:00:00 2001 From: Kamil Baczkowicz Date: Tue, 21 Jun 2016 00:12:03 +0100 Subject: [PATCH] Added support for XPath. #80 --- .../controllers/LineChartPaneController.java | 51 +++++++------------ .../resources/ui/fxml/EditChartSeries.fxml | 12 ++--- .../pl/baczkowicz/spy/xpath/XPathTest.java | 47 +++++++++++++++++ 3 files changed, 71 insertions(+), 39 deletions(-) create mode 100644 spy-common-ui/src/test/java/pl/baczkowicz/spy/xpath/XPathTest.java diff --git a/spy-common-ui/src/main/java/pl/baczkowicz/spy/ui/controllers/LineChartPaneController.java b/spy-common-ui/src/main/java/pl/baczkowicz/spy/ui/controllers/LineChartPaneController.java index a2952337..ea8e47b2 100644 --- a/spy-common-ui/src/main/java/pl/baczkowicz/spy/ui/controllers/LineChartPaneController.java +++ b/spy-common-ui/src/main/java/pl/baczkowicz/spy/ui/controllers/LineChartPaneController.java @@ -25,6 +25,7 @@ import java.io.File; import java.io.IOException; +import java.io.StringReader; import java.net.URL; import java.util.ArrayList; import java.util.Collection; @@ -84,13 +85,18 @@ import javafx.util.StringConverter; import javax.imageio.ImageIO; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; import org.gillius.jfxutils.chart.ChartPanManager; import org.gillius.jfxutils.chart.JFXChartUtil; import org.gillius.jfxutils.chart.StableTicksAxis; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.xml.sax.InputSource; import pl.baczkowicz.spy.eventbus.IKBus; import pl.baczkowicz.spy.messages.FormattedMessage; @@ -377,6 +383,7 @@ public TableCell call( final TableCell cell = new TableCell() { @Override + @SuppressWarnings("unchecked") public void updateItem(final String item, boolean empty) { super.updateItem(item, empty); @@ -423,16 +430,6 @@ else if(ChartSeriesStatusEnum.OK.equals(properties.getSeriesStatus())) expressionColumn.setCellValueFactory(new PropertyValueFactory("valueExpression")); expressionColumn.setCellFactory(TextFieldTableCell.forTableColumn()); - expressionColumn.setOnEditCommit(new EventHandler>() - { - @Override - public void handle(CellEditEvent event) - { - final String newValue = event.getNewValue(); - - // TODO - } - }); visibleColumn.setCellValueFactory(new PropertyValueFactory("visible")); visibleColumn.setCellFactory(new Callback, TableCell>() @@ -624,11 +621,6 @@ public MessageLimitProperties fromString(String id) autoRefreshCheckBox.setSelected(lastAutoRefresh); displaySymbolsCheckBox.setSelected(lastDisplaySymbols); } -// else if (ChartMode.STATS.equals(chartMode)) -// { -// showRangeBox.setVisible(false); -// showRangeLabel.setVisible(false); -// } eventBus.subscribeWithFilterOnly(this, this::onMessageAdded, MessageAddedEvent.class, store.getMessageList()); @@ -810,19 +802,17 @@ else if (ChartSeriesTypeEnum.PAYLOAD_JSON.equals(seriesProperties.typeProperty() final Double value = Double.valueOf(jsonValue); return new XYChart.Data(message.getDate().getTime(), value); } -// else if (ChartSeriesTypeEnum.PAYLOAD_XML.equals(seriesProperties.typeProperty().get())) -// { -// final XPath xpath = XPathFactory.newInstance().newXPath(); -// -// final String xpathValue = String.valueOf( -// xpath.evaluate(seriesProperties.valueExpressionProperty().get(), -// IOUtils.toInputStream(message.getFormattedPayload(), "UTF-8"), XPathConstants.NUMBER)); -// -// logger.debug("XPath value = {}", xpathValue); -// -// final Double value = Double.valueOf(xpathValue); -// return new XYChart.Data(message.getDate().getTime(), value); -// } + else if (ChartSeriesTypeEnum.PAYLOAD_XML.equals(seriesProperties.typeProperty().get())) + { + final XPath xpath = XPathFactory.newInstance().newXPath(); + + final XPathExpression exp = xpath.compile(seriesProperties.valueExpressionProperty().get()); + double value = (Double) exp.evaluate(new InputSource(new StringReader(message.getFormattedPayload())), XPathConstants.NUMBER); + + logger.debug("XPath value = {}", value); + + return new XYChart.Data(message.getDate().getTime(), value); + } else if (ChartSeriesTypeEnum.SIZE.equals(seriesProperties.typeProperty().get())) { final Integer value = Integer.valueOf(message.getPayload().length()); @@ -838,10 +828,6 @@ else if (ChartSeriesTypeEnum.SIZE.equals(seriesProperties.typeProperty().get())) private void addMessageToSeries(final ChartSeriesProperties seriesProperties, final FormattedMessage message) { - //final List topicSeries = topicToSeries.get(message.getTopic()); - - //for (final ChartSeriesProperties seriesProperties : topicSeries) - //{ final Series series = seriesIdToSeriesData.get(seriesProperties.getId()); if (series != null) @@ -866,7 +852,6 @@ private void addMessageToSeries(final ChartSeriesProperties seriesProperties, fi } } } - //} } @FXML diff --git a/spy-common-ui/src/main/resources/ui/fxml/EditChartSeries.fxml b/spy-common-ui/src/main/resources/ui/fxml/EditChartSeries.fxml index 5b266f08..57822858 100644 --- a/spy-common-ui/src/main/resources/ui/fxml/EditChartSeries.fxml +++ b/spy-common-ui/src/main/resources/ui/fxml/EditChartSeries.fxml @@ -31,22 +31,22 @@ - +