Package org.xembly

Class Xembler


  • public final class Xembler
    extends Object
    Processor of Xembly directives, main entry point to the package.

    For example, to modify a DOM document:

     Document dom = DocumentBuilderFactory.newInstance()
       .newDocumentBuilder().newDocument();
     dom.appendChild(dom.createElement("root"));
     new Xembler(
       new Directives()
         .xpath("/root")
         .addIfAbsent("employees")
         .add("employee")
         .attr("id", 6564)
     ).apply(dom);

    You can also convert your Xembly directives directly to XML document:

     String xml = new Xembler(
       new Directives()
         .xpath("/root")
         .addIfAbsent("employees")
         .add("employee")
         .attr("id", 6564)
     ).xml("root");

    Since version 0.18 you can convert directives to XML without a necessity to catch checked exceptions. Use *Quietly() methods for that: xmlQuietly(), domQuietly(), and applyQuietly(Node).

    Since:
    0.1
    • Constructor Detail

      • Xembler

        public Xembler​(Iterable<Directive> dirs)
        Public ctor.
        Parameters:
        dirs - Directives
      • Xembler

        public Xembler​(Iterable<Directive> directives,
                       Transformers transformers)
        Public ctor.
        Parameters:
        directives - Directives
        transformers - Transformers
    • Method Detail

      • applyQuietly

        public Node applyQuietly​(Node dom)
        Apply all changes to the document/node, without any checked exceptions.
        Parameters:
        dom - DOM document/node
        Returns:
        The same document/node
        Since:
        0.18
      • domQuietly

        public Document domQuietly()
        Apply all changes to an empty DOM, without checked exceptions.
        Returns:
        DOM created
        Since:
        0.18
      • xmlQuietly

        public String xmlQuietly()
        Convert to XML document, without checked exceptions.
        Returns:
        XML document
        Since:
        0.18
      • escape

        public static String escape​(String text)
        Utility method to escape text before using it as a text value in XML.

        Use it like this, in order to avoid runtime exceptions:

        new Directives().xpath("/test")
           .set(Xembler.escape("illegal: "));
        Parameters:
        text - Text to escape
        Returns:
        The same text with escaped characters, which are not XML-legal
        Since:
        0.14
        Suppressed Checkstyle violations:
        CyclomaticComplexity (20 lines), BooleanExpressionComplexity (20 lines)