--- /home/cpdev/src/classpath/gnu/xml/transform/Template.java	2006-01-16 17:32:45.000000000 +0000
+++ gnu/xml/transform/Template.java	2005-09-14 21:51:33.000000000 +0000
@@ -1,5 +1,5 @@
 /* Template.java -- 
-   Copyright (C) 2004,2006 Free Software Foundation, Inc.
+   Copyright (C) 2004 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -66,77 +66,51 @@
   final double priority;
   final int precedence;
   final QName mode;
-  final boolean isAnyNode; // is the match simply "node()"?
 
   Template(Stylesheet stylesheet, 
            QName name, Pattern match, TemplateNode node,
-           int precedence, String priorityAttr, QName mode)
+           int precedence, double priority, QName mode)
   {
     this.stylesheet = stylesheet;
     this.name = name;
     this.match = match;
     this.node = node;
-    this.precedence = precedence;
-    this.mode = mode;
-    
-    double p = DEFAULT_PRIORITY;
-    boolean a = false;
-    if (priorityAttr != null)
-      p = Double.parseDouble(priorityAttr);
-    else
-      {
-        // adjust priority if necessary
-        // see XSLT section 5.5
-        if (match instanceof Selector)
+    // adjust priority if necessary
+    // see XSLT section 5.5
+    Test test = getNodeTest(match);
+    if (test != null)
+      {
+        if (test instanceof NameTest)
           {
-            Selector selector = (Selector) match;
-            Test[] tests = selector.getTests();
-            if (tests.length > 0)
+            NameTest nameTest = (NameTest) test;
+            if (nameTest.matchesAny() ||
+                nameTest.matchesAnyLocalName())
+              {
+                priority = -0.25d;
+              }
+            else
               {
-                Test test = tests[0];
-                if (test instanceof NameTest)
-                  {
-                    NameTest nameTest = (NameTest) test;
-                    if (nameTest.matchesAny())
-                      p = -0.25d;
-                    else if (nameTest.matchesAnyLocalName())
-                      p = -0.20d;
-                    else
-                      p = 0.0d;
-                  }
-                else
-                  {
-                    NodeTypeTest nodeTypeTest = (NodeTypeTest) test;
-                    if (nodeTypeTest.getNodeType() ==
-                        Node.PROCESSING_INSTRUCTION_NODE &&
-                        nodeTypeTest.getData() != null)
-                      p = 0.0d;
-                    else
-                      p = -0.5d;
-                    a = (nodeTypeTest.getNodeType() == 0);
-                  }
-                // Add a small difference for predicates
-                if (tests.length > 1)
-                  p += ((double) tests.length - 1) * 0.001;
+                priority = 0.0d;
+              }
+          }
+        else
+          {
+            NodeTypeTest nodeTypeTest = (NodeTypeTest) test;
+            if (nodeTypeTest.getNodeType() ==
+                Node.PROCESSING_INSTRUCTION_NODE &&
+                nodeTypeTest.getData() != null)
+              {
+                priority = 0.0d;
+              }
+            else
+              {
+                priority = -0.5d;
               }
           }
       }
-    this.priority = p;
-    this.isAnyNode = a;
-  }
-  
-  private Template(Stylesheet stylesheet, 
-           QName name, Pattern match, TemplateNode node,
-           int precedence, double priority, QName mode, boolean isAnyNode)
-  {
-    this.stylesheet = stylesheet;
-    this.name = name;
-    this.match = match;
-    this.node = node;
     this.precedence = precedence;
     this.priority = priority;
     this.mode = mode;
-    this.isAnyNode = isAnyNode;
   }
 
   Template clone(Stylesheet stylesheet)
@@ -150,8 +124,7 @@
                         (node == null) ? null : node.clone(stylesheet),
                         precedence,
                         priority,
-                        mode,
-                        isAnyNode);
+                        mode);
   }
   
   public int compareTo(Object other)
@@ -161,16 +134,29 @@
         Template t = (Template) other;
         int d = t.precedence - precedence;
         if (d != 0)
-          return d;
+          {
+            return d;
+          }
         double d2 = t.priority - priority;
         if (d2 != 0.0d)
-          return (int) Math.round(d2 * 1000.0d);
+          {
+            return (int) Math.round(d2 * 1000.0d);
+          }
       }
     return 0;
   }
 
   Test getNodeTest(Expr expr)
   {
+    if (expr instanceof Selector)
+      {
+        Selector selector = (Selector) expr;
+        Test[] tests = selector.getTests();
+        if (tests.length > 0)
+          {
+            return tests[0];
+          }
+      }
     return null;
   }
 
@@ -178,11 +164,13 @@
   {
     if ((mode == null && this.mode != null) ||
         (mode != null && !mode.equals(this.mode)))
-      return false;
+      {
+        return false;
+      }
     if (match == null)
-      return false;
-    if (isAnyNode && node.getNodeType() == Node.DOCUMENT_NODE)
-      return false; // don't match document node
+      {
+        return false;
+      }
     return match.matches(node);
   }
 
@@ -198,7 +186,9 @@
          ctx = ctx.parent)
       {
         if (ctx == stylesheet)
-          return true;
+          {
+            return true;
+          }
       }
     return false;
   }
@@ -216,12 +206,13 @@
              Node parent, Node nextSibling)
     throws TransformerException
   {
-    if (stylesheet.debug)
-      System.err.println("...applying " + toString() + " to " + context);
+    System.err.println("...applying " + toString() + " to " + context);
     if (node != null)
-      node.apply(stylesheet, mode,
-                 context, pos, len,
-                 parent, nextSibling);
+      {
+        node.apply(stylesheet, mode,
+                   context, pos, len,
+                   parent, nextSibling);
+      }
   }
 
   public String toString()
@@ -253,7 +244,9 @@
   {
     out.println(toString());
     if (node != null)
-      node.list(1, out, true);
+      {
+        node.list(1, out, true);
+      }
   }
 
 }
