BrightSide Workbench Full Report + Source Code
WebActionType.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.web.actions;
20 
21 import org.turro.string.Strings;
22 
27 public enum WebActionType {
28 
29  NAVIGATE("nav"),
30  URL("url"),
31  MAIL("mailto"),
32  TEL("tel"),
33  ACTION("action"),
34  TAG("tag"),
35  WHATS("whats"),
36  TGRAM("tgram"),
37  WRONG("#");
38 
39  private final String protocol;
40 
41  private WebActionType(String protocol) {
42  this.protocol = protocol;
43  }
44 
45  public String getProtocol() {
46  return protocol;
47  }
48 
49  public static WebActionType typeOf(String action) {
50  if(!Strings.isBlank(action)) {
51  for(WebActionType type : WebActionType.values()) {
52  if(action.startsWith(type.getProtocol() + ":")) {
53  return type;
54  }
55  }
56  }
57  return WRONG;
58  }
59 
60  public static String actionOf(String action) {
61  WebActionType type = typeOf(action);
62  return type == WRONG ? null : removeImage(action).substring(type.getProtocol().length() + 1);
63  }
64 
65  public static String imageOf(String action) {
66  if(action.matches(".+\\{[a-z \\-\\_\\.]+\\}")) {
67  return action.substring(action.lastIndexOf("{") + 1, action.length() - 1);
68  }
69  return null;
70  }
71 
72  private static String removeImage(String action) {
73  if(action.matches(".+\\{[a-z \\-\\_\\.]+\\}")) {
74  return action.substring(0, action.lastIndexOf("{"));
75  }
76  return action;
77  }
78 
79 }
static WebActionType typeOf(String action)