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.msgpack;
014
015import org.apache.juneau.*;
016import org.apache.juneau.parser.*;
017
018/**
019 * Parses a MessagePack stream into a POJO model.
020 *
021 * <h5 class='topic'>Media types</h5>
022 *
023 * Handles <code>Content-Type</code> types:  <code><b>octal/msgpack</b></code>
024 */
025public class MsgPackParser extends InputStreamParser {
026
027   //-------------------------------------------------------------------------------------------------------------------
028   // Predefined instances
029   //-------------------------------------------------------------------------------------------------------------------
030
031   /** Default parser, all default settings.*/
032   public static final MsgPackParser DEFAULT = new MsgPackParser(PropertyStore.DEFAULT);
033
034   /** Default parser, all default settings, string input encoded as spaced-hex.*/
035   public static final MsgPackParser DEFAULT_SPACED_HEX = new SpacedHex(PropertyStore.DEFAULT);
036
037   /** Default parser, all default settings, string input encoded as BASE64.*/
038   public static final MsgPackParser DEFAULT_BASE64 = new Base64(PropertyStore.DEFAULT);
039
040   //-------------------------------------------------------------------------------------------------------------------
041   // Predefined subclasses
042   //-------------------------------------------------------------------------------------------------------------------
043
044   /** Default parser, string input encoded as spaced-hex. */
045   public static class SpacedHex extends MsgPackParser {
046
047      /**
048       * Constructor.
049       *
050       * @param ps The property store containing all the settings for this object.
051       */
052      public SpacedHex(PropertyStore ps) {
053         super(
054            ps.builder().set(ISPARSER_binaryFormat, BinaryFormat.SPACED_HEX).build()
055         );
056      }
057   }
058
059   /** Default parser, string input encoded as BASE64. */
060   public static class Base64 extends MsgPackParser {
061
062      /**
063       * Constructor.
064       *
065       * @param ps The property store containing all the settings for this object.
066       */
067      public Base64(PropertyStore ps) {
068         super(
069            ps.builder().set(ISPARSER_binaryFormat, BinaryFormat.BASE64).build()
070         );
071      }
072   }
073
074   //-------------------------------------------------------------------------------------------------------------------
075   // Instance
076   //-------------------------------------------------------------------------------------------------------------------
077
078   /**
079    * Constructor.
080    *
081    * @param ps The property store containing all the settings for this object.
082    */
083   public MsgPackParser(PropertyStore ps) {
084      super(ps, "octal/msgpack");
085   }
086
087   @Override /* Context */
088   public MsgPackParserBuilder builder() {
089      return new MsgPackParserBuilder(getPropertyStore());
090   }
091
092   /**
093    * Instantiates a new clean-slate {@link MsgPackParserBuilder} object.
094    *
095    * <p>
096    * This is equivalent to simply calling <code><jk>new</jk> MsgPackParserBuilder()</code>.
097    *
098    * <p>
099    * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies
100    * the settings of the object called on.
101    *
102    * @return A new {@link MsgPackParserBuilder} object.
103    */
104   public static MsgPackParserBuilder create() {
105      return new MsgPackParserBuilder();
106   }
107
108   @Override /* Parser */
109   public MsgPackParserSession createSession(ParserSessionArgs args) {
110      return new MsgPackParserSession(this, args);
111   }
112
113   @Override /* Context */
114   public ObjectMap asMap() {
115      return super.asMap()
116         .append("MsgPackParser", new ObjectMap());
117   }
118}