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 static org.apache.juneau.internal.StringUtils.*;
016
017import org.apache.juneau.*;
018import org.apache.juneau.parser.*;
019import org.apache.juneau.uon.*;
020
021/**
022 * @deprecated Use {@link UonParser}
023 */
024@Deprecated
025@SuppressWarnings({ "unchecked" })
026public class UonPartParser extends UonParser {
027
028   //-------------------------------------------------------------------------------------------------------------------
029   // Predefined instances
030   //-------------------------------------------------------------------------------------------------------------------
031
032   /** Reusable instance of {@link UonPartParser}. */
033   public static final UonPartParser DEFAULT = new UonPartParser(PropertyStore.DEFAULT);
034
035
036   //-------------------------------------------------------------------------------------------------------------------
037   // Instance
038   //-------------------------------------------------------------------------------------------------------------------
039
040   /**
041    * Constructor.
042    *
043    * @param ps The property store containing all the settings for this object.
044    */
045   public UonPartParser(PropertyStore ps) {
046      super(
047         ps.builder()
048            .build(),
049         "application/x-www-form-urlencoded"
050      );
051   }
052
053   @Override /* Context */
054   public UonPartParserBuilder builder() {
055      return new UonPartParserBuilder(getPropertyStore());
056   }
057
058   /**
059    * Instantiates a new clean-slate {@link UonPartParserBuilder} object.
060    *
061    * <p>
062    * This is equivalent to simply calling <code><jk>new</jk> UonPartParserBuilder()</code>.
063    *
064    * <p>
065    * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies
066    * the settings of the object called on.
067    *
068    * @return A new {@link UonPartParserBuilder} object.
069    */
070   public static UonPartParserBuilder create() {
071      return new UonPartParserBuilder();
072   }
073
074   @Override
075   public <T> T parse(HttpPartType partType, String in, ClassMeta<T> type) throws ParseException {
076      if (in == null)
077         return null;
078      if (type.isString() && in.length() > 0) {
079         // Shortcut - If we're returning a string and the value doesn't start with "'" or is "null", then
080         // just return the string since it's a plain value.
081         // This allows us to bypass the creation of a UonParserSession object.
082         char x = firstNonWhitespaceChar(in);
083         if (x != '\'' && x != 'n' && in.indexOf('~') == -1)
084            return (T)in;
085         if (x == 'n' && "null".equals(in))
086            return null;
087      }
088      UonParserSession session = createParameterSession();
089      try (ParserPipe pipe = session.createPipe(in)) {
090         try (UonReader r = session.getUonReader(pipe, false)) {
091            return session.parseAnything(type, r, null, true, null);
092         }
093      } catch (ParseException e) {
094         throw e;
095      } catch (Exception e) {
096         throw new ParseException(e);
097      }
098   }
099}