libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / gnu / xml / transform / CopyOfNode.java
blob4ce84329fc056b5f3ccbc24e8a8ff1a767ed4bf3
1 /* CopyOfNode.java --
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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 gnu.java.lang.CPStringBuilder;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.Iterator;
46 import java.util.List;
47 import javax.xml.namespace.QName;
48 import javax.xml.transform.TransformerException;
49 import org.w3c.dom.Document;
50 import org.w3c.dom.NamedNodeMap;
51 import org.w3c.dom.Node;
52 import org.w3c.dom.Text;
53 import gnu.xml.xpath.Expr;
55 /**
56 * A template node representing an XSLT <code>copy-of</code> instruction.
58 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
60 final class CopyOfNode
61 extends TemplateNode
64 final Expr select;
66 CopyOfNode(Expr select)
68 this.select = select;
71 TemplateNode clone(Stylesheet stylesheet)
73 TemplateNode ret = new CopyOfNode(select.clone(stylesheet));
74 if (children != null)
75 ret.children = children.clone(stylesheet);
76 if (next != null)
77 ret.next = next.clone(stylesheet);
78 return ret;
81 void doApply(Stylesheet stylesheet, QName mode,
82 Node context, int pos, int len,
83 Node parent, Node nextSibling)
84 throws TransformerException
86 Object ret = select.evaluate(context, pos, len);
87 Document doc = (parent instanceof Document) ? (Document) parent :
88 parent.getOwnerDocument();
89 if (ret instanceof Collection)
91 Collection ns = (Collection) ret;
92 List list = new ArrayList(ns);
93 Collections.sort(list, documentOrderComparator);
94 for (Iterator i = list.iterator(); i.hasNext(); )
96 Node src = (Node) i.next();
97 short nodeType = src.getNodeType();
98 if (nodeType == Node.DOCUMENT_NODE)
100 // Use document element
101 src = ((Document) src).getDocumentElement();
102 if (src == null)
103 continue;
104 nodeType = Node.ELEMENT_NODE;
106 else if (nodeType == Node.ATTRIBUTE_NODE)
108 if (parent.getFirstChild() != null)
110 // Ignore attempt to add attribute after children
111 continue;
114 if (parent.getNodeType() == Node.ATTRIBUTE_NODE &&
115 nodeType != Node.TEXT_NODE &&
116 nodeType != Node.ENTITY_REFERENCE_NODE)
118 // Ignore
119 continue;
121 Node node = src.cloneNode(true);
122 node = doc.adoptNode(node);
123 if (nodeType == Node.ATTRIBUTE_NODE)
125 NamedNodeMap attrs = parent.getAttributes();
126 if (attrs != null)
127 attrs.setNamedItemNS(node);
129 else
131 if (nextSibling != null)
132 parent.insertBefore(node, nextSibling);
133 else
134 parent.appendChild(node);
138 else
140 String value = Expr._string(context, ret);
141 if (value != null && value.length() > 0)
143 Text textNode = doc.createTextNode(value);
144 if (nextSibling != null)
145 parent.insertBefore(textNode, nextSibling);
146 else
147 parent.appendChild(textNode);
150 // copy-of doesn't process children
151 if (next != null)
152 next.apply(stylesheet, mode,
153 context, pos, len,
154 parent, nextSibling);
157 public boolean references(QName var)
159 if (select != null && select.references(var))
160 return true;
161 return super.references(var);
164 public String toString()
166 CPStringBuilder buf = new CPStringBuilder("copy-of");
167 buf.append('[');
168 buf.append("select=");
169 buf.append(select);
170 buf.append(']');
171 return buf.toString();