Mercurial > hg > Members > tatsuki > JungleXMLReader
changeset 8:378bfec11a8d
add roleIdsTest and method
author | one |
---|---|
date | Sat, 25 Oct 2014 09:55:00 +0900 |
parents | 47eb9ee2a1db |
children | 28f383f677ff |
files | src/main/java/jp/ac/u_ryukyu/cr/ie/tatsuki/bbs/JuGrix.java src/main/java/jp/ac/u_ryukyu/cr/ie/tatsuki/bbs/loadXml.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/isActiveTest.java src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/roleIdsTest.java |
diffstat | 5 files changed, 162 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/cr/ie/tatsuki/bbs/JuGrix.java Sat Oct 25 07:48:02 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/cr/ie/tatsuki/bbs/JuGrix.java Sat Oct 25 09:55:00 2014 +0900 @@ -1,8 +1,11 @@ package jp.ac.u_ryukyu.cr.ie.tatsuki.bbs; import java.util.Iterator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import fj.data.List; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; 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; @@ -11,9 +14,20 @@ public class JuGrix { - public boolean isActive(JungleTree tree, String id){ + JungleTree personTree; + JungleTree organizationTree; + public JuGrix(){ + try{ + loadXml reader = new loadXml(); + personTree = reader.loadTestData("Person.xml"); + organizationTree = reader.loadTestData("Organization.xml"); + } catch (Exception e) { + e.printStackTrace(); + } + } + public boolean isActive(String id){ - InterfaceTraverser ifTraverser = tree.getTraverser(); + InterfaceTraverser ifTraverser = personTree.getTraverser(); Iterator<Pair<TreeNode,NodePath>> pairIterator = ifTraverser.find( (TreeNode node) -> { String personId = node.getAttributes().getString("id"); @@ -31,24 +45,24 @@ } - public Iterator<String> personIds(JungleTree tree , String id){ + public Iterator<String> personIds(String id){ - List<String> names = personIdsSearch(tree, id); + List<String> names = personIdsSearch(id); return names.iterator(); } - public Iterator<String> personIds(JungleTree tree , List<String> ids){ + public Iterator<String> personIds(List<String> ids){ List<String> personIds = List.nil(); for (String id : ids) { - personIds = personIds.append(personIdsSearch(tree, id)); + personIds = personIds.append(personIdsSearch(id)); } return personIds.iterator(); } - private List<String> personIdsSearch(JungleTree tree, String id) { - InterfaceTraverser ifTraverser = tree.getTraverser(); + private List<String> personIdsSearch(String id) { + InterfaceTraverser ifTraverser = personTree.getTraverser(); Iterator<Pair<TreeNode,NodePath>> pairPersonIterator = ifTraverser.find( (TreeNode node) -> { String personId = node.getAttributes().getString("element"); @@ -80,7 +94,102 @@ } return names; } + + public Iterator<String> roleIds(String id) { + Pattern personPattern = Pattern.compile("p:"); + Matcher personMacher = personPattern.matcher(id); + if (personMacher.find()) { + return searchRoleIds(personTree, id, "Person"); + } + + Pattern organizationPattern = Pattern.compile("o:"); + Matcher organizationMacher = organizationPattern.matcher(id); + if (organizationMacher.find()) { + return searchRoleIds(organizationTree, id, "Organization"); + } + + return null; + } + public Iterator<String> searchRoleIds(JungleTree tree, String id, String element){ + + InterfaceTraverser ifTraverser = tree.getTraverser(); + Iterator<Pair<TreeNode,NodePath>> searchTargetIterator = ifTraverser.find( + (TreeNode node) -> { + String nodeElement = node.getAttributes().getString("element"); + if (nodeElement == null) + return false; + if (!nodeElement.equals(element)) + return false; + String nodeId = node.getAttributes().getString("id"); + if (nodeId == null) + return false; + if (nodeId.equals(id)) + return true; + return false; + } + ,"id",id); + + List<String> ids = List.nil(); + for (;searchTargetIterator.hasNext();) { + + Pair<TreeNode, NodePath> searchTargetPair = searchTargetIterator.next(); + Iterator<Pair<TreeNode,NodePath>> pairIdIterator = ifTraverser.find( + (TreeNode node) -> { + String nodeElement = node.getAttributes().getString("element"); + if (nodeElement == null) + return false; + if (!nodeElement.equals("roleRefId")) + return false; + String nodeId = node.getAttributes().getString("text"); + if (nodeId != null) + return true; + return false; + } + ,searchTargetPair.left()); + + + for (;pairIdIterator.hasNext();){ + Pair<TreeNode, NodePath> idNodePath = pairIdIterator.next(); + String attribute = idNodePath.left().getAttributes().getString("text"); + + Iterator<String> checkList = ids.iterator(); + for (;checkList.hasNext();) { + if(checkList.next().equals(attribute)) + attribute = null; + } + + if (attribute != null) { + ids = ids.cons(attribute); + System.out.println("-------------------------" + attribute + "-------------------------"); + } + } + } + return ids.iterator(); + } + + + public Iterator<String> competentRoleId(String personId) { + + InterfaceTraverser ifTraverser = personTree.getTraverser(); + Iterator<Pair<TreeNode,NodePath>> pairPersonIterator = ifTraverser.find( + (TreeNode node) -> { + String nodeElement = node.getAttributes().getString("element"); + if (nodeElement == null) + return false; + if (!nodeElement.equals("Person")) + return false; + String nodeId = node.getAttributes().getString("id"); + if (nodeId == null) + return false; + if (nodeId.equals(personId)) + return true; + return false; + } + ,"element","Person"); + + return null; + } }
--- a/src/main/java/jp/ac/u_ryukyu/cr/ie/tatsuki/bbs/loadXml.java Sat Oct 25 07:48:02 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/cr/ie/tatsuki/bbs/loadXml.java Sat Oct 25 09:55:00 2014 +0900 @@ -11,6 +11,7 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs.CreateBoardMessageServlet; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; @@ -20,19 +21,25 @@ public class loadXml { - public JungleTree loadTestData(String xmlName) - throws ParserConfigurationException, SAXException, IOException, - FileNotFoundException { + Jungle jungle; + SAXParserFactory saxParserFactory; + + public loadXml() { + saxParserFactory = SAXParserFactory.newInstance(); + jungle = new DefaultJungle(null, "jungle", new DefaultTreeEditor( + new DefaultTraverser())); + } - SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); + public JungleTree loadTestData(String xmlName) + throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { SAXParser saxParser = saxParserFactory.newSAXParser(); - Jungle jungle = new DefaultJungle(null, "jungle", - new DefaultTreeEditor(new DefaultTraverser())); JungleTree tree = jungle.createNewTree(xmlName); ReadXmlHandler readXmlHandler = new ReadXmlHandler(tree); saxParser.parse(new FileInputStream("xml/" + xmlName), readXmlHandler); JungleTree newTree = readXmlHandler.getTree(); + return newTree; } + }
--- a/src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/PersonIdsTest.java Sat Oct 25 07:48:02 2014 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/PersonIdsTest.java Sat Oct 25 09:55:00 2014 +0900 @@ -13,19 +13,14 @@ @Test public void PersonIdsTest() { - try { - loadXml reader = new loadXml(); - JungleTree tree = reader.loadTestData("Person.xml"); + JuGrix jugrix = new JuGrix(); - Iterator<String> ids = jugrix.personIds(tree, "o:2"); + Iterator<String> ids = jugrix.personIds("o:2"); ids.hasNext(); Assert.assertEquals(ids.next(),"p:45"); ids.hasNext(); Assert.assertEquals(ids.next(),"p:1"); - } catch (Exception e) { - e.printStackTrace(); - } } }
--- a/src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/isActiveTest.java Sat Oct 25 07:48:02 2014 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/isActiveTest.java Sat Oct 25 09:55:00 2014 +0900 @@ -27,20 +27,14 @@ @Test public void Person_idsTest(){ - try { - loadXml reader = new loadXml(); - JungleTree newTree = reader.loadTestData("Person.xml"); + JuGrix jugrix = new JuGrix(); String personId = "p:1"; - Assert.assertTrue(jugrix.isActive(newTree,personId)); + Assert.assertTrue(jugrix.isActive(personId)); personId = "p:100"; - Assert.assertFalse(jugrix.isActive(newTree,personId)); - - } catch (Exception e) { - e.printStackTrace(); - } + Assert.assertFalse(jugrix.isActive(personId)); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/functionTest/roleIdsTest.java Sat Oct 25 09:55:00 2014 +0900 @@ -0,0 +1,27 @@ +package jp.ac.u_ryukyu.cr.ie.tatsuki.functionTest; + +import java.util.Iterator; + +import jp.ac.u_ryukyu.cr.ie.tatsuki.bbs.JuGrix; +import junit.framework.Assert; + +import org.junit.Test; + + +public class roleIdsTest { + + @Test + public void roleIdsTest(){ + JuGrix jugrix = new JuGrix(); + Iterator<String> orgIds = jugrix.roleIds("o:6"); + orgIds.hasNext(); + Assert.assertEquals(orgIds.next(),"r:18"); + + Iterator<String> personIds = jugrix.roleIds("p:1"); + personIds.hasNext(); + Assert.assertEquals(personIds.next(),"r:34"); + personIds.hasNext(); + Assert.assertEquals(personIds.next(),"r:10"); + + } +}