OSDN Git Service

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