1 50 51 package org.openlaszlo.iv.flash.xml.apache.functions; 52 53 import org.w3c.dom.*; 54 55 import org.apache.xpath.functions.*; 56 import org.apache.xpath.objects.*; 57 import org.apache.xpath.*; 58 import org.apache.xml.serialize.*; 59 import org.apache.xml.dtm.*; 60 61 import java.io.*; 62 import org.openlaszlo.iv.flash.util.*; 63 64 67 public class FuncSerialize extends FunctionDef1Arg { 68 69 77 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException { 78 79 int context = getArg0AsNode(xctxt); 80 81 Node node = null; 82 if( context != DTM.NULL ) { 83 DTM dtm = xctxt.getDTM(context); 84 node = dtm.getNode(context); 85 } 86 87 if( node != null ) return serialize(node, Method.XML, 0); 88 return null; 89 } 90 91 static XObject serialize( Node node, String method, int indent ) { 92 93 OutputFormat format = new OutputFormat(method, null, indent>0); 94 format.setOmitXMLDeclaration(true); 95 format.setIndent(indent); 96 97 Serializer ser = SerializerFactory.getSerializerFactory(method).makeSerializer(format); 98 StringWriter ow = new StringWriter(); 99 ser.setOutputCharStream(ow); 100 101 try { 102 DOMSerializer domser = ser.asDOMSerializer(); 103 104 switch( node.getNodeType() ) { 105 case Node.ELEMENT_NODE: 106 domser.serialize((Element)node); 107 break; 108 case Node.DOCUMENT_NODE: 109 domser.serialize((Document)node); 110 break; 111 } 112 } catch( IOException e ) { 113 Log.logRB(e); 114 } 115 116 return new XString(ow.toString()); 117 } 118 } 119 120 | Popular Tags |