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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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