01: package abbot.editor;
02:
03: import java.awt.*;
04: import java.io.*;
05: import java.util.*;
06:
07: import abbot.script.*;
08: import abbot.finder.Hierarchy;
09:
10: /** Provides support for writing out a GUI hierarchy as XML. */
11: public class HierarchyWriter {
12: private Hierarchy hierarchy;
13:
14: public HierarchyWriter(Hierarchy h) {
15: this .hierarchy = h;
16: }
17:
18: /** Write to the given writer the GUI hierarchy represented by the given
19: * set of root windows.
20: */
21: public void writeHierarchy(Writer writer) throws IOException {
22: Resolver r = new Script(hierarchy);
23: writer.write("<awtHierarchy>\r\n");
24: Iterator iter = hierarchy.getRoots().iterator();
25: while (iter.hasNext()) {
26: writeComponent(writer, r, (Window) iter.next(), 1);
27: }
28: writer.write("</awtHierarchy>\r\n");
29: writer.close();
30: }
31:
32: // FIXME should include java.awt.MenuBar
33: private void writeComponent(Writer writer, Resolver r, Component c,
34: int level) throws IOException {
35: ComponentReference ref = new ComponentReference(r, c);
36: final String INDENT = " ";
37: String xml = ref.toXMLString();
38: for (int i = 0; i < level; i++) {
39: writer.write(INDENT);
40: }
41: Collection set = hierarchy.getComponents(c);
42: if (set.size() != 0) {
43: writer.write(xml.substring(0, xml.length() - 2));
44: writer.write(">\r\n");
45: Iterator iter = set.iterator();
46: while (iter.hasNext()) {
47: writeComponent(writer, r, (Component) iter.next(),
48: level + 1);
49: }
50: for (int i = 0; i < level; i++) {
51: writer.write(INDENT);
52: }
53: writer.write("</component>\r\n");
54: } else {
55: writer.write(xml);
56: writer.write("\r\n");
57: }
58: }
59: }
|