View Javadoc

1   /**
2    * This file Copyright (c) 2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.i18nsystem.tools;
35  
36  import info.magnolia.i18nsystem.I18nable;
37  import info.magnolia.i18nsystem.I18nText;
38  import info.magnolia.i18nsystem.proxytoys.MethodsAnnotatedWith;
39  import info.magnolia.i18nsystem.proxytoys.ProxytoysI18nizer;
40  import info.magnolia.i18nsystem.proxytoys.ReturnsAnnotatedTypeArgument;
41  
42  import java.lang.reflect.InvocationTargetException;
43  import java.lang.reflect.Method;
44  import java.util.Collection;
45  import java.util.Locale;
46  import java.util.Map;
47  import java.util.Set;
48  
49  import net.sf.cglib.proxy.Enhancer;
50  
51  import org.reflections.ReflectionUtils;
52  
53  /**
54   * THIS IS A PROTOTYPE.
55   * Sample usage in Groovy:
56   * <pre>
57   * g = new info.magnolia.i18nsystem.tools.GenerateTranslationFilesImpl()
58   * manClass = info.magnolia.ui.api.app.registry.AppDescriptorRegistry.class
59   * man = info.magnolia.objectfactory.Components.getSingleton(manClass)
60   *
61   * apps = man.getAppDescriptors()
62   * apps.each { app -> res = g.generateFor(app,null,null); res.actions.each { println it.dump() }; println '' }
63   * </pre>
64   *
65   * Tools could also (potentially) do:
66   * * manage bootstrap files (load objects off of them, modify them, ...)
67   * * inspect objects and report duplicate keys, unused keys, ...
68   */
69  public class GenerateTranslationFilesImpl {
70      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(GenerateTranslationFilesImpl.class);
71  
72      /**
73       * @param o
74       * @param jcrPath
75       * @param jcrPathForBootstrapFile if the bootstrap file contains parent objects
76       */
77      public GeneratorResults generateFor(Locale locale, Object o) throws InvocationTargetException, IllegalAccessException {
78          final GeneratorResults genRes = new GeneratorResults(locale);
79  
80          // inspect object
81          final ProxytoysI18nizer i18nizer = new ToolingProxytoysI18nizer(genRes, locale, new NullTranslationService());
82          final Object root = i18nizer.decorate(o);
83          inspect(root);
84  
85          // modify bootstrap file (or reexport)
86          // generate update task (code or config file ?)
87          return genRes;
88      }
89  
90      private void inspect(Collection c) throws InvocationTargetException, IllegalAccessException {
91          for (Object o : c) {
92              inspect(o);
93          }
94      }
95  
96      private void inspect(Object obj) throws InvocationTargetException, IllegalAccessException {
97          Class clazz = obj.getClass();
98          while (Enhancer.isEnhanced(clazz)) {
99              clazz = clazz.getSuperclass();
100         }
101 
102         final Set<Method> textMethods = ReflectionUtils.getMethods(clazz, new MethodsAnnotatedWith(I18nText.class));
103         final Set<Method> childMethods = ReflectionUtils.getMethods(clazz, new ReturnsAnnotatedTypeArgument(I18nable.class));
104         for (Method textMethod : textMethods) {
105             log.info("Visiting textMethod: " + textMethod.getDeclaringClass().getSimpleName() + "." + textMethod.getName());
106             textMethod.invoke(obj);
107         }
108 
109         for (Method childMethod : childMethods) {
110             // Currently we only support "single", collection and map-values. Anything else isn't going through the proxies anyway.
111             // TODO Treat null entries in maps, collections and "simple" getters ?
112             final Class<?> returnType = childMethod.getReturnType();
113             if (Map.class.isAssignableFrom(returnType)) {
114                 log.info("Visiting map values of child method " + childMethod.getDeclaringClass().getSimpleName() + "." + childMethod.getName() + "()");
115                 inspect(((Map) childMethod.invoke(obj)).values());
116             } else if (Collection.class.isAssignableFrom(returnType)) {
117                 log.info("Visiting items of child method " + childMethod.getDeclaringClass().getSimpleName() + "." + childMethod.getName() + "()");
118                 inspect((Collection) childMethod.invoke(obj));
119             } else {
120                 log.info("Visiting child method " + childMethod.getDeclaringClass().getSimpleName() + "." + childMethod.getName() + "()");
121                 final Object child = childMethod.invoke(obj);
122                 if (child == null) {
123                     log.info("# null returned for " + childMethod);
124                 } else {
125                     inspect(child);
126                 }
127             }
128         }
129     }
130 }