Skip to content

Commit

Permalink
Added support for XPath. #80
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilfb committed Jun 20, 2016
1 parent b0f616e commit 2ca5fff
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -377,6 +383,7 @@ public TableCell<ChartSeriesProperties, String> call(
final TableCell<ChartSeriesProperties, String> cell = new TableCell<ChartSeriesProperties, String>()
{
@Override
@SuppressWarnings("unchecked")
public void updateItem(final String item, boolean empty)
{
super.updateItem(item, empty);
Expand Down Expand Up @@ -423,16 +430,6 @@ else if(ChartSeriesStatusEnum.OK.equals(properties.getSeriesStatus()))

expressionColumn.setCellValueFactory(new PropertyValueFactory<ChartSeriesProperties, String>("valueExpression"));
expressionColumn.setCellFactory(TextFieldTableCell.<ChartSeriesProperties>forTableColumn());
expressionColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<ChartSeriesProperties, String>>()
{
@Override
public void handle(CellEditEvent<ChartSeriesProperties, String> event)
{
final String newValue = event.getNewValue();

// TODO
}
});

visibleColumn.setCellValueFactory(new PropertyValueFactory<ChartSeriesProperties, Boolean>("visible"));
visibleColumn.setCellFactory(new Callback<TableColumn<ChartSeriesProperties, Boolean>, TableCell<ChartSeriesProperties, Boolean>>()
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -810,19 +802,17 @@ else if (ChartSeriesTypeEnum.PAYLOAD_JSON.equals(seriesProperties.typeProperty()
final Double value = Double.valueOf(jsonValue);
return new XYChart.Data<Number, Number>(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<Number, Number>(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<Number, Number>(message.getDate().getTime(), value);
}
else if (ChartSeriesTypeEnum.SIZE.equals(seriesProperties.typeProperty().get()))
{
final Integer value = Integer.valueOf(message.getPayload().length());
Expand All @@ -838,10 +828,6 @@ else if (ChartSeriesTypeEnum.SIZE.equals(seriesProperties.typeProperty().get()))

private void addMessageToSeries(final ChartSeriesProperties seriesProperties, final FormattedMessage message)
{
//final List<ChartSeriesProperties> topicSeries = topicToSeries.get(message.getTopic());

//for (final ChartSeriesProperties seriesProperties : topicSeries)
//{
final Series<Number, Number> series = seriesIdToSeriesData.get(seriesProperties.getId());

if (series != null)
Expand All @@ -866,7 +852,6 @@ private void addMessageToSeries(final ChartSeriesProperties seriesProperties, fi
}
}
}
//}
}

@FXML
Expand Down
12 changes: 6 additions & 6 deletions spy-common-ui/src/main/resources/ui/fxml/EditChartSeries.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="221.0" prefWidth="590.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pl.baczkowicz.spy.ui.controllers.EditChartSeriesController">
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="221.0" prefWidth="590.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.66" fx:controller="pl.baczkowicz.spy.ui.controllers.EditChartSeriesController">
<children>

<Label layoutX="23.0" layoutY="21.0" text="Series name" />
<TextField fx:id="nameText" layoutX="260.0" layoutY="16.0" prefHeight="26.0" prefWidth="316.0" />
<TextField fx:id="nameText" layoutX="260.0" layoutY="16.0" prefHeight="26.0" prefWidth="316.0" AnchorPane.leftAnchor="260.0" AnchorPane.rightAnchor="14.0" />

<Label layoutX="24.0" layoutY="62.0" text="Topic" />
<TextField fx:id="topicText" layoutX="260.0" layoutY="57.0" prefHeight="26.0" prefWidth="316.0" AnchorPane.leftAnchor="260.0" AnchorPane.rightAnchor="14.0" />

<Label layoutX="25.0" layoutY="103.0" text="Type" />
<ComboBox fx:id="typeCombo" layoutX="260.0" layoutY="98.0" prefHeight="26.0" prefWidth="316.0" AnchorPane.rightAnchor="14.0" />
<ComboBox fx:id="typeCombo" layoutX="260.0" layoutY="98.0" prefHeight="26.0" prefWidth="316.0" AnchorPane.leftAnchor="260.0" AnchorPane.rightAnchor="14.0" />

<Label layoutX="24.0" layoutY="144.0" text="Value expression" />
<TextField fx:id="expressionText" layoutX="260.0" layoutY="139.0" prefHeight="26.0" prefWidth="316.0" />
<TextField fx:id="expressionText" layoutX="260.0" layoutY="139.0" prefHeight="26.0" prefWidth="316.0" AnchorPane.leftAnchor="260.0" AnchorPane.rightAnchor="14.0" />

<Button fx:id="saveButton" focusTraversable="false" layoutX="356.0" layoutY="181.0" mnemonicParsing="false" onAction="#save" prefHeight="25.0" prefWidth="102.0" text="Save">
<Button fx:id="saveButton" focusTraversable="false" layoutX="356.0" layoutY="181.0" mnemonicParsing="false" onAction="#save" prefHeight="26.0" prefWidth="102.0" text="Save" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="130.0">
<padding>
<Insets bottom="1.0" left="1.0" right="1.0" top="1.0" />
</padding>
Expand All @@ -59,6 +59,6 @@
</graphic>
</Button>

<Button fx:id="cancelButton" cancelButton="true" focusTraversable="false" layoutX="192.0" layoutY="317.0" prefHeight="25.0" mnemonicParsing="false" onAction="#cancel" prefWidth="102.0" text="Cancel" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="14.0" />
<Button fx:id="cancelButton" cancelButton="true" focusTraversable="false" layoutX="192.0" layoutY="317.0" mnemonicParsing="false" onAction="#cancel" prefHeight="26.0" prefWidth="102.0" text="Cancel" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="14.0" />
</children>
</AnchorPane>
47 changes: 47 additions & 0 deletions spy-common-ui/src/test/java/pl/baczkowicz/spy/xpath/XPathTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package pl.baczkowicz.spy.xpath;

import java.io.StringReader;

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.junit.Test;
import org.xml.sax.InputSource;

import junit.framework.TestCase;

public class XPathTest extends TestCase
{

@Test
public void test()
{
final String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +

"<book category=\"COOKING\"> <year>2005</year> <price>30.00</price> </book>";

assertEquals(evaluateXPath("/book/price", message), 30.0);
}

protected Double evaluateXPath(final String expression, final String message)
{
Double value = 0.0;
final XPath xpath = XPathFactory.newInstance().newXPath();
final InputSource inputSource = new InputSource(new StringReader(message));

try
{
final XPathExpression exp = xpath.compile(expression);
value = (Double) exp.evaluate(inputSource, XPathConstants.NUMBER);
}
catch (XPathExpressionException e)
{
// TODO
}

return value;
}
}

0 comments on commit 2ca5fff

Please sign in to comment.