001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.json.transformer;
016    
017    import com.liferay.portal.json.JSONIncludesManager;
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 = _jsonIncludesManager.lookupExcludes(type);
050    
051                    _exclude(pathExpressions, path, excludes);
052    
053                    String[] includes = _jsonIncludesManager.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                            pathExpressions.add(new PathExpression(path + name, false));
065                    }
066            }
067    
068            private String _getPath() {
069                    JSONContext jsonContext = getContext();
070    
071                    Path path = jsonContext.getPath();
072    
073                    List<String> paths = path.getPath();
074    
075                    if (paths.isEmpty()) {
076                            return StringPool.BLANK;
077                    }
078    
079                    StringBundler sb = new StringBundler(paths.size() * 2);
080    
081                    for (String curPath : paths) {
082                            sb.append(curPath);
083                            sb.append(CharPool.PERIOD);
084                    }
085    
086                    return sb.toString();
087            }
088    
089            private void _include(
090                    List<PathExpression> pathExpressions, String path, String... names) {
091    
092                    for (String name : names) {
093                            PathExpression pathExpression = new PathExpression(
094                                    path + name, true);
095    
096                            pathExpressions.add(0, pathExpression);
097                    }
098            }
099    
100            private static JSONIncludesManager _jsonIncludesManager =
101                    new JSONIncludesManager();
102    
103    }