001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import java.lang.reflect.Field;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class JavaFieldsParser {
030    
031            public static String parse(ClassLoader classLoader, String s) {
032                    int x = s.indexOf("${");
033    
034                    if (x == -1) {
035                            return s;
036                    }
037    
038                    List<String> replaceFrom = new ArrayList<String>();
039                    List<String> replaceWith = new ArrayList<String>();
040    
041                    while (true) {
042                            if (x == -1) {
043                                    break;
044                            }
045    
046                            int y = s.indexOf("}", x);
047    
048                            if (y == -1) {
049                                    break;
050                            }
051    
052                            String javaSnippet = s.substring(x + 2, y);
053    
054                            if (_log.isDebugEnabled()) {
055                                    _log.debug("Java snippet " + javaSnippet);
056                            }
057    
058                            String className = _getClassName(javaSnippet);
059    
060                            if (_log.isDebugEnabled()) {
061                                    _log.debug("Class name " + className);
062                            }
063    
064                            if (className == null) {
065                                    break;
066                            }
067    
068                            Class<?> clazz = null;
069    
070                            try {
071                                    clazz = classLoader.loadClass(className);
072                            }
073                            catch (Exception e) {
074                                    if (_log.isWarnEnabled()) {
075                                            _log.warn("Unable to load class " + className);
076                                    }
077    
078                                    break;
079                            }
080    
081                            String fieldName = _getFieldName(javaSnippet);
082    
083                            if (_log.isDebugEnabled()) {
084                                    _log.debug("Field name " + fieldName);
085                            }
086    
087                            if (fieldName == null) {
088                                    break;
089                            }
090    
091                            String fieldValue = null;
092    
093                            try {
094                                    Field field = clazz.getField(fieldName);
095    
096                                    fieldValue = String.valueOf(field.get(null));
097    
098                                    if (_log.isDebugEnabled()) {
099                                            _log.debug("Field value " + fieldValue);
100                                    }
101                            }
102                            catch (Exception e) {
103                                    if (_log.isWarnEnabled()) {
104                                            _log.warn("Unable to load field " + fieldName);
105                                    }
106    
107                                    break;
108                            }
109    
110                            replaceFrom.add("${".concat(javaSnippet).concat("}"));
111                            replaceWith.add(fieldValue);
112    
113                            x = s.indexOf("${", y);
114                    }
115    
116                    if (replaceFrom.isEmpty()) {
117                            return s;
118                    }
119    
120                    return StringUtil.replace(
121                            s, replaceFrom.toArray(new String[replaceFrom.size()]),
122                            replaceWith.toArray(new String[replaceWith.size()]));
123            }
124    
125            private static String _getClassName(String javaSnippet) {
126                    int x = javaSnippet.lastIndexOf(".");
127    
128                    if (x == -1) {
129                            return null;
130                    }
131    
132                    return javaSnippet.substring(0, x);
133            }
134    
135            private static String _getFieldName(String javaSnippet) {
136                    int x = javaSnippet.lastIndexOf(".");
137    
138                    if (x == -1) {
139                            return null;
140                    }
141    
142                    return javaSnippet.substring(x + 1);
143            }
144    
145            private static Log _log = LogFactoryUtil.getLog(JavaFieldsParser.class);
146    
147    }