From 394f9f2bb6fb6260a176fca4eac090385c433e4c Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Thu, 11 May 2017 17:48:44 +0200 Subject: [PATCH] Handle html entities in XML files --- .../core/compatibility/Entities.scala | 99 +++++++++++++++++++ .../coursier/core/compatibility/package.scala | 16 ++- .../org.codehaus.plexus/plexus/1.0.4 | 1 + .../scala/coursier/test/CentralTests.scala | 7 ++ 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 core/jvm/src/main/scala/coursier/core/compatibility/Entities.scala create mode 100644 tests/shared/src/test/resources/resolutions/org.codehaus.plexus/plexus/1.0.4 diff --git a/core/jvm/src/main/scala/coursier/core/compatibility/Entities.scala b/core/jvm/src/main/scala/coursier/core/compatibility/Entities.scala new file mode 100644 index 000000000..d0ee727c5 --- /dev/null +++ b/core/jvm/src/main/scala/coursier/core/compatibility/Entities.scala @@ -0,0 +1,99 @@ +package coursier.core.compatibility + +object Entities { + + // Generated via https://gist.github.com/alexarchambault/79388ff31ec8cbddf6607b55ab2f6527 + + val entities = Vector( + (" ", " "), + ("¡", "¡"), + ("¢", "¢"), + ("£", "£"), + ("¤", "¤"), + ("¥", "¥"), + ("¦", "¦"), + ("§", "§"), + ("¨", "¨"), + ("©", "©"), + ("ª", "ª"), + ("«", "«"), + ("¬", "¬"), + ("­", "­"), + ("®", "®"), + ("¯", "¯"), + ("°", "°"), + ("±", "±"), + ("´", "´"), + ("µ", "µ"), + ("¶", "¶"), + ("·", "·"), + ("¸", "¸"), + ("º", "º"), + ("»", "»"), + ("¿", "¿"), + ("À", "À"), + ("Á", "Á"), + ("Â", "Â"), + ("Ã", "Ã"), + ("Ä", "Ä"), + ("Å", "Å"), + ("Æ", "Æ"), + ("Ç", "Ç"), + ("È", "È"), + ("É", "É"), + ("Ê", "Ê"), + ("Ë", "Ë"), + ("Ì", "Ì"), + ("Í", "Í"), + ("Î", "Î"), + ("Ï", "Ï"), + ("Ð", "Ð"), + ("Ñ", "Ñ"), + ("Ò", "Ò"), + ("Ó", "Ó"), + ("Ô", "Ô"), + ("Õ", "Õ"), + ("Ö", "Ö"), + ("×", "×"), + ("Ø", "Ø"), + ("Ù", "Ù"), + ("Ú", "Ú"), + ("Û", "Û"), + ("Ü", "Ü"), + ("Ý", "Ý"), + ("Þ", "Þ"), + ("ß", "ß"), + ("à", "à"), + ("á", "á"), + ("â", "â"), + ("ã", "ã"), + ("ä", "ä"), + ("å", "å"), + ("æ", "æ"), + ("ç", "ç"), + ("è", "è"), + ("é", "é"), + ("ê", "ê"), + ("ë", "ë"), + ("ì", "ì"), + ("í", "í"), + ("î", "î"), + ("ï", "ï"), + ("ð", "ð"), + ("ñ", "ñ"), + ("ò", "ò"), + ("ó", "ó"), + ("ô", "ô"), + ("õ", "õ"), + ("ö", "ö"), + ("÷", "÷"), + ("ø", "ø"), + ("ù", "ù"), + ("ú", "ú"), + ("û", "û"), + ("ü", "ü"), + ("ý", "ý"), + ("þ", "þ"), + ("ÿ", "ÿ") + ) +} diff --git a/core/jvm/src/main/scala/coursier/core/compatibility/package.scala b/core/jvm/src/main/scala/coursier/core/compatibility/package.scala index b6b63e269..87146078f 100644 --- a/core/jvm/src/main/scala/coursier/core/compatibility/package.scala +++ b/core/jvm/src/main/scala/coursier/core/compatibility/package.scala @@ -2,6 +2,8 @@ package coursier.core import coursier.util.Xml +import java.util.regex.Pattern.quote + import scala.collection.JavaConverters._ import scala.xml.{ Attribute, MetaData, Null } @@ -14,11 +16,23 @@ package object compatibility { def letter = c.isLetter } + private val entityPattern = (quote("&") + "[a-zA-Z]+" + quote(";")).r + private val utf8Bom = "\ufeff" def xmlParse(s: String): Either[String, Xml.Node] = { + + val content = + if (entityPattern.findFirstIn(s).isEmpty) + s + else + Entities.entities.foldLeft(s) { + case (s0, (target, replacement)) => + s0.replace(target, replacement) + } + def parse = - try Right(scala.xml.XML.loadString(s.stripPrefix(utf8Bom))) + try Right(scala.xml.XML.loadString(content.stripPrefix(utf8Bom))) catch { case e: Exception => Left(e.toString + Option(e.getMessage).fold("")(" (" + _ + ")")) } def fromNode(node: scala.xml.Node): Xml.Node = diff --git a/tests/shared/src/test/resources/resolutions/org.codehaus.plexus/plexus/1.0.4 b/tests/shared/src/test/resources/resolutions/org.codehaus.plexus/plexus/1.0.4 new file mode 100644 index 000000000..8d5ac7bf8 --- /dev/null +++ b/tests/shared/src/test/resources/resolutions/org.codehaus.plexus/plexus/1.0.4 @@ -0,0 +1 @@ +org.codehaus.plexus:plexus:1.0.4:compile diff --git a/tests/shared/src/test/scala/coursier/test/CentralTests.scala b/tests/shared/src/test/scala/coursier/test/CentralTests.scala index 63fe3e665..e494f0c1c 100644 --- a/tests/shared/src/test/scala/coursier/test/CentralTests.scala +++ b/tests/shared/src/test/scala/coursier/test/CentralTests.scala @@ -616,6 +616,13 @@ object CentralTests extends TestSuite { } } + 'entities - { + 'odash - resolutionCheck( + Module("org.codehaus.plexus", "plexus"), + "1.0.4" + ) + } + 'parentBeforeImports - { * - resolutionCheck( Module("org.kie", "kie-api"),