コード例 #1
0
ファイル: XMLMap.java プロジェクト: karthiks/Learn_guild
 // Someone better at Generics can yell at me as to how this should have been
 // done to use the same code for either objects or strings.  Sorry.
 public static Map<String, String> selectSubMap(Map<String, String> sm, String selection) {
   if (sm == null) return null;
   selection = selection.trim();
   if (badSelection(selection)) return null;
   Map<String, String> retval = new TreeMap<String, String>();
   selectSubMap(sm, retval, null, null, selection);
   return retval;
 }
コード例 #2
0
ファイル: XMLMap.java プロジェクト: karthiks/Learn_guild
 public static Map<String, Object> selectFullSubMap(Map<String, Object> om, String selection) {
   if (om == null) return null;
   selection = selection.trim();
   if (badSelection(selection)) return null;
   Map<String, Object> retval = new TreeMap<String, Object>();
   selectSubMap(null, null, om, retval, selection);
   return retval;
 }
コード例 #3
0
ファイル: XMLMap.java プロジェクト: karthiks/Learn_guild
  public static void runSamples() {
    System.out.println("Running XMLMap (www.mdom.org) Samples...");
    debugFlag = false;

    // Test the parsing of a Basic string Map
    Map<String, String> tm = XMLMap.getMap(simpleText);
    // System.out.println("tm="+tm);

    // Test the production of a basic map
    Map<String, String> simpleMap = new TreeMap<String, String>();
    simpleMap.put("/a/b!x", "X");
    simpleMap.put("/a/b", "B");
    simpleMap.put("/a/c/d", "D");
    System.out.println("simpleMap\n" + simpleMap);
    String simpleXml = XMLMap.getXML(simpleMap, true);
    System.out.println("simpleXml\n" + simpleXml);
    unitTest(simpleXml, false);

    // Do a select of a subMap
    Map<String, String> subMap = XMLMap.selectSubMap(tm, "/a/c");
    Map<String, Object> joinedMap = new TreeMap<String, Object>();
    System.out.println("subMap=" + subMap);
    joinedMap.put("/top/id", "1234");
    joinedMap.put("/top/fun", subMap); // Graft the map onto this node
    System.out.println("joinedMap\n" + joinedMap);
    String joinedXml = XMLMap.getXML(joinedMap, true);
    System.out.println("joinedXML\n" + joinedXml);
    unitTest(joinedXml, false);

    // Do an Array
    Map<String, Object> arrayMap = new TreeMap<String, Object>();
    String[] arrayStr = {"first", "second", "third"};
    arrayMap.put("/root/stuff", arrayStr);
    System.out.println("arrayMap\n" + arrayMap);
    String arrayXml = XMLMap.getXML(arrayMap, true);
    System.out.println("arrayXml\n" + arrayXml);
    unitTest(arrayXml, false);

    // Make a Map that is a combination of Maps, String, and Arrays
    Map<String, Object> newMap = new TreeMap<String, Object>();

    newMap.put("/Root/milton", "Root-milton");
    newMap.put("/Root/joe", "Root-joe");

    Map<String, String> m2 = new TreeMap<String, String>();
    m2.put("/fred/a", "fred-a");
    m2.put("/fred/b", "fred-b");
    newMap.put("/Root/freds", m2);

    // Add a list of maps
    // <Root>
    //   <maps>
    //     <map>
    //       <key>key-0</key>
    //       <val>val-0</val>
    //     </map>
    //     <map>
    //       <key>key-1</key>
    //       <val>val-1</val>
    //     </map>
    //   </maps>
    // </Root>

    List<Map<String, String>> lm = new ArrayList<Map<String, String>>();
    Map<String, String> m3 = null;
    m3 = new TreeMap<String, String>();
    m3.put("/key", "key-0");
    m3.put("/val", "val-0");
    lm.add(m3);

    m3 = new TreeMap<String, String>();
    m3.put("/key", "key-1");
    m3.put("/val", "val-1");
    lm.add(m3);

    newMap.put("/Root/maps/map", lm);

    // Add an array of Strings
    // <Root>
    //   <array>first</array>
    //   <array>second</array>
    //   <array>third</array>
    // </Root>

    String[] strar = {"first", "second", "third"};
    newMap.put("/Root/array", strar);

    // Add a list of Maps - this is a bit of a weird application - mostly as a
    // completeness test to insure lists of maps and arrays are equivalent.  Also
    // since the getFullMap returns maps, not Arrays of strings, this is necessary
    // to insure symmetry - i.e. we can take a map structure we produce and
    // regenerate the XML.  Most users will not use this form in construction.
    //
    // <Root>
    //     <item>item-1</item>
    //     <item>item-2</item>
    // </Root>

    List<Map<String, String>> l1 = new ArrayList<Map<String, String>>();
    Map<String, String> m4 = new TreeMap<String, String>();
    m4.put("/", "item-1");
    l1.add(m4);
    Map<String, String> m5 = new TreeMap<String, String>();
    m5.put("/", "item-2");
    l1.add(m5);
    newMap.put("/Root/item", l1);

    // Put in using the XMLMap bracket Syntax - not a particularly good
    // Way to represent multiple items - it is just here for completeness.
    newMap.put("/Root/anns/ann[0]", "Root-ann[0]");
    newMap.put("/Root/anns/ann[1]", "Root-ann[1]");
    newMap.put("/Root/bobs/bob[0]/key", "Root-bobs-bob[0]-key");
    newMap.put("/Root/bobs/bob[0]/val", "Root-bobs-bob[0]-val");
    newMap.put("/Root/bobs/bob[1]/key", "Root-bobs-bob[1]-key");
    newMap.put("/Root/bobs/bob[1]/val", "Root-bobs-bob[1]-val");

    // This is not allowed because maps cannot have duplicates
    /*
          Map<String,String> m6 = new TreeMap<String,String>();
          m5.put("/two", "two-1");
          m5.put("/two", "two-2");
          newMap.put("/Root", m6);
    */

    // Take the Map - turn it into XML and then parse the returned
    // XML into a second map - take the second map and produce more XML
    // If all goes well, the two generated blobs of XML should be the
    // same.  If anything goes wrong - we re-do it with lots of debug
    String complexXml = null;
    boolean success = false;
    debugFlag = false;
    try {
      complexXml = XMLMap.getXML(newMap, true);
      success = true;
    } catch (Exception e) {
      success = false;
    }

    // If we fail - do it again with deep levels of verbosity
    if (success) {
      unitTest(complexXml, false);
    } else {
      debugFlag = true;
      System.out.println("\n MISMATCH AND/OR SOME ERROR HAS OCCURED - REDO in VERBODE MODE");
      System.out.println("Starting out newMap=" + newMap);
      complexXml = XMLMap.getXML(newMap, true);
      unitTest(complexXml, false);
      debugFlag = false;
    }

    // A different example - iterating through nested sets - demonstrating the short form
    // of getSites() with the first parameter a string -the commented code below is the long form.

    // Map<String,Object> theMap = XMLMap.getFullMap(sitesText);
    // List<Map<String,Object>> theList = XMLMap.getList(theMap, "/sites/site");
    // for ( Map<String,Object> siteMap : theList) {

    // The short form using convenience method if you don't need the map for anything else
    System.out.println("\nParsing Sites Structure");
    for (Map<String, Object> siteMap : XMLMap.getList(sitesText, "/sites/site")) {
      System.out.println("Site=" + siteMap);
      System.out.println("Id=" + XMLMap.getString(siteMap, "/id"));
      for (Map<String, Object> toolMap : XMLMap.getList(siteMap, "/tools/tool")) {
        System.out.println("Tool=" + toolMap);
        System.out.println("ToolId=" + XMLMap.getString(toolMap, "/toolid"));
        for (Map<String, Object> property : XMLMap.getList(toolMap, "/properties/property")) {
          System.out.println("key=" + XMLMap.getString(property, "/key"));
          System.out.println("val=" + XMLMap.getString(property, "/val"));
        }
      }
    }

    // Lets parse some RSS as a final kind of easy but quite practical test
    debugFlag = false;
    System.out.println("\nParsing RSS Feed");
    // System.out.println(XMLMap.prettyPrint(rssText));
    Map<String, Object> rssFullMap = XMLMap.getFullMap(rssText);
    System.out.println("RSS Full Map\n" + rssFullMap);
    System.out.println("Rss Version=" + XMLMap.getString(rssFullMap, "/rss!version"));
    System.out.println("Chan-desc=" + XMLMap.getString(rssFullMap, "/rss/channel/description"));
    System.out.println("Chan-title=" + XMLMap.getString(rssFullMap, "/rss/channel/title"));

    Map<String, String> rssStringMap = XMLMap.flattenMap(rssFullMap);
    System.out.println("RSS Flat String Only Map\n" + rssStringMap);
    System.out.println("Rss Version=" + rssStringMap.get("/rss!version"));
    System.out.println("Chan-desc=" + rssStringMap.get("/rss/channel/description"));
    System.out.println("Chan-title=" + rssStringMap.get("/rss/channel/title"));

    for (Map<String, Object> rssItem : XMLMap.getList(rssFullMap, "/rss/channel/item")) {
      System.out.println("=== Item ===");
      System.out.println(" Item-title=" + XMLMap.getString(rssItem, "/title"));
      System.out.println(" Item-description=" + XMLMap.getString(rssItem, "/description"));
      System.out.println(" Item-link=" + XMLMap.getString(rssItem, "/link"));
    }
  }