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)
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
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
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.ArrayList;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.Iterator;
44 import java.util.List;
45 import javax.xml.namespace.QName;
46 import javax.xml.transform.TransformerException;
47 import org.w3c.dom.Document;
48 import org.w3c.dom.NamedNodeMap;
49 import org.w3c.dom.Node;
50 import org.w3c.dom.Text;
51 import gnu.xml.xpath.Expr;
54 * A template node representing an XSLT <code>copy-of</code> instruction.
56 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
58 final class CopyOfNode
64 CopyOfNode(Expr select)
69 TemplateNode clone(Stylesheet stylesheet)
71 TemplateNode ret = new CopyOfNode(select.clone(stylesheet));
73 ret.children = children.clone(stylesheet);
75 ret.next = next.clone(stylesheet);
79 void doApply(Stylesheet stylesheet, QName mode,
80 Node context, int pos, int len,
81 Node parent, Node nextSibling)
82 throws TransformerException
84 Object ret = select.evaluate(context, pos, len);
85 Document doc = (parent instanceof Document) ? (Document) parent :
86 parent.getOwnerDocument();
87 if (ret instanceof Collection)
89 Collection ns = (Collection) ret;
90 List list = new ArrayList(ns);
91 Collections.sort(list, documentOrderComparator);
92 for (Iterator i = list.iterator(); i.hasNext(); )
94 Node src = (Node) i.next();
95 short nodeType = src.getNodeType();
96 if (nodeType == Node.DOCUMENT_NODE)
98 // Use document element
99 src = ((Document) src).getDocumentElement();
102 nodeType = Node.ELEMENT_NODE;
104 else if (nodeType == Node.ATTRIBUTE_NODE)
106 if (parent.getFirstChild() != null)
108 // Ignore attempt to add attribute after children
112 if (parent.getNodeType() == Node.ATTRIBUTE_NODE &&
113 nodeType != Node.TEXT_NODE &&
114 nodeType != Node.ENTITY_REFERENCE_NODE)
119 Node node = src.cloneNode(true);
120 node = doc.adoptNode(node);
121 if (nodeType == Node.ATTRIBUTE_NODE)
123 NamedNodeMap attrs = parent.getAttributes();
125 attrs.setNamedItemNS(node);
129 if (nextSibling != null)
130 parent.insertBefore(node, nextSibling);
132 parent.appendChild(node);
138 String value = Expr._string(context, ret);
139 if (value != null && value.length() > 0)
141 Text textNode = doc.createTextNode(value);
142 if (nextSibling != null)
143 parent.insertBefore(textNode, nextSibling);
145 parent.appendChild(textNode);
148 // copy-of doesn't process children
150 next.apply(stylesheet, mode,
152 parent, nextSibling);
155 public boolean references(QName var)
157 if (select != null && select.references(var))
159 return super.references(var);
162 public String toString()
164 StringBuffer buf = new StringBuffer("copy-of");
166 buf.append("select=");
169 return buf.toString();