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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* AttributeNode.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 javax.xml.XMLConstants;
41
import javax.xml.namespace.QName;
42
import javax.xml.transform.TransformerException;
43
import org.w3c.dom.Document;
44
import org.w3c.dom.DocumentFragment;
45
import org.w3c.dom.Attr;
46
import org.w3c.dom.NamedNodeMap;
47
import org.w3c.dom.Node;
48
import gnu.xml.xpath.Expr;
49
 
50
/**
51
 * A template node representing an XSL <code>attribute</code> instruction.
52
 *
53
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
54
 */
55
final class AttributeNode
56
  extends TemplateNode
57
{
58
 
59
  final TemplateNode name;
60
  final TemplateNode namespace;
61
  final Node source;
62
 
63
  AttributeNode(TemplateNode name,
64
                TemplateNode namespace, Node source)
65
  {
66
    this.name = name;
67
    this.namespace = namespace;
68
    this.source = source;
69
  }
70
 
71
  TemplateNode clone(Stylesheet stylesheet)
72
  {
73
    TemplateNode ret = new AttributeNode(name.clone(stylesheet),
74
                                         (namespace == null) ? null :
75
                                         namespace.clone(stylesheet),
76
                                         source);
77
    if (children != null)
78
      {
79
        ret.children = children.clone(stylesheet);
80
      }
81
    if (next != null)
82
      {
83
        ret.next = next.clone(stylesheet);
84
      }
85
    return ret;
86
  }
87
 
88
  void doApply(Stylesheet stylesheet, QName mode,
89
               Node context, int pos, int len,
90
               Node parent, Node nextSibling)
91
    throws TransformerException
92
  {
93
    Document doc = (parent instanceof Document) ? (Document) parent :
94
      parent.getOwnerDocument();
95
    // Create a document fragment to hold the name
96
    DocumentFragment fragment = doc.createDocumentFragment();
97
    // Apply name to the fragment
98
    name.apply(stylesheet, mode,
99
               context, pos, len,
100
               fragment, null);
101
    // Use XPath string-value of fragment
102
    String nameValue = Expr.stringValue(fragment);
103
 
104
    String namespaceValue = null;
105
    if (namespace != null)
106
      {
107
        // Create a document fragment to hold the namespace
108
        fragment = doc.createDocumentFragment();
109
        // Apply namespace to the fragment
110
        namespace.apply(stylesheet, mode,
111
                        context, pos, len,
112
                        fragment, null);
113
        // Use XPath string-value of fragment
114
        namespaceValue = Expr.stringValue(fragment);
115
        if (namespaceValue.length() == 0)
116
          {
117
            namespaceValue = null;
118
          }
119
      }
120
 
121
    String prefix = getPrefix(nameValue);
122
    if (namespaceValue == null)
123
      {
124
        if (prefix != null)
125
          {
126
            if (XMLConstants.XML_NS_PREFIX.equals(prefix))
127
              {
128
                namespaceValue = XMLConstants.XML_NS_URI;
129
              }
130
            else
131
              {
132
                // Resolve namespace for this prefix
133
                namespaceValue = source.lookupNamespaceURI(prefix);
134
              }
135
          }
136
      }
137
    else
138
      {
139
        if (prefix != null)
140
          {
141
            String ns2 = source.lookupNamespaceURI(prefix);
142
            if (ns2 != null && !ns2.equals(namespaceValue))
143
              {
144
                // prefix clashes, reset it
145
                prefix = null;
146
                int ci = nameValue.indexOf(':');
147
                nameValue = nameValue.substring(ci + 1);
148
              }
149
          }
150
      }
151
    if (prefix == null)
152
      {
153
        // Resolve prefix for this namespace
154
        prefix = source.lookupPrefix(namespaceValue);
155
        if (prefix != null)
156
          {
157
            nameValue = prefix + ":" + nameValue;
158
          }
159
        else
160
          {
161
            if (namespaceValue != null)
162
              {
163
                // Must invent a prefix
164
                prefix = inventPrefix(parent);
165
                nameValue = prefix + ":" + nameValue;
166
              }
167
          }
168
      }
169
    NamedNodeMap attrs = parent.getAttributes();
170
    boolean insert = true;
171
    if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceValue) ||
172
        XMLConstants.XMLNS_ATTRIBUTE.equals(nameValue) ||
173
        nameValue.startsWith("xmlns:"))
174
      {
175
        // Namespace declaration, do not output
176
        insert = false;
177
      }
178
    if (prefix != null && namespaceValue == null)
179
      {
180
        // Not a QName
181
        insert = false;
182
      }
183
    if (parent.getNodeType() == Node.ELEMENT_NODE &&
184
        parent.getFirstChild() != null)
185
      {
186
        // XSLT 7.1.3 Adding an attribute to an element after children have
187
        // been added to it is an error
188
        insert = false;
189
      }
190
    if (insert)
191
      {
192
        // Insert attribute
193
        Attr attr = (namespaceValue != null) ?
194
          doc.createAttributeNS(namespaceValue, nameValue) :
195
              doc.createAttribute(nameValue);
196
        if (attrs != null)
197
          {
198
            if (namespace != null)
199
              {
200
                attrs.setNamedItemNS(attr);
201
              }
202
            else
203
              {
204
                attrs.setNamedItem(attr);
205
              }
206
          }
207
        if (children != null)
208
          {
209
            children.apply(stylesheet, mode,
210
                           context, pos, len,
211
                           attr, null);
212
          }
213
      }
214
    if (next != null)
215
      {
216
        next.apply(stylesheet, mode,
217
                   context, pos, len,
218
                   parent, nextSibling);
219
      }
220
  }
221
 
222
  final String getPrefix(String name)
223
  {
224
    int ci = name.indexOf(':');
225
    return (ci == -1) ? null : name.substring(0, ci);
226
  }
227
 
228
  final String inventPrefix(Node parent)
229
  {
230
    String base = "ns";
231
    int count = 0;
232
    String ret = base + Integer.toString(count);
233
    while (parent.lookupNamespaceURI(ret) != null)
234
      {
235
        count++;
236
        ret = base + Integer.toString(count);
237
      }
238
    return ret;
239
  }
240
 
241
  public boolean references(QName var)
242
  {
243
    if (name != null && name.references(var))
244
      {
245
        return true;
246
      }
247
    if (namespace != null && namespace.references(var))
248
      {
249
        return true;
250
      }
251
    return super.references(var);
252
  }
253
 
254
  public String toString()
255
  {
256
    StringBuffer buf = new StringBuffer(getClass().getName());
257
    buf.append('[');
258
    buf.append("name=");
259
    buf.append(name);
260
    buf.append(']');
261
    return buf.toString();
262
  }
263
 
264
}

powered by: WebSVN 2.1.0

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