OSDN Git Service

libjava/ChangeLog:
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / xml / transform / CopyNode.java
1 /* CopyNode.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.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;
49
50 /**
51  * A template node representing the XSL <code>copy</code> instruction.
52  *
53  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
54  */
55 final class CopyNode
56   extends TemplateNode
57 {
58
59   final String uas;
60
61   CopyNode(String uas)
62   {
63     this.uas = uas;
64   }
65
66   TemplateNode clone(Stylesheet stylesheet)
67   {
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;
74   }
75
76   void doApply(Stylesheet stylesheet, QName mode,
77                Node context, int pos, int len,
78                Node parent, Node nextSibling)
79     throws TransformerException
80   {
81     Node copy = parent;
82     switch (context.getNodeType())
83       {
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)
94           {
95             if (parent.getFirstChild() != null)
96               {
97                 // Ignore attempt to add attribute after children
98               }
99             else
100               {
101                 NamedNodeMap attrs = parent.getAttributes();
102                 if (attrs != null)
103                   attrs.setNamedItemNS(copy);
104               }
105           }
106         else
107           {
108             if (nextSibling != null)
109               parent.insertBefore(copy, nextSibling);
110             else
111               parent.appendChild(copy);
112           }
113       }
114     if (uas != null)
115       {
116         StringTokenizer st = new StringTokenizer(uas, " ");
117         while (st.hasMoreTokens())
118           addAttributeSet(stylesheet, mode, context, pos, len,
119                           copy, null, st.nextToken());
120       }
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);
129   }
130   
131   void addAttributeSet(Stylesheet stylesheet, QName mode,
132                        Node context, int pos, int len,
133                        Node parent, Node nextSibling, String attributeSet)
134     throws TransformerException
135   {
136     for (Iterator i = stylesheet.attributeSets.iterator(); i.hasNext(); )
137       {
138         AttributeSet as = (AttributeSet) i.next();
139         if (!as.name.equals(attributeSet))
140           continue;
141         if (as.uas != null)
142           {
143             StringTokenizer st = new StringTokenizer(as.uas, " ");
144             while (st.hasMoreTokens())
145               addAttributeSet(stylesheet, mode, context, pos, len,
146                               parent, nextSibling, st.nextToken());
147           }
148         if (as.children != null)
149           as.children.apply(stylesheet, mode,
150                             context, pos, len,
151                             parent, nextSibling);
152       }
153   }
154
155   public String toString()
156   {
157     CPStringBuilder buf = new CPStringBuilder("copy");
158     if (uas != null)
159       {
160         buf.append('[');
161         buf.append("uas=");
162         buf.append(uas);
163         buf.append(']');
164       }
165     return buf.toString();
166   }
167
168 }