BrightSide Workbench Full Report + Source Code
ElContextMap.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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;
20 
21 import java.util.Collections;
22 import java.util.SortedSet;
23 import java.util.TreeMap;
24 import org.turro.string.Strings;
25 import org.turro.elephant.context.Application;
26 import org.turro.elephant.context.ElephantApplication;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.elephant.context.IContext;
29 import org.turro.elephant.impl.util.Files;
30 
35 public class ElContextMap extends TreeMap<String, ElContext> {
36 
37  public static ElContext getCurrent() {
38  ElContext elc = null;
40  if(app instanceof ElephantApplication) {
41  IConstructor constructor = app.getConstructor();
42  if(constructor != null) {
43  elc = constructor.getRenderingContext();
44  }
45  }
46  return elc == null ? getContext("/") : elc;
47  }
48 
49  public static ElContext getContext(IConstructor constructor) {
50  return getContext(locateRealContext(constructor));
51  }
52 
53  public static ElContext getContext(IContext context) {
54  return context == null ? null : getInstance().getOrDefault(context.getPath(), null);
55  }
56 
57  public static ElContext getContext(String path) {
58  if(Strings.isBlank(path) || "/".equals(path)) path = "";
59  return getInstance().getOrDefault(path, null);
60  }
61 
62  public static ElContext getContextFromWebTag(String webTag) {
63  return getInstance().values().stream()
64  .filter(context -> webTag.equals(context.getWebContext().getWebTag()))
65  .findFirst().orElse(null);
66  }
67 
68  public static SortedSet<ElContext> getContexts(String from, int level) {
69  String[] levels = from.split("\\/");
70  String tmp = "";
71  if(levels.length >= level) {
72  for(int i = 0; i < level; i++)
73  tmp += "/" + levels[i];
74  tmp = tmp.replaceAll("\\/\\/", "\\/");
75  ElContext ctx = getContext(tmp);
76  if(ctx != null) return ctx.getChildren();
77  }
78  return Collections.emptySortedSet();
79  }
80 
81  public static ElContext addContext(ElContext context) {
82  return getInstance().put(context.path, context);
83  }
84 
85  public static void init() {
86  if(_map.isEmpty()) {
87  _root = new ElContext();
88  }
89  }
90 
91  public static void reset() {
92  _map.clear();
93  _root = new ElContext();
95  }
96 
97  public static ElContext getRoot() {
98  return _root;
99  }
100 
101  public static ElContextMap getInstance() {
102  return _map;
103  }
104 
105  private static final ElContextMap _map = new ElContextMap();
106  private static ElContext _root;
107 
108  private ElContextMap() {}
109 
110  /* Utils */
111 
112  public static String locateRealContext(IConstructor constructor) {
113  String path = null;
114  if(constructor.getRequest() == null) {
115  path = "/";
116  } else {
117  path = extractPath(constructor.getRequest().getServletPath());
118  }
119  if(!Files.exists(path)) path = Files.toExisting(path);
120  if(path.endsWith("/")) path = path.substring(0, path.length() - 1);
121  return path;
122  }
123 
124  public static String extractPath(String path) {
125  if(path == null) {
126  path = "/";
127  } else {
128  int p = path.indexOf("?");
129  if(p > -1) {
130  path = path.substring(0, p);
131  }
132  }
133  return path;
134  }
135 
136 }
static boolean exists(String path)
Definition: Files.java:80
static String toExisting(String path)
Definition: Files.java:85
static ElContext getContextFromWebTag(String webTag)
static ElContext getContext(IContext context)
static ElContext getContext(IConstructor constructor)
static String locateRealContext(IConstructor constructor)
static SortedSet< ElContext > getContexts(String from, int level)
static ElContext getContext(String path)
static String extractPath(String path)
static ElContext addContext(ElContext context)
TreeSet< ElContext > getChildren()
Definition: ElContext.java:196