Package org.xembly

Class Directives

  • All Implemented Interfaces:
    Iterable<Directive>

    public final class Directives
    extends Object
    implements Iterable<Directive>
    Collection of Directives, instantiable from String.

    For example, to fetch directives from a string and apply to the DOM document:

     Document dom = DocumentBuilderFactory.newInstance()
       .newDocumentBuilder().newDocument();
     dom.appendChild(dom.createElement("root"));
     new Xembler(
       new Directives("XPATH 'root'; ADD 'employee';")
     ).apply(dom);

    Directives can be used as a builder of Xembly script:

     Document dom = DocumentBuilderFactory.newInstance()
       .newDocumentBuilder().newDocument();
     dom.appendChild(dom.createElement("root"));
     new Xembler(
       new Directives()
         .xpath("/root")
         .addIf("employees")
         .add("employee")
         .attr("id", 6564)
         .up()
         .xpath("employee[@id='100']")
         .strict(1)
         .remove()
     ).apply(dom);

    The class is mutable and thread-safe.

    Since:
    0.1
    Suppressed Checkstyle violations:
    ClassFanOutComplexity (500 lines)
    • Constructor Detail

      • Directives

        public Directives()
        Public ctor.
      • Directives

        public Directives​(String text)
        Public ctor.
        Parameters:
        text - Xembly script
      • Directives

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

      • copyOf

        public static Iterable<Directive> copyOf​(Node node)
        Create a collection of directives, which can create a copy of provided node.

        For example, you already have a node in an XML document, which you'd like to add to another XML document:

         Document target = parse("<root/>");
         Node node = parse("<user name='Jeffrey'/>");
         new Xembler(
           new Directives()
             .xpath("/*")
             .add("jeff")
             .append(Directives.copyOf(node))
         ).apply(target);
         assert print(target).equals(
           "<root><jeff name='Jeffrey'></root>"
         );
         
        Parameters:
        node - Node to analyze
        Returns:
        Collection of directives
        Since:
        0.13
        Suppressed Checkstyle violations:
        CyclomaticComplexity (50 lines)
      • append

        public Directives append​(Iterable<Directive> dirs)
        Append all directives.
        Parameters:
        dirs - Directives to append
        Returns:
        This object
        Since:
        0.11
      • add

        public Directives add​(Object name)
        Add node to all current nodes.
        Parameters:
        name - Name of the node to add
        Returns:
        This object
        Since:
        0.5
      • add

        public <K,​V> Directives add​(Map<K,​V> nodes)
        Add multiple nodes and set their text values.

        Every pair in the provided map will be treated as a new node name and value. It's a convenient utility method that simplifies the process of adding a collection of nodes with pre-set values. For example:

         new Directives()
           .add("first", "hello, world!")
           .add(
             new ArrayMap<String, Object>()
               .with("alpha", 1)
               .with("beta", "2")
               .with("gamma", new Date())
           )
           .add("second");
         

        If a value provided contains illegal XML characters, a runtime exception will be thrown. To avoid this, it is recommended to use Xembler.escape(String).

        Type Parameters:
        K - Type of key
        V - Type of value
        Parameters:
        nodes - Names and values of nodes to add
        Returns:
        This object
        Since:
        0.8
      • addIf

        public Directives addIf​(Object name)
        Add node if it's absent.
        Parameters:
        name - Name of the node to add
        Returns:
        This object
        Since:
        0.5
      • remove

        public Directives remove()
        Remove all current nodes and move cursor to their parents.
        Returns:
        This object
        Since:
        0.5
      • attr

        public Directives attr​(Object name,
                               Object value)
        Set attribute.

        If a value provided contains illegal XML characters, a runtime exception will be thrown. To avoid this, it is recommended to use Xembler.escape(String).

        Parameters:
        name - Name of the attribute
        value - Value to set
        Returns:
        This object
        Since:
        0.5
      • pi

        public Directives pi​(Object target,
                             Object data)
        Add processing instruction.

        If a value provided contains illegal XML characters, a runtime exception will be thrown. To avoid this, it is recommended to use Xembler.escape(String).

        Parameters:
        target - PI name
        data - Data to set
        Returns:
        This object
        Since:
        0.9
        Suppressed Checkstyle violations:
        MethodName (3 lines)
      • set

        public Directives set​(Object text)
        Set text content.

        If a value provided contains illegal XML characters, a runtime exception will be thrown. To avoid this, it is recommended to use Xembler.escape(String).

        Parameters:
        text - Text to set
        Returns:
        This object
        Since:
        0.5
      • xset

        public Directives xset​(Object text)
        Set text content.
        Parameters:
        text - Text to set
        Returns:
        This object
        Since:
        0.7
      • xattr

        public Directives xattr​(Object attr,
                                Object text)
        Set attribute.
        Parameters:
        attr - Attribute name
        text - Text to set
        Returns:
        This object
        Since:
        0.28
      • up

        public Directives up()
        Go one node/level up.
        Returns:
        This object
        Since:
        0.5
        Suppressed Checkstyle violations:
        MethodName (3 lines)
      • xpath

        public Directives xpath​(Object path)
        Go to XPath.
        Parameters:
        path - Path to go to
        Returns:
        This object
        Since:
        0.5
      • strict

        public Directives strict​(int number)
        Check that there is exactly this number of current nodes.
        Parameters:
        number - Number of expected nodes
        Returns:
        This object
        Since:
        0.5
      • push

        public Directives push()
        Push current cursor to stack.
        Returns:
        This object
        Since:
        0.16
      • pop

        public Directives pop()
        Pop cursor to stack and replace current cursor with it.
        Returns:
        This object
        Since:
        0.16
      • cdata

        public Directives cdata​(Object text)
        Set CDATA section.

        If a value provided contains illegal XML characters, a runtime exception will be thrown. To avoid this, it is recommended to use Xembler.escape(String).

        Parameters:
        text - Text to set
        Returns:
        This object
        Since:
        0.17
      • comment

        public Directives comment​(Object text)
        Add an XML comment.

        If a value provided contains illegal XML characters, a runtime exception will be thrown. To avoid this, it is recommended to use Xembler.escape(String).

        Parameters:
        text - Text to set
        Returns:
        This object
        Since:
        0.23