001    package org.apache.myfaces.tobago.util;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    /*
021     * Date: Oct 30, 2006
022     * Time: 10:26:27 PM
023     */
024    public class ContentType {
025      private String primaryType;
026      private String subType;
027    
028      public ContentType(String contentType) {
029        parse(contentType);
030      }
031    
032      private void parse(String contentType) {
033        // TODO parse Parameter
034        String[] values = contentType.split("/");
035        if (values.length == 2) {
036          primaryType = values[0];
037          subType = values[1];
038        } else {
039          throw new IllegalArgumentException("ContentType '" + contentType + "' not recognized.");
040        }
041      }
042    
043      public String getPrimaryType() {
044        return primaryType;
045      }
046    
047      public String getSubType() {
048        return subType;
049      }
050    
051      public boolean match(ContentType contentType) {
052        return primaryType.equalsIgnoreCase(contentType.getPrimaryType())
053            && ("*".equals(subType) || subType.equalsIgnoreCase(contentType.getSubType()));
054      }
055    
056      public String toString() {
057        return primaryType + "/" + subType;
058      }
059    
060      public static ContentType valueOf(String s) {
061        return new ContentType(s);
062      }
063    }