String searchResultToXml(FieldSearchResult result) {
    StringBuffer xmlBuf = new StringBuffer();

    xmlBuf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    xmlBuf.append("<result xmlns=\"http://www.fedora.info/definitions/1/0/types/\">\n");
    if ((result != null) && (result.getToken() != null)) {
      xmlBuf.append("  <listSession>\n");
      xmlBuf.append("    <token>" + result.getToken() + "</token>\n");
      if (result.getCursor() != -1) {
        xmlBuf.append("    <cursor>" + result.getCursor() + "</cursor>\n");
      }
      if (result.getCompleteListSize() != -1) {
        xmlBuf.append(
            "    <completeListSize>" + result.getCompleteListSize() + "</completeListSize>\n");
      }
      if (result.getExpirationDate() != null) {
        xmlBuf.append(
            "    <expirationDate>"
                + DateUtility.convertDateToString(result.getExpirationDate())
                + "</expirationDate>\n");
      }
      xmlBuf.append("  </listSession>\n");
    }
    xmlBuf.append("  <resultList>\n");

    if (result != null) {
      List<ObjectFields> fieldList = result.objectFieldsList();

      for (int i = 0; i < fieldList.size(); i++) {
        ObjectFields f = fieldList.get(i);
        xmlBuf.append("  <objectFields>\n");
        appendXML("pid", f.getPid(), xmlBuf);
        appendXML("label", f.getLabel(), xmlBuf);
        appendXML("state", f.getState(), xmlBuf);
        appendXML("ownerId", f.getOwnerId(), xmlBuf);
        appendXML("cDate", f.getCDate(), xmlBuf);
        appendXML("mDate", f.getMDate(), xmlBuf);
        appendXML("dcmDate", f.getDCMDate(), xmlBuf);
        appendXML("title", f.titles(), xmlBuf);
        appendXML("creator", f.creators(), xmlBuf);
        appendXML("subject", f.subjects(), xmlBuf);
        appendXML("description", f.descriptions(), xmlBuf);
        appendXML("publisher", f.publishers(), xmlBuf);
        appendXML("contributor", f.contributors(), xmlBuf);
        appendXML("date", f.dates(), xmlBuf);
        appendXML("type", f.types(), xmlBuf);
        appendXML("format", f.formats(), xmlBuf);
        appendXML("identifier", f.identifiers(), xmlBuf);
        appendXML("source", f.sources(), xmlBuf);
        appendXML("language", f.languages(), xmlBuf);
        appendXML("relation", f.relations(), xmlBuf);
        appendXML("coverage", f.coverages(), xmlBuf);
        appendXML("rights", f.rights(), xmlBuf);
        xmlBuf.append("  </objectFields>\n");
      }
    }
    xmlBuf.append("  </resultList>\n");
    xmlBuf.append("</result>\n");

    return xmlBuf.toString();
  }
  String searchResultToHtml(
      String query,
      String terms,
      String[] searchableFields,
      String[] wantedFields,
      int maxResults,
      FieldSearchResult result) {
    StringBuffer html = new StringBuffer();
    HashSet<String> fieldHash = new HashSet<String>();

    if (wantedFields != null) {
      for (String wf : wantedFields) {
        fieldHash.add(wf);
      }
    }

    html.append("<html><head><title>Search Repository</title></head>");
    html.append("<body><center>");
    html.append("<table width=\"784\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
    html.append(
        "<tr><td width=\"141\" height=\"134\" valign=\"top\"><img src=\""
            + "/"
            + fedoraAppServerContext
            + "/images/newlogo2.jpg\" width=\"141\" height=\"134\"/></td>");
    html.append("<td width=\"643\" valign=\"top\">");
    html.append("<center><h2>Fedora Repository</h2>");
    html.append("<h3>Find Objects</h3>");
    html.append("</center></td></tr></table>");
    html.append("\n");

    html.append("<form method=\"get\">");
    html.append("<center><table border=0 cellpadding=6 cellspacing=0>\n");
    html.append("<tr><td colspan=3 valign=top><i>Fields to display:</i></td><td></td></tr>");
    html.append("<tr>");

    int fieldPerCol = (searchableFields.length / 3) + 1;
    for (int i = 0; i < searchableFields.length; i++) {
      boolean everOtherNFields = (i % fieldPerCol == 0);

      if (everOtherNFields) {
        if (i > 0) {
          html.append("</font></td>");
          html.append("\n");
        }

        html.append("<td valign=top><font size=-1>");
        html.append("\n");
      }

      html.append(
          "<input type='checkbox' name='"
              + searchableFields[i]
              + "' value='true' "
              + ((RestParam.PID.equals(searchableFields[i])
                      || "title".equals(searchableFields[i])
                      || fieldHash.contains(searchableFields[i]))
                  ? "checked"
                  : "")
              + "> <a href='#'>"
              + searchableFields[i]
              + "</a><br>");
      html.append("\n");
    }
    html.append("</font></td><td bgcolor=silver valign=top>&nbsp;&nbsp;&nbsp;</td><td valign=top>");
    html.append(
        "Search all fields for phrase: <input type=\"text\" name=\"terms\" size=\"15\" value=\""
            + (terms == null ? "" : StreamUtility.enc(terms))
            + "\"> <a href=\"#\" onClick=\"javascript:alert('Search All Fields\\n\\nEnter a phrase.  Objects where any field contains the phrase will be returned.\\nThis is a case-insensitive search, and you may use the * or ? wildcards.\\n\\nExamples:\\n\\n  *o*\\n    finds objects where any field contains the letter o.\\n\\n  ?edora\\n    finds objects where a word starts with any letter and ends with edora.')\"><i>help</i></a><p> ");
    html.append(
        "Or search specific field(s): <input type=\"text\" name=\"query\" size=\"15\" value=\""
            + (query == null ? "" : StreamUtility.enc(query))
            + "\"> <a href=\"#\" onClick=\"javascript:alert('Search Specific Field(s)\\n\\nEnter one or more conditions, separated by space.  Objects matching all conditions will be returned.\\nA condition is a field (choose from the field names on the left) followed by an operator, followed by a value.\\nThe = operator will match if the field\\'s entire value matches the value given.\\nThe ~ operator will match on phrases within fields, and accepts the ? and * wildcards.\\nThe &lt;, &gt;, &lt;=, and &gt;= operators can be used with numeric values, such as dates.\\n\\nExamples:\\n\\n  pid~demo:* description~fedora\\n    Matches all demo objects with a description containing the word fedora.\\n\\n  cDate&gt;=1976-03-04 creator~*n*\\n    Matches objects created on or after March 4th, 1976 where at least one of the creators has an n in their name.\\n\\n  mDate&gt;2002-10-2 mDate&lt;2002-10-2T12:00:00\\n    Matches objects modified sometime before noon (UTC) on October 2nd, 2002')\"><i>help</i></a><p> ");
    html.append(
        "Maximum Results: <select name=\"maxResults\"><option value=\"20\">20</option><option value=\"40\">40</option><option value=\"60\">60</option><option value=\"80\">80</option></select> ");
    html.append("<p><input type=\"submit\" value=\"Search\"> ");
    html.append("</td></tr></table></center>");
    html.append("</form><hr size=1>");

    if (result != null) {
      List<ObjectFields> objectFieldList = result.objectFieldsList();

      html.append(
          "<center><table width=\"90%\" border=\"1\" cellpadding=\"5\" cellspacing=\"5\" bgcolor=\"silver\">\n");
      html.append("<tr>");
      for (int i = 0; i < wantedFields.length; i++) {
        html.append("<td valign=\"top\"><strong>");
        html.append(wantedFields[i]);
        html.append("</strong></td>");
      }
      html.append("</tr>");

      for (int i = 0; i < objectFieldList.size(); i++) {
        ObjectFields f = objectFieldList.get(i);
        html.append("<tr>");
        for (int j = 0; j < wantedFields.length; j++) {
          String l = wantedFields[j];
          html.append("<td valign=\"top\">");
          if (l.equalsIgnoreCase("pid")) {
            html.append("<a href=\"objects/");
            html.append(f.getPid());
            html.append("\">");
            html.append(f.getPid());
            html.append("</a>");
          } else if (l.equalsIgnoreCase("label")) {
            if (f.getLabel() != null) {
              html.append(StreamUtility.enc(f.getLabel()));
            }
          } else if (l.equalsIgnoreCase("state")) {
            html.append(f.getState());
          } else if (l.equalsIgnoreCase("ownerId")) {
            if (f.getOwnerId() != null) {
              html.append(f.getOwnerId());
            }
          } else if (l.equalsIgnoreCase("cDate")) {
            html.append(DateUtility.convertDateToString(f.getCDate()));
          } else if (l.equalsIgnoreCase("mDate")) {
            html.append(DateUtility.convertDateToString(f.getMDate()));
          } else if (l.equalsIgnoreCase("dcmDate")) {
            if (f.getDCMDate() != null) {
              html.append(DateUtility.convertDateToString(f.getDCMDate()));
            }
          } else if (l.equalsIgnoreCase("title")) {
            html.append(join(f.titles()));
          } else if (l.equalsIgnoreCase("creator")) {
            html.append(join(f.creators()));
          } else if (l.equalsIgnoreCase("subject")) {
            html.append(join(f.subjects()));
          } else if (l.equalsIgnoreCase("description")) {
            html.append(join(f.descriptions()));
          } else if (l.equalsIgnoreCase("publisher")) {
            html.append(join(f.publishers()));
          } else if (l.equalsIgnoreCase("contributor")) {
            html.append(join(f.contributors()));
          } else if (l.equalsIgnoreCase("date")) {
            html.append(join(f.dates()));
          } else if (l.equalsIgnoreCase("type")) {
            html.append(join(f.types()));
          } else if (l.equalsIgnoreCase("format")) {
            html.append(join(f.formats()));
          } else if (l.equalsIgnoreCase("identifier")) {
            html.append(join(f.identifiers()));
          } else if (l.equalsIgnoreCase("source")) {
            html.append(join(f.sources()));
          } else if (l.equalsIgnoreCase("language")) {
            html.append(join(f.languages()));
          } else if (l.equalsIgnoreCase("relation")) {
            html.append(join(f.relations()));
          } else if (l.equalsIgnoreCase("coverage")) {
            html.append(join(f.coverages()));
          } else if (l.equalsIgnoreCase("rights")) {
            html.append(join(f.rights()));
          }
          html.append("</td>");
          html.append("\n");
        }
        html.append("</tr>");
        html.append("\n");
        html.append("<tr><td colspan=\"");
        html.append(wantedFields.length);
        html.append("\"></td></tr>");
        html.append("\n");
      }
      html.append("</table>");
      html.append("\n");

      if (result != null && result.getToken() != null) {
        if (result.getCursor() != -1) {
          long viewingStart = result.getCursor() + 1;
          long viewingEnd = result.objectFieldsList().size() + viewingStart - 1;
          html.append("<p>Viewing results " + viewingStart + " to " + viewingEnd);
          if (result.getCompleteListSize() != -1) {
            html.append(" of " + result.getCompleteListSize());
          }
          html.append("</p>\n");
        }
        html.append("<form method=\"get\" action=\"\">");
        html.append("\n");
        for (String field : wantedFields) {
          html.append("<input type=\"hidden\" name=\"" + field + "\" value=\"true\">");
          html.append("\n");
        }

        html.append("\n");
        html.append(
            "\n<input type=\"hidden\" name=\"sessionToken\" value=\""
                + result.getToken()
                + "\">\n");
        html.append("\n<input type=\"hidden\" name=\"maxResults\" value=\"" + maxResults + "\">\n");
        html.append("<input type=\"submit\" value=\"More Results &gt;\"></form>");
      }
      html.append("</center>\n");
    }

    html.append("</center>");
    html.append("</body>");
    html.append("</html>");

    return html.toString();
  }