001    /**
002     * Copyright (c) 2000-2013 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.json.transformer;
016    
017    import com.liferay.portal.kernel.json.JSONIncludesManagerUtil;
018    import com.liferay.portal.kernel.json.JSONTransformer;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    
023    import flexjson.JSONContext;
024    import flexjson.Path;
025    import flexjson.PathExpression;
026    
027    import flexjson.transformer.ObjectTransformer;
028    
029    import java.util.List;
030    
031    import jodd.bean.BeanUtil;
032    
033    /**
034     * @author Igor Spasic
035     */
036    public class FlexjsonObjectJSONTransformer
037            extends ObjectTransformer implements JSONTransformer {
038    
039            @Override
040            public void transform(Object object) {
041                    Class<?> type = resolveClass(object);
042    
043                    List<PathExpression> pathExpressions =
044                            (List<PathExpression>)BeanUtil.getDeclaredProperty(
045                                    getContext(), "pathExpressions");
046    
047                    String path = _getPath();
048    
049                    String[] excludes = JSONIncludesManagerUtil.lookupExcludes(type);
050    
051                    _exclude(pathExpressions, path, excludes);
052    
053                    String[] includes = JSONIncludesManagerUtil.lookupIncludes(type);
054    
055                    _include(pathExpressions, path, includes);
056    
057                    super.transform(object);
058            }
059    
060            private void _exclude(
061                    List<PathExpression> pathExpressions, String path, String... names) {
062    
063                    for (String name : names) {
064                            PathExpression pathExpression = new PathExpression(
065                                    path.concat(name), false);
066    
067                            if (!pathExpressions.contains(pathExpression)) {
068                                    pathExpressions.add(pathExpression);
069                            }
070                    }
071            }
072    
073            private String _getPath() {
074                    JSONContext jsonContext = getContext();
075    
076                    Path path = jsonContext.getPath();
077    
078                    List<String> paths = path.getPath();
079    
080                    if (paths.isEmpty()) {
081                            return StringPool.BLANK;
082                    }
083    
084                    StringBundler sb = new StringBundler(paths.size() * 2);
085    
086                    for (String curPath : paths) {
087                            sb.append(curPath);
088                            sb.append(CharPool.PERIOD);
089                    }
090    
091                    return sb.toString();
092            }
093    
094            private void _include(
095                    List<PathExpression> pathExpressions, String path, String... names) {
096    
097                    for (String name : names) {
098                            PathExpression pathExpression = new PathExpression(
099                                    path.concat(name), true);
100    
101                            if (!pathExpressions.contains(pathExpression)) {
102                                    pathExpressions.add(0, pathExpression);
103                            }
104                    }
105            }
106    
107    }