source: trunk/gli/src/org/greenstone/gatherer/cdm/Language.java@ 12284

Last change on this file since 12284 was 12277, checked in by kjdon, 18 years ago

Language can now have more than one lang code associated with it. getCode or toString return comma separated lists

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