11package net .juniper .netconf .element ;
22
33import lombok .Builder ;
4- import lombok .Data ;
54import lombok .EqualsAndHashCode ;
65import lombok .Singular ;
7- import lombok .ToString ;
6+ import lombok .Value ;
87import lombok .extern .slf4j .Slf4j ;
98import net .juniper .netconf .NetconfConstants ;
109import org .w3c .dom .Document ;
1413import org .xml .sax .InputSource ;
1514import org .xml .sax .SAXException ;
1615
17- import javax .xml .parsers .DocumentBuilderFactory ;
1816import 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 ;
2517import javax .xml .xpath .XPath ;
2618import javax .xml .xpath .XPathConstants ;
2719import javax .xml .xpath .XPathExpressionException ;
2820import javax .xml .xpath .XPathFactory ;
2921import java .io .IOException ;
3022import java .io .StringReader ;
31- import java .io .StringWriter ;
3223import java .util .List ;
3324
3425/**
3526 * Class to represent a NETCONF hello element - https://datatracker.ietf.org/doc/html/rfc6241#section-8.1
3627 */
37- @ Data
3828@ Slf4j
39- public class Hello {
29+ @ Value
30+ @ EqualsAndHashCode (callSuper = true )
31+ public class Hello extends AbstractNetconfElement {
4032
41- @ ToString .Exclude
42- @ EqualsAndHashCode .Exclude
43- private final Document document ;
33+ private static final String XPATH_HELLO = getXpathFor ("hello" );
34+ private static final String XPATH_HELLO_SESSION_ID = XPATH_HELLO + getXpathFor ("session-id" );
35+ private static final String XPATH_HELLO_CAPABILITIES = XPATH_HELLO + getXpathFor ("capabilities" );
36+ private static final String XPATH_HELLO_CAPABILITIES_CAPABILITY = XPATH_HELLO_CAPABILITIES + getXpathFor ("capability" );
4437
45- @ ToString .Exclude
46- private final String xml ;
47-
48- private final String sessionId ;
38+ String sessionId ;
4939
5040 @ Singular ("capability" )
51- private final List <String > capabilities ;
41+ List <String > capabilities ;
5242
5343 public boolean hasCapability (final String capability ) {
5444 return capabilities .contains (capability );
5545 }
5646
57- public String toXML () {
58- return xml ;
59- }
60-
6147 /**
6248 * Creates a Hello object based on the supplied XML.
6349 *
@@ -71,24 +57,20 @@ public String toXML() {
7157 public static Hello from (final String xml )
7258 throws ParserConfigurationException , IOException , SAXException , XPathExpressionException {
7359
74- final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance ();
75- documentBuilderFactory .setNamespaceAware (true );
76- final Document document = documentBuilderFactory .newDocumentBuilder ()
60+ final Document document = createDocumentBuilderFactory ().newDocumentBuilder ()
7761 .parse (new InputSource (new StringReader (xml )));
7862 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 );
63+ final String sessionId = xPath .evaluate (XPATH_HELLO_SESSION_ID , document );
8064 final HelloBuilder builder = Hello .builder ()
8165 .originalDocument (document )
8266 .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 );
67+ final NodeList capabilities = (NodeList ) xPath .evaluate (XPATH_HELLO_CAPABILITIES_CAPABILITY , document , XPathConstants .NODESET );
8468 for (int i = 0 ; i < capabilities .getLength (); i ++) {
8569 final Node node = capabilities .item (i );
8670 builder .capability (node .getTextContent ());
8771 }
8872 final Hello hello = builder .build ();
89- if (log .isInfoEnabled ()) {
90- log .info ("hello is: {}" , hello .toXML ());
91- }
73+ log .info ("hello is: {}" , hello .getXml ());
9274 return hello ;
9375 }
9476
@@ -98,29 +80,29 @@ private Hello(
9880 final String namespacePrefix ,
9981 final String sessionId ,
10082 @ Singular ("capability" ) final List <String > capabilities ) {
83+ super (getDocument (originalDocument , namespacePrefix , sessionId , capabilities ));
10184 this .sessionId = sessionId ;
10285 this .capabilities = capabilities ;
86+ }
87+
88+ private static Document getDocument (
89+ final Document originalDocument ,
90+ final String namespacePrefix ,
91+ final String sessionId ,
92+ final List <String > capabilities ) {
10393 if (originalDocument != null ) {
104- this . document = originalDocument ;
94+ return originalDocument ;
10595 } else {
106- this . document = createDocument (namespacePrefix , sessionId , capabilities );
96+ return createDocument (namespacePrefix , sessionId , capabilities );
10797 }
108- this .xml = createXml (document );
10998 }
11099
111100 private static Document createDocument (
112101 final String namespacePrefix ,
113102 final String sessionId ,
114103 final List <String > capabilities ) {
115104
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- }
105+ final Document createdDocument = createBlankDocument ();
124106
125107 final Element helloElement
126108 = createdDocument .createElementNS (NetconfConstants .URN_XML_NS_NETCONF_BASE_1_0 , "hello" );
@@ -149,17 +131,4 @@ private static Document createDocument(
149131 return createdDocument ;
150132 }
151133
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-
165134}
0 commit comments