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.plaintext;
014
015import org.apache.juneau.*;
016import org.apache.juneau.annotation.*;
017import org.apache.juneau.parser.*;
018import org.apache.juneau.transform.*;
019
020/**
021 * Parsers HTTP plain text request bodies into Group 5 POJOs.
022 *
023 * <p>
024 * See {@doc PojoCategories}.
025 *
026 * <h5 class='topic'>Media types</h5>
027 *
028 * Handles <c>Accept</c> types:  <bc>text/plain</bc>
029 * <p>
030 * Produces <c>Content-Type</c> types:  <bc>text/plain</bc>
031 *
032 * <h5 class='topic'>Description</h5>
033 *
034 * Essentially just converts plain text to POJOs via static <c>fromString()</c> or <c>valueOf()</c>, or
035 * through constructors that take a single string argument.
036 *
037 * <p>
038 * Also parses objects using a transform if the object class has an {@link PojoSwap PojoSwap&lt;?,String&gt;} transform
039 * defined on it.
040 */
041@ConfigurableContext
042public class PlainTextParser extends ReaderParser {
043
044   //-------------------------------------------------------------------------------------------------------------------
045   // Configurable properties
046   //-------------------------------------------------------------------------------------------------------------------
047
048   static final String PREFIX = "PlainTextParser";
049
050   //-------------------------------------------------------------------------------------------------------------------
051   // Predefined subclasses
052   //-------------------------------------------------------------------------------------------------------------------
053
054   /** Default parser, all default settings.*/
055   public static final PlainTextParser DEFAULT = new PlainTextParser(PropertyStore.DEFAULT);
056
057
058   //-------------------------------------------------------------------------------------------------------------------
059   // Instance
060   //-------------------------------------------------------------------------------------------------------------------
061
062   /**
063    * Constructor.
064    *
065    * @param ps The property store containing all the settings for this object.
066    */
067   public PlainTextParser(PropertyStore ps) {
068      this(ps, "text/plain");
069   }
070
071   /**
072    * Constructor.
073    *
074    * @param ps The property store containing all the settings for this object.
075    * @param consumes The media types that this parser consumes.
076    *    <p>
077    *    Can contain meta-characters per the <c>media-type</c> specification of {@doc RFC2616.section14.1}
078    */
079   public PlainTextParser(PropertyStore ps, String...consumes) {
080      super(ps, consumes);
081   }
082
083   @Override /* Context */
084   public PlainTextParserBuilder builder() {
085      return new PlainTextParserBuilder(getPropertyStore());
086   }
087
088   /**
089    * Instantiates a new clean-slate {@link PlainTextParserBuilder} object.
090    *
091    * <p>
092    * This is equivalent to simply calling <code><jk>new</jk> PlainTextParserBuilder()</code>.
093    *
094    * <p>
095    * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies
096    * the settings of the object called on.
097    *
098    * @return A new {@link PlainTextParserBuilder} object.
099    */
100   public static PlainTextParserBuilder create() {
101      return new PlainTextParserBuilder();
102   }
103
104   @Override /* Parser */
105   public PlainTextParserSession createSession() {
106      return createSession(createDefaultSessionArgs());
107   }
108
109   @Override /* Parser */
110   public PlainTextParserSession createSession(ParserSessionArgs args) {
111      return new PlainTextParserSession(this, args);
112   }
113
114   //-----------------------------------------------------------------------------------------------------------------
115   // Other methods
116   //-----------------------------------------------------------------------------------------------------------------
117
118   @Override /* Context */
119   public ObjectMap toMap() {
120      return super.toMap()
121         .append("PlainTextParser", new DefaultFilteringObjectMap()
122         );
123   }
124}