@@ -17,42 +17,80 @@ package com.github.simy4.xpath
1717package scala .navigator
1818
1919import navigator .Navigator
20- import xml .{ Attribute => XmlAttribute , Elem , Null , Text }
20+ import xml .{ Attribute => XmlAttribute , Elem , Null , Text , TopScope }
2121
2222import javax .xml .namespace .QName
2323
2424@ SuppressWarnings (Array (" org.wartremover.warts.Throw" ))
2525class ScalaXmlNavigator (override val root : Root ) extends Navigator [ScalaXmlNode ] with scala.compat.Converters {
26- override def parentOf (node : ScalaXmlNode ): ScalaXmlNode = node.parent
27- override def elementsOf (parent : ScalaXmlNode ): java.lang.Iterable [? <: ScalaXmlNode ] = parent.elements
28- override def attributesOf (parent : ScalaXmlNode ): java.lang.Iterable [? <: ScalaXmlNode ] = parent.attributes
29- @ throws[XmlBuilderException ](" If unable to create attribute for given node" )
30- override def createAttribute (parent : ScalaXmlNode , attribute : QName ): ScalaXmlNode =
31- parent match {
32- case e : Element =>
33- val pre = attribute.getPrefix
34- val attr = XmlAttribute (if (pre.nonEmpty) Some (pre) else None , attribute.getLocalPart, Text (" " ), Null )
35- e.node = e.node % attr
36- new Attribute (attr, e)
26+ @ SuppressWarnings (Array (" org.wartremover.warts.Null" ))
27+ def parentOf (node : ScalaXmlNode ): ScalaXmlNode = node.parent.orNull
28+ def elementsOf (parent : ScalaXmlNode ): java.lang.Iterable [? <: ScalaXmlNode ] = parent.elements
29+ def attributesOf (parent : ScalaXmlNode ): java.lang.Iterable [? <: ScalaXmlNode ] = parent.attributes
30+ def createAttribute (parent : ScalaXmlNode , attribute : QName ): ScalaXmlNode = {
31+ val pre = attribute.getPrefix
32+ val attr = XmlAttribute (if (pre.nonEmpty) Some (pre) else None , attribute.getLocalPart, Text (" " ), Null )
33+ Attribute (attr)()
34+ }
35+ def createElement (parent : ScalaXmlNode , element : QName ): ScalaXmlNode = {
36+ val scope = parent.node match {
37+ case e : Elem => e.scope
38+ case _ => TopScope
39+ }
40+ val pre = element.getPrefix
41+ val elem = Elem (if (pre.nonEmpty) pre else null , element.getLocalPart, Null , scope, minimizeEmpty = true )
42+ Element (elem)()
43+ }
44+ @ throws[XmlBuilderException ](" If unable to append prev element for given node" )
45+ def appendPrev (node : ScalaXmlNode , prepend : ScalaXmlNode ): Unit =
46+ (node, prepend) match {
47+ case (e : Element , p : Element ) =>
48+ val parent = e.parent.getOrElse(throw new XmlBuilderException (" Unable to prepend to detached node" ))
49+ val idx = e.index
50+ val parentNode = parent.node
51+ parent.node = parentNode.copy(child = parentNode.child.patch(idx, Seq (p.node, e.node), 1 ))
52+ e.index += 1
53+ p.parent = Some (parent)
3754 case _ =>
38- throw new XmlBuilderException (s " Unable to create attribute for ${parent .toString}" )
55+ throw new XmlBuilderException (s " Unable to prepend to ${node .toString}" )
3956 }
40- @ throws[XmlBuilderException ](" If unable to create element for given node" )
41- override def createElement (parent : ScalaXmlNode , element : QName ): ScalaXmlNode =
42- parent match {
43- case e : Element =>
44- val node = e.node
45- val children = node.child
46- val pre = element.getPrefix
47- val elem = Elem (if (pre.nonEmpty) pre else null , element.getLocalPart, Null , node.scope, minimizeEmpty = true )
48- e.node = node.copy(child = children :+ elem)
49- new Element (elem, children.size, e)
57+ @ throws[XmlBuilderException ](" If unable to append child element for given node" )
58+ def appendChild (parent : ScalaXmlNode , node : ScalaXmlNode ): Unit =
59+ (parent, node) match {
60+ case (parent : Root , elem : Element ) =>
61+ val node = parent.node
62+ val child = node.child
63+ parent.node = node.copy(child = child :+ elem.node)
64+ elem.index = child.size
65+ elem.parent = Some (parent)
66+ case (parent : Element , elem : Element ) =>
67+ val node = parent.node
68+ val child = node.child
69+ parent.node = node.copy(child = child :+ elem.node)
70+ elem.index = child.size
71+ elem.parent = Some (parent)
72+ case (parent : Element , attr : Attribute ) =>
73+ parent.node = parent.node % attr.node
74+ attr.parent = Some (parent)
5075 case _ =>
51- throw new XmlBuilderException (s " Unable to create element for ${parent.toString}" )
76+ throw new XmlBuilderException (s " Unable to append child ${node.toString}" )
77+ }
78+ @ throws[XmlBuilderException ](" If unable to append next element for given node" )
79+ def appendNext (node : ScalaXmlNode , append : ScalaXmlNode ): Unit =
80+ (node, append) match {
81+ case (e : Element , a : Element ) =>
82+ val parent = e.parent.getOrElse(throw new XmlBuilderException (" Unable to append to detached node" ))
83+ val idx = e.index
84+ val parentNode = parent.node
85+ parent.node = parentNode.copy(child = parentNode.child.patch(idx, Seq (e.node, a.node), 1 ))
86+ e.index += 1
87+ a.parent = Some (parent)
88+ case _ =>
89+ throw new XmlBuilderException (s " Unable to append to ${node.toString}" )
5290 }
5391 @ SuppressWarnings (Array (" org.wartremover.warts.IsInstanceOf" ))
5492 @ throws[XmlBuilderException ](" If unable to set text to given node" )
55- override def setText (node : ScalaXmlNode , text : String ): Unit =
93+ def setText (node : ScalaXmlNode , text : String ): Unit =
5694 node match {
5795 case e : Element =>
5896 val elem = e.node
@@ -63,34 +101,23 @@ class ScalaXmlNavigator(override val root: Root) extends Navigator[ScalaXmlNode]
63101 case _ =>
64102 throw new XmlBuilderException (s " Unable to set text to ${node.toString}" )
65103 }
66- @ throws[XmlBuilderException ](" If unable to prepend copy to given node" )
67- override def prependCopy (node : ScalaXmlNode ): Unit =
68- node match {
69- case e : Element =>
70- val toCopy = e.node
71- val copy = toCopy.copy()
72- val idx = e.index
73- val parentNode = e.parent.node
74- e.parent.node = parentNode.copy(child = parentNode.child.patch(idx, Seq (copy, toCopy), 1 ))
75- e.index += 1
76- case _ =>
77- throw new XmlBuilderException (s " Unable to prepend copy to ${node.toString}" )
78- }
79104 @ throws[XmlBuilderException ](" If unable to remove given node" )
80- override def remove (node : ScalaXmlNode ): Unit =
105+ def remove (node : ScalaXmlNode ): Unit =
81106 node match {
82107 case e : Element =>
83108 val idx = e.index
84- val parentNode = e.parent.node
85- e.parent.node = parentNode.copy(child = parentNode.child.patch(idx, Nil , 1 ))
109+ val parent = e.parent.getOrElse(throw new XmlBuilderException (" Unable to remove detached node" ))
110+ val parentNode = parent.node
111+ parent.node = parentNode.copy(child = parentNode.child.patch(idx, Nil , 1 ))
86112 case a : Attribute =>
87113 val toDelete = a.node
88- val parentNode = a.parent.node
114+ val parent = a.parent.getOrElse(throw new XmlBuilderException (" Unable to remove detached node" ))
115+ val parentNode = parent.node
89116 val newAttr =
90117 if (toDelete.isPrefixed)
91118 parentNode.attributes.remove(toDelete.getNamespace(parentNode), parentNode, toDelete.key)
92119 else parentNode.attributes.remove(toDelete.key)
93- a. parent.node = parentNode.copy(attributes = newAttr)
120+ parent.node = parentNode.copy(attributes = newAttr)
94121 case _ =>
95122 throw new XmlBuilderException (s " Unable to remove node ${node.toString}" )
96123 }
0 commit comments