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