public class Element
extends java.lang.Object
implements java.io.Serializable
NetconfSession class.
Example:
// start (Netconf) sessions towards our device
SSHConnection connection = new SSHConnection("127.0.0.1");
connection.authenticateWithPassword("admin", "pass");
SSHSession ssh = new SSHSession(connection);
NetconfSession session = new NetconfSession(ssh);
// get system configuration from session
Element sysConfig = session.getConfig("/system").first();
// manipulate the element tree
sysConfig.setValue("dns", "83.100.1.1");
sysConfig.setValue("gateway", "10.0.0.1");
// Write back the updated element tree
session.editConfig(sysConfig);
| Modifier and Type | Field and Description |
|---|---|
protected NodeSet |
children
Children to the node, if container element.
|
static int |
CREATE_MERGE
Creates a new path and merges with the existing nodes when possible.
|
static int |
CREATE_MERGE_MULTI
Creates a new path and merges with the existing nodes when possible.
|
static int |
CREATE_NEW
Creates a new path.
|
static PrefixMap |
defaultPrefixes
Static prefix map that is always a default and will be resolved at root
level.
|
java.lang.String |
name
The name of the node.
|
java.lang.String |
namespace
The namespace this element name belongs to.
|
static java.lang.String |
NETCONF_NAMESPACE
The NETCONF namespace.
|
static int |
OP_CREATE
Operation flag to be used with
merge(Element,int). |
static int |
OP_DELETE
Operation flag to be used with
merge(Element,int). |
static int |
OP_MERGE
Operation flag to be used with
merge(Element,int). |
static int |
OP_REPLACE
Operation flag to be used with
merge(Element,int). |
static java.lang.String |
OPERATION |
protected Element |
parent
The parent to this node.
|
PrefixMap |
prefixes
Prefix map are really xmlns attributes.
|
java.lang.Object |
value
The value of the element.
|
| Constructor and Description |
|---|
Element(java.lang.String ns,
java.lang.String name)
Constructor that creates a new element tree.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addAttr(Attribute attr)
Adds an attribute for this element.
|
void |
addChild(Element child)
Adds child to children and makes this element the parent of child.
|
java.lang.Object |
clone()
Clones the tree, making an exact copy.
|
protected Element |
cloneAttrs(Element copy)
Clones the attributes to the target copy.
|
protected Element |
cloneShallow()
Clones the tree, making an exact copy.
|
protected Element |
cloneValue(Element copy)
clones the value to the target copy.
|
int |
compare(Element b)
Compare two elements.
|
static Element |
create(PrefixMap prefixMap,
java.lang.String pathStr)
Static method that creates a new configuration element tree, given a
path.
|
static Element |
create(Prefix prefix,
java.lang.String pathStr)
Static method that creates a new configuration element tree, given a
path.
|
static Element |
create(java.lang.String namespace,
java.lang.String pathStr)
Static method that creates a new configuration element tree given a
path.
|
Element |
createChild(java.lang.String name)
Creates a child element to the context node.
|
Element |
createChild(java.lang.String name,
java.lang.Object value)
Creates a child element with specified value.
|
Element |
createChild(java.lang.String namespace,
java.lang.String name,
java.lang.Object value)
Creates a child element with specified value.
|
Element |
createPath(int mode,
PrefixMap addPrefixes,
java.lang.String pathStr)
Creates an element tree as a child to the context node.
|
Element |
createPath(int mode,
java.lang.String pathStr)
Creates an element tree as a child to the context node from a create
path expression.
|
Element |
createPath(PrefixMap addPrefixes,
java.lang.String pathStr)
Creates an element tree as a child to the context node from a create
path expression.
|
Element |
createPath(Prefix prefix,
java.lang.String pathStr)
Creates an element tree as a child to the context node from a create
path expression.
|
Element |
createPath(java.lang.String pathStr)
Creates an element tree as a child to the context node from a create
path expression.
|
Element |
createPath(java.lang.String namespace,
java.lang.String pathStr)
Creates an element tree as a child to the context node from a create
path expression.
|
void |
delete()
Deletes this node.
|
NodeSet |
delete(java.lang.String pathStr)
Deletes child node(s).
|
void |
deleteChild(Element child)
Deletes a child node, provided it is present in the children list.
|
void |
deleteValue()
Deletes the value for this node.
|
void |
deleteValue(java.lang.String pathStr)
Deletes value of node(s)
|
protected void |
encode(Transport out)
Encode to XML and send it to the provided stream.
|
protected void |
encode(Transport out,
boolean newline_at_end)
Encode to XML and send it to the provided stream.
|
protected void |
encode(Transport out,
boolean newline_at_end,
Capabilities capas)
Encode to XML and send it to the provided stream.
|
protected void |
encode(Transport out,
Capabilities c)
Encode to XML and send it to the provided stream.
|
boolean |
equals(java.lang.Object other)
Compare if equal to another Object.
|
boolean |
exists(java.lang.String pathStr)
Check if any nodes in this element matches a given path string.
|
NodeSet |
get(java.lang.String pathStr)
Gets all nodes matching a given path expression.
|
Attribute |
getAttr(java.lang.String name)
Gets an Attribute
|
Attribute[] |
getAttrs()
Gets all attributes for this element.
|
java.lang.String |
getAttrValue(java.lang.String name)
Returns the string value of the named attribute.
|
protected Element |
getChild(Element child)
Tries to find an element with the same namespace and name as x.
|
Element |
getChild(java.lang.String name)
Get the (first) child with specified name, from children list
|
NodeSet |
getChildren()
Returns the children of this node.
|
NodeSet |
getChildren(java.lang.String name)
Get the children with specified name, from children list
|
PrefixMap |
getContextPrefixMap()
Returns a prefix map, as it is in the current context.
|
java.lang.String |
getElementPath()
Returns the path as a string
|
java.lang.String |
getElementPath(java.lang.String subPath)
Returns the path for this element including an appended sub-path.
|
Element |
getFirst(java.lang.String pathStr)
Returns first node that matches the path expression, or
null if no such node was found. |
Element |
getLast(java.lang.String pathStr)
Returns the last node that matches the path expression, or
null if no such node was found. |
Element |
getParent()
Returns the parent node of this node.
|
Element |
getRootElement() |
java.lang.Object |
getValue()
Returns the value of this element.
|
java.lang.Object |
getValue(java.lang.String pathStr)
Returns the value of a the first subnode matching a given path string,
or null if there are no matches.
|
java.lang.Object |
getValueOfChild(java.lang.String childName)
Finds the value of child with specified name, if it exists.
|
java.lang.Object[] |
getValues(java.lang.String pathStr)
Returns the value(s) of nodes in a given path expression.
|
java.util.Set<java.lang.String> |
getValuesAsSet(java.lang.String pathStr)
Returns the values of nodes in a given path expression.
|
boolean |
hasChildren()
Returns
true if this node has any children,
false otherwise. |
int |
hashCode()
Returns a hash code value for this object.
|
int |
insertChild(Element child)
Inserts a child element and returns the position of (the first
occurrence of) the inserted child in the list of children.
|
int |
insertChild(Element child,
int index)
Inserts a child element at a specific index in the list of children and
returns that index upon success.
|
int |
insertChild(Element child,
java.lang.String[] childrenNames)
Inserts a child element at the correct position by providing structure
information (the names of all the children, in order).
|
int |
insertFirst(Element child)
Inserts a child element first in the list of children.
|
int |
insertLast(Element child)
Inserts a child element last in the list of children.
|
ElementChildrenIterator |
iterator()
Return an iterator for the children of this node.
|
ElementChildrenIterator |
iterator(java.lang.String name)
Return an iterator for the children of a specified name of this node.
|
java.lang.String |
lookupContextPrefix(java.lang.String prefix)
Lookups a prefix and returns the associated namespace, traverses up the
parent links until the prefix is found.
|
void |
markCreate()
Marks a node with operation create
|
void |
markCreate(java.lang.String pathStr)
Marks node(s) with operation create.
|
void |
markDelete()
Marks a node with operation delete.
|
void |
markDelete(java.lang.String pathStr)
Marks node(s) with operation delete.
|
void |
markMerge()
Marks a node with operation merge.
|
void |
markMerge(java.lang.String pathStr)
Marks node(s) with operation merge.
|
void |
markReplace()
Marks a node with operation replace.
|
void |
markReplace(java.lang.String pathStr)
Marks node(s) with operation replace.
|
Element |
merge(Element root,
int op)
Merges a subtree into a resulting target subtree.
|
java.lang.String |
nsToPrefix(java.lang.String ns)
This method will find the prefix of a specified namespace, from the
given context node.
|
int |
position()
Returns the position of this element in the the parent child list.
|
java.lang.String |
prefix()
Returns the prefix name of this element.
|
java.lang.String |
qualifiedName()
A qualified name is a prefixed name.
|
static Element |
readFile(java.lang.String filename)
Read file with XML text and parse it into a configuration tree.
|
void |
removeAttr(java.lang.String name)
Removes an attribute with specified name.
|
void |
removeAttr(java.lang.String namespace,
java.lang.String name)
Removes an attribute with specified namespace and name from this
element's attribute list.
|
void |
removeDefaultPrefix()
Removes the default prefix mapping on this node (if any).
|
void |
removeMark()
Removes the operation attribute from a node.
|
void |
removeMarks()
Removes all operation attributes from a sub-tree.
|
void |
removePrefix(java.lang.String prefix)
Removes a prefix map from the context node.
|
Attribute |
setAttr(java.lang.String name,
java.lang.String value)
Sets an attribute on this element, treating xmlns attributes as prefix
maps.
|
Attribute |
setAttr(java.lang.String ns,
java.lang.String name,
java.lang.String value)
Sets an attribute on this element, aware of its namespace.
|
static void |
setDebugLevel(int level) |
void |
setDefaultPrefix()
Sets the default prefix mapping on this node.
|
void |
setPrefix(Prefix prefix)
Sets a prefix map to the context node.
|
void |
setPrefix(PrefixMap prefixMap)
Sets prefix mappings to the context node.
|
void |
setPrefix(java.lang.String prefix)
Sets a prefix mapping to the context node.
|
void |
setValue(java.lang.Object value)
Sets a new value for this node element.
|
void |
setValue(java.lang.String pathStr,
java.lang.Object value)
Sets value for all node elements matching specified path string.
|
Tagpath |
tagpath()
Return the full tagpath for this Element
|
java.lang.String |
toString()
Builds a string with the name, value, namespace, prefixes, other
attributes, children and the path of this element subtree.
|
java.lang.String |
toXMLString()
This will format the tree as an XML string, which can be printed.
|
void |
writeFile(java.lang.String filename)
Write this configuration tree to a file.
|
public static final java.lang.String NETCONF_NAMESPACE
public static final java.lang.String OPERATION
public java.lang.String namespace
public java.lang.String name
public java.lang.Object value
public PrefixMap prefixes
xmlns="http:tail-f.com/aaa" or xmlns:aaa="http:tail-f.com/aaa"
public static final PrefixMap defaultPrefixes
protected NodeSet children
protected Element parent
public static final int CREATE_NEW
createPath(int, String).public static final int CREATE_MERGE
createPath(int, String).public static final int CREATE_MERGE_MULTI
createPath(int, String).public static final int OP_CREATE
merge(Element,int).public static final int OP_DELETE
merge(Element,int).public static final int OP_REPLACE
merge(Element,int).public static final int OP_MERGE
merge(Element,int).public Element(java.lang.String ns,
java.lang.String name)
ns - Namespacename - Name of the elementpublic Element getRootElement()
public static Element create(java.lang.String namespace, java.lang.String pathStr) throws JNCException
See PathCreate for more information about path create
expressions.
namespace - NamespacepathStr - A "path create" stringJNCExceptionpublic static Element create(Prefix prefix, java.lang.String pathStr) throws JNCException
See PathCreate for more information about path create
expressions.
prefix - A prefix mapping.pathStr - A "path create" stringJNCExceptionpublic static Element create(PrefixMap prefixMap, java.lang.String pathStr) throws JNCException
See PathCreate for more information about path create
expressions.
prefixMap - Prefix mappings to be addedpathStr - A "path create" stringJNCExceptionpublic Element createChild(java.lang.String name)
name - The name of the child elementpublic Element createChild(java.lang.String name, java.lang.Object value)
name - The name of the child elementvalue - The value of the elementpublic Element createChild(java.lang.String namespace, java.lang.String name, java.lang.Object value)
namespace - The namespace that the name belongs toname - The name of the child elementvalue - The value of the elementpublic java.lang.String getElementPath(java.lang.String subPath)
subPath - A sub-path to be appended to the current pathpublic Element createPath(java.lang.String pathStr) throws JNCException
See PathCreate for more information about path create
expressions.
pathStr - A "path create" string.JNCExceptionpublic Element createPath(int mode, java.lang.String pathStr) throws JNCException
CREATE_NEW,
CREATE_MERGE, CREATE_MERGE_MULTImode - The creation mode.pathStr - A "path create" string.JNCExceptionpublic Element createPath(java.lang.String namespace, java.lang.String pathStr) throws JNCException
See PathCreate for more information about path create
expressions.
namespace - Namespace.pathStr - A "path create" string.JNCExceptionpublic Element createPath(Prefix prefix, java.lang.String pathStr) throws JNCException
See PathCreate for more information about path create
expressions.
prefix - A prefix mappingpathStr - A "path create" string.JNCExceptionpublic Element createPath(PrefixMap addPrefixes, java.lang.String pathStr) throws JNCException
See PathCreate for more information about path create
expressions.
addPrefixes - Prefix mappingspathStr - A "path create" string.JNCExceptionpublic Element createPath(int mode, PrefixMap addPrefixes, java.lang.String pathStr) throws JNCException
The mode parameter is one of: CREATE_NEW, CREATE_MERGE, CREATE_MERGE_MULTI
See PathCreate for more information about path create
expressions.
mode - The creation mode.addPrefixes - Prefix mappingspathStr - A "path create" string.JNCExceptionpublic void setDefaultPrefix()
public void removeDefaultPrefix()
public void setPrefix(java.lang.String prefix)
prefix - String prefix to be used for the namespace.public void setPrefix(Prefix prefix)
prefix - A prefix mappingpublic void setPrefix(PrefixMap prefixMap)
prefixMap - Prefix mappingspublic void removePrefix(java.lang.String prefix)
public Element getParent()
null if none.nullpublic void addChild(Element child)
child - Child element to be addedpublic int insertChild(Element child) throws JNCException
Checks that child is not already in use.
child - Child element to be insertedJNCException - If child is already a child of another element or
if child equals this elementpublic int insertChild(Element child, int index) throws JNCException
child - Child element to be insertedindex - Position in child list to insert child to. 0 is the first.JNCException - If child is already a child of another element.public int insertChild(Element child, java.lang.String[] childrenNames) throws JNCException
child - Child element to be insertedchildrenNames - The names of all children in order.JNCException - If child is already a child of another element.public int insertFirst(Element child) throws JNCException
child - Child element to be insertedJNCException - If child is already a child of another element.public int insertLast(Element child) throws JNCException
child - Child element to be insertedJNCException - If child is already a child of another element or
if child equals this elementpublic int position()
public NodeSet delete(java.lang.String pathStr) throws JNCException
See Path for more information about path expressions.
pathStr - Path string for children that will be deletedJNCExceptionpublic void delete()
public void deleteChild(Element child)
child - Child to deletepublic boolean hasChildren()
true if this node has any children,
false otherwise.true or falsepublic void addAttr(Attribute attr)
public Attribute[] getAttrs()
nullpublic java.lang.String getAttrValue(java.lang.String name)
null if no such attribute is found or "" if no value is
given to the attribute.name - The name of the attributepublic Attribute getAttr(java.lang.String name)
name - Lookup using the name of attributepublic Attribute setAttr(java.lang.String name, java.lang.String value)
name - The name of the attributevalue - The value of the attributepublic Attribute setAttr(java.lang.String ns, java.lang.String name, java.lang.String value)
If name starts with xmlns and ns starts with the xmlns namespace (http://www.w3.org/2000/xmlns/), the value is set as a prefix map.
ns - The namespace that the attribute name belongs toname - The name of the attributevalue - The value of the attributepublic void removeAttr(java.lang.String name)
name - The name of the attribute to be removed.public void removeAttr(java.lang.String namespace,
java.lang.String name)
namespace - the namespace the name belongs to.name - The name of the attribute to be removed.public java.lang.Object getValueOfChild(java.lang.String childName)
childName - Name of childpublic java.lang.Object getValue()
public boolean exists(java.lang.String pathStr)
throws JNCException
See Path for more information about path expressions.
pathStr - The path to matchtrue if any node matches pathStr;
false otherwise.JNCExceptionpublic java.lang.Object getValue(java.lang.String pathStr)
throws JNCException
See Path for more information about path expressions.
pathStr - Path string to find nodenullJNCExceptionpublic java.lang.Object[] getValues(java.lang.String pathStr)
throws JNCException
See Path for more information about path expressions.
pathStr - Path string to find nodesnull)JNCExceptionpublic java.util.Set<java.lang.String> getValuesAsSet(java.lang.String pathStr)
throws JNCException
See Path for more information about path expressions.
pathStr - Path string to find nodesnull)JNCExceptionpublic void setValue(java.lang.Object value)
value - Value to be setpublic void setValue(java.lang.String pathStr,
java.lang.Object value)
throws JNCException
See Path for more information about path expressions.
pathStr - Path string to find nodesvalue - Value to be setJNCExceptionpublic void deleteValue(java.lang.String pathStr)
throws JNCException
See Path for more information about path expressions.
pathStr - Path string to find nodesJNCExceptionpublic void deleteValue()
public Element getFirst(java.lang.String pathStr) throws JNCException
null if no such node was found.
Example:
Element full = NetconfSession:getConfig();
Element first_host = full.getFirst("/hosts/host");
Element last_host = full.getLast("/hosts/host");
See Path for more information about path expressions.pathStr - Path string to find nodesJNCExceptionpublic Element getLast(java.lang.String pathStr) throws JNCException
null if no such node was found.
See Path for more information about path expressions.
pathStr - Path string to find nodesJNCExceptionpublic NodeSet get(java.lang.String pathStr) throws JNCException
Example:
Element full_config = session.get();
NodeSet calle_nodes = full_config.get("host[www='Calle']");
See Path for more information about path expressions.pathStr - Path string to find nodesJNCExceptionpublic NodeSet getChildren()
nullpublic NodeSet getChildren(java.lang.String name)
name - Name of childpublic Element getChild(java.lang.String name)
name - Name of childnullpublic java.lang.Object clone()
clone in class java.lang.Objectprotected Element getChild(Element child)
child - Element to compare againstnull otherwise.protected Element cloneShallow()
public Element merge(Element root, int op) throws JNCException
root - Target subtree. Must start from root node.op - One of OP_CREATE, OP_DELETE,
OP_MERGE or OP_REPLACEJNCExceptionprotected Element cloneAttrs(Element copy)
copy - The target copy to clone the attributes toprotected Element cloneValue(Element copy)
copy - The target copy to clone the value field topublic void removeMark()
removeAttr(Element.NETCONF_NAMESPACE,"operation"); see @removeAttrpublic void removeMarks()
public void markDelete()
public void markDelete(java.lang.String pathStr)
throws JNCException
See Path for more information about path expressions.
pathStr - Path string to find nodesJNCExceptionpublic void markReplace()
public void markReplace(java.lang.String pathStr)
throws JNCException
See Path for more information about path expressions.
pathStr - Path string to find nodesJNCExceptionpublic void markMerge()
public void markMerge(java.lang.String pathStr)
throws JNCException
See Path for more information about path expressions.
pathStr - Path string to find nodesJNCExceptionpublic void markCreate()
public void markCreate(java.lang.String pathStr)
throws JNCException
See Path for more information about path expressions.
pathStr - Path string to find nodesJNCExceptionpublic java.lang.String qualifiedName()
public java.lang.String prefix()
public PrefixMap getContextPrefixMap()
public java.lang.String lookupContextPrefix(java.lang.String prefix)
null if the
prefix is not found.prefix - Prefix string to lookup.public java.lang.String nsToPrefix(java.lang.String ns)
null will be
returned.ns - Namespace string to lookuppublic java.lang.String getElementPath()
public boolean equals(java.lang.Object other)
equals in class java.lang.Objectother - Object to compare this element against.true if other is an Element with same name,
namespace and value; false otherwise.public int hashCode()
hashCode in class java.lang.Objectpublic int compare(Element b)
b - Element to compare this element against.public java.lang.String toString()
toString in class java.lang.Objectpublic java.lang.String toXMLString()
protected void encode(Transport out) throws JNCException
Equivalent to calling encode(out, true, null);
out - Stream to send the encoded version of this element to.JNCException - If a YangElement encode implementation failsprotected void encode(Transport out, Capabilities c) throws JNCException
Equivalent to calling encode(out, true, c);
out - Stream to send the encoded version of this element to.c - Capabilities, used by YangElement instances.JNCException - If a YangElement encode implementation fails.protected void encode(Transport out, boolean newline_at_end) throws JNCException
The newline_at_end argument controls whether a newline char is permitted at the end or not.
Equivalent to calling encode(out, newline_at_end, null);
out - Stream to send the encoded version of this element to.newline_at_end - If 'true' a newline is printed at the end.JNCException - If a YangElement encode implementation fails.protected void encode(Transport out, boolean newline_at_end, Capabilities capas) throws JNCException
The newline_at_end argument controls whether a newline char is permitted at the end or not.
out - Stream to send the encoded version of this element to.newline_at_end - If 'true' a newline is printed at the end.c - Capabilities, used by YangElement instances.JNCException - If a YangElement encode implementation fails.public Tagpath tagpath()
public ElementChildrenIterator iterator()
public ElementChildrenIterator iterator(java.lang.String name)
Example usage:
ElementChildrenIterator hostIter = config.iterator("host");
while (hostIter.hasNext()) {
Element host = hostIter.next();
System.out.println("Host: " + host);
}
name - A filter name, return only children with this name.public void writeFile(java.lang.String filename)
throws java.io.IOException
filename - File name.java.io.IOExceptionreadFile(String)public static Element readFile(java.lang.String filename) throws JNCException
filename - File name.JNCExceptionwriteFile(String)public static void setDebugLevel(int level)