OOP2

DOMUIParser.java

1
package myMiniGUIGenarator;
2
3
import java.awt.GridLayout;
4
import java.awt.LayoutManager;
5
import java.lang.reflect.*;
6
import javax.swing.JComponent;
7
import javax.swing.JFrame;
8
import javax.swing.JPanel;
9
10
import org.omg.CORBA.PRIVATE_MEMBER;
11
import org.w3c.dom.Document;
12
import org.w3c.dom.Element;
13
import org.w3c.dom.Node;
14
import org.w3c.dom.NodeList;
15
16
public class DOMUIParser {
17
	private Element root;
18
	private JPanel app;
19
	private  JFrame f;
20
21
22
//TODO add constructor and main
23
//e.g. $dparser = new org.apache.xerces.parsers.DOMParser();
24
// 	   $document = new File(fileName);
25
26
	protected void buildUI(Document doc){
27
		root = doc.getDocumentElement();
28
		app = new JPanel();
29
		processNode(root, app);
30
		f = new JFrame();
31
		f.setTitle(getTitle());
32
		f.getContentPane().add(app);
33
		f.pack();
34
		f.setVisible(true);
35
	}
36
37
	private String getTitle() {
38
		NodeList children = root.getChildNodes();
39
		for (int i = 0; i < children.getLength(); i++) {
40
			if(children.item(i).getNodeName().equals("text"))
41
				return children.item(i).getNodeValue();
42
		}
43
		return null;
44
	}
45
46
	private String getLabel(Node wchild) {
47
		NodeList children = wchild.getChildNodes();
48
		for (int i = 0; i < children.getLength(); i++) {
49
			if(children.item(i).getNodeName().equals("text"))
50
				return children.item(i).getNodeValue();
51
		}
52
		return null;
53
	}
54
55
	private void processNode(Node n, JComponent container){
56
		if(n.getNodeType()==Node.ELEMENT_NODE)
57
			if(processWidgetElement((Element)n, container)) return;
58
		NodeList nl = n.getChildNodes();
59
		if(nl.getLength()>0)
60
			for(int i=0; i<nl.getLength(); i++)
61
				processNode(nl.item(i), container);
62
	}
63
64
	/* Processes a widget element, adds it to the container 
65
	 * @param e				an element describing a widget 
66
	 * @param container		the container to which the widget should be added
67
	 * @return 				true if the descendants of e are processed by this method
68
	 * 						false otherwise
69
	 * @pre	e != null
70
	 * @pre e.getChildNodes() != null
71
	 * @pre e.getChildNodes().getLength() > 1
72
	 * @pre container != null
73
	 * */
74
	private boolean processWidgetElement(Element element, JComponent container) {
75
		Class cl;
76
		Constructor ct;
77
		try {
78
			cl = Class.forName(element.getNodeName());
79
			//ct = cl.getConstructor(parameterTypes)
80
81
			//TODO 
82
83
84
		} catch (ClassNotFoundException e) {
85
			// TODO: handle exception
86
		}
87
88
		if (element == null || container == null) 
89
			return false;
90
		else if (element.getChildNodes() == null) 
91
			return false;
92
		else if (element.getChildNodes().getLength() < 1)
93
			return false;
94
		else {
95
			return true;
96
		}
97
	}
98
	
99
	private GridLayout MakeGroup(Element element){
100
		GridLayout gridLayout = new GridLayout();
101
		String rows = element.getAttribute("rows");
102
		String columns = element.getAttribute("columns");
103
		return gridLayout;
104
}
105
106
107
}
108