private Item getItem(Locale locale) { for (Item item : items) { if (item.locale.equals(locale)) return item; } Item item = new Item(locale); items.add(item); return item; }
private void compileSpecific() { Node<BasicPermissionTarget> root = new Node<>( new BasicPermissionTarget("category:specific", getMessage("permissionConfig.specificRoot"), null)); specific = new Tree<>(root); final String configName = AppContext.getProperty(PERMISSION_CONFIG_XML_PROP); StringTokenizer tokenizer = new StringTokenizer(configName); for (String location : tokenizer.getTokenArray()) { Resource resource = resources.getResource(location); if (resource.exists()) { InputStream stream = null; try { stream = resource.getInputStream(); String xml = IOUtils.toString(stream, StandardCharsets.UTF_8); compileSpecific(xml, root); } catch (IOException e) { throw new RuntimeException("Unable to read permission config", e); } finally { IOUtils.closeQuietly(stream); } } else { log.warn("Resource {} not found, ignore it", location); } } }
private void walkSpecific(Element element, Node<BasicPermissionTarget> node) { for (Element elem : (List<Element>) element.elements()) { String id = elem.attributeValue("id"); String caption = getMessage("permission-config." + id); if ("category".equals(elem.getName())) { Node<BasicPermissionTarget> existingCategory = null; String categoryPermissionId = "category:" + id; for (Node<BasicPermissionTarget> subNode : node.getChildren()) { if (categoryPermissionId.equals(subNode.getData().getId())) { existingCategory = subNode; break; } } Node<BasicPermissionTarget> categoryNode; if (existingCategory == null) { categoryNode = new Node<>(new BasicPermissionTarget(categoryPermissionId, caption, null)); node.addChild(categoryNode); } else { categoryNode = existingCategory; } walkSpecific(elem, categoryNode); } else if ("permission".equals(elem.getName())) { Node<BasicPermissionTarget> n = new Node<>( new BasicPermissionTarget("permission:" + id, caption, id)); node.addChild(n); } } } }
private void walkMenu(MenuItem info, Node<BasicPermissionTarget> node) { String id = info.getId(); String caption = menuConfig.getItemCaption(id) .replaceAll("<.+?>", "") .replaceAll(">", ""); caption = StringEscapeUtils.unescapeHtml4(caption); if (info.getChildren() != null && !info.getChildren().isEmpty()) { Node<BasicPermissionTarget> n = new Node<>(new BasicPermissionTarget("category:" + id, caption, id)); node.addChild(n); for (MenuItem item : info.getChildren()) { walkMenu(item, n); } } else { if (!info.isSeparator()) { Node<BasicPermissionTarget> n = new Node<>(new BasicPermissionTarget("item:" + id, caption, id)); node.addChild(n); } } }
private void compileSpecific(String xml, Node<BasicPermissionTarget> root) { Document doc = Dom4j.readDocument(xml); Element rootElem = doc.getRootElement(); for (Element element : Dom4j.elements(rootElem, "include")) { String fileName = element.attributeValue("file"); if (!StringUtils.isBlank(fileName)) { String incXml = resources.getResourceAsString(fileName); if (incXml == null) throw new RuntimeException("Config file not found: " + fileName); compileSpecific(incXml, root); } } Element specElem = rootElem.element("specific"); if (specElem == null) return; walkSpecific(specElem, root); }
private void compileScreens() { Node<BasicPermissionTarget> menuRoot = new Node<>( new BasicPermissionTarget("root:menu", getMessage("permissionConfig.mainMenu"), null) ); walkMenu(menuRoot); Node<BasicPermissionTarget> othersRoot = new Node<>( new BasicPermissionTarget("root:others", getMessage("permissionConfig.otherScreens"), null) ); walkOtherScreens(othersRoot, menuRoot); screens = new Tree<>(Arrays.asList(menuRoot, othersRoot)); }
private Item(Locale locale) { this.locale = locale; compileScreens(); compileEntitiesAndAttributes(); compileSpecific(); }
private void walkMenu(Node<BasicPermissionTarget> node) { for (MenuItem info : menuConfig.getRootItems()) { walkMenu(info, node); } }