@@ -380,47 +380,49 @@ public String findValue(List<String> list) {
380380 for (int k =0 ; k <list .size (); k ++) {
381381 nextElementFound = false ;
382382 String nextElementName = list .get (k );
383- if (!nextElementName .contains ("~" )){
384- try {
385- NodeList nextElementList = nextElement .
386- getElementsByTagName (nextElementName );
387- /* If the next to next(n2n) element is a filter based on
388- * text value, then do the required filtering.
383+ if (!nextElementName .contains ("~" )) {
384+ NodeList nextElementList =
385+ nextElement != null
386+ ? nextElement .getElementsByTagName (nextElementName )
387+ : null ;
388+ if (nextElementList == null || nextElementList .getLength () == 0 ) {
389+ logger .fine ("Element '" + nextElementName + "' not found in findValue()" );
390+ return null ;
391+ }
392+
393+ /* If the next‑to‑next (n2n) element is a filter based on
394+ * text value, then do the required filtering.
395+ */
396+ String n2nString = null ;
397+ if (k <list .size ()-1 )
398+ n2nString = list .get (k +1 );
399+ if (n2nString != null && n2nString .contains ("~" )) {
400+ /* Since the n2n element is a filter based on text
401+ * value( decided by '~')
402+ * we now traverse the entire NodeList to find the
403+ * correct nextElement
404+ * based on the text value of the filter.
389405 */
390- String n2nString = null ;
391- if (k <list .size ()-1 )
392- n2nString = list .get (k +1 );
393- if (n2nString != null && n2nString .contains ("~" )) {
394- /* Since the n2n element is a filter based on text
395- * value( decided by '~')
396- * we now traverse the entire NodeList to find the
397- * correct nextElement
398- * based on the text value of the filter.
399- */
400- String n2nText = n2nString .substring (n2nString .
401- indexOf ("~" ) + 1 );
402- String n2nElementName = n2nString .substring (0 ,
403- n2nString .indexOf ("~" ));
404- for (int i =0 ; i <nextElementList .getLength (); i ++) {
405- nextElement = (Element )nextElementList .item (i );
406- NodeList nodes = nextElement .getElementsByTagName (n2nElementName );
407- if (nodes .getLength () == 0 ) continue ;
408- Element n2nElement = (Element ) nodes .item (0 );
409- if (n2nElement == null || n2nElement .getFirstChild () == null ) continue ;
410- String text = trim (n2nElement .getFirstChild ().getNodeValue ());
411- if (text .equals (n2nText )) {
412- nextElementFound = true ;
413- break ;
414- }
406+ String n2nText = n2nString .substring (n2nString .
407+ indexOf ("~" ) + 1 );
408+ String n2nElementName = n2nString .substring (0 ,
409+ n2nString .indexOf ("~" ));
410+ for (int i =0 ; i <nextElementList .getLength (); i ++) {
411+ nextElement = (Element )nextElementList .item (i );
412+ NodeList nodes = nextElement .getElementsByTagName (n2nElementName );
413+ if (nodes .getLength () == 0 ) continue ;
414+ Element n2nElement = (Element ) nodes .item (0 );
415+ if (n2nElement == null || n2nElement .getFirstChild () == null ) continue ;
416+ String text = trim (n2nElement .getFirstChild ().getNodeValue ());
417+ if (text .equals (n2nText )) {
418+ nextElementFound = true ;
419+ break ;
415420 }
416- if (!nextElementFound )
417- return null ;
418- } else {
419- nextElement = (Element )nextElementList .item (0 );
420421 }
421- } catch (NullPointerException e ) {
422- logger .warning ("NullPointerException in findValue at element: " + nextElementName );
423- return null ;
422+ if (!nextElementFound )
423+ return null ;
424+ } else {
425+ nextElement = (Element )nextElementList .item (0 );
424426 }
425427 }
426428 }
@@ -458,57 +460,58 @@ public List<Node> findNodes(List<String> list) {
458460 nextElementFound = false ;
459461 String nextElementName = list .get (k );
460462 if (!nextElementName .contains ("~" )) {
461- try {
462- NodeList nextElementList = nextElement .
463- getElementsByTagName (nextElementName );
464- /* If the next to next(n2n) element is a filter based on
465- * text value,
466- * then do the required filtering.
467- * For example,
468- * ....
469- * <physical-interface>
470- * <name>ge-1/0/0</name>
471- * <logical-interface>
472- * ....
473- * In this case, the list passed to findValue function
474- * should contain (..,"physical-interface","name~ge-1/0/0",
475- * "logical-interface",..)
476- * This will fetch me the required element.
463+ NodeList nextElementList =
464+ nextElement != null
465+ ? nextElement .getElementsByTagName (nextElementName )
466+ : null ;
467+ if (nextElementList == null || nextElementList .getLength () == 0 ) {
468+ logger .fine ("Element '" + nextElementName + "' not found in findNodes()" );
469+ return null ;
470+ }
471+ /* If the next to next(n2n) element is a filter based on
472+ * text value,
473+ * then do the required filtering.
474+ * For example,
475+ * ....
476+ * <physical-interface>
477+ * <name>ge-1/0/0</name>
478+ * <logical-interface>
479+ * ....
480+ * In this case, the list passed to findValue function
481+ * should contain (..,"physical-interface","name~ge-1/0/0",
482+ * "logical-interface",..)
483+ * This will fetch me the required element.
484+ */
485+ String n2nString = null ;
486+ if (k <list .size ()-1 )
487+ n2nString = list .get (k +1 );
488+ if (n2nString != null && n2nString .contains ("~" )) {
489+ /* Since the n2n element is a filter based on text value
490+ * ( decided by '~')
491+ * we now traverse the entire NodeList to find the
492+ * correct nextElement
493+ * based on the text value of the filter.
477494 */
478- String n2nString = null ;
479- if (k <list .size ()-1 )
480- n2nString = list .get (k +1 );
481- if (n2nString != null && n2nString .contains ("~" )) {
482- /* Since the n2n element is a filter based on text value
483- * ( decided by '~')
484- * we now traverse the entire NodeList to find the
485- * correct nextElement
486- * based on the text value of the filter.
487- */
488- String n2nText = n2nString .substring (n2nString .
489- indexOf ("~" ) + 1 );
490- String n2nElementName = n2nString .substring (0 ,
491- n2nString .indexOf ("~" ));
492- for (int i =0 ; i <nextElementList .getLength (); i ++) {
493- nextElement = (Element )nextElementList .item (i );
494- Element n2nElement = (Element )nextElement .
495- getElementsByTagName (n2nElementName ).item (0 );
496- String text = n2nElement .getFirstChild ().
497- getNodeValue ();
498- text = trim (text );
499- if (text .equals (n2nText )) {
500- nextElementFound = true ;
501- break ;
502- }
495+ String n2nText = n2nString .substring (n2nString .
496+ indexOf ("~" ) + 1 );
497+ String n2nElementName = n2nString .substring (0 ,
498+ n2nString .indexOf ("~" ));
499+ for (int i =0 ; i <nextElementList .getLength (); i ++) {
500+ nextElement = (Element )nextElementList .item (i );
501+ Element n2nElement = (Element )nextElement .
502+ getElementsByTagName (n2nElementName ).item (0 );
503+ String text = n2nElement .getFirstChild ().
504+ getNodeValue ();
505+ text = trim (text );
506+ if (text .equals (n2nText )) {
507+ nextElementFound = true ;
508+ break ;
503509 }
504- if (!nextElementFound )
505- return null ;
506- } else {
507- nextElement = (Element )nextElementList .item (0 );
508510 }
509- } catch (NullPointerException e ) {
510- logger .warning ("NullPointerException in findNodes at element: " + nextElementName );
511- return null ;
511+ if (!nextElementFound )
512+ return null ;
513+ } else {
514+ nextElement = (Element )nextElementList .item (0 );
512515 }
513516 }
514517 }
0 commit comments