View Javadoc
1   /**
2    * This file Copyright (c) 2011-2018 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.vaadin.gwt.client.shared.magnoliashell;
35  
36  import java.io.Serializable;
37  
38  /**
39   * Helper class for holding the parsed info from the fragment.
40   */
41  public class Fragment implements Serializable {
42  
43      private ViewportType appViewportType = ViewportType.SHELL_APP;
44  
45      private String appName = "";
46  
47      private String subAppId = "";
48  
49      private String parameter = "";
50  
51      public Fragment() {
52      }
53  
54      public static Fragment fromString(final String fragment) {
55          Fragmenti/vaadin/gwt/client/shared/magnoliashell/Fragment.html#Fragment">Fragment dto = new Fragment();
56          String type = extractAppType(fragment);
57          if (type.equals("shell")) {
58              dto.appViewportType = ViewportType.SHELL_APP;
59              dto.appName = ShellAppType.getTypeByFragmentId(extractAppName(fragment));
60              dto.subAppId = extractSubAppId(fragment);
61              dto.parameter = extractSubAppId(fragment);
62          } else if (type.equals("app")) {
63              dto.appViewportType = ViewportType.APP;
64              dto.appName = extractAppName(fragment);
65              dto.subAppId = extractSubAppId(fragment);
66              dto.parameter = extractParameter(fragment);
67          }
68          return dto;
69      }
70  
71      public ShellAppType resolveShellAppType() {
72          if (isApp()) {
73              return null;
74          }
75          try {
76              return ShellAppType.valueOf(appName.toUpperCase());
77          } catch (IllegalArgumentException e) {
78              return ShellAppType.APPLAUNCHER;
79          }
80      }
81  
82      public void setParameter(String parameter) {
83          this.parameter = parameter;
84      }
85  
86      public void setSubAppId(String subAppId) {
87          this.subAppId = subAppId;
88      }
89  
90      public void setAppName(String appName) {
91          this.appName = appName;
92      }
93  
94      public void setAppViewportType(ViewportType type) {
95          this.appViewportType = type;
96      }
97  
98      public ViewportType getAppViewportType() {
99          return appViewportType;
100     }
101 
102     public String getAppName() {
103         return appName;
104     }
105 
106     public String getSubAppId() {
107         return subAppId;
108     }
109 
110     public String getParameter() {
111         return parameter;
112     }
113 
114     // These methods are duplicated from info.magnolia.ui.framework.location.DefaultLocation
115 
116     public String toFragment() {
117         return toString();
118     }
119 
120     public boolean isSameApp(Fragment other) {
121         return other != null && appViewportType == other.appViewportType && appName.equals(other.appName);
122     }
123 
124     @Override
125     public String toString() {
126         return appViewportType.getFragmentPrefix() + appName + ":" + subAppId + ";" + parameter;
127     }
128 
129     public static String extractAppType(String fragment) {
130         int i = fragment.indexOf(':');
131         String result = i != -1 ? fragment.substring(0, i) : fragment;
132         return result == null || result.isEmpty() ? "shell" : result;
133     }
134 
135     public static String extractAppName(String fragment) {
136         fragment = removeParameter(fragment);
137         int i = fragment.indexOf(':');
138         if (i == -1) {
139             return "";
140         }
141         int j = fragment.indexOf(':', i + 1);
142         return j != -1 ? fragment.substring(i + 1, j) : fragment.substring(i + 1);
143     }
144 
145     public static String extractSubAppId(String fragment) {
146         fragment = removeParameter(fragment);
147 
148         int i = fragment.indexOf(':');
149         if (i == -1) {
150             return "";
151         }
152         int j = fragment.indexOf(':', i + 1);
153         if (j == -1) {
154             return "";
155         }
156         return fragment.substring(j + 1);
157     }
158 
159     public static String extractParameter(String fragment) {
160         int i = fragment.indexOf(';');
161         if (i == -1) {
162             return "";
163         }
164         return fragment.substring(i + 1);
165     }
166 
167     private static String removeParameter(String fragment) {
168         int i = fragment.indexOf(';');
169         return i != -1 ? fragment.substring(0, i) : fragment;
170     }
171 
172     public boolean isShellApp() {
173         return appViewportType == ViewportType.SHELL_APP;
174     }
175 
176     public boolean isApp() {
177         return !isShellApp();
178     }
179 
180     public boolean sameSubApp(Fragment other) {
181         return isSameApp(other) && subAppId.equals(other.subAppId);
182     }
183 
184     @Override
185     public boolean equals(Object o) {
186         if (this == o) return true;
187         if (!(o instanceof Fragment)) return false;
188 
189         Fragment./../../../../../../info/magnolia/ui/vaadin/gwt/client/shared/magnoliashell/Fragment.html#Fragment">Fragment fragment = (Fragment) o;
190 
191         if (!appName.equals(fragment.appName)) return false;
192         if (appViewportType != fragment.appViewportType) return false;
193         if (!parameter.equals(fragment.parameter)) return false;
194         if (!subAppId.equals(fragment.subAppId)) return false;
195 
196         return true;
197     }
198 
199     @Override
200     public int hashCode() {
201         int result = appViewportType.hashCode();
202         result = 31 * result + appName.hashCode();
203         result = 31 * result + subAppId.hashCode();
204         result = 31 * result + parameter.hashCode();
205         return result;
206     }
207 }