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.*;
018import org.apache.juneau.parser.*;
019
020/**
021 * Base class for implementations of {@link HttpPartParser}
022 */
023public abstract class BaseHttpPartParser implements HttpPartParser {
024
025   /**
026    * Converts the specified input to the specified class type.
027    *
028    * @param partType The part type being parsed.
029    * @param schema
030    *    Schema information about the part.
031    *    <br>May be <jk>null</jk>.
032    *    <br>Not all part parsers use the schema information.
033    * @param in The input being parsed.
034    * @param toType The POJO type to transform the input into.
035    * @return The parsed value.
036    * @throws ParseException Malformed input encountered.
037    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
038    */
039   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, ClassMeta<T> toType) throws ParseException, SchemaValidationException {
040      return createPartSession(null).parse(partType, schema, in, toType);
041   }
042
043   /**
044    * Converts the specified input to the specified class type.
045    *
046    * @param partType The part type being parsed.
047    * @param schema
048    *    Schema information about the part.
049    *    <br>May be <jk>null</jk>.
050    *    <br>Not all part parsers use the schema information.
051    * @param in The input being parsed.
052    * @param toType The POJO type to transform the input into.
053    * @return The parsed value.
054    * @throws ParseException Malformed input encountered.
055    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
056    */
057   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Class<T> toType) throws ParseException, SchemaValidationException {
058      return createPartSession(null).parse(partType, schema, in, getClassMeta(toType));
059   }
060
061   /**
062    * Converts the specified input to the specified class type.
063    *
064    * @param partType The part type being parsed.
065    * @param schema
066    *    Schema information about the part.
067    *    <br>May be <jk>null</jk>.
068    *    <br>Not all part parsers use the schema information.
069    * @param in The input being parsed.
070    * @param toType The POJO type to transform the input into.
071    * @param toTypeArgs The generic type arguments of the POJO type to transform the input into.
072    * @return The parsed value.
073    * @throws ParseException Malformed input encountered.
074    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
075    */
076   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Type toType, Type...toTypeArgs) throws ParseException, SchemaValidationException {
077      return createPartSession(null).parse(partType, schema, in, getClassMeta(toType, toTypeArgs));
078   }
079
080   @Override /* HttpPartParser */
081   public <T> ClassMeta<T> getClassMeta(Class<T> c) {
082      return BeanContext.DEFAULT.getClassMeta(c);
083   }
084
085   @Override /* HttpPartParser */
086   public <T> ClassMeta<T> getClassMeta(Type t, Type...args) {
087      return BeanContext.DEFAULT.getClassMeta(t, args);
088   }
089}