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
.Iterator
;
43 import java
.util
.StringTokenizer
;
44 import javax
.xml
.namespace
.QName
;
45 import javax
.xml
.transform
.TransformerException
;
46 import org
.w3c
.dom
.Document
;
47 import org
.w3c
.dom
.NamedNodeMap
;
48 import org
.w3c
.dom
.Node
;
51 * A template node representing the XSL <code>copy</code> instruction.
53 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
66 TemplateNode
clone(Stylesheet stylesheet
)
68 TemplateNode ret
= new CopyNode(uas
);
70 ret
.children
= children
.clone(stylesheet
);
72 ret
.next
= next
.clone(stylesheet
);
76 void doApply(Stylesheet stylesheet
, QName mode
,
77 Node context
, int pos
, int len
,
78 Node parent
, Node nextSibling
)
79 throws TransformerException
82 switch (context
.getNodeType())
85 case Node
.ATTRIBUTE_NODE
:
86 case Node
.ELEMENT_NODE
:
87 case Node
.PROCESSING_INSTRUCTION_NODE
:
88 case Node
.COMMENT_NODE
:
89 Document doc
= (parent
instanceof Document
) ?
(Document
) parent
:
90 parent
.getOwnerDocument();
91 copy
= context
.cloneNode(false);
92 copy
= doc
.adoptNode(copy
);
93 if (copy
.getNodeType() == Node
.ATTRIBUTE_NODE
)
95 if (parent
.getFirstChild() != null)
97 // Ignore attempt to add attribute after children
101 NamedNodeMap attrs
= parent
.getAttributes();
103 attrs
.setNamedItemNS(copy
);
108 if (nextSibling
!= null)
109 parent
.insertBefore(copy
, nextSibling
);
111 parent
.appendChild(copy
);
116 StringTokenizer st
= new StringTokenizer(uas
, " ");
117 while (st
.hasMoreTokens())
118 addAttributeSet(stylesheet
, mode
, context
, pos
, len
,
119 copy
, null, st
.nextToken());
121 if (children
!= null)
122 children
.apply(stylesheet
, mode
,
126 next
.apply(stylesheet
, mode
,
128 parent
, nextSibling
);
131 void addAttributeSet(Stylesheet stylesheet
, QName mode
,
132 Node context
, int pos
, int len
,
133 Node parent
, Node nextSibling
, String attributeSet
)
134 throws TransformerException
136 for (Iterator i
= stylesheet
.attributeSets
.iterator(); i
.hasNext(); )
138 AttributeSet as
= (AttributeSet
) i
.next();
139 if (!as
.name
.equals(attributeSet
))
143 StringTokenizer st
= new StringTokenizer(as
.uas
, " ");
144 while (st
.hasMoreTokens())
145 addAttributeSet(stylesheet
, mode
, context
, pos
, len
,
146 parent
, nextSibling
, st
.nextToken());
148 if (as
.children
!= null)
149 as
.children
.apply(stylesheet
, mode
,
151 parent
, nextSibling
);
155 public String
toString()
157 CPStringBuilder buf
= new CPStringBuilder("copy");
165 return buf
.toString();