@XContent protected void setGuards(DocumentFragment content) { Node node = content.getFirstChild(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { String name = node.getNodeName(); if ("guard".equals(name)) { NamedNodeMap map = node.getAttributes(); Node aId = map.getNamedItem("id"); Node aType = map.getNamedItem("type"); if (aId == null) { throw new IllegalArgumentException("id is required"); } String id = aId.getNodeValue(); if (aType == null) { throw new IllegalArgumentException("type is required"); } else { // String value = node.getTextContent().trim(); // guards.put(id, new ScriptGuard(value)); // TODO: compound guard } String type = aType.getNodeValue(); if ("permission".equals(type)) { String value = node.getTextContent().trim(); guards.put(id, new PermissionGuard(value)); } else if ("isAdministrator".equals(type)) { String value = node.getTextContent().trim(); guards.put(id, new IsAdministratorGuard(value)); } else if ("facet".equals(type)) { String value = node.getTextContent().trim(); guards.put(id, new FacetGuard(value)); } else if ("type".equals(type)) { String value = node.getTextContent().trim(); guards.put(id, new TypeGuard(value)); } else if ("schema".equals(type)) { String value = node.getTextContent().trim(); guards.put(id, new SchemaGuard(value)); } else if ("user".equals(type)) { String value = node.getTextContent().trim(); guards.put(id, new UserGuard(value)); } else if ("group".equals(type)) { String value = node.getTextContent().trim(); guards.put(id, new GroupGuard(value)); } else if ("script".equals(type)) { Node engineNode = map.getNamedItem("engine"); if (engineNode == null) { throw new IllegalArgumentException( "Must specify an engine attribute on script guards"); } String value = node.getTextContent().trim(); guards.put(id, new ScriptGuard(engineNode.getNodeValue(), value)); } else if ("expression".equals(type)) { String value = node.getTextContent().trim(); try { guards.put(id, PermissionService.getInstance().parse(value, guards)); } catch (ParseException e) { log.error(e, e); } } else { // the type should be a guard factory String value = node.getTextContent().trim(); try { Class<?> factory = Class.forName(type); Guard guard = ((GuardFactory) factory.newInstance()).newGuard(value); guards.put(id, guard); } catch (Exception e) { log.error(e, e); // TODO should throw a DeployException } } } } node = node.getNextSibling(); } }
public Guard getGuard() throws ParseException { if (expression == null || expression.length() == 0) { return new And(guards.values()); } return PermissionService.getInstance().parse(expression, guards); }