1 23 24 package org.objectweb.fractal.adl.xml; 25 26 import java.io.IOException ; 27 import java.io.Writer ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import org.objectweb.fractal.adl.Node; 34 35 38 39 public class XMLWriter { 40 41 private Writer w; 42 43 public XMLWriter (final Writer writer) { 44 this.w = writer; 45 } 46 47 public void write (final Node node) throws IOException { 48 write("", node); 49 } 50 51 private void write (final String indent, final Node node) throws IOException { 52 w.write(indent); 53 w.write('<'); 54 w.write(node.astGetType()); 55 Map attrs = node.astGetAttributes(); 56 Iterator i = attrs.keySet().iterator(); 57 while (i.hasNext()) { 58 String attr = (String )i.next(); 59 String value = (String )attrs.get(attr); 60 if (value != null) { 61 w.write(' '); 62 w.write(attr); 63 w.write("=\""); 64 w.write(value); 65 w.write("\""); 66 } 67 } 68 List subNodes = new ArrayList (); 69 String [] subNodeTypes = node.astGetNodeTypes(); 70 for (int j = 0; j < subNodeTypes.length; ++j) { 71 Node[] nodes = node.astGetNodes(subNodeTypes[j]); 72 for (int k = 0; k < nodes.length; ++k) { 73 if (nodes[k] != null) { 74 subNodes.add(nodes[k]); 75 } 76 } 77 } 78 79 if (subNodes.size() > 0) { 80 w.write(">\n"); 81 82 String subIndent = indent + " "; 83 for (int j = 0; j < subNodes.size(); ++j) { 84 write(subIndent, (Node)subNodes.get(j)); 85 } 86 87 w.write(indent); 88 w.write("</"); 89 w.write(node.astGetType()); 90 w.write(">\n"); 91 } else { 92 w.write("/>\n"); 93 } 94 } 95 } 96 | Popular Tags |