4242import java .io .OutputStream ;
4343import java .nio .charset .Charset ;
4444import java .util .*;
45- import java .util .function .Consumer ;
4645import java .util .function .Predicate ;
4746
4847import static javax .xml .transform .OutputKeys .*;
@@ -59,7 +58,7 @@ public class NX {
5958 private final Map <Class <?>, Extractor <?>> extractors = Maps .newHashMap ();
6059
6160 public NX () {
62- this (Collections .< ConfigFeature > emptySet ());
61+ this (Collections .emptySet ());
6362 }
6463
6564 public NX (Set <ConfigFeature > features ) {
@@ -478,7 +477,7 @@ private class NodeCursor implements Cursor {
478477 private final Document document ;
479478
480479 NodeCursor (Document document , Node node ) {
481- this (document , new ArrayList <NodeCursor >(), node , 0 );
480+ this (document , new ArrayList <>(), node , 0 );
482481 }
483482
484483 NodeCursor (Document document , List <NodeCursor > ancestors , Node node , int index ) {
@@ -512,7 +511,10 @@ public Cursor append(String tagName) throws Ex {
512511 Element element = document .createElement (tagName );
513512 Node newNode = node .appendChild (element );
514513
515- return new NodeCursor (document , newNode );
514+ List <NodeCursor > ancestors = Lists .newArrayList (this .ancestors );
515+ ancestors .add (this );
516+
517+ return new NodeCursor (document , ancestors , newNode , 0 );
516518 }
517519
518520 @ Override
@@ -552,9 +554,8 @@ private Optional<Node> findSingleNode(String tagName) throws Ambiguous {
552554 final NodeList childNodes = node .getChildNodes ();
553555 for (int i = 0 ; i < childNodes .getLength (); i ++) {
554556 final Node childNode = childNodes .item (i );
555- final String localName = childNode .getLocalName ();
556557
557- if (localName != null && localName . equalsIgnoreCase ( tagName )) {
558+ if (isNamed ( childNode , tagName )) {
558559 if (found != null ) {
559560 throw new Ambiguous (this , tagName );
560561 }
@@ -567,15 +568,21 @@ private Optional<Node> findSingleNode(String tagName) throws Ambiguous {
567568 return Optional .fromNullable (found );
568569 }
569570
571+ private boolean isNamed (Node childNode , String needle ) {
572+ String nodeName = childNode .getNodeName ();
573+ String localName = childNode .getLocalName ();
574+
575+ return (nodeName != null && nodeName .equalsIgnoreCase (needle )) || (localName != null && localName .equalsIgnoreCase (needle ));
576+ }
577+
570578 @ Override
571579 public Cursor to (int position , String tagName ) throws MissingNode {
572580 int count = 0 ;
573581 final NodeList childNodes = node .getChildNodes ();
574582 for (int i = 0 ; i < childNodes .getLength (); i ++) {
575583 final Node childNode = childNodes .item (i );
576- final String localName = childNode .getLocalName ();
577584
578- if (localName != null && localName . equalsIgnoreCase ( tagName )) {
585+ if (isNamed ( childNode , tagName )) {
579586 count ++;
580587
581588 if (count == position + 1 ) {
@@ -601,9 +608,8 @@ public int count(String tagName) {
601608 final NodeList childNodes = node .getChildNodes ();
602609 for (int i = 0 ; i < childNodes .getLength (); i ++) {
603610 final Node childNode = childNodes .item (i );
604- final String localName = childNode .getLocalName ();
605611
606- if (localName != null && localName . equalsIgnoreCase ( tagName )) {
612+ if (isNamed ( childNode , tagName )) {
607613 count ++;
608614 }
609615 }
@@ -650,14 +656,9 @@ public <R> R extract(Extractor<R> extractor) throws Ex {
650656 public <R > List <R > extractCollection (String needle , final Extractor <R > extractor ) throws Ex {
651657 final List <R > result = Lists .newArrayList ();
652658
653- iterateCollection (needle , new Iterator () {
654-
655- @ Override
656- public void on (Cursor cursor ) throws Ex {
657- final R converted = cursor .extract (extractor );
658- result .add (converted );
659- }
660-
659+ iterateCollection (needle , cursor -> {
660+ final R converted = cursor .extract (extractor );
661+ result .add (converted );
661662 });
662663
663664 return result ;
@@ -670,9 +671,8 @@ public void iterateCollection(String needle, Iterator iterator) throws Ex {
670671 int count = 0 ;
671672 for (int i = 0 ; i < childNodes .getLength (); i ++) {
672673 final Node childNode = childNodes .item (i );
673- final String localName = childNode .getLocalName ();
674674
675- if (localName != null && localName . equalsIgnoreCase ( needle )) {
675+ if (isNamed ( childNode , needle )) {
676676 final List <NodeCursor > newAncestorList = newAncestorList ();
677677 final Cursor cursor = new NodeCursor (document , newAncestorList , childNode , count ++);
678678 iterator .on (cursor );
@@ -752,9 +752,13 @@ public Cursor require(Predicate<Cursor> predicate) throws Ex {
752752 int count = 0 ;
753753 for (int i = 0 ; i < childNodes .getLength (); i ++) {
754754 final Node childNode = childNodes .item (i );
755- final String localName = childNode .getLocalName ();
755+ String name = childNode .getLocalName ();
756+
757+ if (name == null ) {
758+ name = childNode .getNodeName ();
759+ }
756760
757- if (localName != null ) {
761+ if (name != null ) {
758762 final List <NodeCursor > newAncestorList = newAncestorList ();
759763 final Cursor cursor = new NodeCursor (document , newAncestorList , childNode , count ++);
760764
@@ -781,9 +785,8 @@ private Optional<Node> findAttribute(String needle) throws Ambiguous {
781785
782786 for (int i =0 ; i <attributes .getLength (); i ++) {
783787 final Node attribute = attributes .item (i );
784- final String localName = attribute .getLocalName ();
785788
786- if (localName . equalsIgnoreCase ( needle )) {
789+ if (isNamed ( attribute , needle )) {
787790 return Optional .of (attribute );
788791 }
789792 }
@@ -798,7 +801,7 @@ public String text() {
798801
799802 @ Override
800803 public String name () {
801- return node .getLocalName ();
804+ return node .getNodeName ();
802805 }
803806
804807 @ Override
@@ -918,8 +921,14 @@ private static String summarize(NodeList childNodes) {
918921 final Set <String > names = Sets .newTreeSet ();
919922 for (int i =0 ; i <childNodes .getLength (); i ++) {
920923 final Node item = childNodes .item (i );
921- if (item .getLocalName () != null ) {
922- names .add (item .getLocalName ());
924+ String name = item .getLocalName ();
925+
926+ if (name == null ) {
927+ name = item .getNodeName ();
928+ }
929+
930+ if (name != null ) {
931+ names .add (name );
923932 }
924933 }
925934
0 commit comments