1 16 19 package org.apache.xalan.serialize; 20 21 import javax.xml.transform.TransformerException ; 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 ; 30 31 39 public class SerializerUtils 40 { 41 42 52 public static void addAttribute(SerializationHandler handler, int attr) 53 throws TransformerException 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 ns = dtm.getNamespaceURI(attr); 64 65 if (ns == null) 66 ns = ""; 67 68 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 e) 79 { 80 } 82 } 84 91 public static void addAttributes(SerializationHandler handler, int src) 92 throws TransformerException 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 116 public static void outputResultTreeFragment( 117 SerializationHandler handler, 118 XObject obj, 119 XPathContext support) 120 throws org.xml.sax.SAXException 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 if (dtm.getNamespaceURI(n) == null) 136 handler.startPrefixMapping("", ""); 137 dtm.dispatchToEvents(n, handler); 138 } 139 } 140 } 141 142 154 public static void processNSDecls( 155 SerializationHandler handler, 156 int src, 157 int type, 158 DTM dtm) 159 throws TransformerException 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.getNodeNameX(namespace); 173 String desturi = handler.getNamespaceURIFromPrefix(prefix); 174 String 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 prefix = dtm.getNodeNameX(src); 186 String desturi = handler.getNamespaceURIFromPrefix(prefix); 188 String 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 se) 197 { 198 throw new TransformerException (se); 199 } 200 } 201 202 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.getNodeNameX(attr); 223 String uri = serializer.getNamespaceURIFromPrefix(prefix); 224 226 if ((null != uri) && uri.equals(dtm.getStringValue(attr))) 227 return true; 228 } 229 230 return false; 231 } 232 233 247 public static void ensureNamespaceDeclDeclared( 248 SerializationHandler handler, 249 DTM dtm, 250 int namespace) 251 throws org.xml.sax.SAXException 252 { 253 254 String uri = dtm.getNodeValue(namespace); 255 String prefix = dtm.getNodeNameX(namespace); 256 257 if ((uri != null && uri.length() > 0) && (null != prefix)) 258 { 259 String 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 |