Example #1
0
 /**
  * Returns the current serialization options.
  *
  * @param mth consider specified serialization method (may be {@code null})
  * @return options
  * @throws BaseXException database exception
  */
 private SerializerOptions options(final SerialMethod mth) throws BaseXException {
   final SerializerOptions sopts = new SerializerOptions();
   sopts.assign(params.getText());
   sopts.set(SerializerOptions.METHOD, SerialMethod.valueOf(method.getSelectedItem()));
   sopts.set(SerializerOptions.ENCODING, encoding.getSelectedItem());
   if (mth == SerialMethod.JSON) {
     final JsonSerialOptions jopts = new JsonSerialOptions();
     jopts.assign(mparams.getText());
     sopts.set(SerializerOptions.JSON, jopts);
   } else if (mth == SerialMethod.CSV) {
     final CsvOptions copts = new CsvOptions();
     copts.assign(mparams.getText());
     sopts.set(SerializerOptions.CSV, copts);
   }
   return sopts;
 }
Example #2
0
  /**
   * Returns the media type defined in the specified serialization parameters.
   *
   * @param sopts serialization parameters
   * @return media type
   */
  public static String mediaType(final SerializerOptions sopts) {
    // set content type
    final String type = sopts.get(SerializerOptions.MEDIA_TYPE);
    if (!type.isEmpty()) return type;

    // determine content type dependent on output method
    final SerialMethod sm = sopts.get(SerializerOptions.METHOD);
    if (sm == SerialMethod.RAW) return APP_OCTET;
    if (sm == SerialMethod.XML) return APP_XML;
    if (sm == SerialMethod.XHTML || sm == SerialMethod.HTML) return TEXT_HTML;
    if (sm == SerialMethod.JSON) {
      final JsonSerialOptions jprop = sopts.get(SerializerOptions.JSON);
      return jprop.get(JsonOptions.FORMAT) == JsonFormat.JSONML ? APP_JSONML : APP_JSON;
    }
    return TEXT_PLAIN;
  }