source: trunk/gli/src/org/greenstone/gatherer/gems/GValueModel.java@ 8930

Last change on this file since 8930 was 8652, checked in by mdewsnip, 20 years ago

Removed more dead code, and moved most of the XML stuff out of Utility and into XMLTools.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.0 KB
Line 
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 */
27package org.greenstone.gatherer.gems;
28
29import java.util.*;
30import javax.swing.tree.*;
31import org.greenstone.gatherer.util.PatternTokenizer;
32import org.greenstone.gatherer.util.StaticStrings;
33import org.greenstone.gatherer.util.Utility;
34import org.w3c.dom.*;
35/*
36 * @author John Thompson, Greenstone Digital Library, University of Waikato
37 * @version 2.1
38 */
39public class GValueModel
40 extends DefaultTreeModel {
41
42 private ElementWrapper element;
43
44 public GValueModel() {
45 super(new DefaultMutableTreeNode("Temp"));
46 }
47
48 public GValueModel(String root) {
49 super(new DefaultMutableTreeNode(root));
50 }
51
52 public GValueModel(ElementWrapper e) {
53 super(new DefaultMutableTreeNode("Temp"));
54 this.element = e;
55 // Load the template value tree document.
56 Document document = MSMUtils.getValueTreeTemplate();
57 Element new_root = document.getDocumentElement();
58 new_root.setAttribute("element", e.getName());
59 root = new GValueNode(new_root);
60 }
61
62 public GValueModel(ElementWrapper e, Document document) {
63 super(new DefaultMutableTreeNode("Temp"));
64 this.element = e;
65 Element new_root = document.getDocumentElement();
66 new_root.setAttribute("element", e.getName());
67 root = new GValueNode(new_root);
68 }
69
70 /** Value may include path ie news\newssw */
71 public GValueNode addValue(String value) {
72 try {
73 // Tokenize the string using the escaped character
74 PatternTokenizer tokenizer = new PatternTokenizer(value, StaticStrings.PIPE_STR);
75 GValueNode subject = (GValueNode) root;
76 while(tokenizer.hasMoreTokens() && subject != null) {
77 String token = tokenizer.nextToken();
78 subject = addValue(token, subject);
79 }
80 return subject;
81 }
82 catch (Exception error) {
83 error.printStackTrace();
84 }
85 return null;
86 }
87
88 public GValueNode addValue(String value, GValueNode subject) {
89 // 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...
90 GValueNode value_node = subject.getValue(value);
91 if(value_node == null) {
92 // Now add the new value.
93 Document document = subject.getElement().getOwnerDocument();
94 // Now we create a new subject and add it subject
95 Element new_subject = document.createElementNS("","Subject");
96 Element new_value = document.createElementNS("","Value");
97 new_subject.appendChild(new_value);
98 Text new_text = document.createTextNode(value);
99 new_value.appendChild(new_text);
100 value_node = new GValueNode(new_subject);
101 ///ystem.err.println("(GValueModel) addValue()... " + value_node);
102
103 // Figure out where this node will be inserted in subjects
104 // children.
105 int position = -1;
106 for(int i = 0; position == -1 && i < subject.getChildCount(); i++) {
107 Object sibling = subject.getChildAt(i);
108 int rel_pos = value.compareTo(sibling.toString());
109 ///ystem.err.println("'"+value+"'.compareTo('"+sibling+"') = " + rel_pos);
110 if(rel_pos <= 0) {
111 position = i;
112 }
113 }
114 if(position == -1) {
115 position = subject.getChildCount();
116 }
117 // Insert it. If position is still -1, append it to the end of subjects children.
118 ///ystem.err.println("Inserting '" + value + "' at position " + position);
119 insertNodeInto(value_node, subject, position);
120 ///ystem.err.println("(GValueModel) Done insert node into...");
121 // SynchronizedTreeModelTools.insertNodeInto(this, subject, value_node);
122 // Inform listeners that we've changed.
123 // Gatherer.c_man.getCollection().msm.fireValueChanged(element, null, this);
124 }
125 return value_node;
126 }
127
128 public Document getDocument() {
129 return ((GValueNode)root).getElement().getOwnerDocument();
130 }
131
132 public ElementWrapper getElement() {
133 return element;
134 }
135
136 /** Retrieve the hindex for a certain value within the value tree.
137 * @param value The value whose index you wish to determine as a <strong>String</strong>.
138 * @return A <strong>String</strong> containing an index such as "1", "2.1" or "18.2.5".
139 */
140 public String getHIndex(String value) {
141 ///ystem.err.println("getHIndex(" + value + ")");
142 return getHIndex((GValueNode) root, value, null);
143 }
144
145 /** Retrieve a value node given its hierarchical reference or value.
146 * @param index_str The hierarchy index or value as a <strong>String</strong>.
147 */
148 public GValueNode getValue(String index_str) {
149 ///ystem.err.println("Retrieve the value for: " + index_str);
150 GValueNode result = null;
151 if(isHierarchy() && isIndex(index_str)) {
152 // StringTokenize the index
153 StringTokenizer tokenizer = new StringTokenizer(index_str, ".");
154 result = (GValueNode) root;
155 // Using the index numbers retrieve the appropriate node.
156 try {
157 while(result != null && tokenizer.hasMoreTokens()) {
158 int index = Integer.parseInt(tokenizer.nextToken()) - 1;
159 // Retrieve the index'th child of the current result node
160 if(0 <= index && index < result.getChildCount()) {
161 result = (GValueNode) result.getChildAt(index);
162 }
163 // Otherwise we're broken.
164 else {
165 ///ystem.err.println("There is no " + index + "th childnode of " + result);
166 result = null;
167 }
168 }
169 }
170 // Most likely caused by parseInt throwing a wobbly.
171 catch (Exception error) {
172 result = null;
173 }
174 }
175 if(result == null) {
176 ///ystem.err.println("No existing value. Adding " + index_str);
177 result = addValue(index_str);
178 }
179 return result;
180 }
181
182
183 /** A string is a valid hierarchy index if it matches '[0-9](\.[0-9])*' */
184 public boolean isIndex(String raw) {
185 boolean result = true;
186 for(int i = 0; result && i < raw.length(); i++) {
187 char c = raw.charAt(i);
188 if(Character.isDigit(c) || (c == '.' && (i != 0 || i != raw.length() - 1))) {
189 // Valid index
190 }
191 else {
192 result = false;
193 }
194 }
195 return result;
196 }
197
198 public boolean isHierarchy() {
199 boolean result = false;
200 // We are a hierarchy if our element says so....
201 if(element.isHierarchy()) {
202 return true;
203 }
204 // Or if our children are actually a hierarchy.
205 for(int i = 0; i < root.getChildCount() && !result; i++) {
206 GValueNode node = (GValueNode) root.getChildAt(i);
207 if(node != null && node.getChildCount() > 0) {
208 result = true;
209 }
210 }
211 return result;
212 }
213
214 public void removeValue(GValueNode child) {
215 //SynchronizedTreeModelTools.removeNodeFromParent(this, child);
216 removeNodeFromParent(child);
217 // Gatherer.c_man.getCollection().msm.fireValueChanged(new ElementWrapper(child.getElement()), null, this);
218 }
219
220 public void removeValue(String value) {
221 // Retrieve the node to be removed.
222 GValueNode node = getValue(value);
223 if(node != null) {
224 removeValue(node);
225 }
226 }
227
228 public int size() {
229 return size(root);
230 }
231
232 private int size(TreeNode current) {
233 int size = 1;
234 for(int i = 0; i < current.getChildCount(); i++) {
235 size = size + size(current.getChildAt(i));
236 }
237 return size;
238 }
239
240 public String toString() {
241 return element.toString();
242 }
243
244 public Vector traverseTree() {
245 Vector contents = new Vector();
246 contents.addAll(traverseTree((GValueNode) root));
247 contents.remove((GValueNode) root);
248 return contents;
249 }
250
251 private GValueModel copy() {
252 Element document_element = ((GValueNode) root).getElement();
253 Document document_copy = MSMUtils.getValueTreeTemplate();
254 Element document_copy_element = document_copy.getDocumentElement();
255 document_copy_element.setAttribute("element", element.getName());
256 for(Node node = document_element.getFirstChild(); node != null; node = node.getNextSibling()) {
257 if(node.getNodeName().equals("Subject")) {
258 Node node_copy = document_copy.importNode(node, true);
259 document_copy_element.appendChild(node_copy);
260 }
261 }
262 return new GValueModel(element, document_copy);
263 }
264
265 private String getHIndex(GValueNode node, String value, String index) {
266 ///ystem.err.println("Get the HIndex for: " + value);
267 for(int i = node.size(); i != 0; i--) {
268 GValueNode next = (GValueNode)node.get(i - 1);
269 String next_str = next.toString(GValueNode.DOM);
270 ///ystem.err.println("Does " + value + " start with " + next_str + "?");
271 if(value.startsWith(next_str)) {
272 if(index == null) {
273 index = String.valueOf(i);
274 }
275 else {
276 index = index + "." + i;
277 }
278 value = value.substring(next.toString().length());
279 ///ystem.err.println("We matched the start so value is now: " + value);
280 if(value.startsWith(StaticStrings.PIPE_STR) || value.startsWith(StaticStrings.ESCAPE_STR)) {
281 value = value.substring(1);
282 index = getHIndex(next, value, index);
283 }
284 ///ystem.err.println("Returning " + index);
285 return index;
286 }
287 }
288 ///ystem.err.println("Returning " + index);
289 return index;
290 }
291
292 private Vector traverseTree(GValueNode node) {
293 Vector contents = new Vector();
294 contents.add(node);
295 for(int i = 0; i < node.getChildCount(); i++) {
296 contents.addAll(traverseTree((GValueNode)node.getChildAt(i)));
297 }
298 return contents;
299 }
300}
Note: See TracBrowser for help on using the repository browser.