LiteralNode.java 6.9 KB

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