1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | * ClassifierNodeData.java - part of the demo-client for Greenstone 3, of
|
---|
4 | * the Greenstone digital library suite from the New Zealand Digital
|
---|
5 | * Library Project at the * University of Waikato, New Zealand.
|
---|
6 | * <BR><BR>
|
---|
7 | * Copyright (C) 2008 New Zealand Digital Library Project
|
---|
8 | * <BR><BR>
|
---|
9 | * This program is free software; you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation; either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | * <BR><BR>
|
---|
14 | * This program is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *########################################################################
|
---|
19 | */
|
---|
20 |
|
---|
21 | package org.greenstone.gs3client.data;
|
---|
22 |
|
---|
23 | import java.util.Vector;
|
---|
24 | import java.util.Map;
|
---|
25 | import java.util.HashMap;
|
---|
26 |
|
---|
27 | import org.greenstone.gs3client.data.NodeData;
|
---|
28 | import org.w3c.dom.Element;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * ClassifierNodeData represents the information that can be stored in a
|
---|
32 | * <classifierNode> element. A ClassifierNode may have children and
|
---|
33 | * further descendents of type NodeData: either of type DocumentNodeData
|
---|
34 | * or type ClassifierNodeData.
|
---|
35 | * @author ak19
|
---|
36 | */
|
---|
37 | public class ClassifierNodeData extends NodeData {
|
---|
38 | /** Children (of this NodeData) as given in a nodeStructure, which
|
---|
39 | * can be ClassifierNodeData or DocumentNodeData. */
|
---|
40 | NodeData[] childnodes = null;
|
---|
41 |
|
---|
42 | /** Constructor that given a classifierNode element creates
|
---|
43 | * a ClassifierNodeData instance to parse and store its data.
|
---|
44 | * A classifierNodeData <classifierNode> contains just a nodeID
|
---|
45 | * and can contain children. When a response for a classifierNode's
|
---|
46 | * children is returned in a <nodeStructure>, this can be used to
|
---|
47 | * set the children.
|
---|
48 | * This classifierNodeData object is constructed from the data
|
---|
49 | * encapsulated in a <classifierNode> element
|
---|
50 | * @param classifierNodeTag is the <classifierNode> element in a
|
---|
51 | * Greenstone3 response XML message */
|
---|
52 | public ClassifierNodeData(Element classifierNodeTag) {
|
---|
53 | super(classifierNodeTag);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** This method is mainly useful for setting the top-level/root
|
---|
57 | * classifierNode's name. Otherwise, can use method setMetadataList
|
---|
58 | * to set the various metadata (name, values) associated with a
|
---|
59 | * classifierNode.
|
---|
60 | * Adds a new title to nodeMetadata as long as it is not the same
|
---|
61 | * as the previous title added.
|
---|
62 | * @param name is the previous title added */
|
---|
63 | public void setTitle(String name) {
|
---|
64 | if(nodeMetadata == null)
|
---|
65 | nodeMetadata = new HashMap();
|
---|
66 | // the nodeMetadata hashmap exists
|
---|
67 | Vector values;
|
---|
68 | if(nodeMetadata.containsKey("Title")) {
|
---|
69 | values = (Vector)nodeMetadata.get("Title");
|
---|
70 | // check if the last value is the same as
|
---|
71 | // what we're trying to insert
|
---|
72 | String value = (String)values.get(values.size()-1);
|
---|
73 | if(!value.equals(name)) // don't add duplicates
|
---|
74 | values.add(name);
|
---|
75 | } else {
|
---|
76 | values = new Vector();
|
---|
77 | values.add(name);
|
---|
78 | this.nodeMetadata.put("Title", values);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Given a map of nodeIdsToNodes, this method instantiates any
|
---|
83 | * children this ClassifierNodeData object might have. This
|
---|
84 | * method will set member childnodes (or leave it at none)
|
---|
85 | * and update idsToNodes_map as necessary.
|
---|
86 | * @param idsToNodes_map is a Hashmap of all the Node IDs to Node
|
---|
87 | * objects being maintained (Classifier- or DocumentNode) */
|
---|
88 | public void setChildren(Map idsToNodes_map) {
|
---|
89 | Vector children = new Vector();
|
---|
90 | setChildren(children, idsToNodes_map);
|
---|
91 |
|
---|
92 | if(children.size() == 0) // then childnodes[] remains at null
|
---|
93 | return;
|
---|
94 | // else
|
---|
95 | childnodes = new NodeData[children.size()];
|
---|
96 | children.toArray(childnodes); // now childnodes[] contains what's
|
---|
97 | // in the Vector children
|
---|
98 |
|
---|
99 | // now update the idsToNodes_map with any new nodes
|
---|
100 | for(int i = 0; i < childnodes.length; i++)
|
---|
101 | if(!idsToNodes_map.containsKey(childnodes[i].nodeID))
|
---|
102 | idsToNodes_map.put(childnodes[i].nodeID, childnodes[i]);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /** Gets the children of this node, but does not guarantee that *their*
|
---|
106 | * children are set. Will return null either if this NodeData has no
|
---|
107 | * children or if this node's children are not set yet.
|
---|
108 | * @return an array of all the child Nodes of this ClassifierNode
|
---|
109 | * (as per its hierarchical structure) or null if these are not yet set
|
---|
110 | * or if it has no children */
|
---|
111 | public NodeData[] getChildren() { return childnodes; }
|
---|
112 |
|
---|
113 | /**@return a string representation of the values stored in this
|
---|
114 | * ClassifierNodeData object: outputs the nodeID and calls show() on any
|
---|
115 | * children NodeData objects. It's mainly useful for debugging purposes. */
|
---|
116 | public String show() {
|
---|
117 | StringBuffer buf = new StringBuffer(super.show());
|
---|
118 | buf.append(showMeta());
|
---|
119 | if(this.childnodes != null) {
|
---|
120 | for(int i = 0; i < this.childnodes.length; i++)
|
---|
121 | buf.append("\n " + this.childnodes[i].show());
|
---|
122 | }
|
---|
123 | return buf.toString();
|
---|
124 | }
|
---|
125 | } |
---|