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 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
;
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
66 CopyOfNode(Expr select
)
71 TemplateNode
clone(Stylesheet stylesheet
)
73 TemplateNode ret
= new CopyOfNode(select
.clone(stylesheet
));
75 ret
.children
= children
.clone(stylesheet
);
77 ret
.next
= next
.clone(stylesheet
);
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();
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
114 if (parent
.getNodeType() == Node
.ATTRIBUTE_NODE
&&
115 nodeType
!= Node
.TEXT_NODE
&&
116 nodeType
!= Node
.ENTITY_REFERENCE_NODE
)
121 Node node
= src
.cloneNode(true);
122 node
= doc
.adoptNode(node
);
123 if (nodeType
== Node
.ATTRIBUTE_NODE
)
125 NamedNodeMap attrs
= parent
.getAttributes();
127 attrs
.setNamedItemNS(node
);
131 if (nextSibling
!= null)
132 parent
.insertBefore(node
, nextSibling
);
134 parent
.appendChild(node
);
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
);
147 parent
.appendChild(textNode
);
150 // copy-of doesn't process children
152 next
.apply(stylesheet
, mode
,
154 parent
, nextSibling
);
157 public boolean references(QName var
)
159 if (select
!= null && select
.references(var
))
161 return super.references(var
);
164 public String
toString()
166 CPStringBuilder buf
= new CPStringBuilder("copy-of");
168 buf
.append("select=");
171 return buf
.toString();