Class Directives
- java.lang.Object
-
- org.xembly.Directives
-
public final class Directives extends Object implements Iterable<Directive>
Collection ofDirectives, instantiable fromString.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);Directivescan 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 Summary
Constructors Constructor Description Directives()Public ctor.Directives(Iterable<Directive> dirs)Public ctor.Directives(String text)Public ctor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Directivesadd(Object name)Add node to all current nodes.<K,V>
Directivesadd(Map<K,V> nodes)Add multiple nodes and set their text values.DirectivesaddIf(Object name)Add node if it's absent.Directivesappend(Iterable<Directive> dirs)Append all directives.Directivesappend(Node node)Appends thenode.Directivesattr(Object name, Object value)Set attribute.Directivescdata(Object text)Set CDATA section.Directivescomment(Object text)Add an XML comment.static Iterable<Directive>copyOf(Node node)Create a collection of directives, which can create a copy of provided node.Iterator<Directive>iterator()Directivespi(Object target, Object data)Add processing instruction.Directivespop()Pop cursor to stack and replace current cursor with it.Directivespush()Push current cursor to stack.Directivesremove()Remove all current nodes and move cursor to their parents.Directivesset(Object text)Set text content.Directivesstrict(int number)Check that there is exactly this number of current nodes.StringtoString()Directivesup()Go one node/level up.Directivesxattr(Object attr, Object text)Set attribute.Directivesxpath(Object path)Go to XPath.Directivesxset(Object text)Set text content.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
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
-
append
public Directives append(Node node)
Appends thenode.- Parameters:
node- The node to append- Returns:
- This object
- Since:
- 0.23
- See Also:
append(Iterable),copyOf(Node)
-
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 keyV- 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 attributevalue- 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 namedata- 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 nametext- 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
-
-