public final class Directives extends Object implements Iterable<Directive>
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.
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(String name)
Add node to all current nodes.
|
Directives |
addIf(String name)
Add node if it's absent.
|
Directives |
append(Iterable<Directive> dirs)
Append all directives.
|
Directives |
attr(String name,
String value)
Set attribute.
|
Directives |
cdata(String 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(String target,
String data)
Add processing instruction.
|
Directives |
pop()
Pop pointer to stack and replace current pointer with it.
|
Directives |
push()
Push current pointer to stack.
|
Directives |
remove()
Remove all current nodes and move cursor to their parents.
|
Directives |
set(String 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(String path)
Go to XPath.
|
Directives |
xset(String text)
Set text content.
|
public Directives()
public Directives(@NotNull(message="xembly script can\'t be NULL") String text) throws SyntaxException
text
- Xembly scriptSyntaxException
- If syntax is brokenpublic static Iterable<Directive> copyOf(@NotNull(message="node can\'t be NULL") 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(@NotNull(message="list of directives can\'t be NULL") Iterable<Directive> dirs)
dirs
- Directives to appendpublic Directives add(@NotNull(message="name can\'t be NULL") String name)
name
- Name of the node to addpublic <K,V> Directives add(@NotNull(message="map can\'t be NULL") 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(@NotNull(message="name can\'t be NULL") String name)
name
- Name of the node to addpublic Directives remove()
public Directives attr(@NotNull(message="attr name can\'t be NULL") String name, @NotNull(message="value can\'t be NULL") String 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(@NotNull(message="target can\'t be NULL") String target, @NotNull(message="data can\'t be NULL") String 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(@NotNull(message="content can\'t be NULL") String 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(@NotNull(message="content can\'t be NULL") String text)
text
- Text to setpublic Directives up()
public Directives xpath(@NotNull(message="xpath can\'t be NULL") String path)
path
- Path to go topublic Directives strict(@NotNull(message="number can\'t be NULL") int number)
number
- Number of expected nodespublic Directives push()
public Directives pop()
public Directives cdata(@NotNull(message="CDATA section can\'t be NULL") String 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–2015 Xembly. All rights reserved.