Package org.xembly
Class Xembler
- java.lang.Object
-
- org.xembly.Xembler
-
public final class Xembler extends Object
Processor of Xembly directives, main entry point to the package.For example, to modify a DOM document:
Document dom = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); dom.appendChild(dom.createElement("root")); new Xembler( new Directives() .xpath("/root") .addIfAbsent("employees") .add("employee") .attr("id", 6564) ).apply(dom);You can also convert your Xembly directives directly to XML document:
String xml = new Xembler( new Directives() .xpath("/root") .addIfAbsent("employees") .add("employee") .attr("id", 6564) ).xml("root");Since version 0.18 you can convert directives to XML without a necessity to catch checked exceptions. Use
*Quietly()methods for that:xmlQuietly(),domQuietly(), andapplyQuietly(Node).- Since:
- 0.1
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Nodeapply(Node dom)Apply all changes to the document/node.NodeapplyQuietly(Node dom)Apply all changes to the document/node, without any checked exceptions.Documentdom()Apply all changes to an empty DOM.DocumentdomQuietly()Apply all changes to an empty DOM, without checked exceptions.static Stringescape(String text)Utility method to escape text before using it as a text value in XML.Stringxml()Convert to XML document.StringxmlQuietly()Convert to XML document, without checked exceptions.
-
-
-
Constructor Detail
-
Xembler
public Xembler(Iterable<Directive> directives, Transformers transformers)
Public ctor.- Parameters:
directives- Directivestransformers- Transformers
-
-
Method Detail
-
applyQuietly
public Node applyQuietly(Node dom)
Apply all changes to the document/node, without any checked exceptions.- Parameters:
dom- DOM document/node- Returns:
- The same document/node
- Since:
- 0.18
-
apply
public Node apply(Node dom) throws ImpossibleModificationException
Apply all changes to the document/node.- Parameters:
dom- DOM document/node- Returns:
- The same document/node
- Throws:
ImpossibleModificationException- If can't modify
-
domQuietly
public Document domQuietly()
Apply all changes to an empty DOM, without checked exceptions.- Returns:
- DOM created
- Since:
- 0.18
-
dom
public Document dom() throws ImpossibleModificationException
Apply all changes to an empty DOM.- Returns:
- DOM created
- Throws:
ImpossibleModificationException- If can't modify- Since:
- 0.9
-
xmlQuietly
public String xmlQuietly()
Convert to XML document, without checked exceptions.- Returns:
- XML document
- Since:
- 0.18
-
xml
public String xml() throws ImpossibleModificationException
Convert to XML document.- Returns:
- XML document
- Throws:
ImpossibleModificationException- If can't modify- Since:
- 0.9
-
escape
public static String escape(String text)
Utility method to escape text before using it as a text value in XML.Use it like this, in order to avoid runtime exceptions:
new Directives().xpath("/test") .set(Xembler.escape("illegal: "));- Parameters:
text- Text to escape- Returns:
- The same text with escaped characters, which are not XML-legal
- Since:
- 0.14
- Suppressed Checkstyle violations:
- CyclomaticComplexity (20 lines), BooleanExpressionComplexity (20 lines)
-
-