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