CopyOfNode.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* CopyOfNode.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.ArrayList;
  34. import java.util.Collection;
  35. import java.util.Collections;
  36. import java.util.Iterator;
  37. import java.util.List;
  38. import javax.xml.namespace.QName;
  39. import javax.xml.transform.TransformerException;
  40. import org.w3c.dom.Document;
  41. import org.w3c.dom.NamedNodeMap;
  42. import org.w3c.dom.Node;
  43. import org.w3c.dom.Text;
  44. import gnu.xml.xpath.Expr;
  45. /**
  46. * A template node representing an XSLT <code>copy-of</code> instruction.
  47. *
  48. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  49. */
  50. final class CopyOfNode
  51. extends TemplateNode
  52. {
  53. final Expr select;
  54. CopyOfNode(Expr select)
  55. {
  56. this.select = select;
  57. }
  58. TemplateNode clone(Stylesheet stylesheet)
  59. {
  60. TemplateNode ret = new CopyOfNode(select.clone(stylesheet));
  61. if (children != null)
  62. ret.children = children.clone(stylesheet);
  63. if (next != null)
  64. ret.next = next.clone(stylesheet);
  65. return ret;
  66. }
  67. void doApply(Stylesheet stylesheet, QName mode,
  68. Node context, int pos, int len,
  69. Node parent, Node nextSibling)
  70. throws TransformerException
  71. {
  72. Object ret = select.evaluate(context, pos, len);
  73. Document doc = (parent instanceof Document) ? (Document) parent :
  74. parent.getOwnerDocument();
  75. if (ret instanceof Collection)
  76. {
  77. Collection ns = (Collection) ret;
  78. List list = new ArrayList(ns);
  79. Collections.sort(list, documentOrderComparator);
  80. for (Iterator i = list.iterator(); i.hasNext(); )
  81. {
  82. Node src = (Node) i.next();
  83. short nodeType = src.getNodeType();
  84. if (nodeType == Node.DOCUMENT_NODE)
  85. {
  86. // Use document element
  87. src = ((Document) src).getDocumentElement();
  88. if (src == null)
  89. continue;
  90. nodeType = Node.ELEMENT_NODE;
  91. }
  92. else if (nodeType == Node.ATTRIBUTE_NODE)
  93. {
  94. if (parent.getFirstChild() != null)
  95. {
  96. // Ignore attempt to add attribute after children
  97. continue;
  98. }
  99. }
  100. if (parent.getNodeType() == Node.ATTRIBUTE_NODE &&
  101. nodeType != Node.TEXT_NODE &&
  102. nodeType != Node.ENTITY_REFERENCE_NODE)
  103. {
  104. // Ignore
  105. continue;
  106. }
  107. Node node = src.cloneNode(true);
  108. node = doc.adoptNode(node);
  109. if (nodeType == Node.ATTRIBUTE_NODE)
  110. {
  111. NamedNodeMap attrs = parent.getAttributes();
  112. if (attrs != null)
  113. attrs.setNamedItemNS(node);
  114. }
  115. else
  116. {
  117. if (nextSibling != null)
  118. parent.insertBefore(node, nextSibling);
  119. else
  120. parent.appendChild(node);
  121. }
  122. }
  123. }
  124. else
  125. {
  126. String value = Expr._string(context, ret);
  127. if (value != null && value.length() > 0)
  128. {
  129. Text textNode = doc.createTextNode(value);
  130. if (nextSibling != null)
  131. parent.insertBefore(textNode, nextSibling);
  132. else
  133. parent.appendChild(textNode);
  134. }
  135. }
  136. // copy-of doesn't process children
  137. if (next != null)
  138. next.apply(stylesheet, mode,
  139. context, pos, len,
  140. parent, nextSibling);
  141. }
  142. public boolean references(QName var)
  143. {
  144. if (select != null && select.references(var))
  145. return true;
  146. return super.references(var);
  147. }
  148. public String toString()
  149. {
  150. CPStringBuilder buf = new CPStringBuilder("copy-of");
  151. buf.append('[');
  152. buf.append("select=");
  153. buf.append(select);
  154. buf.append(']');
  155. return buf.toString();
  156. }
  157. }