View Javadoc
1   /**
2    * This file Copyright (c) 2016 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.ui.framework.ioc;
35  
36  import java.lang.annotation.Annotation;
37  import java.util.List;
38  import java.util.Objects;
39  
40  import org.apache.commons.lang3.StringUtils;
41  
42  /**
43   * Utility class which is capable of converting string identifiers
44   * into {@link com.google.inject.BindingAnnotation
45   * Magnolia UI binding annotations}.
46   *
47   * Useful when preparing the Guice bindings or looking up annotated types.
48   *
49   * @see UiBaseModule
50   * @see UiComponentConfigurationAggregator
51   * @see UiContextReference
52   */
53  public class UiAnnotations {
54  
55      private UiAnnotations() {
56      }
57  
58      public static UiContextAnnotation forAdmincentral() {
59          return new AdmincentralImpl();
60      }
61  
62      public static UiContextAnnotation forSubApp(String appName, String subAppName) {
63          return new SubAppImpl(appName, subAppName);
64      }
65  
66      public static UiContextAnnotation forSubApps() {
67          return new SubAppImpl("", "");
68      }
69  
70      public static UiContextAnnotation forApp(String appName) {
71          return new AppImpl(appName);
72      }
73  
74      public static UiContextAnnotation forApps() {
75          return new AppImpl("");
76      }
77  
78      public static UiContextAnnotation forView(String viewName) {
79          return new ViewImpl(viewName);
80      }
81  
82      public static UiContextAnnotation forViews() {
83          return new ViewImpl();
84      }
85  
86      static UiContextAnnotation cast(Annotation annotation) {
87          if (annotation instanceof UiContextAnnotation) {
88              return (UiContextAnnotation) annotation;
89          }
90  
91          throw new IllegalArgumentException("Provided annotation [" + annotation + "] is not an instance of UI context annotation");
92      }
93  
94      static Annotation parseAnnotation(String id, List<String> appNames) {
95          switch (id) {
96          case "admincentral":
97              return new AdmincentralImpl();
98          case "app":
99              return new AppImpl();
100         case "subapp":
101             return new SubAppImpl();
102         case "choosedialog":
103             return new ViewImpl("choosedialog");
104         case "mediaeditor":
105             return new ViewImpl("mediaeditor");
106         default:
107             final String appWithSubApp = StringUtils.removeStart(id, "app-");
108             return appNames.stream().filter(appWithSubApp::startsWith).findFirst().map(appName -> {
109                 if (Objects.equals(appName, appWithSubApp)) {
110                     return UiAnnotations.forApp(appName);
111                 }
112 
113                 final String subAppName = StringUtils.removeStart(appWithSubApp, appName + "-");
114                 return UiAnnotations.forSubApp(appName, subAppName);
115             }).orElseThrow(IllegalArgumentException::new);
116         }
117 
118     }
119 
120 }