001: package com.xoetrope.carousel.langed;
002:
003: import com.xoetrope.util.XTextDefaults;
004: import java.io.BufferedWriter;
005: import java.io.FileOutputStream;
006: import java.io.IOException;
007: import java.io.OutputStreamWriter;
008: import java.util.Arrays;
009: import java.util.Comparator;
010: import java.util.Date;
011: import java.util.Enumeration;
012: import java.util.Properties;
013: import java.util.Vector;
014:
015: /**
016: * EdLanguage
017: * Extends the basic language class to provide write functionality.
018: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
019: * the GNU Public License (GPL), please see license.txt for more details. If
020: * you make commercial use of this software you must purchase a commercial
021: * license from Xoetrope.</p>
022: * <p> $Revision: 1.11 $</p>
023: * @author Luan O'Carroll
024: */
025: public class EdLanguage extends Language {
026:
027: public EdLanguage(EdLangMgr lm) {
028: langMgr = lm;
029: langName = new String("<>");
030: }
031:
032: /**
033: * Gets the language name e.g. "English"
034: */
035: public String getLangName() {
036: return langName;
037: }
038:
039: /**
040: * Sets the language name e.g. "English"
041: * @param name The new language name.
042: */
043: public void setLangName(String name) {
044: langName = name;
045: }
046:
047: /**
048: * Gets the language country code e.g. "IRL"
049: */
050: public String getLangCode() {
051: return langCode;
052: }
053:
054: /**
055: * Sets the language country code e.g. "IRL"
056: * @param cc The new language code.
057: */
058: public void setLangCode(String cc) {
059: langCode = cc;
060: }
061:
062: /*
063: * Looks up and returns the string at the specified array position.
064: */
065: public String getStringAt(int idx) {
066: int numStrings = langStrings.size();
067: if (idx > numStrings)
068: System.out.println("Invalid string index");
069:
070: return ((LangItem) langStrings.elementAt(idx)).langStr;
071: }
072:
073: public Object getElementAt(int idx) {
074: int numStrings = langStrings.size();
075: if (idx >= numStrings)
076: return null;//System.out.println( "Invalid string index" );
077:
078: return ((LangItem) langStrings.elementAt(idx));
079: }
080:
081: /*
082: * Looks up and returns the number of strings in the language
083: */
084: public int getNumStrings() {
085: return langStrings.size();
086: }
087:
088: /*
089: * Read a vector.
090: */
091: public void read(Vector translations, int field, boolean bSubstrings) {
092: char buffer[] = new char[1024];
093:
094: int numItems = translations.size();
095:
096: // langStrings = new Vector( numItems );
097: // for ( int i = 0; i < numItems; i++ ) {
098: // LangItem li = new LangItem( bSubstrings );
099: // TranslationObject txObj = (TranslationObject)translations.elementAt( i );
100: // li.id = txObj.getId();
101: // li.langStr = replaceTags( txObj.getStringFieldValue( field ));
102: // if ( li.langStr.length() > 0 )
103: // langStrings.add( li );
104: // }
105: }
106:
107: /*
108: * Returns the number of strings in this language.
109: */
110: public int getSize() {
111: return langStrings.size();
112: }
113:
114: /*
115: * Looks up and returns a string.
116: */
117: public String getString(int id) {
118: int numStrings = langStrings.size();
119:
120: for (int i = 0; i < numStrings; i++) {
121: LangItem li = (LangItem) langStrings.elementAt(i);
122: if (li.id == id)
123: return li.langStr;
124: }
125:
126: return "";
127: }
128:
129: LangItem getLangItem(int id) {
130: int numStrings = langStrings.size();
131:
132: for (int i = 0; i < numStrings; i++) {
133: LangItem li = (LangItem) langStrings.elementAt(i);
134: if (li.id == id)
135: return li;
136: }
137:
138: return null;
139: }
140:
141: /**
142: * Looks up and returns a string.
143: */
144: public int findString(String key) {
145: int numStrings = langStrings.size();
146:
147: for (int i = 0; i < numStrings; i++) {
148: LangItem li = (LangItem) langStrings.elementAt(i);
149: if (li.keyStr.equals(key))
150: return li.id;
151: else if (li.langStr.equals(key))
152: return li.id;
153: }
154:
155: return -1;
156: }
157:
158: /*
159: * Looks up and returns the id of the string at the specified array position.
160: */
161: public int getStringId(int idx) {
162: int numStrings = langStrings.size();
163: if (idx >= numStrings) {
164: System.out.println("Invalid string index");
165: return numStrings - 1;
166: }
167:
168: return ((LangItem) langStrings.elementAt(idx)).id;
169: }
170:
171: /*
172: * Looks up and returns the index of the string with the specified id.
173: */
174: public int getStringIndex(int idx) {
175: int numStrings = langStrings.size();
176: for (int i = 0; i < numStrings; i++) {
177: if (((LangItem) langStrings.elementAt(i)).id == idx)
178: return i;
179: }
180:
181: return -1;
182: }
183:
184: /**
185: * Looks up and returns a string.
186: * @param newKeyStr the new language key
187: * @param newValueStr the new language value
188: */
189: public void setString(int id, String newKeyStr, String newValueStr) {
190: int numStrings = langStrings.size();
191:
192: for (int i = 0; i < numStrings; i++) {
193: if (((LangItem) langStrings.elementAt(i)).id == id) {
194: ((LangItem) langStrings.elementAt(i))
195: .setLangStr(newValueStr);
196: return;
197: }
198: }
199: addString(id, newKeyStr, newValueStr);
200: }
201:
202: /**
203: * Looks up and returns a string.
204: */
205: public void setKeyString(int id, String newStr) {
206: int numStrings = langStrings.size();
207:
208: for (int i = 0; i < numStrings; i++) {
209: if (((LangItem) langStrings.elementAt(i)).id == id) {
210: ((LangItem) langStrings.elementAt(i))
211: .setKeyString(newStr);
212: return;
213: }
214: }
215: }
216:
217: /**
218: * Add a new string to the language.
219: * @param id the id of this new language string
220: * @param newKeyStr the new langauge key
221: * @param newValueStr the new langauge value
222: */
223: public int addString(int id, String newKeyStr, String newValueStr) {
224: int numStrings = langStrings.size();
225: langStrings.ensureCapacity(++numStrings);
226:
227: LangItem li = new LangItem();
228: if (id >= 0)
229: li.id = id;
230: else
231: li.id = id = getMaxId() + 1;
232: li.keyStr = newKeyStr;
233: li.langStr = newValueStr;
234: li.status = li.NEW_ITEM;
235:
236: langStrings.addElement(li);
237: return getStringIndex(id);
238: }
239:
240: /**
241: * Add a new string to the language.
242: * @param newKeyStr the new langauge key
243: * @param newValueStr the new langauge value
244: */
245: public int addString(String newKeyStr, String newValueStr) {
246: int numStrings = langStrings.size();
247: int maxId = getMaxId();
248: langStrings.ensureCapacity(++numStrings);
249:
250: LangItem li = new LangItem();
251: li.id = ++maxId;
252: li.keyStr = newKeyStr;
253: li.langStr = newValueStr;
254: li.status = li.NEW_ITEM;
255:
256: langStrings.addElement(li);
257: return maxId;
258: }
259:
260: /**
261: * Gets the maximum string ID in use.
262: * @return
263: */
264: public int getMaxId() {
265: int numStrings = langStrings.size();
266: int maxId = 0;
267: for (int iItem = 0; iItem < numStrings; iItem++) {
268: int srcId = ((LangItem) langStrings.elementAt(iItem)).id;
269: if (srcId > maxId)
270: maxId = srcId;
271: }
272:
273: return maxId;
274: }
275:
276: /*
277: * Add a new string to the language.
278: */
279: public void removeString(int idx) {
280: int numStrings = langStrings.size();
281: if (idx >= numStrings)
282: return;
283:
284: langStrings.removeElementAt(idx);
285: }
286:
287: private String insertTags(String original) {
288: return original.replace(XTextDefaults.CRLF_PAIR.subSequence(0,
289: XTextDefaults.CRLF_PAIR.length()),
290: XTextDefaults.CRLF_PAIR_ENCODING.subSequence(0,
291: XTextDefaults.CRLF_PAIR_ENCODING.length()));
292: }
293:
294: private String replaceTags(String original) {
295: return original.replace(XTextDefaults.CRLF_PAIR_ENCODING
296: .subSequence(0, XTextDefaults.CRLF_PAIR_ENCODING
297: .length()), XTextDefaults.CRLF_PAIR
298: .subSequence(0, XTextDefaults.CRLF_PAIR.length()));
299: }
300:
301: /*
302: * Saves the language to the URL as a properties file.
303: */
304: public void saveProperties(String newFile, String encoding)
305: throws IOException {
306: BufferedWriter bw;
307: if (encoding == null)
308: bw = new BufferedWriter(new OutputStreamWriter(
309: new FileOutputStream(newFile)));
310: else
311: bw = new BufferedWriter(new OutputStreamWriter(
312: new FileOutputStream(newFile), encoding));
313:
314: writeln(bw, "#" + new Date().toString());
315: int numItems = langStrings.size();
316: for (int i = 0; i < numItems; i++) {
317: LangItem li = ((LangItem) langStrings.elementAt(i));
318: writeln(bw, langMgr.getKey(li.id) + "=" + li.langStr);
319: }
320: bw.flush();
321: bw.close();
322: }
323:
324: private static void writeln(BufferedWriter bw, String s)
325: throws IOException {
326: bw.write(s);
327: bw.newLine();
328: }
329:
330: public void sort(Comparator c) {
331: Object[] langItems = langStrings.toArray();
332: int size = langItems.length;
333: Arrays.sort(langItems, c);
334:
335: langStrings = new Vector();
336: for (int i = 0; i < size; i++)
337: langStrings.add(langItems[i]);
338: }
339:
340: private String langName;
341: private String langCode;
342: private Vector langStrings = new Vector();
343: private EdLangMgr langMgr;
344: }
|