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.StaticStrings;
|
---|
45 | import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
|
---|
46 | import org.greenstone.gatherer.util.Utility;
|
---|
47 | import org.w3c.dom.*;
|
---|
48 | /*
|
---|
49 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
50 | * @version 2.1
|
---|
51 | */
|
---|
52 | public class GValueModel
|
---|
53 | extends DefaultTreeModel {
|
---|
54 |
|
---|
55 | private ElementWrapper element;
|
---|
56 |
|
---|
57 | public GValueModel() {
|
---|
58 | super(new DefaultMutableTreeNode("Temp"));
|
---|
59 | }
|
---|
60 |
|
---|
61 | public GValueModel(String root) {
|
---|
62 | super(new DefaultMutableTreeNode(root));
|
---|
63 | }
|
---|
64 |
|
---|
65 | public GValueModel(ElementWrapper e) {
|
---|
66 | super(new DefaultMutableTreeNode("Temp"));
|
---|
67 | this.element = e;
|
---|
68 | // Load the template value tree document.
|
---|
69 | Document document = MSMUtils.getValueTreeTemplate();
|
---|
70 | Element new_root = document.getDocumentElement();
|
---|
71 | new_root.setAttribute("element", e.getName());
|
---|
72 | root = new GValueNode(new_root);
|
---|
73 | }
|
---|
74 |
|
---|
75 | public GValueModel(ElementWrapper e, Document document) {
|
---|
76 | super(new DefaultMutableTreeNode("Temp"));
|
---|
77 | this.element = e;
|
---|
78 | Element new_root = document.getDocumentElement();
|
---|
79 | new_root.setAttribute("element", e.getName());
|
---|
80 | root = new GValueNode(new_root);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /** Value may include path ie news\newssw */
|
---|
84 | public GValueNode addValue(String value) {
|
---|
85 | try {
|
---|
86 | // Remove any single escape characters
|
---|
87 | value = stripEscapes(value);
|
---|
88 | // Now we can tokenize the string using the double escaped characters
|
---|
89 | StringTokenizer tokenizer = new StringTokenizer(value, StaticStrings.ESCAPE_STR);
|
---|
90 | GValueNode subject = (GValueNode) root;
|
---|
91 | while(tokenizer.hasMoreTokens() && subject != null) {
|
---|
92 | String token = tokenizer.nextToken();
|
---|
93 | subject = addValue(token, subject, null);
|
---|
94 | }
|
---|
95 | return subject;
|
---|
96 | }
|
---|
97 | catch (Exception error) {
|
---|
98 | error.printStackTrace();
|
---|
99 | }
|
---|
100 | return null;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Replace all current double escapes with single escapes, and remove all currently single escapes. */
|
---|
104 | static final private char ESCAPE_CHARACTER = '\\';
|
---|
105 | private String stripEscapes(String raw) {
|
---|
106 | StringBuffer processed_buffer = new StringBuffer();
|
---|
107 | int raw_length = raw.length();
|
---|
108 | for(int i = 0; i < raw_length; i++) {
|
---|
109 | char c = raw.charAt(i);
|
---|
110 | switch(c) {
|
---|
111 | case ESCAPE_CHARACTER:
|
---|
112 | if(i < raw_length - 1 && raw.charAt(i+1) == ESCAPE_CHARACTER) {
|
---|
113 | i = i + 1;
|
---|
114 | processed_buffer.append(c);
|
---|
115 | }
|
---|
116 | break;
|
---|
117 | default:
|
---|
118 | processed_buffer.append(c);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | return processed_buffer.toString();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public GValueNode addValue(String value, GValueNode subject, String alias) {
|
---|
125 | // 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...
|
---|
126 | GValueNode value_node = subject.getValue(Utility.stripNL(value));
|
---|
127 | if(value_node == null) {
|
---|
128 | // Now add the new value.
|
---|
129 | Document document = subject.getElement().getOwnerDocument();
|
---|
130 | // Now we create a new subject and add it subject
|
---|
131 | Element new_subject = document.createElementNS("","Subject");
|
---|
132 | Element new_value = document.createElementNS("","Value");
|
---|
133 | new_subject.appendChild(new_value);
|
---|
134 | Text new_text = document.createTextNode(value);
|
---|
135 | new_value.appendChild(new_text);
|
---|
136 | if(alias != null && alias.length() > 0) {
|
---|
137 | Element new_alias = document.createElementNS("","Alias");
|
---|
138 | new_subject.appendChild(new_alias);
|
---|
139 | Text new_alias_text = document.createTextNode(alias);
|
---|
140 | new_alias.appendChild(new_alias_text);
|
---|
141 | }
|
---|
142 | value_node = new GValueNode(new_subject);
|
---|
143 | ///ystem.err.println("(GValueModel) addValue()... " + value_node);
|
---|
144 |
|
---|
145 | // Figure out where this node will be inserted in subjects
|
---|
146 | // children.
|
---|
147 | int position = -1;
|
---|
148 | for(int i = 0; position == -1 && i < subject.getChildCount(); i++) {
|
---|
149 | Object sibling = subject.getChildAt(i);
|
---|
150 | int rel_pos = value.compareTo(sibling.toString());
|
---|
151 | ///ystem.err.println("'"+value+"'.compareTo('"+sibling+"') = " + rel_pos);
|
---|
152 | if(rel_pos <= 0) {
|
---|
153 | position = i;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | if(position == -1) {
|
---|
157 | position = subject.getChildCount();
|
---|
158 | }
|
---|
159 | // Insert it. If position is still -1, append it to the end of subjects children.
|
---|
160 | ///ystem.err.println("Inserting '" + value + "' at position " + position);
|
---|
161 | insertNodeInto(value_node, subject, position);
|
---|
162 | ///ystem.err.println("(GValueModel) Done insert node into...");
|
---|
163 | // SynchronizedTreeModelTools.insertNodeInto(this, subject, value_node);
|
---|
164 | // Inform listeners that we've changed.
|
---|
165 | Gatherer.c_man.getCollection().msm.fireValueChanged(element, null, this);
|
---|
166 | }
|
---|
167 | return value_node;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public Document getDocument() {
|
---|
171 | return ((GValueNode)root).getElement().getOwnerDocument();
|
---|
172 | }
|
---|
173 |
|
---|
174 | public ElementWrapper getElement() {
|
---|
175 | return element;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** Retrieve the hindex for a certain value within the value tree.
|
---|
179 | * @param value The value whose index you wish to determine as a <strong>String</strong>.
|
---|
180 | * @return A <strong>String</strong> containing an index such as "1", "2.1" or "18.2.5".
|
---|
181 | */
|
---|
182 | public String getHIndex(String value) {
|
---|
183 | ///ystem.err.println("getHIndex(" + value + ")");
|
---|
184 | return getHIndex((GValueNode) root, value, null);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** Retrieve a value node given its hierarchical reference or value.
|
---|
188 | * @param index The hierarchy index or value as a <strong>String</strong>.
|
---|
189 | */
|
---|
190 | public GValueNode getValue(String index_str) {
|
---|
191 | ///ystem.err.println("Retrieve the value for: " + index_str);
|
---|
192 | GValueNode result = null;
|
---|
193 | if(isHierarchy() && Utility.isIndex(index_str)) {
|
---|
194 | // StringTokenize the index
|
---|
195 | StringTokenizer tokenizer = new StringTokenizer(index_str, ".");
|
---|
196 | result = (GValueNode) root;
|
---|
197 | // Using the index numbers retrieve the appropriate node.
|
---|
198 | try {
|
---|
199 | while(result != null && tokenizer.hasMoreTokens()) {
|
---|
200 | int index = Integer.parseInt(tokenizer.nextToken()) - 1;
|
---|
201 | // Retrieve the index'th child of the current result node
|
---|
202 | if(0 <= index && index < result.getChildCount()) {
|
---|
203 | result = (GValueNode) result.getChildAt(index);
|
---|
204 | }
|
---|
205 | // Otherwise we're broken.
|
---|
206 | else {
|
---|
207 | ///ystem.err.println("There is no " + index + "th childnode of " + result);
|
---|
208 | result = null;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 | // Most likely caused by parseInt throwing a wobbly.
|
---|
213 | catch (Exception error) {
|
---|
214 | result = null;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | if(result == null) {
|
---|
218 | ///ystem.err.println("No existing value. Adding " + index_str);
|
---|
219 | result = addValue(index_str);
|
---|
220 | }
|
---|
221 | return result;
|
---|
222 | }
|
---|
223 |
|
---|
224 | public boolean isHierarchy() {
|
---|
225 | boolean result = false;
|
---|
226 | // We are a hierarchy if our element says so....
|
---|
227 | if(element.isHierarchy()) {
|
---|
228 | return true;
|
---|
229 | }
|
---|
230 | // Or if our children are actually a hierarchy.
|
---|
231 | for(int i = 0; i < root.getChildCount() && !result; i++) {
|
---|
232 | GValueNode node = (GValueNode) root.getChildAt(i);
|
---|
233 | if(node != null && node.getChildCount() > 0) {
|
---|
234 | result = true;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | return result;
|
---|
238 | }
|
---|
239 |
|
---|
240 | public void removeValue(GValueNode child) {
|
---|
241 | //SynchronizedTreeModelTools.removeNodeFromParent(this, child);
|
---|
242 | removeNodeFromParent(child);
|
---|
243 | Gatherer.c_man.getCollection().msm.fireValueChanged(new ElementWrapper(child.getElement()), null, this);
|
---|
244 | }
|
---|
245 |
|
---|
246 | public void removeValue(String value) {
|
---|
247 | // Retrieve the node to be removed.
|
---|
248 | GValueNode node = getValue(value);
|
---|
249 | if(node != null) {
|
---|
250 | removeValue(node);
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | public int size() {
|
---|
255 | return size(root);
|
---|
256 | }
|
---|
257 |
|
---|
258 | private int size(TreeNode current) {
|
---|
259 | int size = 1;
|
---|
260 | for(int i = 0; i < current.getChildCount(); i++) {
|
---|
261 | size = size + size(current.getChildAt(i));
|
---|
262 | }
|
---|
263 | return size;
|
---|
264 | }
|
---|
265 |
|
---|
266 | public String toString() {
|
---|
267 | return element.toString();
|
---|
268 | }
|
---|
269 |
|
---|
270 | public Vector traverseTree() {
|
---|
271 | Vector contents = new Vector();
|
---|
272 | contents.addAll(traverseTree((GValueNode) root));
|
---|
273 | contents.remove((GValueNode) root);
|
---|
274 | return contents;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | public void updateValue(String new_value, String old_value) {
|
---|
279 | ///ystem.err.println("Updating '" + old_value + "' to '" + new_value + "'");
|
---|
280 | StringTokenizer tokenizer = new StringTokenizer(new_value, "\\");
|
---|
281 | GValueNode subject = (GValueNode) root;
|
---|
282 | while(tokenizer.hasMoreTokens()) {
|
---|
283 | String token = tokenizer.nextToken();
|
---|
284 | subject = updateValue(token, old_value, subject);
|
---|
285 | }
|
---|
286 | }
|
---|
287 | */
|
---|
288 |
|
---|
289 | /** Called to update a certain value of metadata within a specific
|
---|
290 | * subject within a sbject hierarchy.
|
---|
291 | * Note that this simply in turn calls removeValue() to deal with the
|
---|
292 | * old value, then addValue() to account for the new.
|
---|
293 | * @param new_value A String representing the updated value.
|
---|
294 | * @param old_value A String representing the old value.
|
---|
295 | * @param subject An Element representing the subject you wish to update
|
---|
296 | * this value in.
|
---|
297 | */
|
---|
298 | public GValueNode updateValue(String new_value, String old_value, GValueNode subject) {
|
---|
299 | return addValue(new_value, subject, null);
|
---|
300 | }
|
---|
301 |
|
---|
302 | private GValueModel copy() {
|
---|
303 | Element document_element = ((GValueNode) root).getElement();
|
---|
304 | Document document_copy = MSMUtils.getValueTreeTemplate();
|
---|
305 | Element document_copy_element = document_copy.getDocumentElement();
|
---|
306 | document_copy_element.setAttribute("element", element.getName());
|
---|
307 | for(Node node = document_element.getFirstChild(); node != null; node = node.getNextSibling()) {
|
---|
308 | if(node.getNodeName().equals("Subject")) {
|
---|
309 | Node node_copy = document_copy.importNode(node, true);
|
---|
310 | document_copy_element.appendChild(node_copy);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | return new GValueModel(element, document_copy);
|
---|
314 | }
|
---|
315 |
|
---|
316 | private String getHIndex(GValueNode node, String value, String index) {
|
---|
317 | for(int i = node.size(); i != 0; i--) {
|
---|
318 | GValueNode next = (GValueNode)node.get(i - 1);
|
---|
319 | String next_str = next.toString();
|
---|
320 | if(value.startsWith(next_str)) {
|
---|
321 | if(index == null) {
|
---|
322 | index = String.valueOf(i);
|
---|
323 | }
|
---|
324 | else {
|
---|
325 | index = index + "." + i;
|
---|
326 | }
|
---|
327 | value = value.substring(next.toString().length());
|
---|
328 | if(value.startsWith(StaticStrings.ESCAPE_STR + StaticStrings.ESCAPE_STR)) {
|
---|
329 | value = value.substring(2);
|
---|
330 | index = getHIndex(next, value, index);
|
---|
331 | }
|
---|
332 | return index;
|
---|
333 | }
|
---|
334 | }
|
---|
335 | return index;
|
---|
336 | }
|
---|
337 |
|
---|
338 | private Vector traverseTree(GValueNode node) {
|
---|
339 | Vector contents = new Vector();
|
---|
340 | contents.add(node);
|
---|
341 | for(int i = 0; i < node.getChildCount(); i++) {
|
---|
342 | contents.addAll(traverseTree((GValueNode)node.getChildAt(i)));
|
---|
343 | }
|
---|
344 | return contents;
|
---|
345 | }
|
---|
346 | }
|
---|