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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [xml/] [transform/] [DocumentFunction.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* DocumentFunction.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.util.ArrayList;
41
import java.util.Collection;
42
import java.util.Collections;
43
import java.util.Iterator;
44
import java.util.List;
45
import java.util.TreeSet;
46
import javax.xml.namespace.QName;
47
import javax.xml.transform.TransformerException;
48
import javax.xml.transform.dom.DOMSource;
49
import javax.xml.xpath.XPathFunction;
50
import javax.xml.xpath.XPathFunctionException;
51
import org.w3c.dom.Node;
52
import gnu.xml.xpath.Constant;
53
import gnu.xml.xpath.Expr;
54
import gnu.xml.xpath.Function;
55
import gnu.xml.xpath.IdFunction;
56
 
57
/**
58
 * The XSLT <code>document()</code>function.
59
 *
60
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
61
 */
62
final class DocumentFunction
63
  extends Expr
64
  implements Function, XPathFunction
65
{
66
 
67
  final Stylesheet stylesheet;
68
  final Node base;
69
  List args;
70
  List values;
71
 
72
  DocumentFunction(Stylesheet stylesheet, Node base)
73
  {
74
    this.stylesheet = stylesheet;
75
    this.base = base;
76
  }
77
 
78
  public Object evaluate(List args)
79
    throws XPathFunctionException
80
  {
81
    values = args;
82
    return evaluate(null, 1, 1);
83
  }
84
 
85
  public void setArguments(List args)
86
  {
87
    this.args = args;
88
  }
89
 
90
  public Object evaluate(Node context, int pos, int len)
91
  {
92
    int arity = args.size();
93
    if (values == null)
94
      {
95
        values = new ArrayList(arity);
96
        for (int i = 0; i < arity; i++)
97
          {
98
            Expr arg = (Expr) args.get(i);
99
            values.add(arg.evaluate(context, pos, len));
100
          }
101
      }
102
    Object ret;
103
    switch (arity)
104
      {
105
      case 1:
106
        Object arg = values.get(0);
107
        if (arg instanceof Collection)
108
          {
109
            Collection ns = (Collection) arg;
110
            Collection acc = new TreeSet();
111
            for (Iterator i = ns.iterator(); i.hasNext(); )
112
              {
113
                Node node = (Node) i.next();
114
                String s = Expr.stringValue(node);
115
                acc.addAll(document(s, node.getBaseURI()));
116
              }
117
            ret = acc;
118
          }
119
        else
120
          {
121
            String s = Expr._string(context, arg);
122
            ret = document(s, base.getBaseURI());
123
          }
124
        break;
125
      case 2:
126
        Object arg1 = values.get(0);
127
        Object arg2 = values.get(1);
128
        if (!(arg2 instanceof Collection))
129
          throw new RuntimeException("second argument is not a node-set");
130
        Collection arg2ns = (Collection) arg2;
131
        String base2 = arg2ns.isEmpty() ? null :
132
          ((Node) arg2ns.iterator().next()).getBaseURI();
133
        if (arg1 instanceof Collection)
134
          {
135
            Collection arg1ns = (Collection) arg1;
136
            Collection acc = new TreeSet();
137
            for (Iterator i = arg1ns.iterator(); i.hasNext(); )
138
              {
139
                Node node = (Node) i.next();
140
                String s = Expr.stringValue(node);
141
                acc.addAll(document(s, base2));
142
              }
143
            ret = acc;
144
          }
145
        else
146
          {
147
            String s = Expr._string(context, arg1);
148
            ret = document(s, base2);
149
          }
150
        break;
151
      default:
152
        throw new RuntimeException("invalid arity");
153
      }
154
    values = null;
155
    return ret;
156
  }
157
 
158
  /**
159
   * The XSL <code>document</code> function.
160
   * @see XSLT 12.1
161
   * @param uri the URI from which to retrieve nodes
162
   * @param base the base URI for relative URIs
163
   */
164
  Collection document(String uri, String base)
165
  {
166
    if ("".equals(uri) || uri == null)
167
      uri = this.base.getBaseURI();
168
 
169
    // Get fragment
170
    Expr fragment = null;
171
    int hi = uri.indexOf('#');
172
    if (hi != -1)
173
      {
174
        String f = uri.substring(hi + 1);
175
        uri = uri.substring(0, hi);
176
        // TODO handle xpointer() here
177
        // this only handles IDs
178
        fragment = new IdFunction(new Constant(f));
179
      }
180
 
181
    // Get document source
182
    try
183
      {
184
        DOMSource source;
185
        XSLURIResolver resolver = stylesheet.factory.resolver;
186
        synchronized (resolver)
187
          {
188
            if (stylesheet.transformer != null)
189
              {
190
                resolver.setUserResolver(stylesheet.transformer.uriResolver);
191
                resolver.setUserListener(stylesheet.transformer.errorListener);
192
              }
193
            source = resolver.resolveDOM(null, base, uri);
194
          }
195
        Node node = source.getNode();
196
        // Strip whitespace
197
        TransformerImpl.strip(stylesheet, node);
198
        if (fragment == null)
199
          return Collections.singleton(node);
200
        else
201
          {
202
            Object ret = fragment.evaluate(node, 1, 1);
203
            if (!(ret instanceof Collection))
204
              {
205
                // XXX Report error?
206
                return Collections.EMPTY_SET;
207
              }
208
            return (Collection) ret;
209
          }
210
      }
211
    catch (TransformerException e)
212
      {
213
        String msg = "can't open " + uri;
214
        if (base != null)
215
          msg += " with base " + base;
216
        throw new RuntimeException(msg);
217
      }
218
  }
219
 
220
  public Expr clone(Object context)
221
  {
222
    Stylesheet s = stylesheet;
223
    if (context instanceof Stylesheet)
224
      s = (Stylesheet) context;
225
    DocumentFunction f = new DocumentFunction(s, base);
226
    int len = args.size();
227
    List args2 = new ArrayList(len);
228
    for (int i = 0; i < len; i++)
229
      args2.add(((Expr) args.get(i)).clone(context));
230
    f.setArguments(args2);
231
    return f;
232
  }
233
 
234
  public boolean references(QName var)
235
  {
236
    for (Iterator i = args.iterator(); i.hasNext(); )
237
      {
238
        if (((Expr) i.next()).references(var))
239
          return true;
240
      }
241
    return false;
242
  }
243
 
244
}

powered by: WebSVN 2.1.0

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