Class Query

java.lang.Object
org.libxmq.Query
Direct Known Subclasses:
QueryNetwork, QueryScientific

public class Query extends Object
The Query class implemets the basic querying methods.
  • Constructor Details

    • Query

      public Query(Node node)
      Build a new query from a DOM node.
      Parameters:
      node - The DOM node from which the paths start when querying.
  • Method Details

    • node

      public Node node()
      Return the node from which the query starts.
      Returns:
      A DOM node.
    • getXPathExpression

      protected XPathExpression getXPathExpression(String xpath) throws XPathExpressionException
      Fetch a potentially cached xpath expression.
      Parameters:
      xpath - The xpath to retrieve as an expression.
      Returns:
      The compiled xpath expression.
      Throws:
      XPathExpressionException - if the xpath is invalid.
    • forEach

      public int forEach(String xpath, NodeCallback cb)
      Perform a callback for each node matching the xpath expression.
      Parameters:
      xpath - An xpath, for example: //book
      cb - The callback function.
      Returns:
      The number of nodes matched.
    • expect

      public void expect(String xpath, NodeCallback cb) throws NotFoundException
      Perform a single callback for a single node matching the xpath expression.
      Parameters:
      xpath - An xpath that is assumed to match only a single , for example: /library
      cb - The callback function.
      Throws:
      NotFoundException - if the expected xpath was not found.
    • element

      public Element element(String xpath) throws NotFoundException, TooManyException
      Fetches a single element from the xpath.
      Parameters:
      xpath - An xpath that is assumed to match only a single , for example: /library
      Returns:
      The found element.
      Throws:
      NotFoundException - if the expected xpath was not found.
      TooManyException - if more than one element matched the xpath.
    • optionalElement

      public Optional<Element> optionalElement(String xpath) throws TooManyException
      Fetches a single optional element from the xpath.
      Parameters:
      xpath - An xpath that is assumed to match only a single , for example: /library
      Returns:
      The found element.
      Throws:
      TooManyException - if more than one element matched the xpath.
    • getBoolean

      public boolean getBoolean(String xpath) throws DecodingException, NotFoundException, TooManyException
      Get a boolean from an xpath location. Two strings are valid 'true' and 'false'. Anything else will throw a DecodingException.
      Parameters:
      xpath - An xpath that is used to find the boolean.
      Returns:
      The boolean.
      Throws:
      NotFoundException - if the expected xpath was not found.
      DecodingException - if the value was not 'true' or 'false'.
      TooManyException - if more than one element matched the xpath.
    • getOptionalBoolean

      public Optional<Boolean> getOptionalBoolean(String xpath) throws DecodingException, TooManyException
      Get an optional boolean from an xpath location. Two strings are valid 'true' and 'false'. Anything else will throw a DecodingException.
      Parameters:
      xpath - An xpath that finds the desired boolean.
      Returns:
      The optional boolean.
      Throws:
      DecodingException - if the value was not 'true' or 'false'.
      TooManyException - if more than one element matched the xpath.
    • getDouble

      public double getDouble(String xpath, String restriction) throws DecodingException, NotFoundException, TooManyException
      Get a 64 bit IEEE double floating point value from an xpath location and check that it complies with the restrictions.
      Parameters:
      xpath - Fetch the value found using this xpath.
      restriction - The float can for example be restricted in range.
      Returns:
      A double value.
      Throws:
      NotFoundException - if the expected xpath was not found.
      DecodingException - if the value was not an integer or if it failed the restriction.
      TooManyException - if more than one element matched the xpath.
    • getOptionalDouble

      public Optional<Double> getOptionalDouble(String xpath, String restriction) throws DecodingException, TooManyException
      Get an optional 64 bit IEEE double floating point value from an xpath location and check that it complies with the restrictions.
      Parameters:
      xpath - Fetch the value found using this xpath.
      restriction - The float can for example be restricted in range.
      Returns:
      A double value.
      Throws:
      DecodingException - if the value was not an integer or if it failed the restriction.
      TooManyException - if more than one element matched the xpath.
    • getFloat

      public float getFloat(String xpath, String restriction) throws DecodingException, NotFoundException, TooManyException
      Get a 32 bit IEEE double floating point value from an xpath location and check that it complies with the restrictions.
      Parameters:
      xpath - Fetch the value found using this xpath.
      restriction - The float can for example be restricted in range.
      Returns:
      The float found.
      Throws:
      NotFoundException - if the expected xpath was not found.
      DecodingException - if the value was not an integer or if it failed the restriction.
      TooManyException - if more than one element matched the xpath.
    • getOptionalFloat

      public Optional<Float> getOptionalFloat(String xpath, String restriction) throws DecodingException, TooManyException
      Get an optional 32 bit IEEE double floating point value from an xpath location and check that it complies with the restrictions.
      Parameters:
      xpath - Fetch the value found using this xpath.
      restriction - The float can for example be restricted in range.
      Returns:
      The float found.
      Throws:
      DecodingException - if the value was not an integer or if it failed the restriction.
      TooManyException - if more than one element matched the xpath.
    • getInt

      public int getInt(String xpath, String restriction) throws DecodingException, NotFoundException, TooManyException
      Get a 32 bit signed integer from an xpath location and check that it complies with the restriction.
      Parameters:
      xpath - Fetch the value found using this xpath.
      restriction - The float can for example be restricted in range.
      Returns:
      The integer.
      Throws:
      NotFoundException - if the expected xpath was not found.
      DecodingException - if the value was not an integer or if it failed the restriction.
      TooManyException - if more than one element matched the xpath.
    • getOptionalInt

      public Optional<Integer> getOptionalInt(String xpath, String restriction) throws DecodingException, TooManyException
      Get an optional 32 bit signed integer from an xpath location and check that it complies with the restriction.
      Parameters:
      xpath - Fetch the value found using this xpath.
      restriction - The float can for example be restricted in range.
      Returns:
      The integer.
      Throws:
      DecodingException - if the value was not an integer or if it failed the restriction.
      TooManyException - if more than one element matched the xpath.
    • getLong

      public long getLong(String xpath, String restriction) throws DecodingException, NotFoundException, TooManyException
      Get a 64 bit signed integer from an xpath location that complies with any restriction.
      Parameters:
      xpath - Fetch the value found using this xpath.
      restriction - The float can for example be restricted in range.
      Returns:
      The found long.
      Throws:
      NotFoundException - if the expected xpath was not found.
      DecodingException - if the value was not a long integer or if it failed the restriction.
      TooManyException - if more than one element matched the xpath.
    • getOptionalLong

      public Optional<Long> getOptionalLong(String xpath, String restriction) throws DecodingException, TooManyException
      Get an optional 64 bit signed integer from an xpath location that complies with any restriction.
      Parameters:
      xpath - Fetch the value found using this xpath.
      restriction - The float can for example be restricted in range.
      Returns:
      The found long.
      Throws:
      DecodingException - if the value was not a long integer or if it failed the restriction.
      TooManyException - if more than one element matched the xpath.
    • getString

      public String getString(String xpath, String restriction) throws DecodingException, NotFoundException, TooManyException
      Get a string from an xpath location that complies with a restriction.
      Parameters:
      xpath - Fetch the value found using this xpath.
      restriction - The float can for example be restricted in range.
      Returns:
      The found string.
      Throws:
      NotFoundException - if the expected xpath was not found.
      DecodingException - if the value failed the restriction.
      TooManyException - if more than one element matched the xpath.
    • getOptionalString

      public Optional<String> getOptionalString(String xpath, String restriction) throws DecodingException, TooManyException
      Get an optional string from an xpath location that complies with a restriction.
      Parameters:
      xpath - Fetch the value found using this xpath.
      restriction - The float can for example be restricted in range.
      Returns:
      The found string.
      Throws:
      DecodingException - if the value failed the restriction.
      TooManyException - if more than one element matched the xpath.
    • query

      public Query query(String xpath) throws NotFoundException, TooManyException
      Fetches a single element and wraps it inside a query from the xpath.
      Parameters:
      xpath - An xpath that is assumed to match only a single , for example: /library
      Returns:
      Query element.
      Throws:
      NotFoundException - if the expected xpath was not found.
      TooManyException - if more than one element matched the xpath.