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