diff --git a/examples/1-interfaces/src/com/tailf/jnc/example/interfaces1/Client.java b/examples/1-interfaces/src/com/tailf/jnc/example/interfaces1/Client.java index 3e379b61..152383a2 100644 --- a/examples/1-interfaces/src/com/tailf/jnc/example/interfaces1/Client.java +++ b/examples/1-interfaces/src/com/tailf/jnc/example/interfaces1/Client.java @@ -15,7 +15,6 @@ public class Client { private Device dev; - private DeviceUser duser; public Client() { this.init(); @@ -24,7 +23,7 @@ public Client() { private void init() { String emsUserName = "bobby"; String ip = "localhost"; - duser = new DeviceUser(emsUserName, "admin", "admin"); + DeviceUser duser = new DeviceUser(emsUserName, "admin", "admin"); dev = new Device("mydev", duser, ip, 2022); try { @@ -52,8 +51,7 @@ private NodeSet editConfig(Device d, Element config) throws IOException, private NodeSet getConfig(Device d) throws IOException, JNCException { NetconfSession session = d.getSession("cfg"); - NodeSet reply = session.getConfig(NetconfSession.RUNNING); - return reply; + return session.getConfig(NetconfSession.RUNNING); } public NodeSet getConfig() throws IOException, JNCException { diff --git a/src/main/java/com/tailf/jnc/Element.java b/src/main/java/com/tailf/jnc/Element.java index ea240ece..52f763e3 100644 --- a/src/main/java/com/tailf/jnc/Element.java +++ b/src/main/java/com/tailf/jnc/Element.java @@ -1160,6 +1160,7 @@ protected Element cloneShallow() { * */ public Element merge(Element root, int op) throws JNCException { + Element rootElem = root; // make list of nodes down to this node from root final NodeSet list = new NodeSet(); @@ -1173,9 +1174,9 @@ public Element merge(Element root, int op) throws JNCException { Element x = list.getElement(0); list.remove(0); - if (root == null) { + if (rootElem == null) { if (list.size() >= 1) { - root = x.cloneShallow(); + rootElem = x.cloneShallow(); } else { // special case. only one path in root. Differ already if (op == OP_CREATE) { @@ -1198,16 +1199,16 @@ public Element merge(Element root, int op) throws JNCException { } } - if (x.compare(root) == -1) { + if (x.compare(rootElem) == -1) { System.err.println(" x= " + x); - System.err.println(" root= " + root); - System.err.println(" compare: " + x.compare(root)); + System.err.println(" root= " + rootElem); + System.err.println(" compare: " + x.compare(rootElem)); throw new JNCException(JNCException.ELEMENT_MISSING, - x.getElementPath() + ", " + (root != null ? root.getElementPath() : null)); + x.getElementPath() + ", " + (rootElem != null ? rootElem.getElementPath() : null)); } // same now, go down - Element parent = root; + Element parent = rootElem; while (list.size() > 1) { // pop first element from list x = list.getElement(0); @@ -1249,7 +1250,7 @@ public Element merge(Element root, int op) throws JNCException { } } // done - return root; + return rootElem; } /** @@ -1679,9 +1680,10 @@ public String toXMLString() { } private void toXMLString(int indent, StringBuffer s) { + int currentIndent = indent; final boolean flag = hasChildren(); final String qName = qualifiedName(); - String whitespacePrefix = new String(new char[indent * 2]).replace('\0', ' '); + String whitespacePrefix = new String(new char[currentIndent * 2]).replace('\0', ' '); s.append(whitespacePrefix).append('<').append(qName); // add xmlns attributes (prefixes) if (prefixes != null) { @@ -1695,19 +1697,19 @@ private void toXMLString(int indent, StringBuffer s) { s.append(' ').append(attr.toXMLString(this)); } } - indent++; + currentIndent++; // add children elements if any if (flag) { s.append(">\n"); for (final Element child : children) { - child.toXMLString(indent, s); + child.toXMLString(currentIndent, s); } } else { // add value if any if (value != null) { s.append('>'); final String stringValue = value.toString().replaceAll("&", "&"); - s.append(getIndentationSpacing(false, indent)); + s.append(getIndentationSpacing(false, currentIndent)); s.append(stringValue); } else { // self-closing tag @@ -1715,8 +1717,8 @@ private void toXMLString(int indent, StringBuffer s) { return; } } - indent--; - s.append(getIndentationSpacing(flag, indent)).append("\n"); + currentIndent--; + s.append(getIndentationSpacing(flag, currentIndent)).append("\n"); } public String encodedXMLString(boolean newlineAtEnd) { @@ -1922,16 +1924,11 @@ public ElementChildrenIterator iterator(String name) { * @see #readFile(String) */ public void writeFile(String filename) throws IOException { - final OutputStream fos = Files.newOutputStream(Paths.get(filename)); - try { - final DataOutputStream dos = new DataOutputStream(fos); - try { - dos.writeBytes(toXMLString()); - } finally { - dos.close(); - } - } finally { - fos.close(); + try ( + OutputStream fos = Files.newOutputStream(Paths.get(filename)); + DataOutputStream dos = new DataOutputStream(fos); + ) { + dos.writeBytes(toXMLString()); } } diff --git a/src/main/java/com/tailf/jnc/NetconfSession.java b/src/main/java/com/tailf/jnc/NetconfSession.java index 4f7482c8..693a5ab7 100644 --- a/src/main/java/com/tailf/jnc/NetconfSession.java +++ b/src/main/java/com/tailf/jnc/NetconfSession.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.util.List; +import java.util.function.Supplier; /** * A NETCONF session class. It makes it possible to connect to a NETCONF agent @@ -313,7 +314,7 @@ public Transport getTransport() { * Used from the constructor {@link #NetconfSession(Transport)}. */ protected void hello() throws JNCException, IOException { - trace("hello: "); + trace(() -> "hello: "); encodeHello(out); out.flush(); final String reply = in.readOne(); @@ -324,7 +325,7 @@ protected void hello() throws JNCException, IOException { throw new JNCException(JNCException.SESSION_ERROR, "hello contains no capabilities"); } - trace("capabilities: \n{}", capatree.toXMLString()); + trace(() -> "capabilities: \n" + capatree.toXMLString()); capabilities = new Capabilities(capatree); if (!capabilities.baseCapability && !capabilities.baseCapability_v1_1) { @@ -343,7 +344,7 @@ protected void hello() throws JNCException, IOException { "hello contains no session identifier"); } sessionId = Long.parseLong((String) sess.value); - trace("sessionId = {}", sessionId); + trace(() -> "sessionId = " + sessionId); } /** @@ -453,7 +454,7 @@ public NodeSet getConfig() throws JNCException, IOException { * Gets the device configuration data. */ public NodeSet getConfig(int datastore) throws JNCException, IOException { - trace("getConfig: {}", datastoreToString(datastore)); + trace(() -> "getConfig: " + datastoreToString(datastore)); RPCRequest rpcRequest = prepareGetConfigMessage(encodeDatastore(datastore)); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -464,7 +465,7 @@ public NodeSet getConfig(int datastore) throws JNCException, IOException { * Calls rpc method. */ public NodeSet callRpc(Element data) throws JNCException, IOException { - trace("call: " + data.toXMLString()); + trace(() -> "call: " + data.toXMLString()); RPCRequest rpcRequest = prepareRPCMessage(data); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -477,7 +478,7 @@ public NodeSet callRpc(Element data) throws JNCException, IOException { * Returns the request-id used in the message. */ public int sendRpc(Element data) throws JNCException, IOException { - trace("send rpc: " + data.toXMLString()); + trace(() -> "send rpc: " + data.toXMLString()); RPCRequest rpcRequest = prepareRPCMessage(data); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -503,8 +504,7 @@ public NodeSet getConfig(String xpath) throws JNCException, IOException { */ public NodeSet getConfig(int datastore, Element subtreeFilter) throws JNCException, IOException { - trace("getConfig: {}\n{}", datastoreToString(datastore), - subtreeFilter.toXMLString()); + trace(() -> "getConfig: " + datastoreToString(datastore) + "\n" + subtreeFilter.toXMLString()); RPCRequest rpcRequest = prepareGetConfigMessage(encodeDatastore(datastore), subtreeFilter); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -520,7 +520,7 @@ public NodeSet getConfig(int datastore, Element subtreeFilter) */ public NodeSet getConfig(int datastore, String xpath) throws JNCException, IOException { - trace("getConfig: {} \"{}\"", datastoreToString(datastore), xpath); + trace(() -> "getConfig: " + datastoreToString(datastore) + "\"" + xpath + "\""); if (!capabilities.xpathCapability) { throw new JNCException(JNCException.SESSION_ERROR, "the :xpath capability is not supported by server"); @@ -535,7 +535,7 @@ public NodeSet getConfig(int datastore, String xpath) * Retrieves running configuration and device state information. */ public NodeSet get() throws JNCException, IOException { - trace("get: \"\""); + trace(() -> "get: \"\""); RPCRequest rpcRequest = prepareGetMessage(""); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -549,7 +549,7 @@ public NodeSet get() throws JNCException, IOException { */ public NodeSet get(Element subtreeFilter) throws JNCException, IOException { - trace("get: " + (null!=subtreeFilter?subtreeFilter.toXMLString(): null)); + trace(() -> "get: " + (null!=subtreeFilter?subtreeFilter.toXMLString(): null)); RPCRequest rpcRequest = prepareGetMessage(subtreeFilter); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -563,7 +563,7 @@ public NodeSet get(Element subtreeFilter) throws JNCException, * @param xpath An xpath epxression. */ public NodeSet get(String xpath) throws JNCException, IOException { - trace("get: \"{}\"", xpath); + trace(() -> "get: \"" + xpath + "\""); if (!capabilities.hasXPath()) { throw new JNCException(JNCException.SESSION_ERROR, "the :xpath capability is not supported by server"); @@ -608,7 +608,7 @@ public void editConfig(NodeSet configTrees) throws JNCException, */ public void editConfig(int datastore, Element configTree) throws JNCException, IOException { - trace("editConfig: target={}\n{}", datastoreToString(datastore), configTree.toXMLString()); + trace(() -> "editConfig: target=" + datastoreToString(datastore) + "\n" + configTree.toXMLString()); RPCRequest rpcRequest = prepareEditConfigMessage(encodeDatastore(datastore), new NodeSet(configTree)); out.print(rpcRequest.getMessage().toString()); @@ -618,8 +618,7 @@ public void editConfig(int datastore, Element configTree) public void editConfig(int datastore, NodeSet configTrees) throws JNCException, IOException { - trace("editConfig: target=" + datastoreToString(datastore) + "\n" - + configTrees.toXMLString()); + trace(() -> "editConfig: target=" + datastoreToString(datastore) + "\n" + configTrees.toXMLString()); RPCRequest rpcRequest = prepareEditConfigMessage(encodeDatastore(datastore), configTrees); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -637,7 +636,7 @@ public void editConfig(int datastore, NodeSet configTrees) */ public void editConfig(int datastore, String url) throws JNCException, IOException { - trace("editConfig: target={} source={}", datastoreToString(datastore), url); + trace(() -> "editConfig: target=" + datastoreToString(datastore) + " source=" + url); RPCRequest rpcRequest = prepareEditConfigMessage(encodeDatastore(datastore), url); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -833,7 +832,7 @@ public void copyConfig(Element sourceTree, int target) public void copyConfig(NodeSet sourceTrees, int target) throws JNCException, IOException { - trace("copyConfig: target=" + datastoreToString(target) + "\n" + trace(() -> "copyConfig: target=" + datastoreToString(target) + "\n" + sourceTrees.toXMLString()); RPCRequest rpcRequest = prepareCopyConfigMessage(sourceTrees, encodeDatastore(target)); out.print(rpcRequest.getMessage().toString()); @@ -856,7 +855,7 @@ public void copyConfig(Element sourceTree, String targetUrl) public void copyConfig(NodeSet sourceTrees, String targetUrl) throws JNCException, IOException { - trace("copyConfig: target=" + targetUrl + "\n" + trace(() -> "copyConfig: target=" + targetUrl + "\n" + sourceTrees.toXMLString()); RPCRequest rpcRequest = prepareCopyConfigMessage(sourceTrees, encodeUrl(targetUrl)); out.print(rpcRequest.getMessage().toString()); @@ -874,7 +873,7 @@ public void copyConfig(NodeSet sourceTrees, String targetUrl) */ public void copyConfig(int source, int target) throws JNCException, IOException { - trace("copyConfig: {} {}", datastoreToString(source), datastoreToString(target)); + trace(() -> "copyConfig: " + datastoreToString(source) + " " + datastoreToString(target)); RPCRequest rpcRequest = prepareCopyConfigMessage(encodeDatastore(source), encodeDatastore(target)); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -890,7 +889,7 @@ public void copyConfig(int source, int target) throws JNCException, */ public void copyConfig(int source, String targetUrl) throws JNCException, IOException { - trace("copyConfig: source={} target={}", datastoreToString(source), targetUrl); + trace(() -> "copyConfig: source=" + datastoreToString(source) + " target=" + targetUrl); RPCRequest rpcRequest = prepareCopyConfigMessage(encodeDatastore(source), encodeUrl(targetUrl)); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -907,7 +906,7 @@ public void copyConfig(int source, String targetUrl) throws JNCException, */ public void copyConfig(String sourceUrl, String targetUrl) throws JNCException, IOException { - trace("copyConfig: source={} target={}", sourceUrl, targetUrl); + trace(() -> "copyConfig: source=" + sourceUrl + " target=" + targetUrl); RPCRequest rpcRequest = prepareCopyConfigMessage(encodeUrl(sourceUrl), encodeUrl(targetUrl)); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -923,7 +922,7 @@ public void copyConfig(String sourceUrl, String targetUrl) */ public void copyConfig(String sourceUrl, int target) throws JNCException, IOException { - trace("copyConfig: source={} target={}", sourceUrl, datastoreToString(target)); + trace(() -> "copyConfig: source=" + sourceUrl +" target=" + datastoreToString(target)); RPCRequest rpcRequest = prepareCopyConfigMessage(encodeUrl(sourceUrl), encodeDatastore(target)); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -937,7 +936,7 @@ public void copyConfig(String sourceUrl, int target) throws JNCException, * @param datastore Datastore to be deleted */ public void deleteConfig(int datastore) throws JNCException, IOException { - trace("deleteConfig: {}", datastoreToString(datastore)); + trace(() -> "deleteConfig: " + datastoreToString(datastore)); RPCRequest rpcRequest = prepareDeleteConfigMessage(encodeDatastore(datastore)); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -951,7 +950,7 @@ public void deleteConfig(int datastore) throws JNCException, IOException { */ public void deleteConfig(String targetUrl) throws JNCException, IOException { - trace("deleteConfig: {}", targetUrl); + trace(() -> "deleteConfig: " + targetUrl); RPCRequest rpcRequest = prepareDeleteConfigMessage(encodeUrl(targetUrl)); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -971,7 +970,7 @@ public void deleteConfig(String targetUrl) throws JNCException, * @param datastore The datastore to lock */ public void lock(int datastore) throws JNCException, IOException { - trace("lock: {}", datastoreToString(datastore)); + trace(() -> "lock: " + datastoreToString(datastore)); RPCRequest rpcRequest = prepareLockMessage(encodeDatastore(datastore)); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -985,7 +984,7 @@ public void lock(int datastore) throws JNCException, IOException { * @param datastore The target datastore to unlock */ public void unlock(int datastore) throws JNCException, IOException { - trace("unlock: {}", datastoreToString(datastore)); + trace(() -> "unlock: " + datastoreToString(datastore)); RPCRequest rpcRequest = prepareUnlockMessage(encodeDatastore(datastore)); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -1026,7 +1025,7 @@ public void unlock(int datastore) throws JNCException, IOException { * @return A unique lock reference which should be used to unlockPartial() */ public int lockPartial(String[] select) throws JNCException, IOException { - trace("lockPartial"); + trace(() -> "lockPartial"); if (!capabilities.hasPartialLock()) { throw new JNCException(JNCException.SESSION_ERROR, "capability :partial-lock is not supported by server"); @@ -1067,7 +1066,7 @@ public int lockPartial(String select) throws JNCException, IOException { * {@link #lockPartial(int,String[])} */ public void unlockPartial(int lockId) throws JNCException, IOException { - trace("partialUnlock: {}", lockId); + trace(() -> "partialUnlock: " + lockId); if (!capabilities.hasPartialLock()) { throw new JNCException(JNCException.SESSION_ERROR, "capability :partial-lock is not supported by server"); @@ -1104,7 +1103,7 @@ public void unlockPartial(int lockId) throws JNCException, IOException { * */ public void commit() throws JNCException, IOException { - trace("commit"); + trace(() -> "commit"); if (!capabilities.hasCandidate()) { throw new JNCException(JNCException.SESSION_ERROR, "the :candidate capability is not supported by server"); @@ -1152,7 +1151,7 @@ public void commit() throws JNCException, IOException { * reverting config */ public void confirmedCommit(int timeout) throws JNCException, IOException { - trace("confirmedCommit: {}", timeout); + trace(() -> "confirmedCommit: " + timeout); if (!capabilities.hasCandidate()) { throw new JNCException(JNCException.SESSION_ERROR, "the :candidate capability is not supported by server"); @@ -1173,7 +1172,7 @@ public void confirmedCommit(int timeout) throws JNCException, IOException { * candidate configuration to the current running configuration. */ public void discardChanges() throws JNCException, IOException { - trace("discardChanges"); + trace(() -> "discardChanges"); if (!capabilities.hasCandidate()) { throw new JNCException(JNCException.SESSION_ERROR, "the :candidate capability is not supported by server"); @@ -1193,7 +1192,7 @@ public void discardChanges() throws JNCException, IOException { * associated connections. */ public void closeSession() throws JNCException, IOException { - trace("closeSession"); + trace(() -> "closeSession"); RPCRequest rpcRequest = prepareCloseSessionMessage(); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -1214,7 +1213,7 @@ public void closeSession() throws JNCException, IOException { * @param sessionId The id of the session to terminate */ public void killSession(long sessionId) throws JNCException, IOException { - trace("killSession: {}", sessionId); + trace(() -> "killSession: " + sessionId); if (sessionId == this.sessionId) { throw new JNCException(JNCException.SESSION_ERROR, "illegal to use kill-session on own session id"); @@ -1232,7 +1231,7 @@ public void killSession(long sessionId) throws JNCException, IOException { * @param configTree configuration tree to validate */ public void validate(Element configTree) throws JNCException, IOException { - trace("validate: {}", configTree.toXMLString()); + trace(() -> "validate: " + configTree.toXMLString()); if (!capabilities.hasValidate()) { throw new JNCException(JNCException.SESSION_ERROR, "capability :validate is not supported by server"); @@ -1250,7 +1249,7 @@ public void validate(Element configTree) throws JNCException, IOException { * @param datastore The datastore to validate */ public void validate(int datastore) throws IOException, JNCException { - trace("validate: {}", datastoreToString(datastore)); + trace(() -> "validate: " + datastoreToString(datastore)); if (!capabilities.hasValidate()) { throw new JNCException(JNCException.SESSION_ERROR, "capability :validate is not supported by server"); @@ -1269,7 +1268,7 @@ public void validate(int datastore) throws IOException, JNCException { * @param url The source url to validate */ public void validate(String url) throws IOException, JNCException { - trace("validate: {}", url); + trace(() -> "validate: " + url); if (!capabilities.hasValidate()) { throw new JNCException(JNCException.SESSION_ERROR, "capability :validate is not supported by server"); @@ -1350,8 +1349,10 @@ public void createSubscription(String stream) public void createSubscription(String streamName, NodeSet eventFilter, String startTime, String stopTime) throws IOException, JNCException { - trace("createSubscription: stream={} filter={} form={} to={}", - streamName, eventFilter.toXMLString(), startTime, stopTime); + trace(() -> "createSubscription: stream=" + streamName + + " filter=" + eventFilter.toXMLString() + + " from=" + startTime + + " to=" + stopTime); if (!capabilities.hasNotification()) { throw new JNCException(JNCException.SESSION_ERROR, "capability :notification is not supported by server"); @@ -1378,8 +1379,10 @@ public void createSubscription(String streamName, NodeSet eventFilter, public void createSubscription(String streamName, String eventFilter, String startTime, String stopTime) throws IOException, JNCException { - trace("createSubscription: stream={} filter={} from={} to={}", - streamName, eventFilter, startTime, stopTime); + trace(() -> "createSubscription: stream=" + streamName + + " filter=" + eventFilter + + " from=" + startTime + + " to=" + stopTime); if (!capabilities.hasNotification()) { throw new JNCException(JNCException.SESSION_ERROR, "capability :notification is not supported by server"); @@ -1422,7 +1425,7 @@ public NodeSet getStreams() throws JNCException, IOException { public Element receiveNotification() throws IOException, JNCException { final String notification = in.readOne(); - trace("notification= {}", notification); + trace(() -> "notification= " + notification); if (notification.length() == 0) { throw new JNCException(JNCException.PARSER_ERROR, "empty input"); } @@ -1443,7 +1446,7 @@ public Element receiveNotification() throws IOException, JNCException { * @param data element tree with action-data */ public Element action(Element data) throws JNCException, IOException { - trace("action: {}", data.toXMLString()); + trace(() -> "action: " + data.toXMLString()); RPCRequest rpcRequest = prepareActionMessage(data); out.print(rpcRequest.getMessage().toString()); out.flush(); @@ -1474,7 +1477,7 @@ void recvRpcReplyOk(int mid) throws JNCException, IOException { */ protected Element recvRpcReplyOk(String mid) throws JNCException, IOException { final String reply = in.readOne(); - trace("reply= {}", reply); + trace(() -> "reply= " + reply); if (reply.length() == 0) { throw new JNCException(JNCException.PARSER_ERROR, "empty input"); } @@ -1525,7 +1528,7 @@ NodeSet recvCallRpcReply(Element e, int mid) throws JNCException, NodeSet recvRpcReply(String path, XMLParser parser, String mid) throws JNCException, IOException { final String reply = in.readOne(); - trace("reply= {}", reply); + trace(() -> "reply= " + reply); final Element t = parser.parse(reply); final Element rep = t.getFirst("self::rpc-reply"); @@ -1718,9 +1721,9 @@ String mkXmlnsAttr(String prefix, String ns) { /** * Printout trace if 'debug'-flag is enabled. */ - private static void trace(String format, Object ... args) { + private static void trace(Supplier message) { if (Element.debugLevel >= Element.DEBUG_LEVEL_SESSION) { - System.err.println(String.format("*NetconfSession: " + format, args)); + System.err.println(String.format("*NetconfSession: " + message.get())); } } diff --git a/src/main/java/com/tailf/jnc/SchemaParser.java b/src/main/java/com/tailf/jnc/SchemaParser.java index b0c55520..7c8472be 100644 --- a/src/main/java/com/tailf/jnc/SchemaParser.java +++ b/src/main/java/com/tailf/jnc/SchemaParser.java @@ -244,8 +244,8 @@ private void readFile(InputSource inputSource, } /** - * Scans the classpath for the XML schema file and populates the hashtable with - * SchemaNode objects. Class is passed in so that in the case of multiple {@link ClassLoader}s + * Scans the classpath for the XML schema file and populates the hashtable wit + * SchemaNode objects. Class is passed in so that in the case of multiple {@link ClassLoader}s * the correct one can be used to locate the schema. * * @param filename @@ -253,7 +253,7 @@ private void readFile(InputSource inputSource, * @param clazz * @throws JNCException if the file is not found or cannot be parsed. */ - public void findAndReadFile(final String filename, final Map h, final Class clazz) + public void findAndReadFile(final String filename, final Map h, final Class clazz) throws JNCException { final URL url = clazz.getResource(filename); if (url == null){ diff --git a/src/main/java/com/tailf/jnc/Utils.java b/src/main/java/com/tailf/jnc/Utils.java index 4969cda2..802dd6dc 100644 --- a/src/main/java/com/tailf/jnc/Utils.java +++ b/src/main/java/com/tailf/jnc/Utils.java @@ -208,7 +208,7 @@ public static boolean matches(String value, String[] regexes) // Thanks Dimitris Kolovos for the code below public static String escapeXml(String original) { - String escaped = null; + String escaped; try { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Text text = document.createTextNode(original); @@ -220,7 +220,7 @@ public static String escapeXml(String original) { transformer.transform(source, result); escaped = writer.toString(); } catch (Exception ignored) { - // intentionally ignore + escaped = null; } return (escaped != null) ? escaped : original ; } diff --git a/src/main/java/com/tailf/jnc/YangBaseType.java b/src/main/java/com/tailf/jnc/YangBaseType.java index e9e1d9e9..14e0a108 100644 --- a/src/main/java/com/tailf/jnc/YangBaseType.java +++ b/src/main/java/com/tailf/jnc/YangBaseType.java @@ -4,7 +4,7 @@ /** * Represents a built-in YANG data type. - * + * * @author emil@tail-f.com */ abstract class YangBaseType implements Cloneable, YangType { @@ -13,7 +13,7 @@ abstract class YangBaseType implements Cloneable, YangType { /** * The value of this object, of which this class is a wrapper for. - * + * * @serial */ protected T value; @@ -28,7 +28,7 @@ public YangBaseType() { /** * Creates a YangType object from a String. - * + * * @param s The string. * @throws YangException If an invariant was broken during initialization, * or if value could not be parsed from s. @@ -39,7 +39,7 @@ public YangBaseType(String s) throws YangException { /** * Creates a YangType object from a value of type T. - * + * * @param value The initial value of the new YangType object. * @throws YangException If an invariant was broken during initialization. */ @@ -49,18 +49,18 @@ public YangBaseType(T value) throws YangException { /* * (non-Javadoc) - * + * * @see com.tailf.jnc.YangType#setValue(java.lang.String) */ @Override public void setValue(String s) throws YangException { - s = Utils.wsCollapse(s); - setValue(fromString(s)); + String collapsed = Utils.wsCollapse(s); + setValue(fromString(collapsed)); } /* * (non-Javadoc) - * + * * @see com.tailf.jnc.YangType#setValue(T) */ @Override @@ -74,7 +74,7 @@ public void setValue(T value) throws YangException { /* * (non-Javadoc) - * + * * @see com.tailf.jnc.YangType#getValue() */ @Override @@ -84,7 +84,7 @@ public T getValue() { /* * (non-Javadoc) - * + * * @see com.tailf.jnc.YangType#check() */ @Override @@ -105,7 +105,7 @@ public String toString() { * Returns a value of type T given a String. *

* Note: This method is non-static since T is a non-static type. - * + * * @param s A string representation of a value of type T. * @return A T value parsed from s (not this.value!). * @throws YangException If s does not contain a parsable T. @@ -114,34 +114,35 @@ public String toString() { /** * Compares this object with another object for equality. - * + * * @param obj The object to compare with. * @return true if obj can be cast to a comparable type and the value of * this object is equal to the value of obj; false otherwise. */ @Override public boolean equals(Object obj) { - if (value == null || !canEqual(obj)) { + Object other = obj; + if (value == null || !canEqual(other)) { return false; } - if (obj instanceof YangBaseType) { - final YangBaseType other = (YangBaseType) obj; - if (!other.canEqual(this)) { + if (other instanceof YangBaseType) { + final YangBaseType typedOther = (YangBaseType) other; + if (!typedOther.canEqual(this)) { return false; } - obj = other.getValue(); + other = typedOther.getValue(); } - if (value instanceof Number && obj instanceof Number) { + if (value instanceof Number && other instanceof Number) { final BigDecimal valNum = Utils.bigDecimalValueOf((Number) value); - final BigDecimal objNum = Utils.bigDecimalValueOf((Number) obj); + final BigDecimal objNum = Utils.bigDecimalValueOf((Number) other); return valNum.compareTo(objNum) == 0; } - return value.equals(obj); + return value.equals(other); } /* * (non-Javadoc) - * + * * @see java.lang.Object#hashCode() */ @Override @@ -154,14 +155,14 @@ public int hashCode() { /** * Clones this object without cloning its value. - * + * * @return A shallow clone of this object. */ protected abstract YangBaseType cloneShallow() throws YangException; /* * (non-Javadoc) - * + * * @see java.lang.Object#clone() */ @SuppressWarnings("unchecked") diff --git a/src/main/java/com/tailf/jnc/YangBinary.java b/src/main/java/com/tailf/jnc/YangBinary.java index 726c211b..71a1fd2e 100644 --- a/src/main/java/com/tailf/jnc/YangBinary.java +++ b/src/main/java/com/tailf/jnc/YangBinary.java @@ -42,9 +42,9 @@ public YangBinary(byte[] buffer) { */ @Override public void setValue(String value) throws YangException { - value = Utils.wsCollapse(value); + String collapsed = Utils.wsCollapse(value); try { - this.value = Base64Coder.encodeString(value); + this.value = Base64Coder.encodeString(collapsed); } catch (IllegalArgumentException e) { YangException.throwException(true, e); } diff --git a/src/main/java/com/tailf/jnc/YangBoolean.java b/src/main/java/com/tailf/jnc/YangBoolean.java index c737a9e3..ed2b49c3 100644 --- a/src/main/java/com/tailf/jnc/YangBoolean.java +++ b/src/main/java/com/tailf/jnc/YangBoolean.java @@ -2,7 +2,7 @@ /** * Implements the built-in YANG data type "boolean". - * + * * @author emil@tail-f.com */ public class YangBoolean extends YangBaseType { @@ -11,7 +11,7 @@ public class YangBoolean extends YangBaseType { /** * Creates a YangBoolean object from a String. - * + * * @param s The string. * @throws YangException If value is not one of "true" or "false". */ @@ -21,7 +21,7 @@ public YangBoolean(String s) throws YangException { /** * Creates a YangBoolean object from a boolean. - * + * * @param b The boolean to set the value of the new YangBoolean to. * @throws YangException Never. */ @@ -33,17 +33,17 @@ public YangBoolean(boolean b) throws YangException { * Works much like Boolean.parseBoolean, except that case matters, s is * trimmed with wsCollapse prior to parsing, and an exception is thrown if * the trimmed string is neither "true" nor "false". - * + * * @param s The String. * @return true if s matches " *true *", false if s matches " *false *". * @throws YangException if s does not match a valid boolean value */ @Override protected Boolean fromString(String s) throws YangException { - s = Utils.wsCollapse(s); - if ("true".equals(s)) { + String collapsed = Utils.wsCollapse(s); + if ("true".equals(collapsed)) { return true; - } else if ("false".equals(s)) { + } else if ("false".equals(collapsed)) { return false; } else { throw new YangException(YangException.BAD_VALUE, this); @@ -60,7 +60,7 @@ public void check() throws YangException { /** * Compares type of obj with this object to see if they can be equal. - * + * * @param obj Object to compare type with. * @return true if obj is an instance of YangBoolean or java.lang.Boolean; * false otherwise. @@ -72,7 +72,7 @@ public boolean canEqual(Object obj) { /* * (non-Javadoc) - * + * * @see com.tailf.jnc.YangBaseType#cloneShallow() */ @Override diff --git a/src/main/java/com/tailf/jnc/YangElement.java b/src/main/java/com/tailf/jnc/YangElement.java index d73d4cba..e392389a 100644 --- a/src/main/java/com/tailf/jnc/YangElement.java +++ b/src/main/java/com/tailf/jnc/YangElement.java @@ -313,7 +313,8 @@ public static String camelize(String s) { } StringBuilder sb = new StringBuilder(len); sb.append(Character.toLowerCase(s.charAt(0))); - for (int i = 1; i < len; i++) { + int i; + for (i = 1; i < len; i++) { boolean isLast = i == len - 1; char c = s.charAt(i); char next = isLast ? c : s.charAt(i+1); @@ -331,15 +332,15 @@ public static String camelize(String s) { sb.append(c); } } - s = sb.toString(); - if (isReserved(s)) { - s += "_"; + String builtString = sb.toString(); + if (isReserved(builtString)) { + builtString += "_"; } else if (s.matches("[0-9]")) { - s = "_" + s; + builtString = "_" + builtString; } - return s; + return builtString; } public static String normalize(String s) { @@ -380,7 +381,7 @@ protected void setLeafValue(String ns, String path, Object value, protected void setLeafListValue(String ns, String path, Object value, String[] childrenNames) throws JNCException { final Element listEntry = get(path).last(); - + if (listEntry instanceof Leaf && listEntry.value == null) { listEntry.setValue(value); } else { @@ -486,7 +487,7 @@ public boolean keyCompare(YangElement b) { *

  • 1 - if the key children of this YangElement are equal to those of b, * but non-key children are different. * - * + * * @param b YangElement to compare against. * @return -1 if not equal, 0 if completely equal, 1 if same except for * non-key children @@ -544,7 +545,7 @@ public int compare(YangElement b) { *
  • 1 - if the key children of this YangElement are equal to those of b, * but non-key children are different. * - * + * * @param b Element to compare against. * @return -1 if not equal, 0 if completely equal, 1 if same except for * non-key children @@ -577,7 +578,7 @@ public int compare(Element b) { *

    * Note that both subtrees must have a common starting point YangElement in * order to compare them. - * + * * @param a Subtree A (YangElement) * @param b Subtree B (YangElement) * @param uniqueA Place for elements that are unique to A. @@ -642,7 +643,7 @@ public static void getDiff(YangElement a, YangElement b, NodeSet uniqueA, /** * Checks if two configurations are equal, or if a sync is needed. - * + * * @param b * @return 'true' if both trees are equal. 'false' otherwise. */ @@ -652,7 +653,7 @@ public boolean checkSync(YangElement b) { /** * Checks if two configurations are equal, or if a sync is needed. - * + * * @return 'true' if both trees are equal. 'false' otherwise. */ public static boolean checkSync(NodeSet a, NodeSet b) { @@ -671,7 +672,7 @@ public static boolean checkSync(NodeSet a, NodeSet b) { /** * Checks if two configurations are equal, or if a sync is needed. - * + * * @return 'true' if both trees are equal. 'false' otherwise. */ public static boolean checkSync(YangElement a, YangElement b) { @@ -688,7 +689,7 @@ public static boolean checkSync(YangElement a, YangElement b) { /** * Will return a subtree for syncing a subtree A with all the necessary * operations to make it look like the target tree B. - * + * * @return Return subtree with operations to transmute subtree A into * subtree B. */ @@ -704,7 +705,7 @@ public YangElement sync(YangElement b) throws JNCException { * "nc:operation="replace". An alternative (and better) method is "merge". * The method {@link #syncMerge} produces a NETCONF tree with the merge * operation instead. - * + * * @return Return subtree with operations to transmute subtree A into * subtree B. */ @@ -742,7 +743,7 @@ public static YangElement sync(YangElement a, YangElement b) * Returns a list of subtrees for syncing a subtree A with all the * necessary operations to make it look like the target tree B. This * variant uses the NETCONF merge operation. - * + * * @return Subtrees with operations to transmute subtree A into subtree B. */ @@ -765,7 +766,7 @@ public static NodeSet syncMerge(NodeSet a, NodeSet b) throws JNCException { * Will return a subtree for syncing a subtree A with all the necessary * operations to make it look like the target tree B. This version of sync * will produce a NETCONF tree with NETCONF merge operations. - * + * * @return Subtree with operations to transmute subtree A into subtree B. */ @@ -782,7 +783,7 @@ public static YangElement syncMerge(YangElement a, YangElement b) { /** * Which NETCONF do we need to produce in order to go from a to b? - * + * * @param a Subtree to sync * @param b Copy of subtree to mimic * @param toDel A list with leaves that should be removed from 'b' @@ -916,7 +917,7 @@ private static Element findDeleteChildLeaf(Leaf e, NodeSet s) { /** * Will return a list of subtrees for syncing a subtree A with all the * necessary operations to make it look like the target tree B. - * + * * @return Return subtrees with operations to transmute subtree A into * subtree B. */ @@ -939,7 +940,7 @@ public static NodeSet sync(NodeSet a, NodeSet b) throws JNCException { /** * Clones a YangElement. Only key children are cloned, the other children * are skipped. Attributes and values are cloned. - * + * * @return A clone of this YangElement with the same key children, * attributes and values. */ @@ -952,7 +953,7 @@ public static NodeSet sync(NodeSet a, NodeSet b) throws JNCException { *

    * Note: Used by the generated JNC classes The key children are already * cloned. - * + * * @param copy The target copy to clone the contents to * @return copy with attributes and prefix of this YangElement added. */ @@ -967,7 +968,7 @@ protected YangElement cloneShallowContent(YangElement copy) { *

    * Note: Used by the generated JNC classes. The key children should already * be cloned into copy. - * + * * @param copy The target copy to clone the contents into * @return copy with non-key children, attributes and values of this * YangElement added. @@ -995,7 +996,7 @@ protected YangElement cloneContent(YangElement copy) { /** * Read file with XML text and parse it into a data model aware * configuration tree. - * + * * @param filename File name. * @see #writeFile(String) */ diff --git a/src/main/java/com/tailf/jnc/YangUInt64.java b/src/main/java/com/tailf/jnc/YangUInt64.java index 42129275..adcbcbcb 100644 --- a/src/main/java/com/tailf/jnc/YangUInt64.java +++ b/src/main/java/com/tailf/jnc/YangUInt64.java @@ -5,7 +5,7 @@ /** * Implements the built-in YANG data type "uint64". - * + * * @author emil@tail-f.com */ public class YangUInt64 extends YangBaseInt { @@ -14,7 +14,7 @@ public class YangUInt64 extends YangBaseInt { /** * Creates a YangUInt64 object from a String. - * + * * @param s The string. * @throws YangException If value could not be parsed from s or if it is * negative or larger than 18446744073709551615. @@ -27,7 +27,7 @@ public YangUInt64(String s) throws YangException { /** * Creates a YangUInt64 object from a Number. - * + * * @param n The initial value of the new YangUInt64 object. * @throws YangException If value is negative, larger than * 18446744073709551615 or rounding is necessary. @@ -38,7 +38,7 @@ public YangUInt64(Number n) throws YangException { /** * Sets the value of this object using a Number. - * + * * @param n The new value to set. * @throws YangException If an invariant was broken during assignment or if * the number has a non-zero fractional part. @@ -53,7 +53,7 @@ public void setValue(Number n) throws YangException { /* * (non-Javadoc) - * + * * @see com.tailf.jnc.YangBaseInt#decode(java.lang.String) */ @Override @@ -67,7 +67,7 @@ protected BigInteger decode(String s) throws NumberFormatException { /* * (non-Javadoc) - * + * * @see com.tailf.jnc.YangBaseType#cloneShallow() */ @Override