Beispiel #1
0
  public static Document getXMLDom(Map tm) {
    if (tm == null) return null;
    Document document = null;

    try {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
      factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
      DocumentBuilder parser = factory.newDocumentBuilder();
      document = parser.newDocument();
    } catch (Exception e) {
      return null;
    }

    iterateMap(document, document.getDocumentElement(), tm, 0);
    return document;
  }
Beispiel #2
0
  /*  Remember that the map is a linear list of entries
    /a/b B1
    /a/c Map
         /x X1
         /y Y1
         /y!r R1
    /a/c!q Q1
    /a/d D1

    <a>
      <b>B1</b>
      <c q="Q1">
         <x>X1</x>
         <y r="R1">Y1</y>
      </c>
      <d>D1</d>
    </a>
  */
  private static void iterateMap(Document document, Node parentNode, Map tm, int d) {
    doDebug(d, "> IterateMap parentNode= " + nodeToString(parentNode));
    d++;
    Iterator iter = tm.keySet().iterator();
    while (iter.hasNext()) {
      String key = (String) iter.next();
      if (key == null) continue;
      if (!key.startsWith("/")) continue; // Skip
      Object obj = tm.get(key);
      if (obj == null) {
        continue;
      }
      if (obj instanceof String) {
        storeInDom(document, parentNode, key, (String) obj, 0, d);
      } else if (obj instanceof String[]) {
        String[] strArray = (String[]) obj;
        doDebug(d, "Looping through an array of length " + strArray.length);
        for (int i = 0; i < strArray.length; i++) {
          storeInDom(document, parentNode, key, strArray[i], i, d);
        }
      } else if (obj instanceof Map) {
        Map subMap = (Map) obj;
        Node startNode = getNodeAtPath(document, parentNode, key, 0, d);
        doDebug(d, "descending into Map path=" + key + " startNode=" + nodeToString(startNode));
        iterateMap(document, startNode, subMap, d);
        doDebug(d, "back from descent Map path=" + key + " startNode=" + nodeToString(startNode));
      } else if (obj instanceof List) {
        List lst = (List) obj;
        doDebug(d, "Have a list that is this long " + lst.size());
        Iterator listIter = lst.iterator();
        int newPos = 0;
        while (listIter.hasNext()) {
          Object listObj = listIter.next();
          doDebug(d, "Processing List element@" + newPos + " " + listObj.getClass().getName());
          if (listObj instanceof String) {
            storeInDom(document, parentNode, key, (String) obj, newPos, d);
            newPos++;
          }
          if (listObj instanceof Map) {
            Map subMap = (Map) listObj;
            doDebug(d, "Retrieving key from  List-Map path=" + key + "@" + newPos);
            Node startNode = getNodeAtPath(document, parentNode, key, newPos, d);
            doDebug(
                d,
                "descending into List-Map path="
                    + key
                    + "@"
                    + newPos
                    + " startNode="
                    + nodeToString(startNode));
            iterateMap(document, startNode, subMap, d);
            doDebug(
                d,
                "back from descent List-Map path="
                    + key
                    + "@"
                    + newPos
                    + " startNode="
                    + nodeToString(startNode));
            newPos++;
          } else {
            System.out.println(
                "XMLMap Encountered an object of type "
                    + obj.getClass().getName()
                    + " in a List which should contain only Map objects");
          }
        }
      } else {
        doDebug(d, "Found a " + obj.getClass().getName() + " do not know how to iterate.");
      }
    }
    d--;
    doDebug(d, "< IterateMap parentNode = " + nodeToString(parentNode));
  }