001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.model;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portal.util.InitUtil;
026: import com.liferay.portal.util.PropsUtil;
027: import com.liferay.util.CollectionFactory;
028: import com.liferay.util.ListUtil;
029:
030: import java.io.StringReader;
031:
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Map;
035: import java.util.Set;
036: import java.util.TreeSet;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: import org.dom4j.Document;
042: import org.dom4j.Element;
043: import org.dom4j.io.SAXReader;
044:
045: /**
046: * <a href="ModelHintsUtil.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: *
050: */
051: public class ModelHintsUtil {
052:
053: static {
054: InitUtil.init();
055: }
056:
057: public static Map getDefaultHints(String model) {
058: return _instance._getDefaultHints(model);
059: }
060:
061: public static Element getFieldsEl(String model, String field) {
062: return _instance._getFieldsEl(model, field);
063: }
064:
065: public static List getModels() {
066: return _instance._getModels();
067: }
068:
069: public static String getType(String model, String field) {
070: return _instance._getType(model, field);
071: }
072:
073: public static Map getHints(String model, String field) {
074: return _instance._getHints(model, field);
075: }
076:
077: public static String trimString(String model, String field,
078: String value) {
079: if (value == null) {
080: return value;
081: }
082:
083: Map hints = getHints(model, field);
084:
085: if (hints == null) {
086: return value;
087: }
088:
089: int maxLength = GetterUtil
090: .getInteger(ModelHintsDefaults.TEXT_MAX_LENGTH);
091:
092: maxLength = GetterUtil.getInteger((String) hints
093: .get("max-length"), maxLength);
094:
095: if (value.length() > maxLength) {
096: return value.substring(0, maxLength);
097: } else {
098: return value;
099: }
100: }
101:
102: private ModelHintsUtil() {
103: _hintCollections = CollectionFactory.getHashMap();
104: _defaultHints = CollectionFactory.getHashMap();
105: _modelFields = CollectionFactory.getHashMap();
106: _models = new TreeSet();
107:
108: try {
109: ClassLoader classLoader = getClass().getClassLoader();
110:
111: String[] configs = StringUtil.split(PropsUtil
112: .get(PropsUtil.MODEL_HINTS_CONFIGS));
113:
114: for (int i = 0; i < configs.length; i++) {
115: _read(classLoader, configs[i]);
116: }
117: } catch (Exception e) {
118: _log.error(e, e);
119: }
120: }
121:
122: private Map _getDefaultHints(String model) {
123: return (Map) _defaultHints.get(model);
124: }
125:
126: private Element _getFieldsEl(String model, String field) {
127: Map fields = (Map) _modelFields.get(model);
128:
129: if (fields == null) {
130: return null;
131: } else {
132: return (Element) fields.get(field + _ELEMENTS_SUFFIX);
133: }
134: }
135:
136: private List _getModels() {
137: return ListUtil.fromCollection(_models);
138: }
139:
140: private String _getType(String model, String field) {
141: Map fields = (Map) _modelFields.get(model);
142:
143: if (fields == null) {
144: return null;
145: } else {
146: return (String) fields.get(field + _TYPE_SUFFIX);
147: }
148: }
149:
150: private Map _getHints(String model, String field) {
151: Map fields = (Map) _modelFields.get(model);
152:
153: if (fields == null) {
154: return null;
155: } else {
156: return (Map) fields.get(field + _HINTS_SUFFIX);
157: }
158: }
159:
160: private void _read(ClassLoader classLoader, String source)
161: throws Exception {
162:
163: String xml = null;
164:
165: try {
166: xml = StringUtil.read(classLoader, source);
167: } catch (Exception e) {
168: _log.warn("Cannot load " + source);
169: }
170:
171: if (xml == null) {
172: return;
173: }
174:
175: if (_log.isDebugEnabled()) {
176: _log.debug("Loading " + source);
177: }
178:
179: SAXReader reader = new SAXReader();
180:
181: Document doc = reader.read(new StringReader(xml));
182:
183: Element root = doc.getRootElement();
184:
185: Iterator itr1 = root.elements("hint-collection").iterator();
186:
187: while (itr1.hasNext()) {
188: Element hintCollection = (Element) itr1.next();
189:
190: String name = hintCollection.attributeValue("name");
191:
192: Map hints = (Map) _hintCollections.get(name);
193:
194: if (hints == null) {
195: hints = CollectionFactory.getHashMap();
196:
197: _hintCollections.put(name, hints);
198: }
199:
200: Iterator itr2 = hintCollection.elements("hint").iterator();
201:
202: while (itr2.hasNext()) {
203: Element hint = (Element) itr2.next();
204:
205: String hintName = hint.attributeValue("name");
206: String hintValue = hint.getText();
207:
208: hints.put(hintName, hintValue);
209: }
210: }
211:
212: itr1 = root.elements("model").iterator();
213:
214: while (itr1.hasNext()) {
215: Element model = (Element) itr1.next();
216:
217: String name = model.attributeValue("name");
218:
219: Map defaultHints = CollectionFactory.getHashMap();
220:
221: _defaultHints.put(name, defaultHints);
222:
223: Element defaultHintsEl = model.element("default-hints");
224:
225: if (defaultHintsEl != null) {
226: Iterator itr2 = defaultHintsEl.elements("hint")
227: .iterator();
228:
229: while (itr2.hasNext()) {
230: Element hint = (Element) itr2.next();
231:
232: String hintName = hint.attributeValue("name");
233: String hintValue = hint.getText();
234:
235: defaultHints.put(hintName, hintValue);
236: }
237: }
238:
239: Map fields = (Map) _modelFields.get(name);
240:
241: if (fields == null) {
242: fields = CollectionFactory.getHashMap();
243:
244: _modelFields.put(name, fields);
245: }
246:
247: _models.add(name);
248:
249: Iterator itr2 = model.elements("field").iterator();
250:
251: while (itr2.hasNext()) {
252: Element field = (Element) itr2.next();
253:
254: String fieldName = field.attributeValue("name");
255: String fieldType = field.attributeValue("type");
256:
257: Map fieldHints = CollectionFactory.getHashMap();
258:
259: fieldHints.putAll(defaultHints);
260:
261: Iterator itr3 = field.elements("hint-collection")
262: .iterator();
263:
264: while (itr3.hasNext()) {
265: Element hintCollection = (Element) itr3.next();
266:
267: Map hints = (Map) _hintCollections
268: .get(hintCollection.attributeValue("name"));
269:
270: fieldHints.putAll(hints);
271: }
272:
273: itr3 = field.elements("hint").iterator();
274:
275: while (itr3.hasNext()) {
276: Element hint = (Element) itr3.next();
277:
278: String hintName = hint.attributeValue("name");
279: String hintValue = hint.getText();
280:
281: fieldHints.put(hintName, hintValue);
282: }
283:
284: fields.put(fieldName + _ELEMENTS_SUFFIX, field);
285: fields.put(fieldName + _TYPE_SUFFIX, fieldType);
286: fields.put(fieldName + _HINTS_SUFFIX, fieldHints);
287: }
288: }
289: }
290:
291: private static final String _ELEMENTS_SUFFIX = "_ELEMENTS";
292:
293: private static final String _TYPE_SUFFIX = "_TYPE";
294:
295: private static final String _HINTS_SUFFIX = "_HINTS";
296:
297: private static Log _log = LogFactory.getLog(ModelHintsUtil.class);
298:
299: private static ModelHintsUtil _instance = new ModelHintsUtil();
300:
301: private Map _hintCollections;
302: private Map _defaultHints;
303: private Map _modelFields;
304: private Set _models;
305:
306: }
|