view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/xml/extendXmlTree.java @ 16:eed6235830c9

add timeMeasurementCreateIndex
author one
date Mon, 08 Dec 2014 19:11:19 +0900
parents 89478f2ea07f
children 917ecf2c7350
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.tatsuki.xml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.Random;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import jp.ac.u_ryukyu.cr.ie.tatsuki.bbs.JuGrix;
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.JungleTreeEditor;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.InterfaceTraverser;
import ac.jp.u_ryukyu.cr.ie.tatsuki.xmlReader.ReadXmlHandler;

public class extendXmlTree {

  private extendXmlTree() {

  }


  public static JuGrix createJuGrix(boolean useIndex,int personCount ) throws ParserConfigurationException, SAXException,
      FileNotFoundException, IOException {
    Jungle jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor(new DefaultTraverser()));
    JungleTree readXmlTree = jungle.createNewTree("Person");
     //JungleTree readXmlTree = readXml("Person", jungle);
    JungleTree orgTree = readXml("Organization", jungle);
    JungleTree roleTree = readXml("Role", jungle);
    JungleTreeEditor editor = readXmlTree.getTreeEditor();
    NodePath path = new DefaultNodePath();
    editor = editor.addNewChildAt(path, 0).b();
    editor = editor.putAttribute(path.add(0), "element", ByteBuffer.wrap("Persons".getBytes())).b();
    System.out.println("Create Tree-------------------------------");
    for (int count = 0; count < personCount; ++count) {
      editor = extendsTree(editor, orgTree, roleTree, count);
    }
    editor.success();
 
