OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / xml / transform / CopyOfNode.java
1 /* CopyOfNode.java -- 
2    Copyright (C) 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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.
20
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.
25
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. */
37
38 package gnu.xml.transform;
39
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;
52
53 /**
54  * A template node representing an XSLT <code>copy-of</code> instruction.
55  *
56  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
57  */
58 final class CopyOfNode
59   extends TemplateNode
60 {
61
62   final Expr select;
63
64   CopyOfNode(Expr select)
65   {
66     this.select = select;
67   }
68
69   TemplateNode clone(Stylesheet stylesheet)
70   {
71     TemplateNode ret = new CopyOfNode(select.clone(stylesheet));
72     if (children != null)
73       {
74         ret.children = children.clone(stylesheet);
75       }
76     if (next != null)
77       {
78         ret.next = next.clone(stylesheet);
79       }
80     return ret;
81   }
82
83   void doApply(Stylesheet stylesheet, QName mode,
84                Node context, int pos, int len,
85                Node parent, Node nextSibling)
86     throws TransformerException
87   {
88     Object ret = select.evaluate(context, pos, len);
89     Document doc = (parent instanceof Document) ? (Document) parent :
90       parent.getOwnerDocument();
91     if (ret instanceof Collection)
92       {
93         Collection ns = (Collection) ret;
94         List list = new ArrayList(ns);
95         Collections.sort(list, documentOrderComparator);
96         for (Iterator i = list.iterator(); i.hasNext(); )
97           {
98             Node src = (Node) i.next();
99             short nodeType = src.getNodeType();
100             if (nodeType == Node.DOCUMENT_NODE)
101               {
102                 // Use document element
103                 src = ((Document) src).getDocumentElement();
104                 if (src == null)
105                   {
106                     continue;
107                   }
108                 nodeType = Node.ELEMENT_NODE;
109               }
110             else if (nodeType == Node.ATTRIBUTE_NODE)
111               {
112                 if (parent.getFirstChild() != null)
113                   {
114                     // Ignore attempt to add attribute after children
115                     continue;
116                   }
117               }
118             if (parent.getNodeType() == Node.ATTRIBUTE_NODE &&
119                 nodeType != Node.TEXT_NODE &&
120                 nodeType != Node.ENTITY_REFERENCE_NODE)
121               {
122                 // Ignore
123                 continue;
124               }
125             Node node = src.cloneNode(true);
126             node = doc.adoptNode(node);
127             if (nodeType == Node.ATTRIBUTE_NODE)
128               {
129                 NamedNodeMap attrs = parent.getAttributes();
130                 if (attrs != null)
131                   {
132                     attrs.setNamedItemNS(node);
133                   }
134               }
135             else
136               {
137                 if (nextSibling != null)
138                   {
139                     parent.insertBefore(node, nextSibling);
140                   }
141                 else
142                   {
143                     parent.appendChild(node);
144                   }
145               }
146           }
147       }
148     else
149       {
150         String value = Expr._string(context, ret);
151         if (value != null && value.length() > 0)
152           {
153             Text textNode = doc.createTextNode(value);
154             if (nextSibling != null)
155               {
156                 parent.insertBefore(textNode, nextSibling);
157               }
158             else
159               {
160                 parent.appendChild(textNode);
161               }
162           }
163       }
164     // copy-of doesn't process children
165     if (next != null)
166       {
167         next.apply(stylesheet, mode,
168                    context, pos, len,
169                    parent, nextSibling);
170       }
171   }
172   
173   public boolean references(QName var)
174   {
175     if (select != null && select.references(var))
176       {
177         return true;
178       }
179     return super.references(var);
180   }
181   
182   public String toString()
183   {
184     StringBuffer buf = new StringBuffer(getClass().getName());
185     buf.append('[');
186     buf.append("select=");
187     buf.append(select);
188     buf.append(']');
189     return buf.toString();
190   }
191   
192 }