private static Map<String, XmlNode> nodeListToMap( List<XmlNode> subNodes, String keyPropertyName) { Map<String, XmlNode> nodeMap = new HashMap<String, XmlNode>(); for (XmlNode node : subNodes) { String value = node.getAttribute(keyPropertyName); nodeMap.put(value, node); } return nodeMap; }
protected void initParam(XmlNode xmlNode) { NameFilter<XmlNode> nameFilter = new NameFilter<XmlNode>(xmlNode); List<XmlNode> initParamNodes = nameFilter.findNodeList(INIT_PARAM); for (XmlNode initParamNode : initParamNodes) { String name = initParamNode.getAttribute("name"); String value = initParamNode.getAttribute("value"); initParamMap.put(name, value); logger.logMessage( LogLevel.DEBUG, "<{}>的初始化参数name='{}',value='{}'", this.getClass().getName(), name, value); } }
public NameFilterTest() { node = new XmlNode("root"); for (int i = 0; i < 10; i++) { XmlNode a = node.addNode(new XmlNode("a" + i)); for (int j = 0; j < 10; j++) { XmlNode b = a.addNode(new XmlNode("b" + j)); for (int k = 0; k < 10; k++) { b.addNode(new XmlNode("c" + k)); } } } }
private static void checkNodeName(XmlNode applicationNode, XmlNode componentNode) { if (applicationNode == null || componentNode == null) { // 如果有一个为空,则返回 return; } String applicationNodeName = applicationNode.getNodeName(); String componentNodeName = componentNode.getNodeName(); if (applicationNodeName != null && componentNodeName != null && !applicationNodeName.equals(componentNodeName)) { throw new RuntimeException(applicationNodeName + "与" + componentNodeName + "两个节点名称不一致!"); } }
/** * 获取属性值,应用配置的优先级更高 * * @param applicationNode * @param componentNode * @param attributeName * @return */ public static String getPropertyName( XmlNode applicationNode, XmlNode componentNode, String attributeName) { String value = null; checkNodeName(applicationNode, componentNode); if (applicationNode != null) { value = applicationNode.getAttribute(attributeName); } if (value == null && componentNode != null) { value = componentNode.getAttribute(attributeName); } return value; }
protected void initPattern(XmlNode xmlNode) { NameFilter<XmlNode> nameFilter = new NameFilter<XmlNode>(xmlNode); List<XmlNode> filterMappings = nameFilter.findNodeList(FILTER_MAPPING); for (XmlNode filterMapping : filterMappings) { String urlPattern = filterMapping.getAttribute("url-pattern"); if (!patternStrs.contains(urlPattern)) { patterns.add(Pattern.compile(urlPattern)); patternStrs.add(urlPattern); } logger.logMessage( LogLevel.DEBUG, "<{}>的url-pattern:'{}'", this.getClass().getName(), urlPattern); } }
/** * 简单合并 * * @param nodeName * @param applicationNode * @param componentNode * @return */ public static List<XmlNode> combineSubList( String nodeName, XmlNode applicationNode, XmlNode componentNode) { checkNodeName(applicationNode, componentNode); List<XmlNode> result = new ArrayList<XmlNode>(); if (applicationNode == null && componentNode == null) { return result; } if (componentNode != null && componentNode.getSubNodes(nodeName) != null) { result.addAll(componentNode.getSubNodes(nodeName)); } if (applicationNode != null && applicationNode.getSubNodes(nodeName) != null) { result.addAll(applicationNode.getSubNodes(nodeName)); } return result; }
/** * 根据关键属性进行子节点合并 * * @param applicationNode * @param componentNode * @param keyPropertyName * @return */ public static List<XmlNode> combineSubList( XmlNode applicationNode, XmlNode componentNode, String nodeName, String keyPropertyName) { checkNodeName(applicationNode, componentNode); List<XmlNode> result = new ArrayList<XmlNode>(); if (applicationNode == null && componentNode == null) { return result; } List<XmlNode> applicationNodeList = null; if (applicationNode != null) { applicationNodeList = applicationNode.getSubNodes(nodeName); } List<XmlNode> componentNodeList = null; if (componentNode != null) { componentNodeList = componentNode.getSubNodes(nodeName); } if (componentNodeList == null && applicationNodeList == null) { return result; } if (componentNodeList == null || componentNodeList.size() == 0) { // 如果组件配置为空 result.addAll(applicationNode.getSubNodes(nodeName)); return result; } if (applicationNodeList == null || applicationNodeList.size() == 0) { // 如果应用配置为空 result.addAll(componentNode.getSubNodes(nodeName)); return result; } Map<String, XmlNode> appConfigMap = nodeListToMap(applicationNodeList, keyPropertyName); Map<String, XmlNode> compConfigMap = nodeListToMap(componentNodeList, keyPropertyName); for (String key : appConfigMap.keySet()) { XmlNode compNode = compConfigMap.get(key); XmlNode appNode = appConfigMap.get(key); if (compNode == null) { result.add(appNode); } else { // 如果两个都有,则合并之 result.add(combine(appNode, compNode)); } } for (String key : compConfigMap.keySet()) { XmlNode appNode = compConfigMap.get(key); if (appNode == null) { result.add(appNode); } } return result; }
public void testGetFooter() { StringBuffer sb = new StringBuffer(); node.getFooter(sb); assertEquals("-->", sb.toString()); }
private static XmlNode combine(XmlNode appNode, XmlNode compNode) { XmlNode result = new XmlNode(appNode.getNodeName()); result.setAttribute(compNode.getAttributes()); result.setAttribute(appNode.getAttributes()); if (!CollectionUtil.isEmpty(compNode.getSubNodes())) { result.addAll(compNode.getSubNodes()); } if (!CollectionUtil.isEmpty(appNode.getSubNodes())) { result.addAll(appNode.getSubNodes()); } return result; }