Class XMQ

java.lang.Object
org.libxmq.XMQ

public class XMQ extends Object
The XMQ class is used to parse/print/render XMQ/XML/HTML/JSON documents.
  • Constructor Details

    • XMQ

      public XMQ()
      Construct an XMQ using the xmq DOM implementation.
  • Method Details

    • parseFile

      public XMQDoc parseFile(Path file, InputSettings is) throws IOException, ParseException
      Parse a file containing XMQ/XML/HTML/JSON/CLINES.
      Parameters:
      file - The file to parse.
      is - The input settings, should white space be trimmed, should text nodes be merged etc.
      Returns:
      A DOM document.
      Throws:
      IOException - if file cannot be read.
      ParseException - if the parse failed.
    • parseBuffer

      public XMQDoc parseBuffer(String buffer, InputSettings is) throws ParseException
      Parse a buffer containing XMQ/XML/HTML/JSON/CLINES.
      Parameters:
      buffer - The string to parse.
      is - The input settings, should white space be trimmed, should text nodes be merged etc.
      Returns:
      A DOM document.
      Throws:
      ParseException - if the parse failed.
    • queryFile

      public Query queryFile(Path file, InputSettings is) throws IOException, ParseException
      Parse a file containing XMQ/XML/HTML/JSON/CLINES and return a Query object to the DOM.
      Parameters:
      file - The file to parse.
      is - The input settings, should white space be trimmed, should text nodes be merged etc.
      Returns:
      A Query object.
      Throws:
      IOException - if file cannot be read.
      ParseException - if the parse failed.
    • queryBuffer

      public Query queryBuffer(String buffer, InputSettings is) throws ParseException
      Parse a buffer containing XMQ/XML/HTML/JSON/CLINES and return a Query object for the DOM.
      Parameters:
      buffer - The string to parse.
      is - The input settings, should white space be trimmed, should text nodes be merged etc.
      Returns:
      A Query object.
      Throws:
      ParseException - if the parse failed.
    • newDoc

      public XMQDoc newDoc()
      Create a new empty document, like xmqNewDoc.
      Returns:
      The document.
    • of

      public static XMQDoc of(String s) throws ParseException
      Create a document from an xmq buffer, like xmqOf in C: a newDoc and a parseBuffer in one call.
      Parameters:
      s - The xmq to parse.
      Returns:
      The document.
      Throws:
      ParseException - if the buffer cannot be parsed as xmq.
    • setDocType

      public void setDocType(XMQDoc doc, String name)
      Set the doctype of the document, eg html, like xmqSetDocType.
      Parameters:
      doc - The document.
      name - The doctype name.
    • addRootElement

      public XMQElement addRootElement(XMQDoc doc, String name, NS ns)
      Add the root element to the document, like xmqAddRootElement.
      Parameters:
      doc - The document.
      name - The element name.
      ns - The namespace, NS.NONE or NS.here(uri). NS.PARENT is the same as NS.NONE here.
      Returns:
      The root element.
      Throws:
      IllegalArgumentException - if the namespace is invalid.
    • addNamespace

      public void addNamespace(XMQElement element, NS ns)
      Declare a namespace with a prefix on an element, without using it for the element itself, like xmqAddNamespace. Use it in children with NS.ancestor(uri).
      Parameters:
      element - The element.
      ns - The namespace, NS.here("{prefix}uri"), the prefix is required.
      Throws:
      IllegalArgumentException - if the namespace is not NS.here with a prefix, or if the prefix is already declared on the element for another uri.
    • addElement

      public XMQElement addElement(XMQElement parent, String name, NS ns)
      Add an element as the last child of parent, like xmqAddElement.
      Parameters:
      parent - The parent element.
      name - The element name.
      ns - The namespace, NS.NONE, NS.PARENT, NS.here(uri) or NS.ancestor(uri).
      Returns:
      The new element.
      Throws:
      IllegalArgumentException - if the namespace is invalid.
    • addElementWithAttrs

      public XMQElement addElementWithAttrs(XMQElement parent, String name, NS ns, String... attrs)
      Add an element with attributes, like xmqAddElementWithAttrs.
      Parameters:
      parent - The parent element.
      name - The element name.
      ns - The namespace, see addElement.
      attrs - The attributes as name, value pairs, eg "href", "https://libxmq.org".
      Returns:
      The new element.
    • addKeyValue

      public XMQElement addKeyValue(XMQElement parent, String key, String value, NS ns)
      Add an element with a text value, key=value in xmq, like xmqAddKeyValue.
      Parameters:
      parent - The parent element.
      key - The element name.
      value - The text, an empty or null value gives an empty element.
      ns - The namespace, see addElement.
      Returns:
      The new element.
    • addKeyValueWithAttrs

      public XMQElement addKeyValueWithAttrs(XMQElement parent, String key, String value, NS ns, String... attrs)
      Add an element with a text value and attributes, like xmqAddKeyValueWithAttrs.
      Parameters:
      parent - The parent element.
      key - The element name.
      value - The text.
      ns - The namespace, see addElement.
      attrs - The attributes as name, value pairs.
      Returns:
      The new element.
    • compileIXML

      public IXML compileIXML(String grammar) throws ParseException
      Compile an ixml grammar, to parse inputs with parseBufferWithIXML. Like parsing the grammar with xmqParseBufferWithType and XMQ_CONTENT_IXML.
      Parameters:
      grammar - The ixml grammar source.
      Returns:
      The compiled grammar.
      Throws:
      ParseException - if the grammar has a syntax error.
    • parseBufferWithIXML

      public XMQDoc parseBufferWithIXML(String buffer, IXML ixml) throws ParseException
      Parse a buffer with an ixml grammar, like xmqParseBufferWithIXML.
      Parameters:
      buffer - The input to parse.
      ixml - The compiled grammar.
      Returns:
      The parse tree.
      Throws:
      ParseException - if the input is rejected by the grammar.
    • parseBufferWithIXML

      public XMQDoc parseBufferWithIXML(byte[] buffer, IXML ixml) throws ParseException
      Parse an UTF-8 buffer with an ixml grammar, like xmqParseBufferWithIXML. The Earley parser decodes the code points directly from the bytes.
      Parameters:
      buffer - The input to parse, UTF-8.
      ixml - The compiled grammar.
      Returns:
      The parse tree.
      Throws:
      ParseException - if the input is rejected by the grammar, or is not valid UTF-8.
    • compactQuote

      public String compactQuote(String content)
      Quote a value as xmq on a single line, eg 'car go' or ('howdy'
'dowdy'), like xmqCompactQuote.
      Parameters:
      content - The value.
      Returns:
      The quoted value.
    • toXMQ

      public String toXMQ(XMQNode doc, OutputSettings os)
      Print the document as XMQ.
      Parameters:
      doc - Document to print.
      os - Settings for printing.
      Returns:
      A string with XMQ.
    • toXML

      public String toXML(XMQNode doc, OutputSettings os)
      Print the document as XML. This means that the argument doc is assumed to store an XML document. By printing it as XML the document will follow the XML rules.
      Parameters:
      doc - Document to print.
      os - Settings for printing.
      Returns:
      A string with XML.
    • toHTML

      public String toHTML(XMQDoc doc, OutputSettings os)
      Print the document as HTML. This means that the argument doc is assumed to store a HTML document. By printing it as HTML the document will follow the HTML rules (using self closing tags like <br> and potentially using HTML entities like &nbsp;)
      Parameters:
      doc - Document to print.
      os - Settings for printing.
      Returns:
      A string with HTML.
    • toJSON

      public String toJSON(XMQDoc doc, OutputSettings os)
      Print the document as json.
      Parameters:
      doc - Document to print.
      os - Settings for printing.
      Returns:
      A string with JSON.
    • toCLINES

      public String toCLINES(XMQDoc doc, OutputSettings os)
      Print the document as clines.
      Parameters:
      doc - Document to print.
      os - Settings for printing.
      Returns:
      A string with clines.
    • renderHTML

      public String renderHTML(XMQDoc doc, OutputSettings os)
      Render the document as XMQ in HTML. I.e. build a new HTML document that when viewed in a browser makes it easy to read the XMQ.
      Parameters:
      doc - Document to render
      os - Settings for rendering
      Returns:
      A string with HTML.
    • renderTEX

      public String renderTEX(XMQDoc doc, OutputSettings os)
      Render the document as XMQ in TeX. When the TeX document is rendered using for example xetex/xelatex, it will generate a pdf containing the human readable XMQ.
      Parameters:
      doc - Document to render
      os - Settings for rendering
      Returns:
      A string with TeX commands.
    • printException

      public static String printException(ParseException e, Locale locale)
      Render a locale adapted text cli error printout of a parse exception.
      Parameters:
      e - The parse exception.
      locale - The locale to use.
      Returns:
      A string to be printed on the console.
    • printException

      public static String printException(ParseException e)
      Render a locale adapted text cli error printout of a parse exception using the default locale.
      Parameters:
      e - The parse exception.
      Returns:
      A string to be printed on the console.