OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [gnu/] [xml/] [transform/] [Template.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* Template.java --
2
   Copyright (C) 2004 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
package gnu.xml.transform;
39
 
40
import java.io.PrintStream;
41
import javax.xml.namespace.QName;
42
import javax.xml.transform.TransformerException;
43
import org.w3c.dom.Node;
44
import gnu.xml.xpath.Expr;
45
import gnu.xml.xpath.NameTest;
46
import gnu.xml.xpath.NodeTypeTest;
47
import gnu.xml.xpath.Pattern;
48
import gnu.xml.xpath.Selector;
49
import gnu.xml.xpath.Test;
50
 
51
/**
52
 * A template in an XSL stylesheet.
53
 *
54
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
55
 */
56
class Template
57
  implements Comparable
58
{
59
 
60
  static final double DEFAULT_PRIORITY = 0.5d;
61
 
62
  final Stylesheet stylesheet;
63
  final QName name;
64
  final Pattern match;
65
  final TemplateNode node;
66
  final double priority;
67
  final int precedence;
68
  final QName mode;
69
 
70
  Template(Stylesheet stylesheet,
71
           QName name, Pattern match, TemplateNode node,
72
           int precedence, double priority, QName mode)
73
  {
74
    this.stylesheet = stylesheet;
75
    this.name = name;
76
    this.match = match;
77
    this.node = node;
78
    // adjust priority if necessary
79
    // see XSLT section 5.5
80
    Test test = getNodeTest(match);
81
    if (test != null)
82
      {
83
        if (test instanceof NameTest)
84
          {
85
            NameTest nameTest = (NameTest) test;
86
            if (nameTest.matchesAny() ||
87
                nameTest.matchesAnyLocalName())
88
              {
89
                priority = -0.25d;
90
              }
91
            else
92
              {
93
                priority = 0.0d;
94
              }
95
          }
96
        else
97
          {
98
            NodeTypeTest nodeTypeTest = (NodeTypeTest) test;
99
            if (nodeTypeTest.getNodeType() ==
100
                Node.PROCESSING_INSTRUCTION_NODE &&
101
                nodeTypeTest.getData() != null)
102
              {
103
                priority = 0.0d;
104
              }
105
            else
106
              {
107
                priority = -0.5d;
108
              }
109
          }
110
      }
111
    this.precedence = precedence;
112
    this.priority = priority;
113
    this.mode = mode;
114
  }
115
 
116
  Template clone(Stylesheet stylesheet)
117
  {
118
    // FIXME by cloning we lose the imports() functionality, so
119
    // apply-imports will be broken.
120
    return new Template(stylesheet,
121
                        name,
122
                        (match == null) ? null :
123
                        (Pattern) match.clone(stylesheet),
124
                        (node == null) ? null : node.clone(stylesheet),
125
                        precedence,
126
                        priority,
127
                        mode);
128
  }
129
 
130
  public int compareTo(Object other)
131
  {
132
    if (other instanceof Template)
133
      {
134
        Template t = (Template) other;
135
        int d = t.precedence - precedence;
136
        if (d != 0)
137
          {
138
            return d;
139
          }
140
        double d2 = t.priority - priority;
141
        if (d2 != 0.0d)
142
          {
143
            return (int) Math.round(d2 * 1000.0d);
144
          }
145
      }
146
    return 0;
147
  }
148
 
149
  Test getNodeTest(Expr expr)
150
  {
151
    if (expr instanceof Selector)
152
      {
153
        Selector selector = (Selector) expr;
154
        Test[] tests = selector.getTests();
155
        if (tests.length > 0)
156
          {
157
            return tests[0];
158
          }
159
      }
160
    return null;
161
  }
162
 
163
  boolean matches(QName mode, Node node)
164
  {
165
    if ((mode == null && this.mode != null) ||
166
        (mode != null && !mode.equals(this.mode)))
167
      {
168
        return false;
169
      }
170
    if (match == null)
171
      {
172
        return false;
173
      }
174
    return match.matches(node);
175
  }
176
 
177
  boolean matches(QName name)
178
  {
179
    return name.equals(this.name);
180
  }
181
 
182
  boolean imports(Template other)
183
  {
184
    for (Stylesheet ctx = other.stylesheet.parent;
185
         ctx != null;
186
         ctx = ctx.parent)
187
      {
188
        if (ctx == stylesheet)
189
          {
190
            return true;
191
          }
192
      }
193
    return false;
194
  }
195
 
196
  /**
197
   * @param stylesheet the stylesheet
198
   * @param parent the parent of result nodes
199
   * @param context the context node in the source document
200
   * @param pos the context position
201
   * @param len the context size
202
   * @param nextSibling if non-null, add result nodes before this node
203
   */
204
  void apply(Stylesheet stylesheet, QName mode,
205
             Node context, int pos, int len,
206
             Node parent, Node nextSibling)
207
    throws TransformerException
208
  {
209
    System.err.println("...applying " + toString() + " to " + context);
210
    if (node != null)
211
      {
212
        node.apply(stylesheet, mode,
213
                   context, pos, len,
214
                   parent, nextSibling);
215
      }
216
  }
217
 
218
  public String toString()
219
  {
220
    StringBuffer buf = new StringBuffer(getClass().getName());
221
    buf.append('[');
222
    if (name != null)
223
      {
224
        buf.append("name=");
225
        buf.append(name);
226
      }
227
    else if (match != null)
228
      {
229
        buf.append("match=");
230
        buf.append(match);
231
      }
232
    if (mode != null)
233
      {
234
        buf.append(",mode=");
235
        buf.append(mode);
236
      }
237
    buf.append(']');
238
    return buf.toString();
239
 
240
    //return (name != null) ? name.toString() : match.toString();
241
  }
242
 
243
  void list(PrintStream out)
244
  {
245
    out.println(toString());
246
    if (node != null)
247
      {
248
        node.list(1, out, true);
249
      }
250
  }
251
 
252
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.