KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > validation > JahiaMltHelper


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 28-Feb-2005, Commaro, Benjamin Papez
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40 package org.jahia.engines.validation;
41
42 import java.util.*;
43
44 import org.apache.commons.lang.StringUtils;
45 import org.apache.commons.lang.builder.ToStringBuilder;
46
47 /**
48 *
49 * <p>Title: JahiaMltHelper</p>
50 * <p>Description: This class converts all languages of a field to an array. This is then
51 * used in the user input validation methods.</p>
52 * <p>Copyright: Copyright (c) 2004</p>
53 * <p>Company: Jahia Ltd</p>
54 * @author not attributable
55 * @version 1.0
56 */

57 public class JahiaMltHelper {
58     private String JavaDoc[] text = null;
59     private String JavaDoc[] language = null;
60     private List languageSettings = null;
61
62     /**
63      *
64      */

65     public JahiaMltHelper(List currentLanguageSettings) {
66         super();
67         languageSettings = currentLanguageSettings;
68     }
69
70     /**
71      * Get the text for a given language key, returns empty string if not found
72      *
73      * @param lang a language key
74      * @return text
75      */

76     public String JavaDoc getText(String JavaDoc lang) {
77         for (int i = 0; i < language.length; i++) {
78             if (language[i].equals(lang)) {
79                 return text[i];
80             }
81         }
82         return "";
83     }
84
85     /**
86      * @return language
87      */

88     public String JavaDoc[] getLanguage() {
89         return language;
90     }
91
92     /**
93      * @return text
94      */

95     public String JavaDoc[] getText() {
96         return text;
97     }
98
99     /**
100      * @param strings
101      */

102     public void setLanguage(String JavaDoc[] strings) {
103         language = strings;
104     }
105
106     /**
107      * @param strings
108      */

109     public void setText(String JavaDoc[] strings) {
110         text = strings;
111     }
112
113     /**
114      *
115      * @param lang
116      * @param txt
117      */

118     public void addMltItem(String JavaDoc lang, String JavaDoc txt) {
119         if (language != null && text != null) {
120             int oldLen = language.length;
121             String JavaDoc newLanguage[] = new String JavaDoc[oldLen + 1];
122             String JavaDoc newText[] = new String JavaDoc[oldLen + 1];
123
124             System.arraycopy(language, 0, newLanguage, 0, oldLen);
125             System.arraycopy(text, 0, newText, 0, oldLen);
126             newLanguage[oldLen] = lang;
127             newText[oldLen] = txt;
128             language = newLanguage;
129             text = newText;
130         } else {
131             language = new String JavaDoc[1];
132             language[0] = lang;
133             text = new String JavaDoc[1];
134             text[0] = txt;
135         }
136     }
137
138     /**
139      * @return languageSettings
140      */

141     public List getLanguageSettings() {
142         return languageSettings;
143     }
144
145     public String JavaDoc toString() {
146         return ToStringBuilder.reflectionToString(this);
147     }
148
149     public boolean isEmpty() {
150         boolean nonEmpty = false;
151         for (int i = 0; i < text.length && !nonEmpty; i++) {
152             String JavaDoc s = text[i];
153             if(StringUtils.trimToNull(s)!=null)nonEmpty =true;
154         }
155         return !nonEmpty;
156     }
157 }
158
Popular Tags