001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.httppart;
014
015import org.apache.juneau.*;
016import org.apache.juneau.internal.*;
017import org.apache.juneau.parser.*;
018
019/**
020 * An implementation of {@link HttpPartParser} that takes in the strings and tries to convert them to POJOs using constructors and static create methods.
021 * 
022 * <p>
023 * The class being created must be one of the following in order to convert it from a string:
024 * 
025 * <ul>
026 *    <li>
027 *       An <jk>enum</jk>.
028 *    <li>
029 *       Have a public constructor with a single <code>String</code> parameter.
030 *    <li>
031 *       Have one of the following public static methods that takes in a single <code>String</code> parameter: 
032 *       <ul>
033 *          <li><code>fromString</code>
034 *          <li><code>fromValue</code>
035 *          <li><code>valueOf</code>
036 *          <li><code>parse</code>
037 *          <li><code>parseString</code>
038 *          <li><code>forName</code>
039 *          <li><code>forString</code>
040 *    </ul>
041 * </ul>
042 */
043public class SimplePartParser implements HttpPartParser {
044
045   //-------------------------------------------------------------------------------------------------------------------
046   // Predefined instances
047   //-------------------------------------------------------------------------------------------------------------------
048
049   /** Reusable instance of {@link SimplePartParser}, all default settings. */
050   public static final SimplePartParser DEFAULT = new SimplePartParser();
051
052
053   //-------------------------------------------------------------------------------------------------------------------
054   // Instance
055   //-------------------------------------------------------------------------------------------------------------------
056
057   @Override
058   public <T> T parse(HttpPartType partType, String in, ClassMeta<T> type) throws ParseException {
059      return ClassUtils.fromString(type.getInnerClass(), in);
060   }
061}