11package net .juniper .netconf .element ;
22
33import lombok .Builder ;
4- import lombok .Data ;
54import lombok .EqualsAndHashCode ;
65import lombok .Singular ;
76import lombok .ToString ;
7+ import lombok .Value ;
88import lombok .extern .slf4j .Slf4j ;
99import net .juniper .netconf .NetconfConstants ;
1010import org .w3c .dom .Document ;
1414import org .xml .sax .InputSource ;
1515import org .xml .sax .SAXException ;
1616
17- import javax .xml .parsers .DocumentBuilderFactory ;
1817import javax .xml .parsers .ParserConfigurationException ;
19- import javax .xml .transform .OutputKeys ;
20- import javax .xml .transform .Transformer ;
21- import javax .xml .transform .TransformerException ;
22- import javax .xml .transform .TransformerFactory ;
23- import javax .xml .transform .dom .DOMSource ;
24- import javax .xml .transform .stream .StreamResult ;
2518import javax .xml .xpath .XPath ;
2619import javax .xml .xpath .XPathConstants ;
2720import javax .xml .xpath .XPathExpressionException ;
2821import javax .xml .xpath .XPathFactory ;
2922import java .io .IOException ;
3023import java .io .StringReader ;
31- import java .io .StringWriter ;
3224import java .util .List ;
3325
3426/**
3527 * Class to represent a NETCONF hello element - https://datatracker.ietf.org/doc/html/rfc6241#section-8.1
3628 */
37- @ Data
3829@ Slf4j
39- public class Hello {
30+ @ Value
31+ @ ToString (callSuper = true )
32+ @ EqualsAndHashCode (callSuper = true )
33+ public class Hello extends AbstractNetconfElement {
4034
41- @ ToString .Exclude
42- @ EqualsAndHashCode .Exclude
43- private final Document document ;
35+ private static final String XPATH_HELLO = getXpathFor ("hello" );
36+ private static final String XPATH_HELLO_SESSION_ID = XPATH_HELLO + getXpathFor ("session-id" );
37+ private static final String XPATH_HELLO_CAPABILITIES = XPATH_HELLO + getXpathFor ("capabilities" );
38+ private static final String XPATH_HELLO_CAPABILITIES_CAPABILITY = XPATH_HELLO_CAPABILITIES + getXpathFor ("capability" );
4439
45- @ ToString .Exclude
46- private final String xml ;
47-
48- private final String sessionId ;
40+ String sessionId ;
4941
5042 @ Singular ("capability" )
51- private final List <String > capabilities ;
43+ List <String > capabilities ;
5244
5345 public boolean hasCapability (final String capability ) {
5446 return capabilities .contains (capability );
5547 }
5648
57- public String toXML () {
58- return xml ;
59- }
60-
6149 /**
6250 * Creates a Hello object based on the supplied XML.
6351 *
@@ -71,24 +59,20 @@ public String toXML() {
7159 public static Hello from (final String xml )
7260 throws ParserConfigurationException , IOException , SAXException , XPathExpressionException {
7361
74- final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance ();
75- documentBuilderFactory .setNamespaceAware (true );
76- final Document document = documentBuilderFactory .newDocumentBuilder ()
62+ final Document document = createDocumentBuilderFactory ().newDocumentBuilder ()
7763 .parse (new InputSource (new StringReader (xml )));
7864 final XPath xPath = XPathFactory .newInstance ().newXPath ();
79- final String sessionId = xPath .evaluate ("/*[namespace-uri()='urn:ietf:params:xml:ns:netconf:base:1.0' and local-name()='hello']/*[namespace-uri()='urn:ietf:params:xml:ns:netconf:base:1.0' and local-name()='session-id']" , document );
65+ final String sessionId = xPath .evaluate (XPATH_HELLO_SESSION_ID , document );
8066 final HelloBuilder builder = Hello .builder ()
8167 .originalDocument (document )
8268 .sessionId (sessionId );
83- final NodeList capabilities = (NodeList ) xPath .evaluate ("/*[namespace-uri()='urn:ietf:params:xml:ns:netconf:base:1.0' and local-name()='hello']/*[namespace-uri()='urn:ietf:params:xml:ns:netconf:base:1.0' and local-name()='capabilities']/*[namespace-uri()='urn:ietf:params:xml:ns:netconf:base:1.0' and local-name()='capability']" , document , XPathConstants .NODESET );
69+ final NodeList capabilities = (NodeList ) xPath .evaluate (XPATH_HELLO_CAPABILITIES_CAPABILITY , document , XPathConstants .NODESET );
8470 for (int i = 0 ; i < capabilities .getLength (); i ++) {
8571 final Node node = capabilities .item (i );
8672 builder .capability (node .getTextContent ());
8773 }
8874 final Hello hello = builder .build ();
89- if (log .isInfoEnabled ()) {
90- log .info ("hello is: {}" , hello .toXML ());
91- }
75+ log .info ("hello is: {}" , hello .getXml ());
9276 return hello ;
9377 }
9478
@@ -98,29 +82,29 @@ private Hello(
9882 final String namespacePrefix ,
9983 final String sessionId ,
10084 @ Singular ("capability" ) final List <String > capabilities ) {
85+ super (getDocument (originalDocument , namespacePrefix , sessionId , capabilities ));
10186 this .sessionId = sessionId ;
10287 this .capabilities = capabilities ;
88+ }
89+
90+ private static Document getDocument (
91+ final Document originalDocument ,
92+ final String namespacePrefix ,
93+ final String sessionId ,
94+ final List <String > capabilities ) {
10395 if (originalDocument != null ) {
104- this . document = originalDocument ;
96+ return originalDocument ;
10597 } else {
106- this . document = createDocument (namespacePrefix , sessionId , capabilities );
98+ return createDocument (namespacePrefix , sessionId , capabilities );
10799 }
108- this .xml = createXml (document );
109100 }
110101
111102 private static Document createDocument (
112103 final String namespacePrefix ,
113104 final String sessionId ,
114105 final List <String > capabilities ) {
115106
116- final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance ();
117- documentBuilderFactory .setNamespaceAware (true );
118- final Document createdDocument ;
119- try {
120- createdDocument = documentBuilderFactory .newDocumentBuilder ().newDocument ();
121- } catch (final ParserConfigurationException e ) {
122- throw new IllegalStateException ("Unable to create document builder" , e );
123- }
107+ final Document createdDocument = createBlankDocument ();
124108
125109 final Element helloElement
126110 = createdDocument .createElementNS (NetconfConstants .URN_XML_NS_NETCONF_BASE_1_0 , "hello" );
@@ -149,17 +133,4 @@ private static Document createDocument(
149133 return createdDocument ;
150134 }
151135
152- private static String createXml (final Document document ) {
153- try {
154- final TransformerFactory transformerFactory = TransformerFactory .newInstance ();
155- final Transformer transformer = transformerFactory .newTransformer ();
156- transformer .setOutputProperty (OutputKeys .OMIT_XML_DECLARATION , "yes" );
157- final StringWriter stringWriter = new StringWriter ();
158- transformer .transform (new DOMSource (document ), new StreamResult (stringWriter ));
159- return stringWriter .toString ();
160- } catch (final TransformerException e ) {
161- throw new IllegalStateException ("Unable to transform document to XML" , e );
162- }
163- }
164-
165136}
0 commit comments