KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > serialize > SerializerUtils


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: SerializerUtils.java,v 1.3 2004/02/16 20:27:14 minchau Exp $
18  */

19 package org.apache.xalan.serialize;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.xalan.transformer.TransformerImpl;
24 import org.apache.xml.dtm.DTM;
25 import org.apache.xml.serializer.NamespaceMappings;
26 import org.apache.xml.serializer.SerializationHandler;
27 import org.apache.xpath.XPathContext;
28 import org.apache.xpath.objects.XObject;
29 import org.xml.sax.SAXException JavaDoc;
30
31 /**
32  * Class that contains only static methods that are used to "serialize",
33  * these methods are used by Xalan and are not in org.apache.xml.serializer
34  * because they have dependancies on the packages org.apache.xpath or org.
35  * apache.xml.dtm or org.apache.xalan.transformer. The package org.apache.xml.
36  * serializer should not depend on Xalan or XSLTC.
37  * @xsl.usage internal
38  */

39 public class SerializerUtils
40 {
41
42     /**
43      * Copy an DOM attribute to the created output element, executing
44      * attribute templates as need be, and processing the xsl:use
45      * attribute.
46      *
47      * @param handler SerializationHandler to which the attributes are added.
48      * @param attr Attribute node to add to SerializationHandler.
49      *
50      * @throws TransformerException
51      */

52     public static void addAttribute(SerializationHandler handler, int attr)
53         throws TransformerException JavaDoc
54     {
55
56         TransformerImpl transformer =
57             (TransformerImpl) handler.getTransformer();
58         DTM dtm = transformer.getXPathContext().getDTM(attr);
59
60         if (SerializerUtils.isDefinedNSDecl(handler, attr, dtm))
61             return;
62
63         String JavaDoc ns = dtm.getNamespaceURI(attr);
64
65         if (ns == null)
66             ns = "";
67
68         // %OPT% ...can I just store the node handle?
69
try
70         {
71             handler.addAttribute(
72                 ns,
73                 dtm.getLocalName(attr),
74                 dtm.getNodeName(attr),
75                 "CDATA",
76                 dtm.getNodeValue(attr));
77         }
78         catch (SAXException JavaDoc e)
79         {
80             // do something?
81
}
82     } // end copyAttributeToTarget method
83

84     /**
85      * Copy DOM attributes to the result element.
86      *
87      * @param src Source node with the attributes
88      *
89      * @throws TransformerException
90      */

91     public static void addAttributes(SerializationHandler handler, int src)
92         throws TransformerException JavaDoc
93     {
94
95         TransformerImpl transformer =
96             (TransformerImpl) handler.getTransformer();
97         DTM dtm = transformer.getXPathContext().getDTM(src);
98
99         for (int node = dtm.getFirstAttribute(src);
100             DTM.NULL != node;
101             node = dtm.getNextAttribute(node))
102         {
103             addAttribute(handler, node);
104         }
105     }
106
107     /**
108      * Given a result tree fragment, walk the tree and
109      * output it to the SerializationHandler.
110      *
111      * @param obj Result tree fragment object
112      * @param support XPath context for the result tree fragment
113      *
114      * @throws org.xml.sax.SAXException
115      */

116     public static void outputResultTreeFragment(
117         SerializationHandler handler,
118         XObject obj,
119         XPathContext support)
120         throws org.xml.sax.SAXException JavaDoc
121     {
122
123         int doc = obj.rtf();
124         DTM dtm = support.getDTM(doc);
125
126         if (null != dtm)
127         {
128             for (int n = dtm.getFirstChild(doc);
129                 DTM.NULL != n;
130                 n = dtm.getNextSibling(n))
131             {
132                 handler.flushPending();
133                 // I think. . . . This used to have a (true) arg
134
// to flush prefixes, will that cause problems ???
135
if (dtm.getNamespaceURI(n) == null)
136                     handler.startPrefixMapping("", "");
137                 dtm.dispatchToEvents(n, handler);
138             }
139         }
140     }
141
142     /**
143      * Copy <KBD>xmlns:</KBD> attributes in if not already in scope.
144      *
145      * As a quick hack to support ClonerToResultTree, this can also be used
146      * to copy an individual namespace node.
147      *
148      * @param src Source Node
149      * NEEDSDOC @param type
150      * NEEDSDOC @param dtm
151      *
152      * @throws TransformerException
153      */

154     public static void processNSDecls(
155         SerializationHandler handler,
156         int src,
157         int type,
158         DTM dtm)
159         throws TransformerException JavaDoc
160     {
161
162         try
163         {
164             if (type == DTM.ELEMENT_NODE)
165             {
166                 for (int namespace = dtm.getFirstNamespaceNode(src, true);
167                     DTM.NULL != namespace;
168                     namespace = dtm.getNextNamespaceNode(src, namespace, true))
169                 {
170
171                     // String prefix = dtm.getPrefix(namespace);
172
String JavaDoc prefix = dtm.getNodeNameX(namespace);
173                     String JavaDoc desturi = handler.getNamespaceURIFromPrefix(prefix);
174                     // String desturi = getURI(prefix);
175
String JavaDoc srcURI = dtm.getNodeValue(namespace);
176
177                     if (!srcURI.equalsIgnoreCase(desturi))
178                     {
179                         handler.startPrefixMapping(prefix, srcURI, false);
180                     }
181                 }
182             }
183             else if (type == DTM.NAMESPACE_NODE)
184             {
185                 String JavaDoc prefix = dtm.getNodeNameX(src);
186                 // bjm - some changes here to get desturi
187
String JavaDoc desturi = handler.getNamespaceURIFromPrefix(prefix);
188                 String JavaDoc srcURI = dtm.getNodeValue(src);
189
190                 if (!srcURI.equalsIgnoreCase(desturi))
191                 {
192                     handler.startPrefixMapping(prefix, srcURI, false);
193                 }
194             }
195         }
196         catch (org.xml.sax.SAXException JavaDoc se)
197         {
198             throw new TransformerException JavaDoc(se);
199         }
200     }
201
202     /**
203      * Returns whether a namespace is defined
204      *
205      *
206      * @param attr Namespace attribute node
207      * @param dtm The DTM that owns attr.
208      *
209      * @return True if the namespace is already defined in
210      * list of namespaces
211      */

212     public static boolean isDefinedNSDecl(
213         SerializationHandler serializer,
214         int attr,
215         DTM dtm)
216     {
217
218         if (DTM.NAMESPACE_NODE == dtm.getNodeType(attr))
219         {
220
221             // String prefix = dtm.getPrefix(attr);
222
String JavaDoc prefix = dtm.getNodeNameX(attr);
223             String JavaDoc uri = serializer.getNamespaceURIFromPrefix(prefix);
224             // String uri = getURI(prefix);
225

226             if ((null != uri) && uri.equals(dtm.getStringValue(attr)))
227                 return true;
228         }
229
230         return false;
231     }
232
233     /**
234      * This function checks to make sure a given prefix is really
235      * declared. It might not be, because it may be an excluded prefix.
236      * If it's not, it still needs to be declared at this point.
237      * TODO: This needs to be done at an earlier stage in the game... -sb
238      *
239      * @param ns Namespace URI of the element
240      * @param rawName Raw name of element (with prefix)
241      *
242      * NEEDSDOC @param dtm
243      * NEEDSDOC @param namespace
244      *
245      * @throws org.xml.sax.SAXException
246      */

247     public static void ensureNamespaceDeclDeclared(
248         SerializationHandler handler,
249         DTM dtm,
250         int namespace)
251         throws org.xml.sax.SAXException JavaDoc
252     {
253
254         String JavaDoc uri = dtm.getNodeValue(namespace);
255         String JavaDoc prefix = dtm.getNodeNameX(namespace);
256
257         if ((uri != null && uri.length() > 0) && (null != prefix))
258         {
259             String JavaDoc foundURI;
260             NamespaceMappings ns = handler.getNamespaceMappings();
261             if (ns != null)
262             {
263
264                 foundURI = ns.lookupNamespace(prefix);
265                 if ((null == foundURI) || !foundURI.equals(uri))
266                 {
267                     handler.startPrefixMapping(prefix, uri, false);
268                 }
269             }
270         }
271     }
272 }
273
Popular Tags