Looser property parsing (for scala-js)

This commit is contained in:
Alexandre Archambault 2015-06-18 00:25:16 +02:00
parent e65ef0ecfe
commit fd495cc9f8
3 changed files with 61 additions and 10 deletions

View File

@ -17,10 +17,7 @@ package object compatibility {
def label = node.label
def child = node.child.map(fromNode)
def isText = node match { case _: scala.xml.Text => true; case _ => false }
def textContent = node match {
case t: scala.xml.Text => t.data
case _ => throw new NoSuchElementException("text of non text node")
}
def textContent = node.text
def isElement = node match { case _: scala.xml.Elem => true; case _ => false }
}

View File

@ -41,12 +41,10 @@ object Xml {
.toRightDisjunction(s"$description not found")
}
private def property(elem: Node): String \/ (String, String) = {
elem.child match {
case Seq() => \/-(elem.label -> "")
case Seq(Text(t)) => \/-(elem.label -> t)
case _ => -\/(s"Can't parse property $elem")
}
def property(elem: Node): String \/ (String, String) = {
// Not matching with Text, which fails on scala-js if the property value has xml comments
if (elem.isElement) \/-(elem.label -> elem.textContent)
else -\/(s"Can't parse property $elem")
}
// TODO Allow no version in some contexts

View File

@ -138,6 +138,62 @@ object PomParsingTests extends TestSuite {
assert(result == expected)
}
'beFineWithCommentsInProperties{
import scalaz._, Scalaz._
val properties =
"""
| <properties>
| <maven.compile.source>1.6</maven.compile.source>
| <maven.compile.target>1.6</maven.compile.target>
| <commons.componentid>io</commons.componentid>
| <commons.rc.version>RC1</commons.rc.version>
| <commons.release.version>2.4</commons.release.version>
| <commons.release.desc>(requires JDK 1.6+)</commons.release.desc>
| <commons.release.2.version>2.2</commons.release.2.version>
| <commons.release.2.desc>(requires JDK 1.5+)</commons.release.2.desc>
| <commons.jira.id>IO</commons.jira.id>
| <commons.jira.pid>12310477</commons.jira.pid>
| <commons.osgi.export>
| <!-- Explicit list of packages from IO 1.4 -->
| org.apache.commons.io;
| org.apache.commons.io.comparator;
| org.apache.commons.io.filefilter;
| org.apache.commons.io.input;
| org.apache.commons.io.output;version=1.4.9999;-noimport:=true,
| <!-- Same list plus * for new packages -->
| org.apache.commons.io;
| org.apache.commons.io.comparator;
| org.apache.commons.io.filefilter;
| org.apache.commons.io.input;
| org.apache.commons.io.output;
| org.apache.commons.io.*;version=${project.version};-noimport:=true
| </commons.osgi.export>
| </properties>
|
""".stripMargin
val parsed = core.compatibility.xmlParse(properties)
assert(parsed.isRight)
val node = parsed.right.get
assert(node.label == "properties")
val children = node.child.collect{case elem if elem.isElement => elem}
val props0 = children.toList.traverseU(Xml.property)
assert(props0.isRight)
val props = props0.getOrElse(???).toMap
assert(props.contains("commons.release.2.desc"))
assert(props.contains("commons.osgi.export"))
assert(props("commons.rc.version") == "RC1")
assert(props("commons.release.2.desc") == "(requires JDK 1.5+)")
assert(props("commons.osgi.export").contains("org.apache.commons.io.filefilter;"))
assert(props("commons.osgi.export").contains("org.apache.commons.io.input;"))
}
}
}