public static void bindDictionaryToElement(Dictionary dict, Document doc, Element element) {
    for (Map.Entry<String, Object> entry : dict) {
      String k = entry.getKey();
      Object v = entry.getValue();

      Element key = doc.createElement("key");
      key.appendChild(doc.createTextNode(k));
      element.appendChild(key);

      if (v instanceof String) {
        Element value = doc.createElement("string");
        value.appendChild(doc.createTextNode(v.toString()));
        element.appendChild(value);
      } else if (v instanceof Integer) {
        Element value = doc.createElement("integer");
        value.appendChild(doc.createTextNode(((Integer) v).toString()));
        element.appendChild(value);
      } else if (v instanceof Boolean) {
        Element value = doc.createElement("boolean");
        value.appendChild(doc.createTextNode(((Boolean) v).toString()));
        element.appendChild(value);
      } else if (v instanceof List<?>) {
        Element value = doc.createElement("list");
        ListParser.bindListToElement((List<Object>) v, doc, value);
        element.appendChild(value);
      } else if (v instanceof Dictionary) {
        Element value = doc.createElement("dict");
        bindDictionaryToElement((Dictionary) v, doc, value);
        element.appendChild(value);
      }
    }
  }
Пример #2
0
 protected static void parseValue(
     exprType expr, ArrayList<String> lines, int indent, int maxLength) {
   String result = "";
   if (expr instanceof Num) {
     result = ((Num) expr).num;
     lines.add(result);
   } else if (expr instanceof Str) {
     result = ((Str) expr).s;
     lines.add(result);
   } else if (expr instanceof Name) {
     result = ((Name) expr).id;
     lines.add(result);
   } else if (expr instanceof Dict) {
     ArrayList<Paragraph> sublist = new ArrayList<Paragraph>();
     parse((Dict) expr, sublist, indent + 2, maxLength);
     for (String subline : sublist.get(0).getLines()) {
       lines.add(subline);
     }
   } else if (expr instanceof List) {
     ArrayList<Paragraph> sublist = new ArrayList<Paragraph>();
     ListParser.parse((List) expr, sublist, indent + 4, maxLength);
     for (String subline : sublist.get(0).getLines()) {
       lines.add(subline);
     }
   } else {
     System.err.println("[DICTVAL] Unable to parse element " + expr);
     result = "<?>";
   }
 }
  public static Dictionary getDictionaryFromElement(Element element) {
    Dictionary ret = new Dictionary();

    NodeList l = element.getChildNodes();
    String lastKey = "";

    for (int i = 0; i < l.getLength(); ++i) {
      Node node = l.item(i);

      if ("key".equalsIgnoreCase(node.getNodeName())) {
        lastKey = node.getTextContent();
        continue;
      } else if ("string".equalsIgnoreCase(node.getNodeName())) {
        if (!lastKey.equals("")) {
          ret.put(lastKey, node.getTextContent());
        }

        continue;
      } else if ("integer".equalsIgnoreCase(node.getNodeName())) {
        if (!lastKey.equals("")) {
          ret.put(lastKey, Integer.parseInt(node.getTextContent()));
        }

        continue;
      } else if ("boolean".equalsIgnoreCase(node.getNodeName())) {
        if (!lastKey.equals("")) {
          ret.put(lastKey, Boolean.parseBoolean(node.getTextContent()));
        }

        continue;
      } else if ("list".equalsIgnoreCase(node.getNodeName())) {
        if (!lastKey.equals("")) {
          ret.put(lastKey, ListParser.getListFromElement((Element) node));
        }

        continue;
      } else if ("dict".equalsIgnoreCase(node.getNodeName())) {
        if (!lastKey.equals("")) {
          ret.put(lastKey, getDictionaryFromElement((Element) node));
        }

        continue;
      }
    }

    return ret;
  }