`
xinyoulinglei
  • 浏览: 123618 次
社区版块
存档分类
最新评论

XML的TreeConfig

    博客分类:
  • java
xml 
阅读更多
/**
     * <?xml version="1.0" encoding="UTF-8"?>
<root>
    <tree id="root" useArrows="false">
        <node>
            <id>unifyProxy</id>
            <parentId></parentId>
            <text>开通接口</text>
            <method></method>
        </node>
        <node expanded="false">
            <id>corpManager</id>
            <parentId>unifyProxy</parentId>
            <text>集团管理</text>
            <method></method>
        </node>
        <node>
            <id>corp</id>
            <parentId>corpManager</parentId>
            <text>集团</text>
            <method>initBusinessGroup()</method>
      </node>      
    </tree>
</root>

TreeNode:
     private String id;
    
     private String parentId;
    
     private String text;
    
     private String method;
    
Tree:
    private String id = null;

    private List<TreeNode> nodeList = new ArrayList<TreeNode>();



     */
    private static final Map<String, Tree> treeMap = new HashMap<String, Tree>();

    private String nodeConfigFile;

    public static Map<String, Tree> getTreeMap()
    {
        return treeMap;
    }

    /**
     * 读取配置文件
     */
    public void init()
    {
        try
        {
//            String cmClientPath = SystemLoader.getCmClientPath();
           // File configFile = new File(cmClientPath + File.separator + nodeConfigFile.trim());
            File configFile = new File("D://我的文档//workspace/CMClient//WebRoot//WEB-INF//conf//common//tree_config.xml");
           
            SAXReader reader = new SAXReader();
            Document document = reader.read(configFile);
            parse(document);

        }
        catch (DocumentException e)
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 解析配置值
     * @param document
     * @author  liudong
     * @throws Exception 抛出异常
     */
    private void parse(Document document)
        throws Exception
    {
        //Tree就是
        Tree tree = null;
        TreeNode treeNode = null;
        final Element root = document.getRootElement();
        final List< ? > trees = root.elements("tree");
        for (final Iterator< ? > iterator = trees.iterator(); iterator.hasNext();)
        {
            Element treeElement = (Element) iterator.next();

            tree = new Tree();
            parseTreeAttribute(tree, treeElement);

            List< ? > nodes = treeElement.elements("node");
            for (final Iterator< ? > iter = nodes.iterator(); iter.hasNext();)
            {

                final Element nodeElement = (Element) iter.next();
               
                treeNode = new TreeNode();

                parseNodeAttribute(treeNode, nodeElement);
               
                parseNodeValue(treeNode,nodeElement);

                tree.getNodeList().add(treeNode);
            }
            treeMap.put(tree.getId(), tree);
        }
    }

    private void parseTreeAttribute(Tree tree, Element element)
    {

        //
        final Attribute idAtt = element.attribute("id");
        tree.setId("root");
        if (null != idAtt && ParamChecking.isBlank(idAtt.getValue()))
        {
            tree.setId(UnifyUtils.processTrim(idAtt.getValue()));
        }

        final Attribute useArrowsAtt = element.attribute("useArrows");
        if (null != useArrowsAtt && "false".equals(UnifyUtils.processTrim(useArrowsAtt.getValue())))
        {
            tree.getTreeOptions().setUseArrows(false);
        }

    }

    private void parseNodeAttribute(TreeNode node, Element element)
    {
        final Attribute expanded = element.attribute("expanded");
        if (null != expanded && "false".equals(expanded.getValue()))
        {
            node.getTreeNodeOptions().setExpanded(false);
        }

    }

    private void parseNodeValue(TreeNode node, Element element)
    {
        org.dom4j.Node idNode = element.element("id");
        org.dom4j.Node textNode = element.element("text");
        org.dom4j.Node parentIdNode = element.element("parentId");
        org.dom4j.Node methodNode = element.element("method");

        node.setId(idNode.getText().trim());
        node.setParentId(parentIdNode.getText().trim());
        node.setText(textNode.getText().trim());
        node.setMethod(methodNode.getText().trim());
    }

    /**
     * @return Returns the nodeConfigFile.
     */
    public String getNodeConfigFile()
    {
        return nodeConfigFile;
    }

    /**
     * @param nodeConfigFile The nodeConfigFile to set.
     */
    public void setNodeConfigFile(String nodeConfigFile)
    {
        this.nodeConfigFile = nodeConfigFile;
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics