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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* LiteralNode.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.StringTokenizer;
44
import javax.xml.namespace.QName;
45
import javax.xml.transform.TransformerException;
46
import org.w3c.dom.Document;
47
import org.w3c.dom.NamedNodeMap;
48
import org.w3c.dom.Node;
49
 
50
/**
51
 * A template node that copies a DOM node in the template to the result
52
 * tree.
53
 *
54
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
55
 */
56
final class LiteralNode
57
  extends TemplateNode
58
{
59
 
60
  /**
61
   * The source node in the XSL template.
62
   */
63
  final Node source;
64
 
65
  final Collection elementExcludeResultPrefixes;
66
 
67
  LiteralNode(Node source)
68
  {
69
    this.source = source;
70
    if (source.getNodeType() == Node.ELEMENT_NODE)
71
      {
72
        NamedNodeMap attrs = source.getAttributes();
73
        Node attr = attrs.getNamedItemNS(Stylesheet.XSL_NS,
74
                                         "exclude-result-prefixes");
75
        if (attr != null)
76
          {
77
            elementExcludeResultPrefixes = new HashSet();
78
            StringTokenizer st = new StringTokenizer(attr.getNodeValue());
79
            while (st.hasMoreTokens())
80
              {
81
                elementExcludeResultPrefixes.add(st.nextToken());
82
              }
83
          }
84
        else
85
          {
86
            elementExcludeResultPrefixes = Collections.EMPTY_SET;
87
          }
88
      }
89
    else
90
      {
91
        elementExcludeResultPrefixes = null;
92
      }
93
  }
94
 
95
  TemplateNode clone(Stylesheet stylesheet)
96
  {
97
    TemplateNode ret = new LiteralNode(source);
98
    if (children != null)
99
      {
100
        ret.children = children.clone(stylesheet);
101
      }
102
    if (next != null)
103
      {
104
        ret.next = next.clone(stylesheet);
105
      }
106
    return ret;
107
  }
108
 
109
  void doApply(Stylesheet stylesheet, QName mode,
110
             Node context, int pos, int len,
111
             Node parent, Node nextSibling)
112
    throws TransformerException
113
  {
114
    Node result = null;
115
    Document doc = (parent instanceof Document) ? (Document) parent :
116
      parent.getOwnerDocument();
117
    short nodeType = source.getNodeType();
118
    if (nodeType == Node.ATTRIBUTE_NODE &&
119
        parent.getFirstChild() != null)
120
      {
121
        // Ignore attributes added after child elements
122
      }
123
    else
124
      {
125
        // Namespace aliasing
126
        if (nodeType == Node.ELEMENT_NODE)
127
          {
128
            String prefix = source.getPrefix();
129
            if (prefix == null)
130
              {
131
                prefix = "#default";
132
              }
133
            String resultPrefix =
134
              (String) stylesheet.namespaceAliases.get(prefix);
135
            if (resultPrefix != null)
136
              {
137
                if ("#default".equals(resultPrefix))
138
                  {
139
                    resultPrefix = null;
140
                  }
141
                String uri = source.lookupNamespaceURI(resultPrefix);
142
                String name = source.getNodeName();
143
                // Create a new element node in the result document
144
                result = doc.createElementNS(uri, name);
145
                // copy attributes
146
                NamedNodeMap srcAttrs = source.getAttributes();
147
                NamedNodeMap dstAttrs = result.getAttributes();
148
                int l = srcAttrs.getLength();
149
                for (int i = 0; i < l; i++)
150
                  {
151
                    Node attr = srcAttrs.item(i);
152
                    if (!Stylesheet.XSL_NS.equals(attr.getNamespaceURI()))
153
                      {
154
                        attr = attr.cloneNode(true);
155
                        attr = doc.adoptNode(attr);
156
                        dstAttrs.setNamedItemNS(attr);
157
                      }
158
                  }
159
              }
160
          }
161
        if (result == null)
162
          {
163
            // Create result node
164
            result = source.cloneNode(false);
165
            // Remove any XSL attributes
166
            NamedNodeMap attrs = result.getAttributes();
167
            if (attrs != null)
168
              {
169
                int l = attrs.getLength();
170
                for (int i = 0; i < l; i++)
171
                  {
172
                    Node attr = attrs.item(i);
173
                    if (Stylesheet.XSL_NS.equals(attr.getNamespaceURI()))
174
                      {
175
                        attrs.removeNamedItem(attr.getNodeName());
176
                        i--;
177
                        l--;
178
                      }
179
                  }
180
              }
181
            Node result2 = doc.adoptNode(result);
182
            if (result2 == null)
183
              {
184
                String msg = "Error adopting node to result tree: " +
185
                  result + " (" + result.getClass().getName() + ")";
186
                DOMSourceLocator l = new DOMSourceLocator(context);
187
                throw new TransformerException(msg, l);
188
              }
189
            result = result2;
190
          }
191
        if (nextSibling != null)
192
          {
193
            parent.insertBefore(result, nextSibling);
194
          }
195
        else
196
          {
197
            parent.appendChild(result);
198
          }
199
        if (nodeType == Node.ELEMENT_NODE)
200
          {
201
            stylesheet.addNamespaceNodes(source, result, doc,
202
                                         elementExcludeResultPrefixes);
203
          }
204
        // children
205
        if (children != null)
206
          {
207
            children.apply(stylesheet, mode,
208
                           context, pos, len,
209
                           result, null);
210
          }
211
      }
212
    // next sibling
213
    if (next != null)
214
      {
215
        next.apply(stylesheet, mode,
216
                   context, pos, len,
217
                   parent, nextSibling);
218
      }
219
  }
220
 
221
  public String toString()
222
  {
223
    StringBuffer buf = new StringBuffer(getClass().getName());
224
    buf.append('[');
225
    buf.append("source=");
226
    buf.append(source);
227
    buf.append(']');
228
    return buf.toString();
229
  }
230
 
231
}

powered by: WebSVN 2.1.0

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