001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.httppart;
018
019import java.lang.reflect.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.parser.*;
023
024/**
025 * Base class for implementations of {@link HttpPartParser}
026 *
027 * <h5 class='section'>Notes:</h5><ul>
028 *    <li class='note'>This class is thread safe and reusable.
029 * </ul>
030 *
031 * <h5 class='section'>See Also:</h5><ul>
032 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HttpPartSerializersParsers">HTTP Part Serializers and Parsers</a>
033 * </ul>
034 */
035public abstract class BaseHttpPartParser extends BeanContextable implements HttpPartParser {
036
037   //-------------------------------------------------------------------------------------------------------------------
038   // Builder
039   //-------------------------------------------------------------------------------------------------------------------
040
041   /**
042    * Builder class.
043    */
044   public abstract static class Builder extends BeanContextable.Builder {
045
046      /**
047       * Constructor.
048       */
049      protected Builder() {
050      }
051
052      /**
053       * Copy constructor.
054       *
055       * @param builder The builder to copy.
056       */
057      protected Builder(Builder builder) {
058         super(builder);
059      }
060   }
061
062   //-------------------------------------------------------------------------------------------------------------------
063   // Instance
064   //-------------------------------------------------------------------------------------------------------------------
065
066   /**
067    * Constructor.
068    *
069    * @param builder The builder for this object.
070    */
071   protected BaseHttpPartParser(Builder builder) {
072      super(builder);
073   }
074
075   /**
076    * Converts the specified input to the specified class type.
077    *
078    * @param <T> The POJO type to transform the input into.
079    * @param partType The part type being parsed.
080    * @param schema
081    *    Schema information about the part.
082    *    <br>May be <jk>null</jk>.
083    *    <br>Not all part parsers use the schema information.
084    * @param in The input being parsed.
085    * @param toType The POJO type to transform the input into.
086    * @return The parsed value.
087    * @throws ParseException Malformed input encountered.
088    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
089    */
090   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, ClassMeta<T> toType) throws ParseException, SchemaValidationException {
091      return getPartSession().parse(partType, schema, in, toType);
092   }
093
094   /**
095    * Converts the specified input to the specified class type.
096    *
097    * @param <T> The POJO type to transform the input into.
098    * @param partType The part type being parsed.
099    * @param schema
100    *    Schema information about the part.
101    *    <br>May be <jk>null</jk>.
102    *    <br>Not all part parsers use the schema information.
103    * @param in The input being parsed.
104    * @param toType The POJO type to transform the input into.
105    * @return The parsed value.
106    * @throws ParseException Malformed input encountered.
107    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
108    */
109   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Class<T> toType) throws ParseException, SchemaValidationException {
110      return getPartSession().parse(partType, schema, in, getClassMeta(toType));
111   }
112
113   /**
114    * Converts the specified input to the specified class type.
115    *
116    * @param <T> The POJO type to transform the input into.
117    * @param partType The part type being parsed.
118    * @param schema
119    *    Schema information about the part.
120    *    <br>May be <jk>null</jk>.
121    *    <br>Not all part parsers use the schema information.
122    * @param in The input being parsed.
123    * @param toType The POJO type to transform the input into.
124    * @param toTypeArgs The generic type arguments of the POJO type to transform the input into.
125    * @return The parsed value.
126    * @throws ParseException Malformed input encountered.
127    * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation.
128    */
129   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Type toType, Type...toTypeArgs) throws ParseException, SchemaValidationException {
130      return getPartSession().parse(partType, schema, in, getClassMeta(toType, toTypeArgs));
131   }
132
133   @Override /* HttpPartParser */
134   public <T> ClassMeta<T> getClassMeta(Class<T> c) {
135      return BeanContext.DEFAULT.getClassMeta(c);
136   }
137
138   @Override /* HttpPartParser */
139   public <T> ClassMeta<T> getClassMeta(Type t, Type...args) {
140      return BeanContext.DEFAULT.getClassMeta(t, args);
141   }
142}