Example #1
0
  protected Map<String, Object> parseResponse(String response) {
    if (debug) {
      Debug.logInfo("Raw Response : " + response, module);
    }

    // covert to all lowercase and trim off the html header
    String subResponse = response.toLowerCase();
    int firstIndex = subResponse.indexOf("<tr>");
    int lastIndex = subResponse.lastIndexOf("</tr>");
    subResponse = subResponse.substring(firstIndex, lastIndex);

    // check for a history table
    String history = null;
    List<Map<String, String>> historyMapList = null;
    if (subResponse.indexOf("<table") > -1) {
      int startHistory = subResponse.indexOf("<table");
      int endHistory = subResponse.indexOf("</table>") + 8;
      history = subResponse.substring(startHistory, endHistory);

      // replace the subResponse string so it doesn't conflict
      subResponse = StringUtil.replaceString(subResponse, history, "[_HISTORY_]");

      // parse the history into a list of maps
      historyMapList = this.parseHistoryResponse(history);
    }

    // replace all end rows with | this is the name delimiter
    subResponse = StringUtil.replaceString(subResponse, "</tr>", "|");

    // replace all </TD><TD> with = this is the value delimiter
    subResponse = StringUtil.replaceString(subResponse, "</td><td>", "=");

    // clean off a bunch of other useless stuff
    subResponse = StringUtil.replaceString(subResponse, "<tr>", "");
    subResponse = StringUtil.replaceString(subResponse, "<td>", "");
    subResponse = StringUtil.replaceString(subResponse, "</td>", "");

    // make the map
    Map<String, Object> responseMap = FastMap.newInstance();
    responseMap.putAll(StringUtil.strToMap(subResponse, true));

    // add the raw html back in just in case we need it later
    responseMap.put("_rawHtmlResponse", response);

    // if we have a history add it back in
    if (history != null) {
      responseMap.put("_rawHistoryHtml", history);
      responseMap.put("history", historyMapList);
    }

    if (debug) {
      Debug.logInfo("Response Map : " + responseMap, module);
    }

    return responseMap;
  }
Example #2
0
  private List<Map<String, String>> parseHistoryResponse(String response) {
    if (debug) {
      Debug.logInfo("Raw History : " + response, module);
    }

    // covert to all lowercase and trim off the html header
    String subResponse = response.toLowerCase();
    int firstIndex = subResponse.indexOf("<tr>");
    int lastIndex = subResponse.lastIndexOf("</tr>");
    subResponse = subResponse.substring(firstIndex, lastIndex);

    // clean up the html and replace the delimiters with '|'
    subResponse = StringUtil.replaceString(subResponse, "<td>", "");
    subResponse = StringUtil.replaceString(subResponse, "</td>", "|");

    // test the string to make sure we have fields to parse
    String testResponse = StringUtil.replaceString(subResponse, "<tr>", "");
    testResponse = StringUtil.replaceString(testResponse, "</tr>", "");
    testResponse = StringUtil.replaceString(testResponse, "|", "");
    testResponse = testResponse.trim();
    if (testResponse.length() == 0) {
      if (debug) {
        Debug.logInfo("History did not contain any fields, returning null", module);
      }
      return null;
    }

    // break up the keys from the values
    int valueStart = subResponse.indexOf("</tr>");
    String keys = subResponse.substring(4, valueStart - 1);
    String values = subResponse.substring(valueStart + 9, subResponse.length() - 6);

    // split sets of values up
    values = StringUtil.replaceString(values, "|</tr><tr>", "&");
    List<String> valueList = StringUtil.split(values, "&");

    // create a List of Maps for each set of values
    List<Map<String, String>> valueMap = FastList.newInstance();
    for (int i = 0; i < valueList.size(); i++) {
      valueMap.add(
          StringUtil.createMap(
              StringUtil.split(keys, "|"), StringUtil.split(valueList.get(i), "|")));
    }

    if (debug) {
      Debug.logInfo("History Map : " + valueMap, module);
    }

    return valueMap;
  }