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 *
023 * <h5 class='section'>Notes:</h5><ul>
024 *    <li class='note'>This class is thread safe and reusable.
025 * </ul>
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.HttpPartSerializersParsers">HTTP Part Serializers and Parsers</a>
029 * </ul>
030 */
031public abstract class BaseHttpPartParser extends BeanContextable implements HttpPartParser {
032
033   //-------------------------------------------------------------------------------------------------------------------
034   // Builder
035   //-------------------------------------------------------------------------------------------------------------------
036
037   /**
038    * Builder class.
039    */
040   public abstract static class Builder extends BeanContextable.Builder {
041
042      /**
043       * Constructor.
044       */
045      protected Builder() {
046      }
047
048      /**
049       * Copy constructor.
050       *
051       * @param builder The builder to copy.
052       */
053      protected Builder(Builder builder) {
054         super(builder);
055      }
056   }
057
058   //-------------------------------------------------------------------------------------------------------------------
059   // Instance
060   //-------------------------------------------------------------------------------------------------------------------
061
062   /**
063    * Constructor.
064    *
065    * @param builder The builder for this object.
066    */
067   protected BaseHttpPartParser(Builder builder) {
068      super(builder);
069   }
070
071   /**
072    * Converts the specified input to the specified class type.
073    *
074    * @param <T> The POJO type to transform the input into.
075    * @param partType The part type being parsed.
076    * @param schema
077    *    Schema information about the part.
078    *    <br>May be <jk>null</jk>.
079    *    <br>Not all part parsers use the schema information.
080    * @param in The input being parsed.
081    * @param toType The POJO type to transform the input into.
082    * @return The parsed value.
083    * @throws ParseException Malformed input encountered.
084    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
085    */
086   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, ClassMeta<T> toType) throws ParseException, SchemaValidationException {
087      return getPartSession().parse(partType, schema, in, toType);
088   }
089
090   /**
091    * Converts the specified input to the specified class type.
092    *
093    * @param <T> The POJO type to transform the input into.
094    * @param partType The part type being parsed.
095    * @param schema
096    *    Schema information about the part.
097    *    <br>May be <jk>null</jk>.
098    *    <br>Not all part parsers use the schema information.
099    * @param in The input being parsed.
100    * @param toType The POJO type to transform the input into.
101    * @return The parsed value.
102    * @throws ParseException Malformed input encountered.
103    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
104    */
105   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Class<T> toType) throws ParseException, SchemaValidationException {
106      return getPartSession().parse(partType, schema, in, getClassMeta(toType));
107   }
108
109   /**
110    * Converts the specified input to the specified class type.
111    *
112    * @param <T> The POJO type to transform the input into.
113    * @param partType The part type being parsed.
114    * @param schema
115    *    Schema information about the part.
116    *    <br>May be <jk>null</jk>.
117    *    <br>Not all part parsers use the schema information.
118    * @param in The input being parsed.
119    * @param toType The POJO type to transform the input into.
120    * @param toTypeArgs The generic type arguments of the POJO type to transform the input into.
121    * @return The parsed value.
122    * @throws ParseException Malformed input encountered.
123    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
124    */
125   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Type toType, Type...toTypeArgs) throws ParseException, SchemaValidationException {
126      return getPartSession().parse(partType, schema, in, getClassMeta(toType, toTypeArgs));
127   }
128
129   @Override /* HttpPartParser */
130   public <T> ClassMeta<T> getClassMeta(Class<T> c) {
131      return BeanContext.DEFAULT.getClassMeta(c);
132   }
133
134   @Override /* HttpPartParser */
135   public <T> ClassMeta<T> getClassMeta(Type t, Type...args) {
136      return BeanContext.DEFAULT.getClassMeta(t, args);
137   }
138}