libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / gnu / xml / transform / CopyNode.java
blobbae628d9632ab3903eb4d53fbc2ba7074cbf79ef
1 /* CopyNode.java --
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)
9 any later version.
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
19 02110-1301 USA.
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.
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;
50 /**
51 * A template node representing the XSL <code>copy</code> instruction.
53 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
55 final class CopyNode
56 extends TemplateNode
59 final String uas;
61 CopyNode(String uas)
63 this.uas = uas;
66 TemplateNode clone(Stylesheet stylesheet)
68 TemplateNode ret = new CopyNode(uas);
69 if (children != null)
70 ret.children = children.clone(stylesheet);
71 if (next != null)
72 ret.next = next.clone(stylesheet);
73 return ret;
76 void doApply(Stylesheet stylesheet, QName mode,
77 Node context, int pos, int len,
78 Node parent, Node nextSibling)
79 throws TransformerException
81 Node copy = parent;
82 switch (context.getNodeType())
84 case Node.TEXT_NODE:
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
99 else
101 NamedNodeMap attrs = parent.getAttributes();
102 if (attrs != null)
103 attrs.setNamedItemNS(copy);
106 else
108 if (nextSibling != null)
109 parent.insertBefore(copy, nextSibling);
110 else
111 parent.appendChild(copy);
114 if (uas != null)
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,
123 context, pos, len,
124 copy, null);
125 if (next != null)
126 next.apply(stylesheet, mode,
127 context, pos, len,
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))
140 continue;
141 if (as.uas != null)
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,
150 context, pos, len,
151 parent, nextSibling);
155 public String toString()
157 CPStringBuilder buf = new CPStringBuilder("copy");
158 if (uas != null)
160 buf.append('[');
161 buf.append("uas=");
162 buf.append(uas);
163 buf.append(']');
165 return buf.toString();