/*
 * Copyright 2012-2017 MarkLogic Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.marklogic.client.io;

import com.marklogic.client.MarkLogicInternalException;

/**
 * The Format enumerates different kinds of document content.
 */
public enum Format {
  /**
   * Identifies the format of binary documents such as images.
   */
  BINARY,
  /**
   * Identifies the format of JSON documents.
   */
  JSON,
  /**
   * Identifies the format of text documents such as plain text and HTML.
   */
  TEXT,
  /**
   * Identifies the format of XML documents such as XHTML.
   */
  XML,
  /**
   * Used for documents with unknown or multiple formats.
   */
  UNKNOWN;
  /**
   * Returns the default mime type for the format.
   * @return	the default mime type
   */
  public String getDefaultMimetype() {
    switch(this) {
      case UNKNOWN:
        return null;
      case BINARY:
        // TODO: or possibly "application/x-unknown-content-type"
        return "application/octet-stream";
      case JSON:
        return "application/json";
      case TEXT:
        return "text/plain";
      case XML:
        return "application/xml";
      default:
        throw new MarkLogicInternalException("Unknown format "+this.toString());
    }
  }

  public static Format getFromMimetype(String mimeType) {
    if      ( mimeType == null ) return UNKNOWN;
    else if ( "application/xml".equals(mimeType) ) return XML;
    else if ( "text/xml".equals(mimeType) ) return XML;
    else if ( "application/json".equals(mimeType) ) return JSON;
    else if ( "text/xml".equals(mimeType) ) return JSON;
    else if ( "application/octet-stream".equals(mimeType) ) return BINARY;
    else if ( mimeType.startsWith("text/") ) return TEXT;
    else return UNKNOWN;
  }
}

@@ -1881,32 +1881,52 @@
 ;%0A    else if ( 
+mimeType.startsWith(
 %22application/xml
@@ -1926,32 +1926,16 @@
 ion/xml%22
-.equals(mimeType
 ) ) retu
@@ -1960,34 +1960,38 @@
 f ( 
-%22text/xml%22.equals(mimeType
+mimeType.startsWith(%22text/xml%22
 ) ) 
@@ -2008,32 +2008,52 @@
 ;%0A    else if ( 
+mimeType.startsWith(
 %22application/jso
@@ -2054,32 +2054,16 @@
 on/json%22
-.equals(mimeType
 ) ) retu
@@ -2089,34 +2089,38 @@
 f ( 
-%22text/xml%22.equals(mimeType
+mimeType.startsWith(%22text/xml%22
 ) ) 
@@ -2146,16 +2146,36 @@
 se if ( 
+mimeType.startsWith(
 %22applica
@@ -2196,24 +2196,8 @@
 eam%22
-.equals(mimeType
 ) ) 

fix #777 by loosening Format.getFromMimetype to only require mimeType to startWith the appropriate string -- allows for Content-Type headers that have a semicolon followed by charset