Class Directives
Directive
s, 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 Summary
ConstructorDescriptionPublic ctor.Directives
(Iterable<Directive> dirs) Public ctor.Directives
(String text) Public ctor. -
Method Summary
Modifier and TypeMethodDescriptionAdd node to all current nodes.<K,
V> Directives Add multiple nodes and set their text values.Add node if it's absent.Append all directives.Appends thenode
.Set attribute.Set CDATA section.Add an XML comment.Create a collection of directives, which can create a copy of provided node.iterator()
Add processing instruction.pop()
Pop cursor to stack and replace current cursor with it.push()
Push current cursor to stack.remove()
Remove all current nodes and move cursor to their parents.Set text content.strict
(int number) Check that there is exactly this number of current nodes.toString()
up()
Go one node/level up.Set attribute.Go to XPath.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
-
Constructor Details
-
Directives
public Directives()Public ctor. -
Directives
Public ctor.- Parameters:
text
- Xembly script
-
Directives
Public ctor.- Parameters:
dirs
- Directives
-
-
Method Details
-
toString
-
iterator
-
copyOf
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
Append all directives.- Parameters:
dirs
- Directives to append- Returns:
- This object
- Since:
- 0.11
-
append
Appends thenode
.- Parameters:
node
- The node to append- Returns:
- This object
- Since:
- 0.23
- See Also:
-
add
Add node to all current nodes.- Parameters:
name
- Name of the node to add- Returns:
- This object
- Since:
- 0.5
-
add
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
Add node if it's absent.- Parameters:
name
- Name of the node to add- Returns:
- This object
- Since:
- 0.5
-
remove
Remove all current nodes and move cursor to their parents.- Returns:
- This object
- Since:
- 0.5
-
attr
Set attribute.If it's necessary to add an attribute with a namespace, append the namespace to the name of the attribute, separating them with a space.
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
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
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
Set text content.- Parameters:
text
- Text to set- Returns:
- This object
- Since:
- 0.7
-
xattr
Set attribute.- Parameters:
attr
- Attribute nametext
- Text to set- Returns:
- This object
- Since:
- 0.28
-
up
Go one node/level up.- Returns:
- This object
- Since:
- 0.5
- Suppressed Checkstyle violations:
- MethodName (3 lines)
-
xpath
Go to XPath.- Parameters:
path
- Path to go to- Returns:
- This object
- Since:
- 0.5
-
strict
Check that there is exactly this number of current nodes.- Parameters:
number
- Number of expected nodes- Returns:
- This object
- Since:
- 0.5
-
push
Push current cursor to stack.- Returns:
- This object
- Since:
- 0.16
-
pop
Pop cursor to stack and replace current cursor with it.- Returns:
- This object
- Since:
- 0.16
-
cdata
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
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
-