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