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         super();
047      }
048
049      /**
050       * Copy constructor.
051       *
052       * @param builder The builder to copy.
053       */
054      protected Builder(Builder builder) {
055         super(builder);
056      }
057   }
058
059   //-------------------------------------------------------------------------------------------------------------------
060   // Instance
061   //-------------------------------------------------------------------------------------------------------------------
062
063   /**
064    * Constructor.
065    *
066    * @param builder The builder for this object.
067    */
068   protected BaseHttpPartParser(Builder builder) {
069      super(builder);
070   }
071
072   /**
073    * Converts the specified input to the specified class type.
074    *
075    * @param <T> The POJO type to transform the input into.
076    * @param partType The part type being parsed.
077    * @param schema
078    *    Schema information about the part.
079    *    <br>May be <jk>null</jk>.
080    *    <br>Not all part parsers use the schema information.
081    * @param in The input being parsed.
082    * @param toType The POJO type to transform the input into.
083    * @return The parsed value.
084    * @throws ParseException Malformed input encountered.
085    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
086    */
087   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, ClassMeta<T> toType) throws ParseException, SchemaValidationException {
088      return getPartSession().parse(partType, schema, in, toType);
089   }
090
091   /**
092    * Converts the specified input to the specified class type.
093    *
094    * @param <T> The POJO type to transform the input into.
095    * @param partType The part type being parsed.
096    * @param schema
097    *    Schema information about the part.
098    *    <br>May be <jk>null</jk>.
099    *    <br>Not all part parsers use the schema information.
100    * @param in The input being parsed.
101    * @param toType The POJO type to transform the input into.
102    * @return The parsed value.
103    * @throws ParseException Malformed input encountered.
104    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
105    */
106   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Class<T> toType) throws ParseException, SchemaValidationException {
107      return getPartSession().parse(partType, schema, in, getClassMeta(toType));
108   }
109
110   /**
111    * Converts the specified input to the specified class type.
112    *
113    * @param <T> The POJO type to transform the input into.
114    * @param partType The part type being parsed.
115    * @param schema
116    *    Schema information about the part.
117    *    <br>May be <jk>null</jk>.
118    *    <br>Not all part parsers use the schema information.
119    * @param in The input being parsed.
120    * @param toType The POJO type to transform the input into.
121    * @param toTypeArgs The generic type arguments of the POJO type to transform the input into.
122    * @return The parsed value.
123    * @throws ParseException Malformed input encountered.
124    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
125    */
126   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Type toType, Type...toTypeArgs) throws ParseException, SchemaValidationException {
127      return getPartSession().parse(partType, schema, in, getClassMeta(toType, toTypeArgs));
128   }
129
130   @Override /* HttpPartParser */
131   public <T> ClassMeta<T> getClassMeta(Class<T> c) {
132      return BeanContext.DEFAULT.getClassMeta(c);
133   }
134
135   @Override /* HttpPartParser */
136   public <T> ClassMeta<T> getClassMeta(Type t, Type...args) {
137      return BeanContext.DEFAULT.getClassMeta(t, args);
138   }
139}