Mercurial > hg > Members > tatsuki > JungleXMLReader
changeset 12:f93d15042315
fix bag
author | one |
---|---|
date | Mon, 27 Oct 2014 00:14:35 +0900 |
parents | 67305f53aad0 |
children | 54ac3718ab24 |
files | src/main/java/jp/ac/u_ryukyu/cr/ie/tatsuki/bbs/JuGrix.java src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/PersonIdsTest.java src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/roleIdsTest.java |
diffstat | 3 files changed, 182 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/cr/ie/tatsuki/bbs/JuGrix.java Sat Oct 25 17:10:03 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/cr/ie/tatsuki/bbs/JuGrix.java Mon Oct 27 00:14:35 2014 +0900 @@ -5,7 +5,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; +import fj.Ord; +import fj.P2; +import fj.data.TreeMap; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; @@ -18,15 +20,18 @@ JungleTree personTree; JungleTree organizationTree; + JungleTree roleTree; public JuGrix(){ try{ loadXml reader = new loadXml(); personTree = reader.loadTestData("Person.xml"); organizationTree = reader.loadTestData("Organization.xml"); + roleTree = reader.loadTestData("Role.xml"); } catch (Exception e) { e.printStackTrace(); } } + public boolean isActive(String id){ InterfaceTraverser ifTraverser = personTree.getTraverser(); @@ -47,25 +52,29 @@ } - public Iterator<String> personIds(String id){ + public Iterator<String> personIds(String orgId, LinkedList<String> roleIds){ - LinkedList<String> names = personIdsSearch(id); + LinkedList<String> names = personIdsSearch(orgId, roleIds); return names.iterator(); } - public Iterator<String> personIds(LinkedList<String> ids){ + public Iterator<String> personIds(LinkedList<String> orgIds ,LinkedList<String> roleIds){ - LinkedList<String> personIds = new LinkedList(); + LinkedList<String> personIds = new LinkedList<String>(); - for (String id : ids) { - personIds.addAll(personIdsSearch(id)); + for (String orgId : orgIds) { + personIds.addAll(personIdsSearch(orgId, roleIds)); } return personIds.iterator(); } - private LinkedList<String> personIdsSearch(String id) { - InterfaceTraverser ifTraverser = personTree.getTraverser(); - Iterator<Pair<TreeNode,NodePath>> pairPersonIterator = ifTraverser.find( + private LinkedList<String> personIdsSearch(String id, LinkedList<String> roleIds) { + + if (!filter(id, roleIds)) + return new LinkedList<String>(); + + InterfaceTraverser personTraverser = personTree.getTraverser(); + Iterator<Pair<TreeNode, NodePath>> pairPersonIterator = personTraverser.find( (TreeNode node) -> { String personId = node.getAttributes().getString("element"); if (personId == null) @@ -77,10 +86,11 @@ ,"element","Person"); LinkedList<String> names = new LinkedList<String>(); + for (;pairPersonIterator.hasNext();) { Pair<TreeNode, NodePath> pairPerson = pairPersonIterator.next(); - Iterator<Pair<TreeNode,NodePath>> pairIdIterator = ifTraverser.find( + Iterator<Pair<TreeNode,NodePath>> pairIdIterator = personTraverser.find( (TreeNode node) -> { String personId = node.getAttributes().getString("text"); if (personId == null) @@ -90,11 +100,158 @@ return false; } ,pairPerson.left(),"text",id); - if (pairIdIterator.hasNext()) - names.add(pairPerson.left().getAttributes().getString("id")); + + if (pairIdIterator.hasNext()) { + String name = pairPerson.left().getAttributes().getString("id"); + names.add(name); + } + } + return names; + } + + + public boolean filter(String orgId, LinkedList<String> roleIds) { + if (roleIds == null) + return true; + + InterfaceTraverser orgTraverser = organizationTree.getTraverser(); + + Iterator<Pair<TreeNode,NodePath>> orgNodeIterator = orgTraverser.find( + (TreeNode node) -> { + String personId = node.getAttributes().getString("element"); + if (personId == null) + return false; + if (!personId.equals("Organization")) + return false; + String compareOrgId = node.getAttributes().getString("id"); + if (orgId == null) + return false; + if (compareOrgId.equals(orgId)) + return true; + return false; + } + ,"element","orgId"); + + if (!orgNodeIterator.hasNext()) + return false; + + TreeNode searchNode = orgNodeIterator.next().left(); + + for (String id : roleIds) { + Pattern personPattern = Pattern.compile("r:"); + Matcher personMacher = personPattern.matcher(id); + if (personMacher.find()) { + if (!roleFilter(id , searchNode)) + return false; + } + + Pattern organizationPattern = Pattern.compile("rde:"); + Matcher organizationMacher = organizationPattern.matcher(id); + if (organizationMacher.find()) { + if (!rdeFilter(id,searchNode)) + return false; + } + } + return true; + } + + private boolean rdeFilter(String id, TreeNode targetNode) { + + InterfaceTraverser orgTraverser = organizationTree.getTraverser(); + Iterator<Pair<TreeNode,NodePath>> pairIdIterator = orgTraverser.find( + (TreeNode node) -> { + String element = node.getAttributes().getString("element"); + if (element == null) + return false; + if (!element.equals("roleRefId")) + return false; + String roleId = node.getAttributes().getString("text"); + if (roleId != null) + return true; + return false; + } + ,targetNode,"element","roleRefId"); + + TreeMap<String, String> idMap = TreeMap.empty(Ord.stringOrd); + for (; pairIdIterator.hasNext();) { + String roleId = pairIdIterator.next().left().getAttributes().getString("text"); + idMap = idMap.set(roleId,roleId); + } + + Iterator<P2<String, String>> ids = idMap.iterator(); + InterfaceTraverser roleTraverser = roleTree.getTraverser(); + + for (;ids.hasNext();) { + String roleId = ids.next()._1(); + Iterator<Pair<TreeNode,NodePath>> roleNodeIterator = roleTraverser.find( + (TreeNode node) -> { + String element = node.getAttributes().getString("element"); + if (element == null) + return false; + if (!element.equals("Role")) + return false; + String compareRoleId = node.getAttributes().getString("id"); + if (compareRoleId == null) + return false; + if (compareRoleId.equals(roleId)) + return true; + return false; + } + ,"id",roleId); + + for (;roleNodeIterator.hasNext();) { + TreeNode roleNode = roleNodeIterator.next().left(); + Iterator<Pair<TreeNode,NodePath>> rdeNodeIterator = roleTraverser.find( + (TreeNode node) -> { + String elementName = node.getAttributes().getString("element"); + if (elementName == null) + return false; + if (!elementName.equals("roleDescriptionElementRefIds")) + return false; + String rdeId = node.getAttributes().getString("text"); + if (rdeId == null) + return false; + Pattern personPattern = Pattern.compile(id); + Matcher personMacher = personPattern.matcher(rdeId); + if (personMacher.find()) + return true; + return false; + } + ,roleNode,"text",id); + + if (!rdeNodeIterator.hasNext()) + return false; + } } - return names; + return true; + } + + public boolean roleFilter(String id, TreeNode orgNode) { + + InterfaceTraverser orgTraverser = organizationTree.getTraverser(); + + Iterator<Pair<TreeNode,NodePath>> pairIdIterator = orgTraverser.find( + (TreeNode node) -> { + String elementName = node.getAttributes().getString("element"); + if (elementName == null) + return false; + if (!elementName.equals("roleRefId")) + return false; + String roleId = node.getAttributes().getString("text"); + if (roleId == null) + return false; + if (roleId.equals(id)) + return true; + return false; + } + ,orgNode,"text",id); + + if (pairIdIterator.hasNext()){ + System.out.println(pairIdIterator.next().left().getAttributes().getString("text")); + return true; + } + return false; } public Iterator<String> roleIds(String id) {
--- a/src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/PersonIdsTest.java Sat Oct 25 17:10:03 2014 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/PersonIdsTest.java Mon Oct 27 00:14:35 2014 +0900 @@ -1,6 +1,7 @@ package jp.ac.u_ryukyu.cr.ie.tatsuki.functionTest; import java.util.Iterator; +import java.util.LinkedList; import jp.ac.u_ryukyu.cr.ie.tatsuki.bbs.JuGrix; import jp.ac.u_ryukyu.cr.ie.tatsuki.bbs.loadXml; @@ -15,12 +16,18 @@ public void PersonIdsTest() { JuGrix jugrix = new JuGrix(); - Iterator<String> ids = jugrix.personIds("o:2"); + LinkedList<String> filterIds = new LinkedList<String>(); + filterIds.add("r:9"); + Iterator<String> ids = jugrix.personIds("o:2",filterIds); ids.hasNext(); Assert.assertEquals(ids.next(),"p:1"); + + filterIds.add("rde:3"); + ids = jugrix.personIds("o:2",filterIds); + ids.hasNext(); - Assert.assertEquals(ids.next(),"p:45"); + Assert.assertEquals(ids.next(),"p:1"); } }
--- a/src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/roleIdsTest.java Sat Oct 25 17:10:03 2014 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/roleIdsTest.java Mon Oct 27 00:14:35 2014 +0900 @@ -19,9 +19,9 @@ Iterator<String> personIds = jugrix.roleIds("p:1"); personIds.hasNext(); - Assert.assertEquals(personIds.next(),"r:34"); + Assert.assertEquals(personIds.next(),"r:10"); personIds.hasNext(); - Assert.assertEquals(personIds.next(),"r:10"); + Assert.assertEquals(personIds.next(),"r:34"); } }