예제 #1
0
  private void serializeCollection(
      StringWriter writer, String nestedFieldName, MetaCollection field, UserData data) {
    int size, maxSize;
    String typeName = field.getTypeName();

    if ((typeName.equals("c_string"))
        || (typeName.equals("c_wstring"))
        || (typeName.startsWith("C_STRING<"))
        || (typeName.startsWith("C_WSTRING<"))) {
      this.serializeString(writer, nestedFieldName, data);
    } else { // Not a String collection.
      maxSize = field.getMaxSize();

      if (maxSize == 0) {
        this.serializeUnboundedSequence(writer, nestedFieldName, field, data);
      } else if (maxSize == -1) {
          /* recursive type */
        this.serializeRecursiveType(writer, nestedFieldName, field, data);
      } else {
        /* This is an array or bounded sequence. */
        if (typeName.startsWith("C_ARRAY")) {
          /* When dealing with an array, then the actual size
           * is the same as the maximum size. */
          size = maxSize;
        } else {
          /* When dealing with a bounded sequence, then the actual
           * size can differ from the maximum size. */
          size = getSequenceSize(nestedFieldName, field, data);
          /* Also, we need this size within the data. */
          writer.write("<size>" + size + "</size>");
        }
        /* Now, fill up the elements. */
        for (int i = 0; i < size; i++) {
          writer.write("<element>");
          this.serializeType(
              writer, getCollectionFieldName(nestedFieldName, i), field.getSubType(), data, false);
          writer.write("</element>");
        }
      }
    }
  }
예제 #2
0
  /**
   * Serializes the contents of the supplied collection.
   *
   * @param nestedFieldName The name of the collection field in the data.
   * @param colType The type of the collection.
   * @param data The data to serialize.
   * @return The serialized representation of the collection data.
   */
  private String getSerializedStringCollection(
      String nestedFieldName, MetaCollection colType, UserData data) {
    String result = null;
    String size = null;
    String typeName = colType.getTypeName();
    int index = nestedFieldName.indexOf('[');
    String temp = null;

    if ((typeName.equals("c_string"))
        || (typeName.equals("c_wstring"))
        || (typeName.startsWith("C_STRING<"))
        || (typeName.startsWith("C_WSTRING<"))) {
      String strData = data.getFieldValue(nestedFieldName);
      result = strData.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    } else if (colType.getSubType() instanceof MetaCollection) {
      result = "";

      if (colType.getSubType().getTypeName().startsWith("C_SEQUENCE<")) {
        size = "<size>" + ((MetaCollection) colType.getSubType()).getMaxSize() + "</size>";
      }

      if (index != -1) {
        temp = nestedFieldName.substring(0, index);

        for (int i = 0; i < colType.getMaxSize(); i++) {
          result += "<element>";

          if (size != null) {
            result += size;
          }
          result +=
              getSerializedStringCollection(
                  temp + "[" + i + "]" + nestedFieldName.substring(index),
                  (MetaCollection) colType.getSubType(),
                  data);
          result += "</element>";
        }
      } else {
        for (int i = 0; i < colType.getMaxSize(); i++) {
          result += "<element>";

          if (size != null) {
            result += size;
          }
          result +=
              getSerializedStringCollection(
                  nestedFieldName + "[" + i + "]", (MetaCollection) colType.getSubType(), data);
          result += "</element>";
        }
      }
    } else {
      result = "";

      if (index != -1) {
        temp = nestedFieldName.substring(0, index);

        for (int i = 0; i < colType.getMaxSize(); i++) {
          result += "<element>";
          result += data.getFieldValue(temp + "[" + i + "]" + nestedFieldName.substring(index));
          result += "</element>";
        }
      } else {
        for (int i = 0; i < colType.getMaxSize(); i++) {
          result += "<element>";
          result += data.getFieldValue(nestedFieldName + "[" + i + "]");
          result += "</element>";
        }
      }
    }
    return result;
  }