KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > content > DCLanguage


1 /*
2  * DCLanguage.java
3  *
4  * Version: $Revision: 1.5 $
5  *
6  * Date: $Date: 2005/04/20 14:22:32 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.content;
41
42 import java.util.Locale JavaDoc;
43
44 /**
45  * Utility class for dealing with languages
46  *
47  * @author Robert Tansley
48  * @version $Revision: 1.5 $
49  */

50 public class DCLanguage
51 {
52     /** The country code */
53     private String JavaDoc country;
54
55     /** The language code. Special values: "" and "other". */
56     private String JavaDoc language;
57
58     /**
59      * Construct a language object from a database entry
60      *
61      * @param l
62      * the language text from the database
63      */

64     public DCLanguage(String JavaDoc l)
65     {
66         setLanguage(l);
67     }
68
69     /**
70      * Write the language out to the database
71      *
72      * @return the language in a form for writing to the DCValue table
73      */

74     public String JavaDoc toString()
75     {
76         if (language.equals(""))
77         {
78             return "";
79         }
80         else if (country.equals(""))
81         {
82             return language;
83         }
84         else
85         {
86             return country + "_" + language;
87         }
88     }
89
90     /**
91      * Set the language and country
92      *
93      * @param l
94      * The language and country code, e.g. "en_US" or "fr"
95      */

96     public void setLanguage(String JavaDoc l)
97     {
98         if (l.equals("other"))
99         {
100             language = "other";
101             country = "";
102         }
103         else if (l.length() == 2)
104         {
105             language = l;
106             country = "";
107         }
108         else if (l.length() == 5)
109         {
110             language = l.substring(0, 2);
111             country = l.substring(3);
112         }
113         else
114         {
115             language = "";
116             country = "";
117         }
118     }
119
120     /**
121      * Get the displayable name for this language
122      *
123      * @return the displayable name
124      */

125     public String JavaDoc getDisplayName()
126     {
127         Locale JavaDoc locale;
128
129         if (language.equals("other"))
130         {
131             return "(Other)";
132         }
133         else if (language.equals(""))
134         {
135             return "N/A";
136         }
137         else
138         {
139             locale = new Locale JavaDoc(language, country);
140
141             return locale.getDisplayName();
142         }
143     }
144 }
145
Popular Tags