source: trunk/greenstone3-extensions/gs3build/src/org/greenstone/gsdl3/gs3build/metadata/MetaElement.java@ 12188

Last change on this file since 12188 was 12188, checked in by kjdon, 18 years ago

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 1.8 KB
Line 
1package org.greenstone.gsdl3.gs3build.metadata;
2
3import org.w3c.dom.Element;
4import org.w3c.dom.Node;
5import org.w3c.dom.NodeList;
6import org.w3c.dom.NamedNodeMap;
7
8import java.util.List;
9import java.util.Map;
10import java.util.ArrayList;
11import java.util.HashMap;
12
13import org.greenstone.gsdl3.gs3build.schema.Schema;
14import org.greenstone.gsdl3.gs3build.schema.SchemaNode;
15
16public 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}
Note: See TracBrowser for help on using the repository browser.