View Javadoc

1   /**
2    * This file Copyright (c) 2012 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.location;
35  
36  import org.apache.commons.lang.StringUtils;
37  
38  import java.util.StringTokenizer;
39  
40  /**
41   * Default location implementation.<p>
42   * {@code appType:appId:subAppId;some/parameter}
43   */
44  public class DefaultLocation implements Location {
45  
46      public static final String LOCATION_TYPE_APP = "app";
47      public static final String LOCATION_TYPE_SHELL_APP = "shell";
48  
49      private String appType;
50      private String appId;
51      private String subAppId;
52  
53      private String parameter;
54  
55      public DefaultLocation(String appType, String appId) {
56          this.appType = appType;
57          this.appId = appId;
58      }
59  
60      public DefaultLocation(String appType, String appId, String subAppId) {
61          this.appType = appType;
62          this.appId = appId;
63          this.subAppId = subAppId;
64      }
65  
66      public DefaultLocation(String appType, String appId, String subAppId, String parameter) {
67          this.appType = appType;
68          this.appId = appId;
69          this.subAppId = subAppId;
70          this.parameter = parameter;
71      }
72  
73      public DefaultLocation(String fragment) {
74          parseLocation(fragment);
75      }
76  
77      private void parseLocation(String fragment) {
78          String[] split = StringUtils.split(";");
79          setAppParams(split[0]);
80          this.parameter = split[1];
81      }
82  
83      private void setAppParams(String appParams) {
84          StringTokenizer tokenizer = new StringTokenizer(appParams, ":");
85          this.appType = (tokenizer.hasMoreTokens()) ? tokenizer.nextToken() : "";
86          this.appId = (tokenizer.hasMoreTokens()) ? tokenizer.nextToken() : "";
87          this.subAppId = (tokenizer.hasMoreTokens()) ? tokenizer.nextToken() : "";
88      }
89  
90      public String getAppType() {
91          return appType;
92      }
93  
94      public String getAppId() {
95          return appId;
96      }
97  
98      public String getSubAppId() {
99          return subAppId;
100     }
101 
102     public String getParameter() {
103         return parameter;
104     }
105 
106     public void setParameter(String parameter) {
107         this.parameter = parameter;
108     }
109 
110     @Override
111     public boolean equals(Object o) {
112         if (this == o) {
113             return true;
114         }
115 
116         DefaultLocation that = (DefaultLocation) o;
117 
118         if (appType != null ? !appType.equals(that.appType) : that.appType != null) {
119             return false;
120         }
121         if (appId != null ? !appId.equals(that.appId) : that.appId != null) {
122             return false;
123         }
124         if (subAppId != null ? !subAppId.equals(that.subAppId) : that.subAppId != null) {
125             return false;
126         }
127         if (parameter != null ? !parameter.equals(that.parameter) : that.parameter != null) {
128             return false;
129         }
130 
131         return true;
132     }
133 
134     @Override
135     public int hashCode() {
136         int result = appType != null ? appType.hashCode() : 0;
137         result = 31 * result + (appId != null ? appId.hashCode() : 0);
138         result = 31 * result + (subAppId != null ? subAppId.hashCode() : 0);
139         result = 31 * result + (parameter != null ? parameter.hashCode() : 0);
140         return result;
141     }
142 
143     @Override
144     public String toString() {
145         StringBuilder sb = new StringBuilder();
146         if (appType != null && appType.length() != 0) {
147             sb.append(appType);
148             if (appId != null && appId.length() != 0) {
149                 sb.append(":").append(appId);
150             }
151             if (subAppId != null && subAppId.length() != 0) {
152                 sb.append(":").append(subAppId);
153             }
154             if (parameter != null && parameter.length() != 0) {
155                 sb.append(";").append(parameter);
156             }
157         }
158         return sb.toString();
159     }
160 
161     public static String extractAppType(String fragment) {
162         int i = fragment.indexOf(':');
163         return i != -1 ? fragment.substring(0, i) : fragment;
164     }
165 
166     public static String extractAppId(String fragment) {
167         fragment = removeParameter(fragment);
168         int i = fragment.indexOf(':');
169         if (i == -1) {
170             return "";
171         }
172         int j = fragment.indexOf(':', i + 1);
173         return j != -1 ? fragment.substring(i + 1, j) : fragment.substring(i + 1);
174     }
175 
176     public static String extractSubAppId(String fragment) {
177         fragment = removeParameter(fragment);
178 
179         int i = fragment.indexOf(':');
180         if (i == -1) {
181             return "";
182         }
183         int j = fragment.indexOf(':', i + 1);
184         if (j == -1) {
185             return "";
186         }
187         return fragment.substring(j + 1);
188     }
189 
190     public static String extractParameter(String fragment) {
191         int i = fragment.indexOf(';');
192         if (i == -1) {
193             return "";
194         }
195          return fragment.substring(i + 1);
196     }
197 
198     private static String removeParameter(String fragment) {
199         int i = fragment.indexOf(';');
200         return i != -1 ? fragment.substring(0, i) : fragment;
201     }
202 
203 }