~ fix tests by taking care of templates
authorAlexandre Bertails <bertails@w3.org>
Wed, 27 Jul 2011 17:18:40 -0400
changeset 6 7e0ad96e9467
parent 5 70417e3eadfd
child 7 4fbcf56effae
~ fix tests by taking care of templates
src/main/scala/FragmentDependencies.scala
--- a/src/main/scala/FragmentDependencies.scala	Wed Jul 27 16:35:09 2011 -0400
+++ b/src/main/scala/FragmentDependencies.scala	Wed Jul 27 17:18:40 2011 -0400
@@ -46,23 +46,37 @@
   * we're only interested in the backward and forward dependencies
   *
   * @param definedIn an optional String telling where this recipe comes from
+  * @param targetsPage a page containing potential targets for this recipe
   * @param regex a regex describing the target of this recipe
   * @param template an optional link to the template used to bake the target
   * @param rules the rules where the backward dependencies are found
   */
 case class Recipe (
   val definedIn:Option[String],
+  val targetsPage:Page,
   val regex: Regex,
   val template: Option[URL],
   val rules: Seq[Rule]
 ) {
 
+  /** the pages concerned by the uriList tag.
+    * It's needed only for a forward dependency on a page used as a template
+    * It's computed only when needed (lazy val)
+    */
+  lazy val targets:Seq[Page] = targetsPage.extractPages
+
   override def toString:String =
     definedIn getOrElse "<unknown>"
 
   /** computes the forward dependencies for the given page  */
   def impactedPage(page:Page):Seq[Page] =
-    rules flatMap ( _.impactedPage(page, regex) )
+    template match {
+      // if the given page is used as a template,
+      // returns the pages from the <uriList>
+      case Some(t) if t == page.url => targets
+      // else, be clever and delegate to the rules
+      case _                        => rules flatMap ( _.impactedPage(page, regex) )
+    }
 
 }
 
@@ -108,15 +122,22 @@
 
 object Recipe {
 
-  def fromElem(e:Elem, from:Option[String] = None):Recipe = {
-    val regex = new Regex(e.attrs get "regexp" orElse (e.attrs get "href") getOrElse error("couldn't find regexp attribute in " + (from getOrElse "<unknown>")))
+  def fromElem(e:Elem, from:Option[String], targetsPage:Page):Recipe = {
+    val fromString = from getOrElse "<unknown>"
+    val regex = new Regex(e.attrs get "regexp" orElse (e.attrs get "href") getOrElse error("couldn't find regexp attribute in " + fromString))
     val template = (e \ "template").headOption flatMap ( _.attrs get "src" ) map ( new URL(_) )
     val rules = e \ "rule" map ( rule => Rule.fromNode(rule) )
-    Recipe(from, regex, template, rules)
+    Recipe(from, targetsPage, regex, template, rules)
   }
 
   def fromXML(xml:Elem, from:Option[String] = None):Seq[Recipe] = {
-    xml \ "page" map ( fromElem(_, from) )
+    val fromString = from getOrElse "<unknown>"
+    val targetsPage = {
+      val uriList = (xml \ "uriList").headOption getOrElse error("no uriList for " + fromString)
+      val page = Page(new URL(uriList.attrs get "src" getOrElse error("no src in <uriList> for " + fromString)))
+      page
+    }
+    xml \ "page" map ( fromElem(_, from, targetsPage) )
   }
 
   def fromSource(source:Source, from:Option[String] = None):Seq[Recipe] =