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 | */
|
---|
27 | package org.greenstone.gatherer.cdm;
|
---|
28 |
|
---|
29 | import java.util.ArrayList;
|
---|
30 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
31 | import org.w3c.dom.*;
|
---|
32 |
|
---|
33 |
|
---|
34 | /** A pretty unexciting extension of a two character string, in that it has a field which details if its the default language.
|
---|
35 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
36 | * @version 2.1
|
---|
37 | */
|
---|
38 | public class Language
|
---|
39 | implements Comparable, DOMProxyListEntry {
|
---|
40 | /** The Element this language entry is based upon. */
|
---|
41 | private Element element = null;
|
---|
42 | /** A comma separated list of two character codes */
|
---|
43 | private String code = null;
|
---|
44 | /** A comma separated list of language names */
|
---|
45 | private String name = null;
|
---|
46 |
|
---|
47 | public Language() {
|
---|
48 | }
|
---|
49 |
|
---|
50 | public Language(Element element) {
|
---|
51 | this.element = element;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /** Constructor for a brand new language. */
|
---|
55 | public Language(String code) {
|
---|
56 | this.code = code;
|
---|
57 | // Create the new element
|
---|
58 | element = CollectionConfiguration.createElement(StaticStrings.LANGUAGE_ELEMENT);
|
---|
59 | element.setAttribute(StaticStrings.NAME_ATTRIBUTE, code);
|
---|
60 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** Constructor which takes an ArrayList of codes */
|
---|
64 | public Language(ArrayList codes) {
|
---|
65 | StringBuffer code_str = new StringBuffer();
|
---|
66 | boolean first = true;
|
---|
67 | for (int i=0; i<codes.size(); i++) {
|
---|
68 | if (!first) {
|
---|
69 | code_str.append(",");
|
---|
70 | } else {
|
---|
71 | first = false;
|
---|
72 | }
|
---|
73 | code_str.append(codes.get(i));
|
---|
74 | }
|
---|
75 | this.code = code_str.toString();
|
---|
76 | element = CollectionConfiguration.createElement(StaticStrings.LANGUAGE_ELEMENT);
|
---|
77 | element.setAttribute(StaticStrings.NAME_ATTRIBUTE, code);
|
---|
78 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Method to compare two languages for ordering purposes.
|
---|
83 | * @param object the other language as an Object
|
---|
84 | * @return an int which indicates the order between this language and the given one: < 0, 0 or > 0 for before, equal to or after respectively
|
---|
85 | */
|
---|
86 | public int compareTo(Object object) {
|
---|
87 | return toString().compareTo(object.toString());
|
---|
88 | }
|
---|
89 |
|
---|
90 | public DOMProxyListEntry create(Element element) {
|
---|
91 | return new Language(element);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** Method to test for the equality of two languages.
|
---|
95 | * @param object The other language as an <strong>Object</strong>.
|
---|
96 | * @return <i>true</i> if the languages are equal, <i>false</i> otherwise.
|
---|
97 | */
|
---|
98 | public boolean equals(Object object) {
|
---|
99 | // two langs are equal if their codes are equal
|
---|
100 | if (object instanceof Language) {
|
---|
101 | return getCode().equals(((Language)object).getCode());
|
---|
102 | } else if (object instanceof String) {
|
---|
103 | return getCode().equals((String)object);
|
---|
104 | }
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** Method to retrieve the code of this language.
|
---|
109 | * @return A <strong>String</strong> representing the two letter code.
|
---|
110 | */
|
---|
111 | public String getCode() {
|
---|
112 | if(code == null && element != null) {
|
---|
113 | code = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
114 | }
|
---|
115 | return code;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public Element getElement() {
|
---|
119 | return element;
|
---|
120 | }
|
---|
121 |
|
---|
122 | public String getName() {
|
---|
123 | if(name == null) {
|
---|
124 | String code_str = getCode();
|
---|
125 | String [] codes = code_str.split(",");
|
---|
126 | StringBuffer name_str = new StringBuffer();
|
---|
127 | boolean first = true;
|
---|
128 | for (int i=0; i<codes.length; i++) {
|
---|
129 | if (!first) {
|
---|
130 | name_str.append(",");
|
---|
131 | } else {
|
---|
132 | first = false;
|
---|
133 | }
|
---|
134 | name_str.append(CollectionDesignManager.language_manager.getLanguageName(codes[i]));
|
---|
135 | }
|
---|
136 | name = name_str.toString();
|
---|
137 | }
|
---|
138 | return name;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public boolean isAssigned() {
|
---|
142 | return (element != null && element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void setAssigned(boolean value) {
|
---|
146 | if(element != null) {
|
---|
147 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void setCode(String new_code) {
|
---|
152 | code = new_code;
|
---|
153 | // Set element
|
---|
154 | if(element != null) {
|
---|
155 | element.setAttribute(StaticStrings.NAME_ATTRIBUTE, new_code);
|
---|
156 | }
|
---|
157 | // Reset name
|
---|
158 | name = null;
|
---|
159 | }
|
---|
160 |
|
---|
161 | public void setElement(Element new_element) {
|
---|
162 | element = new_element;
|
---|
163 | code = null;
|
---|
164 | name = null;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /** Method to display the language code.
|
---|
168 | * @return A <strong>String</strong> representing the language code.
|
---|
169 | */
|
---|
170 | public String toString() {
|
---|
171 | return getName();
|
---|
172 | }
|
---|
173 | }
|
---|