1 | package org.greenstone.gatherer.valuetree;
|
---|
2 | /**
|
---|
3 | *#########################################################################
|
---|
4 | *
|
---|
5 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
6 | * library suite from the New Zealand Digital Library Project at the
|
---|
7 | * University of Waikato, New Zealand.
|
---|
8 | *
|
---|
9 | * <BR><BR>
|
---|
10 | *
|
---|
11 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
12 | *
|
---|
13 | * <BR><BR>
|
---|
14 | *
|
---|
15 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
16 | *
|
---|
17 | * <BR><BR>
|
---|
18 | *
|
---|
19 | * This program is free software; you can redistribute it and/or modify
|
---|
20 | * it under the terms of the GNU General Public License as published by
|
---|
21 | * the Free Software Foundation; either version 2 of the License, or
|
---|
22 | * (at your option) any later version.
|
---|
23 | *
|
---|
24 | * <BR><BR>
|
---|
25 | *
|
---|
26 | * This program is distributed in the hope that it will be useful,
|
---|
27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
29 | * GNU General Public License for more details.
|
---|
30 | *
|
---|
31 | * <BR><BR>
|
---|
32 | *
|
---|
33 | * You should have received a copy of the GNU General Public License
|
---|
34 | * along with this program; if not, write to the Free Software
|
---|
35 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
36 | *########################################################################
|
---|
37 | */
|
---|
38 | import java.util.*;
|
---|
39 | import javax.swing.tree.*;
|
---|
40 | import org.greenstone.gatherer.Gatherer;
|
---|
41 | import org.greenstone.gatherer.msm.ElementWrapper;
|
---|
42 | import org.greenstone.gatherer.msm.MetadataSetManager;
|
---|
43 | import org.greenstone.gatherer.msm.MSMUtils;
|
---|
44 | import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
|
---|
45 | import org.greenstone.gatherer.util.Utility;
|
---|
46 | import org.w3c.dom.*;
|
---|
47 | /*
|
---|
48 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
49 | * @version 2.1
|
---|
50 | */
|
---|
51 | public class GValueModel
|
---|
52 | extends DefaultTreeModel {
|
---|
53 |
|
---|
54 | private ElementWrapper element;
|
---|
55 |
|
---|
56 | public GValueModel() {
|
---|
57 | super(new DefaultMutableTreeNode("Temp"));
|
---|
58 | }
|
---|
59 |
|
---|
60 | public GValueModel(String root) {
|
---|
61 | super(new DefaultMutableTreeNode(root));
|
---|
62 | }
|
---|
63 |
|
---|
64 | public GValueModel(ElementWrapper e) {
|
---|
65 | super(new DefaultMutableTreeNode("Temp"));
|
---|
66 | this.element = e;
|
---|
67 | // Load the template value tree document.
|
---|
68 | Document document = MSMUtils.getValueTreeTemplate();
|
---|
69 | Element new_root = document.getDocumentElement();
|
---|
70 | new_root.setAttribute("element", e.getName());
|
---|
71 | root = new GValueNode(new_root);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public GValueModel(ElementWrapper e, Document document) {
|
---|
75 | super(new DefaultMutableTreeNode("Temp"));
|
---|
76 | this.element = e;
|
---|
77 | Element new_root = document.getDocumentElement();
|
---|
78 | new_root.setAttribute("element", e.getName());
|
---|
79 | root = new GValueNode(new_root);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Value may include path ie news/newssw */
|
---|
83 | public GValueNode addValue(String value) {
|
---|
84 | StringTokenizer tokenizer = new StringTokenizer(value, "\\");
|
---|
85 | GValueNode subject = (GValueNode) root;
|
---|
86 | while(tokenizer.hasMoreTokens() && subject != null) {
|
---|
87 | String token = tokenizer.nextToken();
|
---|
88 | subject = addValue(token, subject, null);
|
---|
89 | ///ystem.err.println("Found or created a node " + token);
|
---|
90 | }
|
---|
91 | return subject;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public GValueNode addValue(String value, GValueNode subject, String alias) {
|
---|
95 | // To add a value we must first ensure it isn't already present in -this- nodes children. The bummer is that the nice getElements functions search the whole tree so...
|
---|
96 | GValueNode value_node = subject.getValue(Utility.stripNL(value));
|
---|
97 | if(value_node == null) {
|
---|
98 | // Now add the new value.
|
---|
99 | Document document = subject.getElement().getOwnerDocument();
|
---|
100 | // Now we create a new subject and add it subject
|
---|
101 | Element new_subject = document.createElementNS("","Subject");
|
---|
102 | Element new_value = document.createElementNS("","Value");
|
---|
103 | new_subject.appendChild(new_value);
|
---|
104 | Text new_text = document.createTextNode(value);
|
---|
105 | new_value.appendChild(new_text);
|
---|
106 | if(alias != null && alias.length() > 0) {
|
---|
107 | Element new_alias = document.createElementNS("","Alias");
|
---|
108 | new_subject.appendChild(new_alias);
|
---|
109 | Text new_alias_text = document.createTextNode(alias);
|
---|
110 | new_alias.appendChild(new_alias_text);
|
---|
111 | }
|
---|
112 | value_node = new GValueNode(new_subject);
|
---|
113 | ///ystem.err.println("(GValueModel) addValue()... " + value_node);
|
---|
114 |
|
---|
115 | // Figure out where this node will be inserted in subjects
|
---|
116 | // children.
|
---|
117 | int position = -1;
|
---|
118 | for(int i = 0; position == -1 && i < subject.getChildCount(); i++) {
|
---|
119 | Object sibling = subject.getChildAt(i);
|
---|
120 | int rel_pos = value.compareTo(sibling.toString());
|
---|
121 | ///ystem.err.println("'"+value+"'.compareTo('"+sibling+"') = " + rel_pos);
|
---|
122 | if(rel_pos <= 0) {
|
---|
123 | position = i;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | if(position == -1) {
|
---|
127 | position = subject.getChildCount();
|
---|
128 | }
|
---|
129 | // Insert it. If position is still -1, append it to the end of subjects children.
|
---|
130 | ///ystem.err.println("Inserting '" + value + "' at position " + position);
|
---|
131 | insertNodeInto(value_node, subject, position);
|
---|
132 | ///ystem.err.println("(GValueModel) Done insert node into...");
|
---|
133 | // SynchronizedTreeModelTools.insertNodeInto(this, subject, value_node);
|
---|
134 | // Inform listeners that we've changed.
|
---|
135 | Gatherer.c_man.getCollection().msm.fireValueChanged(element, null, this);
|
---|
136 | }
|
---|
137 | return value_node;
|
---|
138 | }
|
---|
139 |
|
---|
140 | public Document getDocument() {
|
---|
141 | return ((GValueNode)root).getElement().getOwnerDocument();
|
---|
142 | }
|
---|
143 |
|
---|
144 | public ElementWrapper getElement() {
|
---|
145 | return element;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Retrieve the hindex for a certain value within the value tree.
|
---|
149 | * @param value The value whose index you wish to determine as a <strong>String</strong>.
|
---|
150 | * @return A <strong>String</strong> containing an index such as "1", "2.1" or "18.2.5".
|
---|
151 | */
|
---|
152 | public String getHIndex(String value) {
|
---|
153 | ///ystem.err.println("getHIndex(" + value + ")");
|
---|
154 | return getHIndex((GValueNode) root, value, null);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** Retrieve a value node given its hierarchical reference or value.
|
---|
158 | * @param index The hierarchy index or value as a <strong>String</strong>.
|
---|
159 | */
|
---|
160 | public GValueNode getValue(String index_str) {
|
---|
161 | ///ystem.err.println("Retrieve the value for: " + index_str);
|
---|
162 | GValueNode result = null;
|
---|
163 | if(isHierarchy() && Utility.isIndex(index_str)) {
|
---|
164 | // StringTokenize the index
|
---|
165 | StringTokenizer tokenizer = new StringTokenizer(index_str, ".");
|
---|
166 | result = (GValueNode) root;
|
---|
167 | // Using the index numbers retrieve the appropriate node.
|
---|
168 | try {
|
---|
169 | while(result != null && tokenizer.hasMoreTokens()) {
|
---|
170 | int index = Integer.parseInt(tokenizer.nextToken()) - 1;
|
---|
171 | // Retrieve the index'th child of the current result node
|
---|
172 | if(0 <= index && index < result.getChildCount()) {
|
---|
173 | result = (GValueNode) result.getChildAt(index);
|
---|
174 | }
|
---|
175 | // Otherwise we're broken.
|
---|
176 | else {
|
---|
177 | ///ystem.err.println("There is no " + index + "th childnode of " + result);
|
---|
178 | result = null;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | // Most likely caused by parseInt throwing a wobbly.
|
---|
183 | catch (Exception error) {
|
---|
184 | result = null;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | if(result == null) {
|
---|
188 | ///ystem.err.println("No existing value. Adding " + index_str);
|
---|
189 | result = addValue(index_str);
|
---|
190 | }
|
---|
191 | return result;
|
---|
192 | }
|
---|
193 |
|
---|
194 | public boolean isHierarchy() {
|
---|
195 | boolean result = false;
|
---|
196 | // We are a hierarchy if our element says so....
|
---|
197 | if(element.isHierarchy()) {
|
---|
198 | return true;
|
---|
199 | }
|
---|
200 | // Or if our children are actually a hierarchy.
|
---|
201 | for(int i = 0; i < root.getChildCount() && !result; i++) {
|
---|
202 | GValueNode node = (GValueNode) root.getChildAt(i);
|
---|
203 | if(node != null && node.getChildCount() > 0) {
|
---|
204 | result = true;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | return result;
|
---|
208 | }
|
---|
209 |
|
---|
210 | public void removeValue(GValueNode child) {
|
---|
211 | //SynchronizedTreeModelTools.removeNodeFromParent(this, child);
|
---|
212 | removeNodeFromParent(child);
|
---|
213 | Gatherer.c_man.getCollection().msm.fireValueChanged(new ElementWrapper(child.getElement()), null, this);
|
---|
214 | }
|
---|
215 |
|
---|
216 | public void removeValue(String value) {
|
---|
217 | // Retrieve the node to be removed.
|
---|
218 | GValueNode node = getValue(value);
|
---|
219 | if(node != null) {
|
---|
220 | removeValue(node);
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | public int size() {
|
---|
225 | return size(root);
|
---|
226 | }
|
---|
227 |
|
---|
228 | private int size(TreeNode current) {
|
---|
229 | int size = 1;
|
---|
230 | for(int i = 0; i < current.getChildCount(); i++) {
|
---|
231 | size = size + size(current.getChildAt(i));
|
---|
232 | }
|
---|
233 | return size;
|
---|
234 | }
|
---|
235 |
|
---|
236 | public String toString() {
|
---|
237 | return element.toString();
|
---|
238 | }
|
---|
239 |
|
---|
240 | public Vector traverseTree() {
|
---|
241 | Vector contents = new Vector();
|
---|
242 | contents.addAll(traverseTree((GValueNode) root));
|
---|
243 | contents.remove((GValueNode) root);
|
---|
244 | return contents;
|
---|
245 | }
|
---|
246 |
|
---|
247 | public void updateValue(String new_value, String old_value) {
|
---|
248 | ///ystem.err.println("Updating '" + old_value + "' to '" + new_value + "'");
|
---|
249 | StringTokenizer tokenizer = new StringTokenizer(new_value, "\\");
|
---|
250 | GValueNode subject = (GValueNode) root;
|
---|
251 | while(tokenizer.hasMoreTokens()) {
|
---|
252 | String token = tokenizer.nextToken();
|
---|
253 | subject = updateValue(token, old_value, subject);
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | /** Called to update a certain value of metadata within a specific
|
---|
258 | * subject within a sbject hierarchy.
|
---|
259 | * Note that this simply in turn calls removeValue() to deal with the
|
---|
260 | * old value, then addValue() to account for the new.
|
---|
261 | * @param new_value A String representing the updated value.
|
---|
262 | * @param old_value A String representing the old value.
|
---|
263 | * @param subject An Element representing the subject you wish to update
|
---|
264 | * this value in.
|
---|
265 | */
|
---|
266 | public GValueNode updateValue(String new_value, String old_value, GValueNode subject) {
|
---|
267 | return addValue(new_value, subject, null);
|
---|
268 | }
|
---|
269 |
|
---|
270 | private GValueModel copy() {
|
---|
271 | Element document_element = ((GValueNode) root).getElement();
|
---|
272 | Document document_copy = MSMUtils.getValueTreeTemplate();
|
---|
273 | Element document_copy_element = document_copy.getDocumentElement();
|
---|
274 | document_copy_element.setAttribute("element", element.getName());
|
---|
275 | for(Node node = document_element.getFirstChild(); node != null; node = node.getNextSibling()) {
|
---|
276 | if(node.getNodeName().equals("Subject")) {
|
---|
277 | Node node_copy = document_copy.importNode(node, true);
|
---|
278 | document_copy_element.appendChild(node_copy);
|
---|
279 | }
|
---|
280 | }
|
---|
281 | return new GValueModel(element, document_copy);
|
---|
282 | }
|
---|
283 |
|
---|
284 | private String getHIndex(GValueNode node, String value, String index) {
|
---|
285 | for(int i = node.size(); i != 0; i--) {
|
---|
286 | GValueNode next = (GValueNode)node.get(i - 1);
|
---|
287 | String next_str = next.toString();
|
---|
288 | if(value.startsWith(next_str)) {
|
---|
289 | if(index == null) {
|
---|
290 | index = String.valueOf(i);
|
---|
291 | }
|
---|
292 | else {
|
---|
293 | index = index + "." + i;
|
---|
294 | }
|
---|
295 | value = value.substring(next.toString().length());
|
---|
296 | if(value.startsWith("\\")) {
|
---|
297 | value = value.substring(1);
|
---|
298 | index = getHIndex(next, value, index);
|
---|
299 | }
|
---|
300 | return index;
|
---|
301 | }
|
---|
302 | }
|
---|
303 | return index;
|
---|
304 | }
|
---|
305 |
|
---|
306 | private Vector traverseTree(GValueNode node) {
|
---|
307 | Vector contents = new Vector();
|
---|
308 | contents.add(node);
|
---|
309 | for(int i = 0; i < node.getChildCount(); i++) {
|
---|
310 | contents.addAll(traverseTree((GValueNode)node.getChildAt(i)));
|
---|
311 | }
|
---|
312 | return contents;
|
---|
313 | }
|
---|
314 | }
|
---|