1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
42
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 }