KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > transformer > ClonerToResultTree


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ClonerToResultTree.java,v 1.18 2004/02/16 20:41:29 minchau Exp $
18  */

19 package org.apache.xalan.transformer;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.xalan.serialize.SerializerUtils;
24 import org.apache.xml.dtm.DTM;
25 import org.apache.xml.serializer.SerializationHandler;
26 import org.apache.xml.utils.XMLString;
27
28 /**
29  * Class used to clone a node, possibly including its children to
30  * a result tree.
31  * @xsl.usage internal
32  */

33 public class ClonerToResultTree
34 {
35
36 // /**
37
// * Clone an element with or without children.
38
// * TODO: Fix or figure out node clone failure!
39
// * the error condition is severe enough to halt processing.
40
// *
41
// * @param node The node to clone
42
// * @param shouldCloneAttributes Flag indicating whether to
43
// * clone children attributes
44
// *
45
// * @throws TransformerException
46
// */
47
// public void cloneToResultTree(int node, boolean shouldCloneAttributes)
48
// throws TransformerException
49
// {
50
//
51
// try
52
// {
53
// XPathContext xctxt = m_transformer.getXPathContext();
54
// DTM dtm = xctxt.getDTM(node);
55
//
56
// int type = dtm.getNodeType(node);
57
// switch (type)
58
// {
59
// case DTM.TEXT_NODE :
60
// dtm.dispatchCharactersEvents(node, m_rth, false);
61
// break;
62
// case DTM.DOCUMENT_FRAGMENT_NODE :
63
// case DTM.DOCUMENT_NODE :
64
//
65
// // Can't clone a document, but refrain from throwing an error
66
// // so that copy-of will work
67
// break;
68
// case DTM.ELEMENT_NODE :
69
// {
70
// Attributes atts;
71
//
72
// if (shouldCloneAttributes)
73
// {
74
// m_rth.addAttributes(node);
75
// m_rth.processNSDecls(node, type, dtm);
76
// }
77
//
78
// String ns = dtm.getNamespaceURI(node);
79
// String localName = dtm.getLocalName(node);
80
//
81
// m_rth.startElement(ns, localName, dtm.getNodeNameX(node), null);
82
// }
83
// break;
84
// case DTM.CDATA_SECTION_NODE :
85
// m_rth.startCDATA();
86
// dtm.dispatchCharactersEvents(node, m_rth, false);
87
// m_rth.endCDATA();
88
// break;
89
// case DTM.ATTRIBUTE_NODE :
90
// m_rth.addAttribute(node);
91
// break;
92
// case DTM.COMMENT_NODE :
93
// XMLString xstr = dtm.getStringValue (node);
94
// xstr.dispatchAsComment(m_rth);
95
// break;
96
// case DTM.ENTITY_REFERENCE_NODE :
97
// m_rth.entityReference(dtm.getNodeNameX(node));
98
// break;
99
// case DTM.PROCESSING_INSTRUCTION_NODE :
100
// {
101
// // %REVIEW% Is the node name the same as the "target"?
102
// m_rth.processingInstruction(dtm.getNodeNameX(node),
103
// dtm.getNodeValue(node));
104
// }
105
// break;
106
// default :
107
// //"Can not create item in result tree: "+node.getNodeName());
108
// m_transformer.getMsgMgr().error(null,
109
// XSLTErrorResources.ER_CANT_CREATE_ITEM,
110
// new Object[]{ dtm.getNodeName(node) });
111
// }
112
// }
113
// catch(org.xml.sax.SAXException se)
114
// {
115
// throw new TransformerException(se);
116
// }
117
// } // end cloneToResultTree function
118

119   /**
120    * Clone an element with or without children.
121    * TODO: Fix or figure out node clone failure!
122    * the error condition is severe enough to halt processing.
123    *
124    * @param node The node to clone
125    * @param shouldCloneAttributes Flag indicating whether to
126    * clone children attributes
127    *
128    * @throws TransformerException
129    */

130   public static void cloneToResultTree(int node, int nodeType, DTM dtm,
131                                              SerializationHandler rth,
132                                              boolean shouldCloneAttributes)
133     throws TransformerException JavaDoc
134   {
135
136     try
137     {
138       switch (nodeType)
139       {
140       case DTM.TEXT_NODE :
141         dtm.dispatchCharactersEvents(node, rth, false);
142         break;
143       case DTM.DOCUMENT_FRAGMENT_NODE :
144       case DTM.DOCUMENT_NODE :
145         // Can't clone a document, but refrain from throwing an error
146
// so that copy-of will work
147
break;
148       case DTM.ELEMENT_NODE :
149         {
150           // Note: SAX apparently expects "no namespace" to be
151
// represented as "" rather than null.
152
String JavaDoc ns = dtm.getNamespaceURI(node);
153           if (ns==null) ns="";
154           String JavaDoc localName = dtm.getLocalName(node);
155       // rth.startElement(ns, localName, dtm.getNodeNameX(node), null);
156
// don't call a real SAX startElement (as commented out above),
157
// call a SAX-like startElement, to be able to add attributes after this call
158
rth.startElement(ns, localName, dtm.getNodeNameX(node));
159           
160       // If outputting attrs as separate events, they must
161
// _follow_ the startElement event. (Think of the
162
// xsl:attribute directive.)
163
if (shouldCloneAttributes)
164           {
165             SerializerUtils.addAttributes(rth, node);
166             SerializerUtils.processNSDecls(rth, node, nodeType, dtm);
167           }
168         }
169         break;
170       case DTM.CDATA_SECTION_NODE :
171         rth.startCDATA();
172         dtm.dispatchCharactersEvents(node, rth, false);
173         rth.endCDATA();
174         break;
175       case DTM.ATTRIBUTE_NODE :
176         SerializerUtils.addAttribute(rth, node);
177         break;
178             case DTM.NAMESPACE_NODE:
179                 // %REVIEW% Normally, these should have been handled with element.
180
// It's possible that someone may write a stylesheet that tries to
181
// clone them explicitly. If so, we need the equivalent of
182
// rth.addAttribute().
183
SerializerUtils.processNSDecls(rth,node,DTM.NAMESPACE_NODE,dtm);
184                 break;
185       case DTM.COMMENT_NODE :
186         XMLString xstr = dtm.getStringValue (node);
187         xstr.dispatchAsComment(rth);
188         break;
189       case DTM.ENTITY_REFERENCE_NODE :
190         rth.entityReference(dtm.getNodeNameX(node));
191         break;
192       case DTM.PROCESSING_INSTRUCTION_NODE :
193         {
194           // %REVIEW% Is the node name the same as the "target"?
195
rth.processingInstruction(dtm.getNodeNameX(node),
196                                       dtm.getNodeValue(node));
197         }
198         break;
199       default :
200         //"Can not create item in result tree: "+node.getNodeName());
201
throw new TransformerException JavaDoc(
202                          "Can't clone node: "+dtm.getNodeName(node));
203       }
204     }
205     catch(org.xml.sax.SAXException JavaDoc se)
206     {
207       throw new TransformerException JavaDoc(se);
208     }
209   } // end cloneToResultTree function
210
}
211
Popular Tags