1 | package org.greenstone.gsdl3.gs3build.metadata;
|
---|
2 |
|
---|
3 | import org.w3c.dom.Element;
|
---|
4 | import org.w3c.dom.Node;
|
---|
5 | import org.w3c.dom.NodeList;
|
---|
6 | import org.w3c.dom.NamedNodeMap;
|
---|
7 |
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.HashMap;
|
---|
12 |
|
---|
13 | import org.greenstone.gsdl3.gs3build.schema.Schema;
|
---|
14 | import org.greenstone.gsdl3.gs3build.schema.SchemaNode;
|
---|
15 |
|
---|
16 | public class MetaElement
|
---|
17 | {
|
---|
18 | Map attributes;
|
---|
19 | List children;
|
---|
20 | String name;
|
---|
21 |
|
---|
22 | public MetaElement(Element element, Schema schema)
|
---|
23 | {
|
---|
24 | // basic setup
|
---|
25 | this.attributes = new HashMap();
|
---|
26 | this.children = new ArrayList();
|
---|
27 | this.name = element.getNodeName();
|
---|
28 | StringBuffer contentBuffer = new StringBuffer();
|
---|
29 |
|
---|
30 | // split the name into namespace/workspace
|
---|
31 | String workSpace = SchemaNode.getNamespace(name);
|
---|
32 | name = SchemaNode.getElementName(name);
|
---|
33 |
|
---|
34 | // get attributes
|
---|
35 | NamedNodeMap attributes = element.getAttributes();
|
---|
36 | for (int a = 0; a < attributes.getLength(); a ++)
|
---|
37 | { Node attribute = attributes.item(a);
|
---|
38 |
|
---|
39 | this.attributes.put(attribute.getNodeName(), attribute.getNodeValue());
|
---|
40 | }
|
---|
41 |
|
---|
42 | // get children
|
---|
43 | NodeList children = element.getChildNodes();
|
---|
44 | for (int c = 0; c < children.getLength(); c++)
|
---|
45 | { Node child = children.item(c);
|
---|
46 |
|
---|
47 | if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE)
|
---|
48 | { // make a child
|
---|
49 | if (contentBuffer.length() > 0) {
|
---|
50 | this.children.add(contentBuffer.toString());
|
---|
51 | contentBuffer.setLength(0);
|
---|
52 | }
|
---|
53 | Object childElement = new MetaElement((Element) child, schema);
|
---|
54 | this.children.add(childElement);
|
---|
55 | }
|
---|
56 | else
|
---|
57 | { // append to content if not an element - this may be wasteful & type of
|
---|
58 | // element should be checked
|
---|
59 | contentBuffer.append(child.getNodeValue());
|
---|
60 | }
|
---|
61 | }
|
---|
62 | if (contentBuffer.length() > 0) {
|
---|
63 | this.children.add(contentBuffer.toString());
|
---|
64 | }
|
---|
65 | }
|
---|
66 | } |
---|