I'm writing a Java program that uses a XML file as a template to populate a data-structure that the program then takes user input and interacts with.

What I'm going for is something that keeps this hierarchy:

  <CYO>
    <node>
      <to>
    home
      </to>
      <message>
    This is the message that will be displayed to the user.
      </message>
      <option>
    <name>
      fun <!-- option that is dispalyed and command to be entered -->
    </name>
    <goto>
      not home <!-- next node to be displayed for this command -->
    </goto>
      </option>
      <option>
    <name>
      no fun
    </name>
    <goto>
      home <!-- should trigger the reprinting of the same message; goto/home = to/home -->
    </goto>
      </option>
    </node>
    <node>
      <to>
    not home
      </to>
      <message>
    Welcome you have just left home.
    What else do you want to do?
      </message>
      <option>
    <name>
      back home
    </name>
    <goto>
      home
    </goto>
      </option>
      <option>
    <name>
      fun
    </name>
    <goto>
      not home <!-- should trigger the reprinting of the same message; goto/not home = to/not home -->
    </goto>
      </option>
    </node>
  </CYO>

In Java I've been trying to get sorted the logic of how the node items will be called from 'option/name::goto' and then have the program return the 'node/to' matching the 'option/name::goto'. Really the problem is I'm not sure how this needs to be represented in Java so that calls like this will be returned correctly. This is what I've done so far is Java:

List<Map<String, Object<String, String>>> CYO =  new ArrayList();

Map<String, Object> home = new HashMap<String, Object>();
home.put("to", "home");
home.put("message", "This is the message that will be displayed to the user.");
Map<String, String> Option1 = new HashMap<String, String>();
Option1.put("fun", "not home");
Option1.put("no fun", "home");

home.put("option", Option1);

Map<String, Object> not_home = new HashMap<String, Object>();
not_home.put("to", "not home");
not_home.put("message", "Welcome you have just left home.\\nWhat else do you want to do?");
not_home.put("option", "not home");
Map<String, String> Option2 = new HashMap<String, String>();
Option2.put("back home", "home");
Option2.put("fun", "not_home");

not_home.put("option", Option2);


CYO.add(home);
CYO.add(not_home);

System.out.println(CYO.indexOf("not_home"));

I get an error from IDE saying, "The type Object is not generic; it cannot be parameterized with arguments .

1 Answers

0
Espresso On Best Solutions

There is two parts in your problem.

  1. Reading an xml file / configuration into a datastructure (jaxb does that for you).

    • Create an xsd for this xml representation,
    • Use xjc or jaxb to generate the java code.
  2. Getting the referred node, from a goto.

    • Read the xml file to java object Cyo.
    • Once read, store the named nodes in a hashmap Map map -> HashMap().
    • When you traverse to goto, Node gotoNode = map.get(theNodeName)

    • gotoNode.execute()