Skip to content

Commit 3804f00

Browse files
committed
Refactoring static_menu.xml
1 parent b4b207d commit 3804f00

10 files changed

Lines changed: 841 additions & 190 deletions

File tree

knowage-core/src/main/java/it/eng/spagobi/commons/serializer/v3/MenuListJSONSerializerForREST.java

Lines changed: 150 additions & 188 deletions
Large diffs are not rendered by default.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package it.eng.spagobi.commons.serializer.v3;
2+
3+
import java.io.InputStream;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import javax.xml.parsers.DocumentBuilderFactory;
8+
9+
import org.w3c.dom.Document;
10+
import org.w3c.dom.Element;
11+
import org.w3c.dom.Node;
12+
13+
import it.eng.spagobi.commons.serializer.v3.dto.AllowedUserFunctionalities;
14+
import it.eng.spagobi.commons.serializer.v3.dto.CommonUserFunctionalities;
15+
import it.eng.spagobi.commons.serializer.v3.dto.GroupItem;
16+
import it.eng.spagobi.commons.serializer.v3.dto.ItemMenu;
17+
import it.eng.spagobi.commons.serializer.v3.dto.StaticMenu;
18+
import it.eng.spagobi.commons.serializer.v3.dto.TechnicalUserFunctionalities;
19+
20+
public final class StaticMenuRegistry {
21+
22+
private static StaticMenuRegistry INSTANCE;
23+
24+
private static final String CONFIG_FILE = "conf/static_menu.xml";
25+
26+
private final StaticMenu cache;
27+
28+
private StaticMenuRegistry() {
29+
try {
30+
this.cache = parse();
31+
} catch (Exception e) {
32+
throw new ExceptionInInitializerError("Errore caricando conf/static_menu.xml: " + e.getMessage());
33+
}
34+
}
35+
36+
public static synchronized StaticMenuRegistry getInstance() {
37+
if (INSTANCE == null) {
38+
INSTANCE = new StaticMenuRegistry();
39+
}
40+
return INSTANCE;
41+
}
42+
43+
44+
public StaticMenu getMenu() {
45+
return cache;
46+
}
47+
48+
// ======== PARSER ========
49+
50+
private StaticMenu parse() {
51+
try (InputStream input = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)) {
52+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
53+
// Hardening XXE
54+
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
55+
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
56+
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
57+
dbf.setXIncludeAware(false);
58+
dbf.setExpandEntityReferences(false);
59+
dbf.setNamespaceAware(false);
60+
61+
Document doc = dbf.newDocumentBuilder().parse(input);
62+
Element root = doc.getDocumentElement();
63+
64+
// TECHNICAL_USER_FUNCTIONALITIES
65+
TechnicalUserFunctionalities technical = null;
66+
Element techEl = firstChild(root, "TECHNICAL_USER_FUNCTIONALITIES");
67+
if (techEl != null) {
68+
List<GroupItem> groups = new ArrayList<>();
69+
for (Element gEl : directChildren(techEl, "GROUP_ITEM")) {
70+
String gid = attr(gEl, "id");
71+
String glabel = attr(gEl, "label");
72+
String gicon = attr(gEl, "iconCls");
73+
String gto = attr(gEl, "to");
74+
String glicensed = attr(gEl, "toBeLicensed");
75+
76+
List<ItemMenu> items = new ArrayList<>();
77+
for (Element itEl : directChildren(gEl, "ITEM")) {
78+
items.add(parseItem(itEl));
79+
}
80+
groups.add(new GroupItem(gid, glabel, gicon, gto, glicensed, items));
81+
}
82+
technical = new TechnicalUserFunctionalities(groups);
83+
}
84+
85+
// COMMON_USER_FUNCTIONALITIES
86+
CommonUserFunctionalities common = null;
87+
Element comEl = firstChild(root, "COMMON_USER_FUNCTIONALITIES");
88+
if (comEl != null) {
89+
List<ItemMenu> items = new ArrayList<>();
90+
for (Element itEl : directChildren(comEl, "ITEM")) {
91+
items.add(parseItem(itEl));
92+
}
93+
common = new CommonUserFunctionalities(items);
94+
}
95+
96+
// ALLOWED_USER_FUNCTIONALITIES
97+
AllowedUserFunctionalities allowed = null;
98+
Element allEl = firstChild(root, "ALLOWED_USER_FUNCTIONALITIES");
99+
if (allEl != null) {
100+
String containerId = attr(allEl, "id");
101+
List<ItemMenu> items = new ArrayList<>();
102+
for (Element itEl : directChildren(allEl, "ITEM")) {
103+
items.add(parseItem(itEl));
104+
}
105+
allowed = new AllowedUserFunctionalities(containerId, items);
106+
}
107+
108+
return new StaticMenu(technical, common, allowed);
109+
} catch (Exception e) {
110+
throw new IllegalStateException("Errore nel parsing del menu statico: " + e.getMessage(), e);
111+
}
112+
}
113+
114+
private ItemMenu parseItem(Element itEl) {
115+
return new ItemMenu(attr(itEl, "id"), attr(itEl, "label"), attr(itEl, "requiredFunctionality"), attr(itEl, "to"), attr(itEl, "command"),
116+
attr(itEl, "iconCls"), attr(itEl, "condition"), attr(itEl, "conditionedView"), attr(itEl, "toBeAuthorized"), attr(itEl, "toBeLicensed"));
117+
}
118+
119+
// ======== HELPERS XML & IO ========
120+
121+
private static String attr(Element e, String name) {
122+
return e.hasAttribute(name) ? emptyToNull(e.getAttribute(name)) : null;
123+
}
124+
125+
private static String emptyToNull(String s) {
126+
return (s == null || s.isBlank()) ? null : s;
127+
}
128+
129+
130+
private static Element firstChild(Element parent, String tag) {
131+
for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
132+
if (n.getNodeType() == Node.ELEMENT_NODE && tag.equals(((Element) n).getTagName())) {
133+
return (Element) n;
134+
}
135+
}
136+
return null;
137+
}
138+
139+
private static List<Element> directChildren(Element parent, String tag) {
140+
List<Element> out = new ArrayList<>();
141+
for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
142+
if (n.getNodeType() == Node.ELEMENT_NODE) {
143+
Element el = (Element) n;
144+
if (tag.equals(el.getTagName())) {
145+
out.add(el);
146+
}
147+
}
148+
}
149+
return out;
150+
}
151+
152+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package it.eng.spagobi.commons.serializer.v3.dto;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public final class AllowedUserFunctionalities {
8+
private final String id;
9+
private final List<ItemMenu> items;
10+
11+
public AllowedUserFunctionalities(String id, List<ItemMenu> items) {
12+
this.id = id;
13+
this.items = Collections.unmodifiableList(new ArrayList<>(items));
14+
}
15+
16+
public String getId() {
17+
return id;
18+
}
19+
20+
public List<ItemMenu> getItems() {
21+
return items;
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package it.eng.spagobi.commons.serializer.v3.dto;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public final class CommonUserFunctionalities {
8+
private final List<ItemMenu> items;
9+
10+
public CommonUserFunctionalities(List<ItemMenu> items) {
11+
this.items = Collections.unmodifiableList(new ArrayList<>(items));
12+
}
13+
14+
public List<ItemMenu> getItems() {
15+
return items;
16+
}
17+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package it.eng.spagobi.commons.serializer.v3.dto;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public final class GroupItem {
8+
private final String id;
9+
private final String label;
10+
private final String iconCls;
11+
private final String to;
12+
private final String toBeLicensed;
13+
private final List<ItemMenu> items;
14+
15+
public GroupItem(String id, String label, String iconCls, String to, String toBeLicensed, List<ItemMenu> items) {
16+
this.id = id;
17+
this.label = label;
18+
this.iconCls = iconCls;
19+
this.to = to;
20+
this.toBeLicensed = toBeLicensed;
21+
this.items = Collections.unmodifiableList(new ArrayList<>(items));
22+
}
23+
24+
public String getId() {
25+
return id;
26+
}
27+
28+
public String getLabel() {
29+
return label;
30+
}
31+
32+
public String getIconCls() {
33+
return iconCls;
34+
}
35+
36+
public String getTo() {
37+
return to;
38+
}
39+
40+
public String getToBeLicensed() {
41+
return toBeLicensed;
42+
}
43+
44+
public List<ItemMenu> getItems() {
45+
return items;
46+
}
47+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package it.eng.spagobi.commons.serializer.v3.dto;
2+
3+
public final class ItemMenu {
4+
private final String id;
5+
private final String label;
6+
private final String requiredFunctionality;
7+
private final String to;
8+
private final String command;
9+
private final String iconCls;
10+
private final String condition;
11+
private final String conditionedView;
12+
private final String toBeAuthorized;
13+
private final String toBeLicensed;
14+
15+
public ItemMenu(String id, String label, String requiredFunctionality, String to, String command, String iconCls, String condition, String conditionedView,
16+
String toBeAuthorized, String toBeLicensed) {
17+
this.id = id;
18+
this.label = label;
19+
this.requiredFunctionality = requiredFunctionality;
20+
this.to = to;
21+
this.command = command;
22+
this.iconCls = iconCls;
23+
this.condition = condition;
24+
this.conditionedView = conditionedView;
25+
this.toBeAuthorized = toBeAuthorized;
26+
this.toBeLicensed = toBeLicensed;
27+
}
28+
29+
public String getId() {
30+
return id;
31+
}
32+
33+
public String getLabel() {
34+
return label;
35+
}
36+
37+
public String getRequiredFunctionality() {
38+
return requiredFunctionality;
39+
}
40+
41+
public String getTo() {
42+
return to;
43+
}
44+
45+
public String getCommand() {
46+
return command;
47+
}
48+
49+
public String getIconCls() {
50+
return iconCls;
51+
}
52+
53+
public String getCondition() {
54+
return condition;
55+
}
56+
57+
public String getConditionedView() {
58+
return conditionedView;
59+
}
60+
61+
public String getToBeAuthorized() {
62+
return toBeAuthorized;
63+
}
64+
65+
public String getToBeLicensed() {
66+
return toBeLicensed;
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return "Item [id=" + id + ", label=" + label + ", requiredFunctionality=" + requiredFunctionality + ", to=" + to + ", command=" + command + ", iconCls="
72+
+ iconCls + ", condition=" + condition + ", conditionedView=" + conditionedView + ", toBeAuthorized=" + toBeAuthorized + ", toBeLicensed="
73+
+ toBeLicensed + "]";
74+
}
75+
76+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
package it.eng.spagobi.commons.serializer.v3.dto;
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public final class StaticMenu {
8+
private final TechnicalUserFunctionalities technical;
9+
private final CommonUserFunctionalities common;
10+
private final AllowedUserFunctionalities allowed;
11+
12+
public StaticMenu(TechnicalUserFunctionalities technical, CommonUserFunctionalities common, AllowedUserFunctionalities allowed) {
13+
this.technical = technical;
14+
this.common = common;
15+
this.allowed = allowed;
16+
17+
Map<String, ItemMenu> itemsIdx = new HashMap<>();
18+
Map<String, GroupItem> groupsIdx = new HashMap<>();
19+
if (technical != null) {
20+
for (GroupItem g : technical.getGroups()) {
21+
if (g.getId() != null) {
22+
groupsIdx.put(g.getId(), g);
23+
}
24+
for (ItemMenu it : g.getItems()) {
25+
if (it.getId() != null) {
26+
itemsIdx.put(it.getId(), it);
27+
}
28+
}
29+
}
30+
}
31+
if (common != null) {
32+
for (ItemMenu it : common.getItems()) {
33+
if (it.getId() != null) {
34+
itemsIdx.put(it.getId(), it);
35+
}
36+
}
37+
}
38+
if (allowed != null) {
39+
for (ItemMenu it : allowed.getItems()) {
40+
if (it.getId() != null) {
41+
itemsIdx.put(it.getId(), it);
42+
}
43+
}
44+
}
45+
46+
}
47+
48+
public TechnicalUserFunctionalities getTechnical() {
49+
return technical;
50+
}
51+
52+
public CommonUserFunctionalities getCommon() {
53+
return common;
54+
}
55+
56+
public AllowedUserFunctionalities getAllowed() {
57+
return allowed;
58+
}
59+
}

0 commit comments

Comments
 (0)