    int PersonCount = readXmlTree.getRootNode().getChildren().at(0).b().getChildren().size();
    System.out.println("PersonCount = " + PersonCount);
    System.out.println("end-------------------------------");
    JuGrix jugrix = new JuGrix(useIndex, readXmlTree, orgTree, roleTree);
    return jugrix;
  }

  public static JungleTreeEditor extendsTree(JungleTreeEditor editor, JungleTree orgTree, JungleTree roleTree,
      int count) {
    Random rdm = new Random();
    int orgNum = rdm.nextInt(30);
    String orgId = "o:" + String.valueOf(orgNum);
    String orgRoleId = searchOrgRole(orgTree, orgId);
    String roleId = searchRole(roleTree, orgRoleId);
    orgNum = rdm.nextInt(30);

    String orgId2 = "o:" + String.valueOf(orgNum);
    String orgRoleId2 = searchOrgRole(orgTree, orgId2);
    String roleId2 = searchRole(roleTree, orgRoleId2);

    NodePath path = new DefaultNodePath().add(0);
    editor = editor.addNewChildAt(path, count).b();
    path = path.add(count);
    editor = editor.putAttribute(path, "element", ByteBuffer.wrap("Person".getBytes())).b();
    String personId = String.valueOf(count);
    editor = editor.putAttribute(path, "Person-id", ByteBuffer.wrap(("p:" + personId).getBytes())).b();
    editor = editor.putAttribute(path, "Person-type", ByteBuffer.wrap("Person".getBytes())).b();

    editor = editor.addNewChildAt(path, 0).b();
    editor = editor.putAttribute(path.add(0), "element", ByteBuffer.wrap(("accountId").getBytes())).b();
    editor = editor.putAttribute(path.add(0), "text-accountId", ByteBuffer.wrap(("a:" + personId).getBytes())).b();

    editor = editor.addNewChildAt(path, 1).b();
    editor = editor.putAttribute(path.add(1), "element", ByteBuffer.wrap(("lastName").getBytes())).b();
    editor = editor.putAttribute(path.add(1), "text-lastName", ByteBuffer.wrap(("金川").getBytes())).b();

    editor = editor.addNewChildAt(path, 2).b();
    editor = editor.putAttribute(path.add(2), "element", ByteBuffer.wrap(("name").getBytes())).b();
    editor = editor.putAttribute(path.add(2), "text-name", ByteBuffer.wrap(("竜己").getBytes())).b();

    editor = editor.addNewChildAt(path, 3).b();
    editor = editor.putAttribute(path.add(3), "element", ByteBuffer.wrap(("nameReading").getBytes())).b();
    editor = editor.putAttribute(path.add(3), "text-nameReading", ByteBuffer.wrap(("かながわたつき").getBytes())).b();

    editor = editor.addNewChildAt(path, 4).b();
    editor = editor.putAttribute(path.add(4), "element", ByteBuffer.wrap(("roleRefIds").getBytes())).b();

    editor = editor.addNewChildAt(path.add(4), 0).b();
    editor = editor.putAttribute(path.add(4).add(0), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
    editor = editor.putAttribute(path.add(4).add(0), "text-roleRefId", ByteBuffer.wrap((roleId).getBytes())).b();
    editor = editor.addNewChildAt(path.add(4), 1).b();
    editor = editor.putAttribute(path.add(4).add(1), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
    editor = editor.putAttribute(path.add(4).add(1), "text-roleRefId", ByteBuffer.wrap((roleId2).getBytes())).b();

    editor = editor.addNewChildAt(path, 5).b();
    editor = editor.putAttribute(path.add(5), "element", ByteBuffer.wrap(("parentOrganizations").getBytes())).b();
    editor = editor.putAttribute(path.add(5), "parentOrganizations-type",
        ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();

    NodePath parentOrgPath = path.add(5);
    editor = editor.addNewChildAt(parentOrgPath, 0).b();
    editor = editor.putAttribute(parentOrgPath.add(0), "element",
        ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();
    editor = editor.putAttribute(parentOrgPath.add(0), "OrganizationMappedByRole-type",
        ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();

    editor = editor.addNewChildAt(parentOrgPath.add(0), 0).b();
    editor = editor.putAttribute(parentOrgPath.add(0).add(0), "element",
        ByteBuffer.wrap(("organizationRefId").getBytes())).b();
    editor = editor.putAttribute(parentOrgPath.add(0).add(0), "text-organizationRefId",
        ByteBuffer.wrap((orgId).getBytes())).b();

    editor = editor.addNewChildAt(parentOrgPath.add(0), 1).b();
    editor = editor.putAttribute(parentOrgPath.add(0).add(1), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
    editor = editor.putAttribute(parentOrgPath.add(0).add(1), "text-roleRefId", ByteBuffer.wrap((roleId).getBytes()))
        .b();

    editor = editor.addNewChildAt(parentOrgPath, 1).b();
    editor = editor.putAttribute(parentOrgPath.add(1), "element",
        ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();
    editor = editor.putAttribute(parentOrgPath.add(1), "OrganizationMappedByRole-type",
        ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();

    editor = editor.addNewChildAt(parentOrgPath.add(1), 0).b();
    editor = editor.putAttribute(parentOrgPath.add(1).add(0), "element",
        ByteBuffer.wrap(("organizationRefId").getBytes())).b();
    editor = editor.putAttribute(parentOrgPath.add(1).add(0), "text-organizationRefId",
        ByteBuffer.wrap((orgId2).getBytes())).b();

    editor = editor.addNewChildAt(parentOrgPath.add(1), 1).b();
    editor = editor.putAttribute(parentOrgPath.add(1).add(1), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
    editor = editor.putAttribute(parentOrgPath.add(1).add(1), "text-roleRefId", ByteBuffer.wrap((roleId2).getBytes()))
        .b();

    editor = editor.addNewChildAt(path, 6).b();
    editor = editor.putAttribute(path.add(6), "element", ByteBuffer.wrap(("priorities").getBytes())).b();
    editor = editor.putAttribute(path.add(6), "priorities-type",
        ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();

    //
    editor = editor.addNewChildAt(path.add(6), 0).b();
    editor = editor.putAttribute(path.add(6).add(0), "element", ByteBuffer.wrap(("PriorityMappedByRole").getBytes()))
        .b();
    editor = editor.putAttribute(path.add(6).add(0), "PriorityMappedByRole-type",
        ByteBuffer.wrap(("PriorityMappedByRole").getBytes())).b();

    editor = editor.addNewChildAt(path.add(6).add(0), 0).b();
    editor = editor.putAttribute(path.add(6).add(0).add(0), "element", ByteBuffer.wrap(("priority").getBytes())).b();
    editor = editor.putAttribute(path.add(6).add(0).add(0), "text-priority", ByteBuffer.wrap(("0").getBytes())).b();
    editor = editor.addNewChildAt(path.add(6).add(0), 1).b();
    editor = editor.putAttribute(path.add(6).add(0).add(1), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
    editor = editor.putAttribute(path.add(6).add(0).add(1), "text-roleRefId", ByteBuffer.wrap((roleId).getBytes())).b();

    editor = editor.addNewChildAt(path.add(6), 1).b();
    editor = editor.putAttribute(path.add(6).add(1), "element", ByteBuffer.wrap(("PriorityMappedByRole").getBytes()))
        .b();
    editor = editor.putAttribute(path.add(6).add(1), "PriorityMappedByRole-type",
        ByteBuffer.wrap(("PriorityMappedByRole").getBytes())).b();

    editor = editor.addNewChildAt(path.add(6).add(1), 0).b();
    editor = editor.putAttribute(path.add(6).add(1).add(0), "element", ByteBuffer.wrap(("priority").getBytes())).b();
    editor = editor.putAttribute(path.add(6).add(1).add(0), "text-priority", ByteBuffer.wrap(("1").getBytes())).b();
    editor = editor.addNewChildAt(path.add(6).add(1), 1).b();
    editor = editor.putAttribute(path.add(6).add(1).add(1), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
    editor = editor.putAttribute(path.add(6).add(1).add(1), "text-roleRefId", ByteBuffer.wrap((roleId2).getBytes()))
        .b();
    return editor;

  }

  private static String searchRole(JungleTree roleTree, String orgRoleId) {
    InterfaceTraverser traverser = roleTree.getTraverser(false);
    Iterator<TreeNode> roleIdIterator = traverser.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("Role-id");
      if (compareRoleId == null)
        return false;
      if (compareRoleId.equals(orgRoleId))
        return true;
      return false;
    }, "Role-id", orgRoleId);

    if (!roleIdIterator.hasNext())
      return orgRoleId;

    TreeNode orgIdNode = roleIdIterator.next();
    TreeNode roleRefIdNode = orgIdNode.getChildren().at(0).b().getChildren().at(0).b();
    if (roleRefIdNode == null)
      return orgRoleId;
    String roleId = roleRefIdNode.getAttributes().getString("text-childrenRoleRefId");

    return roleId;
  }

  private static String searchOrgRole(JungleTree orgTree, String orgId) {
    InterfaceTraverser traverser = orgTree.getTraverser(false);
    Iterator<TreeNode> orgIdIterator = traverser.find((TreeNode node) -> {
      String element = node.getAttributes().getString("element");
      if (element == null)
        return false;
      if (!element.equals("Organization"))
        return false;
      String compareRoleId = node.getAttributes().getString("Organization-id");
      if (compareRoleId == null)
        return false;
      if (compareRoleId.equals(orgId))
        return true;
      return false;
    }, "Organization-id", orgId);

    if (!orgIdIterator.hasNext())
      return "";

    TreeNode orgIdNode = orgIdIterator.next();
    TreeNode roleRefIdNode = orgIdNode.getChildren().at(2).b().getChildren().at(0).b();
    String roleId = roleRefIdNode.getAttributes().getString("text-roleRefId");
    return roleId;
  }

  private static JungleTree readXml(String xml, Jungle jungle) throws ParserConfigurationException, SAXException,
      IOException, FileNotFoundException {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    SAXParser saxParser = saxParserFactory.newSAXParser();
    JungleTree tree = jungle.createNewTree(xml);
    ReadXmlHandler readXmlHandler = new ReadXmlHandler(tree);
    saxParser
        .parse(new FileInputStream("/Users/one/Documents/workspace/JungleXMLReader/xml/" + xml + ".xml"), readXmlHandler);
    return readXmlHandler.getTree();
  }

}