AttributeNode.java 7.5 KB

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