ElementNode.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* ElementNode.java --
  2. Copyright (C) 2004,2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.transform;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.util.Collection;
  34. import java.util.Collections;
  35. import java.util.HashSet;
  36. import java.util.Iterator;
  37. import java.util.StringTokenizer;
  38. import javax.xml.XMLConstants;
  39. import javax.xml.namespace.QName;
  40. import javax.xml.transform.TransformerException;
  41. import org.w3c.dom.Document;
  42. import org.w3c.dom.DocumentFragment;
  43. import org.w3c.dom.Element;
  44. import org.w3c.dom.NamedNodeMap;
  45. import org.w3c.dom.Node;
  46. import gnu.xml.xpath.Expr;
  47. /**
  48. * A template node representing an XSL <code>element</code> instruction.
  49. *
  50. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  51. */
  52. final class ElementNode
  53. extends TemplateNode
  54. {
  55. final TemplateNode name;
  56. final TemplateNode namespace;
  57. final String uas;
  58. final Node source;
  59. final Collection elementExcludeResultPrefixes;
  60. ElementNode(TemplateNode name,
  61. TemplateNode namespace, String uas, Node source)
  62. {
  63. this.name = name;
  64. this.namespace = namespace;
  65. this.uas = uas;
  66. this.source = source;
  67. NamedNodeMap attrs = source.getAttributes();
  68. Node attr = attrs.getNamedItemNS(Stylesheet.XSL_NS,
  69. "exclude-result-prefixes");
  70. if (attr != null)
  71. {
  72. elementExcludeResultPrefixes = new HashSet();
  73. StringTokenizer st = new StringTokenizer(attr.getNodeValue());
  74. while (st.hasMoreTokens())
  75. elementExcludeResultPrefixes.add(st.nextToken());
  76. }
  77. else
  78. elementExcludeResultPrefixes = Collections.EMPTY_SET;
  79. }
  80. TemplateNode clone(Stylesheet stylesheet)
  81. {
  82. TemplateNode ret = new ElementNode(name.clone(stylesheet),
  83. (namespace == null) ? null :
  84. namespace.clone(stylesheet),
  85. uas, source);
  86. if (children != null)
  87. ret.children = children.clone(stylesheet);
  88. if (next != null)
  89. ret.next = next.clone(stylesheet);
  90. return ret;
  91. }
  92. void doApply(Stylesheet stylesheet, QName mode,
  93. Node context, int pos, int len,
  94. Node parent, Node nextSibling)
  95. throws TransformerException
  96. {
  97. Document doc = (parent instanceof Document) ? (Document) parent :
  98. parent.getOwnerDocument();
  99. // Create a document fragment to hold the name
  100. DocumentFragment fragment = doc.createDocumentFragment();
  101. // Apply name to the fragment
  102. name.apply(stylesheet, mode,
  103. context, pos, len,
  104. fragment, null);
  105. // Use XPath string-value of fragment
  106. String nameValue = Expr.stringValue(fragment);
  107. String namespaceValue = null;
  108. if (namespace != null)
  109. {
  110. // Create a document fragment to hold the namespace
  111. fragment = doc.createDocumentFragment();
  112. // Apply namespace to the fragment
  113. namespace.apply(stylesheet, mode,
  114. context, pos, len,
  115. fragment, null);
  116. // Use XPath string-value of fragment
  117. namespaceValue = Expr.stringValue(fragment);
  118. if (namespaceValue.length() == 0)
  119. namespaceValue = null;
  120. }
  121. else
  122. {
  123. String prefix = getPrefix(nameValue);
  124. if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
  125. {
  126. int ci = nameValue.indexOf(':');
  127. nameValue = nameValue.substring(ci + 1);
  128. }
  129. else
  130. {
  131. // Namespace aliasing
  132. if (prefix == null)
  133. prefix = "#default";
  134. String resultPrefix =
  135. (String) stylesheet.namespaceAliases.get(prefix);
  136. if (resultPrefix != null)
  137. {
  138. if ("#default".equals(resultPrefix))
  139. resultPrefix = null;
  140. namespaceValue = source.lookupNamespaceURI(resultPrefix);
  141. }
  142. if (prefix == "#default")
  143. prefix = null;
  144. // Look up ordinary namespace for this prefix
  145. if (namespaceValue == null)
  146. {
  147. if (XMLConstants.XML_NS_PREFIX.equals(prefix))
  148. namespaceValue = XMLConstants.XML_NS_URI;
  149. else
  150. {
  151. // Resolve namespace for this prefix
  152. namespaceValue = source.lookupNamespaceURI(prefix);
  153. }
  154. }
  155. }
  156. }
  157. // Create element
  158. Element element = (namespaceValue != null) ?
  159. doc.createElementNS(namespaceValue, nameValue) :
  160. doc.createElement(nameValue);
  161. if (nextSibling != null)
  162. parent.insertBefore(element, nextSibling);
  163. else
  164. parent.appendChild(element);
  165. stylesheet.addNamespaceNodes(source, element, doc,
  166. elementExcludeResultPrefixes);
  167. if (uas != null)
  168. {
  169. StringTokenizer st = new StringTokenizer(uas, " ");
  170. while (st.hasMoreTokens())
  171. addAttributeSet(stylesheet, mode, context, pos, len,
  172. element, null, st.nextToken());
  173. }
  174. if (children != null)
  175. children.apply(stylesheet, mode,
  176. context, pos, len,
  177. element, null);
  178. if (next != null)
  179. next.apply(stylesheet, mode,
  180. context, pos, len,
  181. parent, nextSibling);
  182. }
  183. final String getPrefix(String name)
  184. {
  185. int ci = name.indexOf(':');
  186. return (ci == -1) ? null : name.substring(0, ci);
  187. }
  188. void addAttributeSet(Stylesheet stylesheet, QName mode,
  189. Node context, int pos, int len,
  190. Node parent, Node nextSibling, String attributeSet)
  191. throws TransformerException
  192. {
  193. stylesheet.bindings.global = true;
  194. for (Iterator i = stylesheet.attributeSets.iterator(); i.hasNext(); )
  195. {
  196. AttributeSet as = (AttributeSet) i.next();
  197. if (!as.name.equals(attributeSet))
  198. continue;
  199. if (as.uas != null)
  200. {
  201. StringTokenizer st = new StringTokenizer(as.uas, " ");
  202. while (st.hasMoreTokens())
  203. addAttributeSet(stylesheet, mode, context, pos, len,
  204. parent, nextSibling, st.nextToken());
  205. }
  206. if (as.children != null)
  207. as.children.apply(stylesheet, mode,
  208. context, pos, len,
  209. parent, nextSibling);
  210. }
  211. stylesheet.bindings.global = false;
  212. }
  213. public boolean references(QName var)
  214. {
  215. if (name != null && name.references(var))
  216. return true;
  217. if (namespace != null && namespace.references(var))
  218. return true;
  219. return super.references(var);
  220. }
  221. public String toString()
  222. {
  223. CPStringBuilder buf = new CPStringBuilder("element");
  224. buf.append('[');
  225. buf.append("name=");
  226. if (namespace != null)
  227. {
  228. buf.append(",namespace=");
  229. buf.append(namespace);
  230. }
  231. buf.append(name);
  232. if (uas != null)
  233. {
  234. buf.append(",uas=");
  235. buf.append(uas);
  236. }
  237. buf.append(']');
  238. return buf.toString();
  239. }
  240. }