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 java
.util
.Collection
;
41 import java
.util
.Collections
;
42 import java
.util
.HashSet
;
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 that copies a DOM node in the template to the result
54 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
56 final class LiteralNode
61 * The source node in the XSL template.
65 final Collection elementExcludeResultPrefixes
;
67 LiteralNode(Node source
)
70 if (source
.getNodeType() == Node
.ELEMENT_NODE
)
72 NamedNodeMap attrs
= source
.getAttributes();
73 Node attr
= attrs
.getNamedItemNS(Stylesheet
.XSL_NS
,
74 "exclude-result-prefixes");
77 elementExcludeResultPrefixes
= new HashSet();
78 StringTokenizer st
= new StringTokenizer(attr
.getNodeValue());
79 while (st
.hasMoreTokens())
80 elementExcludeResultPrefixes
.add(st
.nextToken());
83 elementExcludeResultPrefixes
= Collections
.EMPTY_SET
;
86 elementExcludeResultPrefixes
= null;
89 TemplateNode
clone(Stylesheet stylesheet
)
91 TemplateNode ret
= new LiteralNode(source
);
93 ret
.children
= children
.clone(stylesheet
);
95 ret
.next
= next
.clone(stylesheet
);
99 void doApply(Stylesheet stylesheet
, QName mode
,
100 Node context
, int pos
, int len
,
101 Node parent
, Node nextSibling
)
102 throws TransformerException
105 Document doc
= (parent
instanceof Document
) ?
(Document
) parent
:
106 parent
.getOwnerDocument();
107 short nodeType
= source
.getNodeType();
108 if (nodeType
== Node
.ATTRIBUTE_NODE
&&
109 parent
.getFirstChild() != null)
111 // Ignore attributes added after child elements
115 // Namespace aliasing
116 if (nodeType
== Node
.ELEMENT_NODE
)
118 String prefix
= source
.getPrefix();
121 String resultPrefix
=
122 (String
) stylesheet
.namespaceAliases
.get(prefix
);
123 if (resultPrefix
!= null)
125 if ("#default".equals(resultPrefix
))
127 String uri
= source
.lookupNamespaceURI(resultPrefix
);
128 String name
= source
.getNodeName();
129 // Create a new element node in the result document
130 result
= doc
.createElementNS(uri
, name
);
132 NamedNodeMap srcAttrs
= source
.getAttributes();
133 NamedNodeMap dstAttrs
= result
.getAttributes();
134 int l
= srcAttrs
.getLength();
135 for (int i
= 0; i
< l
; i
++)
137 Node attr
= srcAttrs
.item(i
);
138 if (!Stylesheet
.XSL_NS
.equals(attr
.getNamespaceURI()))
140 attr
= attr
.cloneNode(true);
141 attr
= doc
.adoptNode(attr
);
142 dstAttrs
.setNamedItemNS(attr
);
149 // Create result node
150 result
= source
.cloneNode(false);
151 // Remove any XSL attributes
152 NamedNodeMap attrs
= result
.getAttributes();
155 int l
= attrs
.getLength();
156 for (int i
= 0; i
< l
; i
++)
158 Node attr
= attrs
.item(i
);
159 if (Stylesheet
.XSL_NS
.equals(attr
.getNamespaceURI()))
161 attrs
.removeNamedItem(attr
.getNodeName());
167 Node result2
= doc
.adoptNode(result
);
170 String msg
= "Error adopting node to result tree: " +
171 result
+ " (" + result
.getClass().getName() + ")";
172 DOMSourceLocator l
= new DOMSourceLocator(context
);
173 throw new TransformerException(msg
, l
);
177 if (nextSibling
!= null)
178 parent
.insertBefore(result
, nextSibling
);
180 parent
.appendChild(result
);
181 if (nodeType
== Node
.ELEMENT_NODE
)
182 stylesheet
.addNamespaceNodes(source
, result
, doc
,
183 elementExcludeResultPrefixes
);
185 if (children
!= null)
186 children
.apply(stylesheet
, mode
,
192 next
.apply(stylesheet
, mode
,
194 parent
, nextSibling
);
197 public String
toString()
199 return source
.toString();