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/] [ElementNode.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* ElementNode.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.Collection;
41
import java.util.Collections;
42
import java.util.HashSet;
43
import java.util.Iterator;
44
import java.util.StringTokenizer;
45
import javax.xml.XMLConstants;
46
import javax.xml.namespace.QName;
47
import javax.xml.transform.TransformerException;
48
import org.w3c.dom.Document;
49
import org.w3c.dom.DocumentFragment;
50
import org.w3c.dom.Element;
51
import org.w3c.dom.NamedNodeMap;
52
import org.w3c.dom.Node;
53
import gnu.xml.xpath.Expr;
54
 
55
/**
56
 * A template node representing an XSL <code>element</code> instruction.
57
 *
58
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
59
 */
60
final class ElementNode
61
  extends TemplateNode
62
{
63
 
64
  final TemplateNode name;
65
  final TemplateNode namespace;
66
  final String uas;
67
  final Node source;
68
  final Collection elementExcludeResultPrefixes;
69
 
70
  ElementNode(TemplateNode name,
71
              TemplateNode namespace, String uas, Node source)
72
  {
73
    this.name = name;
74
    this.namespace = namespace;
75
    this.uas = uas;
76
    this.source = source;
77
    NamedNodeMap attrs = source.getAttributes();
78
    Node attr = attrs.getNamedItemNS(Stylesheet.XSL_NS,
79
                                     "exclude-result-prefixes");
80
    if (attr != null)
81
      {
82
        elementExcludeResultPrefixes = new HashSet();
83
        StringTokenizer st = new StringTokenizer(attr.getNodeValue());
84
        while (st.hasMoreTokens())
85
          {
86
            elementExcludeResultPrefixes.add(st.nextToken());
87
          }
88
      }
89
    else
90
      {
91
        elementExcludeResultPrefixes = Collections.EMPTY_SET;
92
      }
93
  }
94
 
95
  TemplateNode clone(Stylesheet stylesheet)
96
  {
97
    TemplateNode ret = new ElementNode(name.clone(stylesheet),
98
                                       (namespace == null) ? null :
99
                                       namespace.clone(stylesheet),
100
                                       uas, source);
101
    if (children != null)
102
      {
103
        ret.children = children.clone(stylesheet);
104
      }
105
    if (next != null)
106
      {
107
        ret.next = next.clone(stylesheet);
108
      }
109
    return ret;
110
  }
111
 
112
  void doApply(Stylesheet stylesheet, QName mode,
113
             Node context, int pos, int len,
114
             Node parent, Node nextSibling)
115
    throws TransformerException
116
  {
117
    Document doc = (parent instanceof Document) ? (Document) parent :
118
      parent.getOwnerDocument();
119
    // Create a document fragment to hold the name
120
    DocumentFragment fragment = doc.createDocumentFragment();
121
    // Apply name to the fragment
122
    name.apply(stylesheet, mode,
123
               context, pos, len,
124
               fragment, null);
125
    // Use XPath string-value of fragment
126
    String nameValue = Expr.stringValue(fragment);
127
 
128
    String namespaceValue = null;
129
    if (namespace != null)
130
      {
131
        // Create a document fragment to hold the namespace
132
        fragment = doc.createDocumentFragment();
133
        // Apply namespace to the fragment
134
        namespace.apply(stylesheet, mode,
135
                        context, pos, len,
136
                        fragment, null);
137
        // Use XPath string-value of fragment
138
        namespaceValue = Expr.stringValue(fragment);
139
        if (namespaceValue.length() == 0)
140
          {
141
            namespaceValue = null;
142
          }
143
      }
144
 
145
    String prefix = getPrefix(nameValue);
146
    if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
147
      {
148
        int ci = nameValue.indexOf(':');
149
        nameValue = nameValue.substring(ci + 1);
150
      }
151
    else
152
      {
153
        // Namespace aliasing
154
        if (prefix == null)
155
          {
156
            prefix = "#default";
157
          }
158
        String resultPrefix =
159
          (String) stylesheet.namespaceAliases.get(prefix);
160
        if (resultPrefix != null)
161
          {
162
            if ("#default".equals(resultPrefix))
163
              {
164
                resultPrefix = null;
165
              }
166
            namespaceValue = source.lookupNamespaceURI(resultPrefix);
167
          }
168
        if (prefix == "#default")
169
          {
170
            prefix = null;
171
          }
172
        // Look up ordinary namespace for this prefix
173
        if (namespaceValue == null)
174
          {
175
            if (XMLConstants.XML_NS_PREFIX.equals(prefix))
176
              {
177
                namespaceValue = XMLConstants.XML_NS_URI;
178
              }
179
            else
180
              {
181
                // Resolve namespace for this prefix
182
                namespaceValue = source.lookupNamespaceURI(prefix);
183
              }
184
          }
185
        /*if (prefix == null)
186
          {
187
            // Resolve prefix for this namespace
188
            prefix = parent.lookupPrefix(namespaceValue);
189
            if (prefix != null)
190
              {
191
                nameValue = prefix + ":" + nameValue;
192
              }
193
          }*/
194
      }
195
    // Create element
196
    Element element = (namespaceValue != null) ?
197
      doc.createElementNS(namespaceValue, nameValue) :
198
          doc.createElement(nameValue);
199
    if (nextSibling != null)
200
      {
201
        parent.insertBefore(element, nextSibling);
202
      }
203
    else
204
      {
205
        parent.appendChild(element);
206
      }
207
    stylesheet.addNamespaceNodes(source, element, doc,
208
                                 elementExcludeResultPrefixes);
209
    if (uas != null)
210
      {
211
        StringTokenizer st = new StringTokenizer(uas, " ");
212
        while (st.hasMoreTokens())
213
          {
214
            addAttributeSet(stylesheet, mode, context, pos, len,
215
                            element, null, st.nextToken());
216
          }
217
      }
218
    if (children != null)
219
      {
220
        children.apply(stylesheet, mode,
221
                       context, pos, len,
222
                       element, null);
223
      }
224
    if (next != null)
225
      {
226
        next.apply(stylesheet, mode,
227
                   context, pos, len,
228
                   parent, nextSibling);
229
      }
230
  }
231
 
232
  final String getPrefix(String name)
233
  {
234
    int ci = name.indexOf(':');
235
    return (ci == -1) ? null : name.substring(0, ci);
236
  }
237
 
238
  void addAttributeSet(Stylesheet stylesheet, QName mode,
239
                       Node context, int pos, int len,
240
                       Node parent, Node nextSibling, String attributeSet)
241
    throws TransformerException
242
  {
243
    for (Iterator i = stylesheet.attributeSets.iterator(); i.hasNext(); )
244
      {
245
        AttributeSet as = (AttributeSet) i.next();
246
        if (!as.name.equals(attributeSet))
247
          {
248
            continue;
249
          }
250
        if (as.uas != null)
251
          {
252
            StringTokenizer st = new StringTokenizer(as.uas, " ");
253
            while (st.hasMoreTokens())
254
              {
255
                addAttributeSet(stylesheet, mode, context, pos, len,
256
                                parent, nextSibling, st.nextToken());
257
              }
258
          }
259
        if (as.children != null)
260
          {
261
            as.children.apply(stylesheet, mode,
262
                              context, pos, len,
263
                              parent, nextSibling);
264
          }
265
      }
266
  }
267
 
268
  public boolean references(QName var)
269
  {
270
    if (name != null && name.references(var))
271
      {
272
        return true;
273
      }
274
    if (namespace != null && namespace.references(var))
275
      {
276
        return true;
277
      }
278
    return super.references(var);
279
  }
280
 
281
  public String toString()
282
  {
283
    StringBuffer buf = new StringBuffer(getClass().getName());
284
    buf.append('[');
285
    buf.append("name=");
286
    buf.append(name);
287
    if (uas != null)
288
      {
289
        buf.append(",uas=");
290
        buf.append(uas);
291
      }
292
    buf.append(']');
293
    return buf.toString();
294
  }
295
 
296
}

powered by: WebSVN 2.1.0

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