KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > xml > XMLWriter


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl.xml;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.objectweb.fractal.adl.Node;
34
35 /**
36  * A class to write {@link Node} based abstract syntax trees in XML form.
37  */

38
39 public class XMLWriter {
40
41   private Writer JavaDoc w;
42   
43   public XMLWriter (final Writer JavaDoc writer) {
44     this.w = writer;
45   }
46   
47   public void write (final Node node) throws IOException JavaDoc {
48     write("", node);
49   }
50   
51   private void write (final String JavaDoc indent, final Node node) throws IOException JavaDoc {
52     w.write(indent);
53     w.write('<');
54     w.write(node.astGetType());
55     Map JavaDoc attrs = node.astGetAttributes();
56     Iterator JavaDoc i = attrs.keySet().iterator();
57     while (i.hasNext()) {
58       String JavaDoc attr = (String JavaDoc)i.next();
59       String JavaDoc value = (String JavaDoc)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 JavaDoc subNodes = new ArrayList JavaDoc();
69     String JavaDoc[] 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 JavaDoc 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