package symplik.flower.sample; //~--- non-JDK imports -------------------------------------------------------- import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.xpath.XPath; import symplik.flower.Answer; import symplik.flower.Helper; import symplik.flower.Question; //~--- JDK imports ------------------------------------------------------------ import java.io.IOException; import java.io.InputStream; public class TestResult extends Question { private String explain = null; private String result = null; private int totalScore = 0; @Override public boolean enterQuestion() { totalScore = Integer.valueOf(Answer.getInstance().getB("TOTAL_SCORE")); try { InputStream is = this.getClass().getResourceAsStream("/symplik/flower/sample/Test.xml"); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(is); String xpathStr = "//Result[number(@scoreFrom) <= " + totalScore + " and number(@scoreTo) > " + totalScore + "]"; XPath xpath = XPath.newInstance(xpathStr); result = ((Element) xpath.selectSingleNode(doc)).getValue(); xpath = XPath.newInstance("//Results/Suggestion"); explain = ((Element) xpath.selectSingleNode(doc)).getValue(); } catch (JDOMException je) { Helper.log(LOG_ERROR, "Malformat XML file"); System.exit(1); } catch (IOException ioe) { Helper.log(LOG_ERROR, "Unable to read question XML file"); System.exit(1); } return true; } @Override public String getQuestion() { return "Your score is " + totalScore + "\n" + result; } @Override public String getExplanation() { return explain; } }
TestResult.java