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 org.apache.juneau.*;
020import org.apache.juneau.internal.*;
021import org.apache.juneau.utils.*;
022
023/**
024 * An implementation of {@link HttpPartParser} that takes in the strings and tries to convert them to POJOs using constructors and static create methods.
025 *
026 * <p>
027 * The class being created must be one of the following in order to convert it from a string:
028 *
029 * <ul>
030 *    <li>
031 *       An <jk>enum</jk>.
032 *    <li>
033 *       Have a public constructor with a single <c>String</c> parameter.
034 *    <li>
035 *       Have one of the following public static methods that takes in a single <c>String</c> parameter:
036 *       <ul>
037 *          <li><c>fromString</c>
038 *          <li><c>fromValue</c>
039 *          <li><c>valueOf</c>
040 *          <li><c>parse</c>
041 *          <li><c>parseString</c>
042 *          <li><c>forName</c>
043 *          <li><c>forString</c>
044 *    </ul>
045 * </ul>
046 *
047 * <h5 class='section'>Notes:</h5><ul>
048 *    <li class='note'>This class is thread safe and reusable.
049 * </ul>
050 *
051 * <h5 class='section'>See Also:</h5><ul>
052 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HttpPartSerializersParsers">HTTP Part Serializers and Parsers</a>
053
054 * </ul>
055 */
056public class SimplePartParser extends BaseHttpPartParser {
057
058   //-----------------------------------------------------------------------------------------------------------------
059   // Static
060   //-----------------------------------------------------------------------------------------------------------------
061
062   /** Reusable instance of {@link SimplePartParser}, all default settings. */
063   public static final SimplePartParser DEFAULT = create().build();
064
065   /** Reusable instance of {@link SimplePartParser}, all default settings. */
066   public static final SimplePartParserSession DEFAULT_SESSION = DEFAULT.getPartSession();
067
068   /**
069    * Creates a new builder for this object.
070    *
071    * @return A new builder.
072    */
073   public static Builder create() {
074      return new Builder();
075   }
076
077   //-----------------------------------------------------------------------------------------------------------------
078   // Builder
079   //-----------------------------------------------------------------------------------------------------------------
080
081   /**
082    * Builder class.
083    */
084   public static class Builder extends BaseHttpPartParser.Builder {
085
086      private static final Cache<HashKey,SimplePartParser> CACHE = Cache.of(HashKey.class, SimplePartParser.class).build();
087
088      /**
089       * Constructor.
090       */
091      protected Builder() {
092      }
093
094      /**
095       * Copy constructor.
096       *
097       * @param copyFrom The builder to copy.
098       */
099      protected Builder(Builder copyFrom) {
100         super(copyFrom);
101      }
102
103      @Override
104      public SimplePartParser build() {
105         return cache(CACHE).build(SimplePartParser.class);
106      }
107
108      @Override
109      public Builder copy() {
110         return new Builder(this);
111      }
112
113   @Override /* Overridden from Context */
114   public Builder cache(Cache<HashKey,? extends Context> value) {
115         super.cache(value);
116         return this;
117      }
118   }
119
120   //-------------------------------------------------------------------------------------------------------------------
121   // Instance
122   //-------------------------------------------------------------------------------------------------------------------
123
124   /**
125    * Constructor
126    *
127    * @param builder The builder for this object.
128    */
129   public SimplePartParser(Builder builder) {
130      super(builder);
131   }
132
133   @Override
134   public SimplePartParserSession getPartSession() {
135      return new SimplePartParserSession();
136   }
137}