001    /**
002     * Copyright (c) 2000-present 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<>();
039                    List<String> replaceWith = new ArrayList<>();
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                                    _log.error("Unable to load class " + className, e);
075    
076                                    break;
077                            }
078    
079                            String fieldName = _getFieldName(javaSnippet);
080    
081                            if (_log.isDebugEnabled()) {
082                                    _log.debug("Field name " + fieldName);
083                            }
084    
085                            if (fieldName == null) {
086                                    break;
087                            }
088    
089                            String fieldValue = null;
090    
091                            try {
092                                    Field field = clazz.getField(fieldName);
093    
094                                    fieldValue = String.valueOf(field.get(null));
095    
096                                    if (_log.isDebugEnabled()) {
097                                            _log.debug("Field value " + fieldValue);
098                                    }
099                            }
100                            catch (Exception e) {
101                                    _log.error("Unable to load field " + fieldName, e);
102    
103                                    break;
104                            }
105    
106                            replaceFrom.add("${".concat(javaSnippet).concat("}"));
107                            replaceWith.add(fieldValue);
108    
109                            x = s.indexOf("${", y);
110                    }
111    
112                    if (replaceFrom.isEmpty()) {
113                            return s;
114                    }
115    
116                    return StringUtil.replace(
117                            s, replaceFrom.toArray(new String[replaceFrom.size()]),
118                            replaceWith.toArray(new String[replaceWith.size()]));
119            }
120    
121            private static String _getClassName(String javaSnippet) {
122                    int x = javaSnippet.lastIndexOf(".");
123    
124                    if (x == -1) {
125                            return null;
126                    }
127    
128                    return javaSnippet.substring(0, x);
129            }
130    
131            private static String _getFieldName(String javaSnippet) {
132                    int x = javaSnippet.lastIndexOf(".");
133    
134                    if (x == -1) {
135                            return null;
136                    }
137    
138                    return javaSnippet.substring(x + 1);
139            }
140    
141            private static final Log _log = LogFactoryUtil.getLog(
142                    JavaFieldsParser.class);
143    
144    }