public final class Directives extends Object implements Iterable<Directive>
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.
| Constructor and Description |
|---|
Directives()
Public ctor.
|
Directives(Iterable<Directive> dirs)
Public ctor.
|
Directives(String text)
Public ctor.
|
| Modifier and Type | Method and Description |
|---|---|
<K,V> Directives |
add(Map<K,V> nodes)
Add multiple nodes and set their text values.
|
Directives |
add(Object name)
Add node to all current nodes.
|
Directives |
addIf(Object name)
Add node if it's absent.
|
Directives |
append(Iterable<Directive> dirs)
Append all directives.
|
Directives |
attr(Object name,
Object value)
Set attribute.
|
Directives |
cdata(Object text)
Set CDATA section.
|
static Iterable<Directive> |
copyOf(Node node)
Create a collection of directives, which can create a copy
of provided node.
|
Iterator<Directive> |
iterator() |
Directives |
pi(Object target,
Object data)
Add processing instruction.
|
Directives |
pop()
Pop cursor to stack and replace current cursor with it.
|
Directives |
push()
Push current cursor to stack.
|
Directives |
remove()
Remove all current nodes and move cursor to their parents.
|
Directives |
set(Object text)
Set text content.
|
Directives |
strict(int number)
Check that there is exactly this number of current nodes.
|
String |
toString() |
Directives |
up()
Go one node/level up.
|
Directives |
xpath(Object path)
Go to XPath.
|
Directives |
xset(Object text)
Set text content.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitforEach, spliteratorpublic Directives()
public Directives(String text) throws SyntaxException
text - Xembly scriptSyntaxException - If syntax is brokenpublic static Iterable<Directive> copyOf(Node 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>"
);
node - Node to analyzepublic Directives append(Iterable<Directive> dirs)
dirs - Directives to appendpublic Directives add(Object name)
name - Name of the node to addpublic <K,V> Directives add(Map<K,V> nodes)
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).
K - Type of keyV - Type of valuenodes - Names and values of nodes to addpublic Directives addIf(Object name)
name - Name of the node to addpublic Directives remove()
public Directives attr(Object name, Object value)
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).
name - Name of the attributevalue - Value to setpublic Directives pi(Object target, Object data)
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).
target - PI namedata - Data to setpublic Directives set(Object text)
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).
text - Text to setpublic Directives xset(Object text)
text - Text to setpublic Directives up()
public Directives xpath(Object path)
path - Path to go topublic Directives strict(int number)
number - Number of expected nodespublic Directives push()
public Directives pop()
public Directives cdata(Object text)
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).
text - Text to setCopyright © 2013–2018 Xembly. All rights reserved.