001package org.hl7.fhir.utilities;
002
003/*-
004 * #%L
005 * org.hl7.fhir.utilities
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029public class MimeType {
030
031  private String source;
032  private String base;
033  private Map<String, String> params = new HashMap<String, String>();
034
035  public MimeType(String s) {
036    source = s;
037    for (String p : s.split("\\;"))
038      if (base == null)
039        base = p;
040      else
041        params.put(p.substring(0, p.indexOf("=")), p.substring(p.indexOf("=")+1));
042    if ("xml".equals(base))
043      base = "application/fhir+xml";
044    if ("json".equals(base))
045      base = "application/fhir+json";
046    if ("ttl".equals(base))
047      base = "application/fhir+ttl";
048  }
049
050  public String main() {
051    if (base.contains("/"))
052      return base.substring(0, base.indexOf("/"));
053    else
054      return base;
055  }
056
057  public String sub() {
058    if (base.contains("/"))
059      return base.substring(base.indexOf("/")+1);
060    else
061      return null;
062  }
063
064  public boolean hasParam(String name) {
065    return params.containsKey(name);
066  }
067
068  public boolean isValid() {
069    return (Utilities.existsInList(main(), "application", "audio", "font", "example", "image", "message", "model", "multipart", "text", "video") || main().startsWith("x-")) && !Utilities.noString(sub());
070  }
071
072  public static List<MimeType> parseList(String s) {
073    List<MimeType> result = new ArrayList<MimeType>();
074    for (String e : s.split("\\,"))
075        result.add(new MimeType(e));
076    return result;
077  }
078
079  public String display() {
080    return source;
081  }
082
083}