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 java.lang.reflect.*;
016
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 <c>String</c> parameter.
030 *    <li>
031 *       Have one of the following public static methods that takes in a single <c>String</c> parameter:
032 *       <ul>
033 *          <li><c>fromString</c>
034 *          <li><c>fromValue</c>
035 *          <li><c>valueOf</c>
036 *          <li><c>parse</c>
037 *          <li><c>parseString</c>
038 *          <li><c>forName</c>
039 *          <li><c>forString</c>
040 *    </ul>
041 * </ul>
042 */
043public class SimplePartParser extends BaseHttpPartParser {
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   // Instance
054   //-------------------------------------------------------------------------------------------------------------------
055
056   @Override
057   public SimplePartParserSession createPartSession(ParserSessionArgs args) {
058      return new SimplePartParserSession();
059   }
060
061   @Override /* HttpPartParser */
062   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Class<T> toType) throws ParseException, SchemaValidationException {
063      return createPartSession().parse(partType, schema, in, toType);
064   }
065
066   @Override /* HttpPartParser */
067   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Type toType, Type...toTypeArgs) throws ParseException, SchemaValidationException {
068      return createPartSession().parse(partType, schema, in, toType, toTypeArgs);
069   }
070}