source: trunk/gli/src/org/greenstone/gatherer/shell/GShellElement.java@ 6388

Last change on this file since 6388 was 6388, checked in by jmt12, 20 years ago

Completely rewrote monitors to be language independant - fixed several bugs including a decreasing progress bar and added a new reset() method to ensure that progress bars are pristine now that they can be onscreen at all times (lower detail modes)

  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 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, New Zealand Digital Library, University of Waikato
9 *
10 * Copyright (C) 2004 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.shell;
28import java.util.HashMap;
29import java.util.StringTokenizer;
30import org.greenstone.gatherer.util.StaticStrings;
31/** This class wraps around a pseudo-XML message we have recieved from an external script, and allows access to the element name, attributes etc via method calls. */
32public class GShellElement {
33 private boolean is_closed = false;
34 private HashMap attributes;
35 private String element_name;
36 /** Create a new element around the message string. The string is expected to start with < and end with > */
37 public GShellElement(String message) {
38 // Remove the angle brackets
39 message = message.substring(1, message.length() - 1);
40 // Tokenize the message
41 StringTokenizer tokenizer = new StringTokenizer(message);
42 // The first token is the element name - be aware it may start with the close tag indicator
43 element_name = tokenizer.nextToken();
44 if(element_name.startsWith(StaticStrings.URL_SEPARATOR_CHARACTER)) {
45 element_name = element_name.substring(1);
46 is_closed = true;
47 }
48 // Now while we have remaining tokens
49 while(tokenizer.hasMoreTokens()) {
50 int index = -1;
51 // The next token might be a attribute=value pair or it could be the close tag indicator
52 String token = tokenizer.nextToken();
53 if(token.equals(StaticStrings.URL_SEPARATOR_CHARACTER)) {
54 is_closed = true;
55 }
56 else if(token.length() > 3 && (index = token.indexOf(StaticStrings.EQUALS_CHARACTER)) != -1) {
57 String attribute_name = token.substring(0, index);
58 String attribute_value = token.substring(index + 1);
59 // Value may end in a /
60 if(attribute_value.endsWith(StaticStrings.URL_SEPARATOR_CHARACTER)) {
61 is_closed = true;
62 attribute_value = attribute_value.substring(0, attribute_value.length() - 1);
63 }
64 // We may have to strip ' marks off value
65 if(attribute_value.startsWith(StaticStrings.SINGLE_QUOTE_CHARACTER) && attribute_value.endsWith(StaticStrings.SINGLE_QUOTE_CHARACTER)) {
66 attribute_value = attribute_value.substring(1, attribute_value.length() - 1);
67 }
68 if(attributes == null) {
69 attributes = new HashMap();
70 }
71 attributes.put(attribute_name, attribute_value);
72 attribute_value = null;
73 attribute_name = null;
74 }
75 token = null;
76 }
77 tokenizer = null;
78 }
79
80 public String getAttribute(String attribute_name) {
81 String result = null;
82 if(attributes != null) {
83 result = (String) attributes.get(attribute_name);
84 }
85 if(result == null) {
86 result = "";
87 }
88 return result;
89 }
90
91 public String getElementName() {
92 return element_name;
93 }
94
95 public boolean isClosed() {
96 return is_closed;
97 }
98}
99
Note: See TracBrowser for help on using the repository browser.