-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to ignore child ordering in comparisons (#4)
- Loading branch information
1 parent
324cc40
commit 571701c
Showing
9 changed files
with
421 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
xml-compare/src/main/scala/software/purpledragon/xml/XmlUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2017 Michael Stringer | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package software.purpledragon.xml | ||
|
||
import scala.xml.Node | ||
|
||
/** | ||
* Utilities for dealing with XML. | ||
*/ | ||
object XmlUtils { | ||
|
||
/** | ||
* Extracts names of attributes and a map of attributes from an XML [[scala.xml.Node Node]]. | ||
* | ||
* @param node the XML node to extract attributes for. | ||
* @return a sequence of attribute names and a map of attribute values. | ||
*/ | ||
def extractAttributes(node: Node): (Seq[String], Map[String, String]) = { | ||
node.attributes.foldLeft(Seq.empty[String], Map.empty[String, String]) { | ||
case ((keys, attribs), attrib) => | ||
(keys :+ attrib.key, attribs + (attrib.key -> attrib.value.text)) | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
xml-compare/src/main/scala/software/purpledragon/xml/compare/NormalisedNodeOrdering.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2017 Michael Stringer | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package software.purpledragon.xml.compare | ||
|
||
import software.purpledragon.xml.XmlUtils.extractAttributes | ||
|
||
import scala.xml._ | ||
|
||
/** | ||
* Arbitrary ordering for XML nodes used to normalise XML when we are ignoring child ordering. | ||
*/ | ||
private[compare] object NormalisedNodeOrdering extends Ordering[Node] { | ||
private def typeToOrdering(node: Node): Int = { | ||
node match { | ||
case _: Elem => 1 | ||
case _: Text => 2 | ||
case _: PCData => 3 | ||
case _: Comment => 4 | ||
} | ||
} | ||
|
||
override def compare(x: Node, y: Node): Int = { | ||
(x, y) match { | ||
case (xe: Elem, ye: Elem) => | ||
val labelOrder = xe.label compareTo ye.label | ||
|
||
if (labelOrder != 0) { | ||
labelOrder | ||
} else { | ||
val (xAttributeNames, xAttributes) = extractAttributes(xe) | ||
val (yAttributeNames, yAttributes) = extractAttributes(ye) | ||
|
||
// order by attribute count | ||
val attributeSizeOrder = xAttributeNames.size compareTo yAttributeNames.size | ||
|
||
if (attributeSizeOrder != 0) { | ||
attributeSizeOrder | ||
} else { | ||
// compare attribute names | ||
val attributeNamesOrder = xAttributeNames.sorted zip yAttributeNames.sorted map { | ||
case (x, y) => x compareTo y | ||
} | ||
|
||
// take first difference | ||
attributeNamesOrder.find(_ != 0) match { | ||
case Some(v) => | ||
v | ||
case None => | ||
// if not compare values | ||
val attributeValuesOrder = xAttributeNames map { name => | ||
xAttributes(name) compareTo yAttributes(name) | ||
} | ||
|
||
attributeValuesOrder.find(_ != 0).getOrElse(0) | ||
} | ||
} | ||
} | ||
|
||
case (xe: Text, ye: Text) => | ||
xe.text compareTo ye.text | ||
|
||
case (xe: PCData, ye: PCData) => | ||
xe.data compareTo ye.data | ||
|
||
case (xe: Comment, ye: Comment) => | ||
xe.commentText compareTo ye.commentText | ||
|
||
case _ => | ||
// different types - order by type | ||
typeToOrdering(x) compareTo typeToOrdering(y) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
xml-compare/src/test/scala/software/purpledragon/xml/XmlUtilsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2017 Michael Stringer | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package software.purpledragon.xml | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class XmlUtilsSpec extends FlatSpec with Matchers { | ||
"XmlUtils.extractAttributes" should "return empty values for no attributes" in { | ||
XmlUtils.extractAttributes(<empty/>) shouldBe (Nil, Map.empty[String, String]) | ||
} | ||
|
||
it should "return attribute names and value map" in { | ||
val (names, attributes) = XmlUtils.extractAttributes(<test field3="value-3" field1="value-1" field2="value-2"/>) | ||
|
||
names shouldBe Seq("field3", "field1", "field2") | ||
attributes shouldBe Map( | ||
"field1" -> "value-1", | ||
"field2" -> "value-2", | ||
"field3" -> "value-3" | ||
) | ||
} | ||
} |
Oops, something went wrong.