Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / xml / transform / ElementNode.java
blobb6a5c365b120fdf4ba317958721f221cca8d3e55
1 /* ElementNode.java --
2 Copyright (C) 2004,2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package gnu.xml.transform;
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;
55 /**
56 * A template node representing an XSL <code>element</code> instruction.
58 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
60 final class ElementNode
61 extends TemplateNode
64 final TemplateNode name;
65 final TemplateNode namespace;
66 final String uas;
67 final Node source;
68 final Collection elementExcludeResultPrefixes;
70 ElementNode(TemplateNode name,
71 TemplateNode namespace, String uas, Node source)
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)
82 elementExcludeResultPrefixes = new HashSet();
83 StringTokenizer st = new StringTokenizer(attr.getNodeValue());
84 while (st.hasMoreTokens())
85 elementExcludeResultPrefixes.add(st.nextToken());
87 else
88 elementExcludeResultPrefixes = Collections.EMPTY_SET;
91 TemplateNode clone(Stylesheet stylesheet)
93 TemplateNode ret = new ElementNode(name.clone(stylesheet),
94 (namespace == null) ? null :
95 namespace.clone(stylesheet),
96 uas, source);
97 if (children != null)
98 ret.children = children.clone(stylesheet);
99 if (next != null)
100 ret.next = next.clone(stylesheet);
101 return ret;
104 void doApply(Stylesheet stylesheet, QName mode,
105 Node context, int pos, int len,
106 Node parent, Node nextSibling)
107 throws TransformerException
109 Document doc = (parent instanceof Document) ? (Document) parent :
110 parent.getOwnerDocument();
111 // Create a document fragment to hold the name
112 DocumentFragment fragment = doc.createDocumentFragment();
113 // Apply name to the fragment
114 name.apply(stylesheet, mode,
115 context, pos, len,
116 fragment, null);
117 // Use XPath string-value of fragment
118 String nameValue = Expr.stringValue(fragment);
120 String namespaceValue = null;
121 if (namespace != null)
123 // Create a document fragment to hold the namespace
124 fragment = doc.createDocumentFragment();
125 // Apply namespace to the fragment
126 namespace.apply(stylesheet, mode,
127 context, pos, len,
128 fragment, null);
129 // Use XPath string-value of fragment
130 namespaceValue = Expr.stringValue(fragment);
131 if (namespaceValue.length() == 0)
132 namespaceValue = null;
134 else
136 String prefix = getPrefix(nameValue);
137 if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
139 int ci = nameValue.indexOf(':');
140 nameValue = nameValue.substring(ci + 1);
142 else
144 // Namespace aliasing
145 if (prefix == null)
146 prefix = "#default";
147 String resultPrefix =
148 (String) stylesheet.namespaceAliases.get(prefix);
149 if (resultPrefix != null)
151 if ("#default".equals(resultPrefix))
152 resultPrefix = null;
153 namespaceValue = source.lookupNamespaceURI(resultPrefix);
155 if (prefix == "#default")
156 prefix = null;
157 // Look up ordinary namespace for this prefix
158 if (namespaceValue == null)
160 if (XMLConstants.XML_NS_PREFIX.equals(prefix))
161 namespaceValue = XMLConstants.XML_NS_URI;
162 else
164 // Resolve namespace for this prefix
165 namespaceValue = source.lookupNamespaceURI(prefix);
171 // Create element
172 Element element = (namespaceValue != null) ?
173 doc.createElementNS(namespaceValue, nameValue) :
174 doc.createElement(nameValue);
175 if (nextSibling != null)
176 parent.insertBefore(element, nextSibling);
177 else
178 parent.appendChild(element);
179 stylesheet.addNamespaceNodes(source, element, doc,
180 elementExcludeResultPrefixes);
181 if (uas != null)
183 StringTokenizer st = new StringTokenizer(uas, " ");
184 while (st.hasMoreTokens())
185 addAttributeSet(stylesheet, mode, context, pos, len,
186 element, null, st.nextToken());
188 if (children != null)
189 children.apply(stylesheet, mode,
190 context, pos, len,
191 element, null);
192 if (next != null)
193 next.apply(stylesheet, mode,
194 context, pos, len,
195 parent, nextSibling);
198 final String getPrefix(String name)
200 int ci = name.indexOf(':');
201 return (ci == -1) ? null : name.substring(0, ci);
204 void addAttributeSet(Stylesheet stylesheet, QName mode,
205 Node context, int pos, int len,
206 Node parent, Node nextSibling, String attributeSet)
207 throws TransformerException
209 stylesheet.bindings.global = true;
210 for (Iterator i = stylesheet.attributeSets.iterator(); i.hasNext(); )
212 AttributeSet as = (AttributeSet) i.next();
213 if (!as.name.equals(attributeSet))
214 continue;
215 if (as.uas != null)
217 StringTokenizer st = new StringTokenizer(as.uas, " ");
218 while (st.hasMoreTokens())
219 addAttributeSet(stylesheet, mode, context, pos, len,
220 parent, nextSibling, st.nextToken());
222 if (as.children != null)
223 as.children.apply(stylesheet, mode,
224 context, pos, len,
225 parent, nextSibling);
227 stylesheet.bindings.global = false;
230 public boolean references(QName var)
232 if (name != null && name.references(var))
233 return true;
234 if (namespace != null && namespace.references(var))
235 return true;
236 return super.references(var);
239 public String toString()
241 StringBuffer buf = new StringBuffer("element");
242 buf.append('[');
243 buf.append("name=");
244 if (namespace != null)
246 buf.append(",namespace=");
247 buf.append(namespace);
249 buf.append(name);
250 if (uas != null)
252 buf.append(",uas=");
253 buf.append(uas);
255 buf.append(']');
256 return buf.toString();