1 | package org.greenstone.gsdl3.gs3build.schema;
|
---|
2 |
|
---|
3 | import java.util.*;
|
---|
4 |
|
---|
5 | import org.w3c.dom.Element;
|
---|
6 | import org.w3c.dom.Node;
|
---|
7 | import org.w3c.dom.NodeList;
|
---|
8 |
|
---|
9 | public class SchemaElement
|
---|
10 | {
|
---|
11 | // the following may contain duplicates when compared to SchemaAttribute, but this is
|
---|
12 | // deliberate...
|
---|
13 | public static final String ELEMENT_TYPE_STRING = "String";
|
---|
14 |
|
---|
15 | String name;
|
---|
16 | String type;
|
---|
17 | List attributes;
|
---|
18 | List childElements;
|
---|
19 | int minOccurs;
|
---|
20 | int maxOccurs;
|
---|
21 |
|
---|
22 | public SchemaElement(Element element)
|
---|
23 | {
|
---|
24 | // get the name of this element
|
---|
25 | this.name = element.getAttribute("name");
|
---|
26 | if (this.name == null) {
|
---|
27 | // TODO: this is a reference object
|
---|
28 | String ref = element.getAttribute("ref");
|
---|
29 |
|
---|
30 | if (ref != null) {
|
---|
31 | // TODO: fetch the actual object
|
---|
32 | }
|
---|
33 | else {
|
---|
34 | // TODO: abort
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | this.type = element.getAttribute("type");
|
---|
39 | if (this.type == null || this.type.length() == 0)
|
---|
40 | { this.type = SchemaElement.ELEMENT_TYPE_STRING;
|
---|
41 | }
|
---|
42 |
|
---|
43 | String temp;
|
---|
44 | temp = element.getAttribute("minOccurs");
|
---|
45 | if (temp != null && temp.length() > 0) {
|
---|
46 | this.minOccurs = Integer.parseInt(temp);
|
---|
47 | this.minOccurs = 1;
|
---|
48 | }
|
---|
49 | temp = element.getAttribute("maxOccurs");
|
---|
50 | if (temp != null && temp.length() > 0) {
|
---|
51 | this.maxOccurs = Integer.parseInt(temp);
|
---|
52 | }
|
---|
53 | else {
|
---|
54 | this.maxOccurs = 1;
|
---|
55 | }
|
---|
56 |
|
---|
57 | this.expand(element);
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void expand(Element element)
|
---|
61 | { // TODO
|
---|
62 | // get the first child element
|
---|
63 | NodeList children = element.getChildNodes();
|
---|
64 | if (children != null) {
|
---|
65 | for (int i = 0; i < children.getLength(); i ++)
|
---|
66 | { if (children.item(i).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
|
---|
67 | // TODO: raise an error
|
---|
68 | continue;
|
---|
69 | }
|
---|
70 |
|
---|
71 | String name = children.item(i).getNodeName();
|
---|
72 | if (name == null) {
|
---|
73 | continue;
|
---|
74 | }
|
---|
75 |
|
---|
76 | try {
|
---|
77 | Object child = SchemaNodeFactory.makeNode((Element) children.item(i));
|
---|
78 | if (child != null) {
|
---|
79 | this.childElements.add(child);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | catch (SchemaException ex)
|
---|
83 | {
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public SchemaElement(String name)
|
---|
90 | { this.name = name;
|
---|
91 | this.attributes = new ArrayList();
|
---|
92 | this.childElements = new ArrayList();
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void addAttribute(SchemaAttribute attribute)
|
---|
96 | {
|
---|
97 | this.attributes.add(attribute);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public void addElement(SchemaElement element)
|
---|
101 | {
|
---|
102 | this.childElements.add(element);
|
---|
103 | }
|
---|
104 | }
|
---|