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 | */
|
---|
27 | package org.greenstone.gatherer.shell;
|
---|
28 | import java.util.HashMap;
|
---|
29 | import java.util.StringTokenizer;
|
---|
30 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
31 |
|
---|
32 | /** 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. */
|
---|
33 | public class GShellElement {
|
---|
34 | private boolean is_closed = false;
|
---|
35 | private HashMap attributes;
|
---|
36 | private String element_name;
|
---|
37 | /** Create a new element around the message string. The string is expected to start with < and end with > */
|
---|
38 | /** this is a hack - why aren't we using proper XML parsing?? */
|
---|
39 | public GShellElement(String message) {
|
---|
40 | // Remove the angle brackets
|
---|
41 | message = message.substring(1, message.length() - 1);
|
---|
42 | // Tokenize the message
|
---|
43 | StringTokenizer tokenizer = new StringTokenizer(message);
|
---|
44 | // The first token is the element name - be aware it may start with the close tag indicator
|
---|
45 | element_name = tokenizer.nextToken();
|
---|
46 | if(element_name.startsWith(StaticStrings.URL_SEPARATOR_CHARACTER)) {
|
---|
47 | element_name = element_name.substring(1);
|
---|
48 | is_closed = true;
|
---|
49 | }
|
---|
50 | // Now while we have remaining tokens
|
---|
51 | while(tokenizer.hasMoreTokens()) {
|
---|
52 | int index = -1;
|
---|
53 | // The next token might be a attribute=value pair or it could be the close tag indicator
|
---|
54 | String token = tokenizer.nextToken();
|
---|
55 | if(token.equals(StaticStrings.URL_SEPARATOR_CHARACTER)) {
|
---|
56 | is_closed = true;
|
---|
57 | }
|
---|
58 | else if(token.length() > 3 && (index = token.indexOf(StaticStrings.EQUALS_CHARACTER)) != -1) {
|
---|
59 | String attribute_name = token.substring(0, index);
|
---|
60 | String attribute_value = token.substring(index + 1);
|
---|
61 | // Value may end in a /
|
---|
62 | if(attribute_value.endsWith(StaticStrings.URL_SEPARATOR_CHARACTER)) {
|
---|
63 | is_closed = true;
|
---|
64 | attribute_value = attribute_value.substring(0, attribute_value.length() - 1);
|
---|
65 | }
|
---|
66 | // We may have to strip ' marks off value - if it starts with ' and doesn't end with ' we read in more tokens until we get an ending '
|
---|
67 | if(attribute_value.startsWith(StaticStrings.SINGLE_QUOTE_CHARACTER)) {
|
---|
68 | while (!attribute_value.endsWith(StaticStrings.SINGLE_QUOTE_CHARACTER) && tokenizer.hasMoreTokens()) {
|
---|
69 | attribute_value += " "+tokenizer.nextToken();
|
---|
70 | }
|
---|
71 | if (attribute_value.endsWith(StaticStrings.SINGLE_QUOTE_CHARACTER)) {
|
---|
72 | attribute_value = attribute_value.substring(1, attribute_value.length() - 1);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | if(attributes == null) {
|
---|
76 | attributes = new HashMap();
|
---|
77 | }
|
---|
78 | attributes.put(attribute_name, attribute_value);
|
---|
79 | attribute_value = null;
|
---|
80 | attribute_name = null;
|
---|
81 | }
|
---|
82 | token = null;
|
---|
83 | }
|
---|
84 | tokenizer = null;
|
---|
85 | }
|
---|
86 |
|
---|
87 | public String getAttribute(String attribute_name) {
|
---|
88 | String result = null;
|
---|
89 | if(attributes != null) {
|
---|
90 | result = (String) attributes.get(attribute_name);
|
---|
91 | }
|
---|
92 | if(result == null) {
|
---|
93 | result = "";
|
---|
94 | }
|
---|
95 | return result;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public String getElementName() {
|
---|
99 | return element_name;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public boolean isClosed() {
|
---|
103 | return is_closed;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|