Monday, October 3, 2016

Scala xml RewriteRules appear to be applied out of order

Leave a Comment

I'm trying to put together an object that contains a bunch of RewriteRules and can return a RuleTransformer. That works, but for some reason the last two RewriteRules (and others, for all I know) seem to be applied out of order.

This is a simplified set of the rules I am including the RuleTransformer I'm returning:

  def getTransformer(): RuleTransformer = {     val contentSourceNodeLabels = List("contentgroup", "byline")     val copyrightReplacementMapping = Map(       """Version 1""".r  -> "All rights reserved. Used with permission.",       """Version 2""".r    -> "By example.com, © Acme Co. All rights reserved. Used with permission.",       "".r                -> "By example.com, © Acme Co. All rights reserved. Used with permission."     )     val metadataNodeOrder = List(       "title",       "subtitle",       "abstract",       "copyrighttext"     )      new RuleTransformer(       new alterCopyrightNode("contentitem", contentSourceNodeLabels, copyrightReplacementMapping),       new alterMetadataOrder(metadataNodeOrder)     )   } 

Note that the last label in the metadataNodeOrder is "copyrighttext". In order to correctly order the metadata elements, alterMetadataOrder needs the copyright element label to be changed from "copyright" to "copyrighttext" - which is what the alterCopyrightNode rule does. So the order of these rules is significant.

I added a println() to each of those rules and, in fact, they do seem to be run out of order:

> run data/articles_20120711.xml  data/articles_20120711.xml:   ..formatted   >> [alterMetadataOrder]: reordered child nodes   >> [alterCopyrightNode]: corrected copyright text   ..normalized   ..NORMALIZED XML FAILED VALIDATION   ..done [success] Total time: 1 s, completed Sep 20, 2016 3:26:33 PM 

It looks like the Scala RulesTransformer basically takes the rules as a Seq and does a .foldLeft on them, so I don't understand how I'm getting the results I'm seeing. Any ideas?

class RuleTransformer(rules: RewriteRule*) extends BasicTransformer {   override def transform(n: Node): Seq[Node] =     rules.foldLeft(super.transform(n)) { (res, rule) => rule transform res } } 

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment