org.apache.juneau.uon.UonParser Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.uon;
import org.apache.juneau.*;
import org.apache.juneau.parser.*;
import org.apache.juneau.urlencoding.*;
/**
* Parses UON (a notation for URL-encoded query parameter values) text into POJO models.
*
* Media types:
*
* Handles Content-Type
types: text/uon
*
* Description:
*
* This parser uses a state machine, which makes it very fast and efficient.
*/
public class UonParser extends ReaderParser {
//-------------------------------------------------------------------------------------------------------------------
// Configurable properties
//-------------------------------------------------------------------------------------------------------------------
private static final String PREFIX = "UonParser.";
/**
* Configuration property: Decode "%xx" sequences.
*
*
* - Name:
"UonParser.decodeChars"
* - Data type:
Boolean
* - Default:
false for {@link UonParser}, true for {@link UrlEncodingParser}
* - Session-overridable:
true
*
*
*
* Specify true if URI encoded characters should be decoded, false if they've already been decoded
* before being passed to this parser.
*/
public static final String UON_decodeChars = PREFIX + "decodeChars";
//-------------------------------------------------------------------------------------------------------------------
// Predefined instances
//-------------------------------------------------------------------------------------------------------------------
/** Reusable instance of {@link UonParser}, all default settings. */
public static final UonParser DEFAULT = new UonParser(PropertyStore.create());
/** Reusable instance of {@link UonParser} with decodeChars set to true. */
public static final UonParser DEFAULT_DECODING = new UonParser.Decoding(PropertyStore.create());
//-------------------------------------------------------------------------------------------------------------------
// Predefined subclasses
//-------------------------------------------------------------------------------------------------------------------
/** Default parser, decoding. */
public static class Decoding extends UonParser {
/**
* Constructor.
*
* @param propertyStore The property store containing all the settings for this object.
*/
public Decoding(PropertyStore propertyStore) {
super(propertyStore.copy().append(UON_decodeChars, true));
}
}
//-------------------------------------------------------------------------------------------------------------------
// Instance
//-------------------------------------------------------------------------------------------------------------------
private final UonParserContext ctx;
/**
* Constructor.
*
* @param propertyStore
* The property store containing all the settings for this object.
*/
public UonParser(PropertyStore propertyStore) {
this(propertyStore, "text/uon");
}
/**
* Constructor.
*
* @param propertyStore
* The property store containing all the settings for this object.
* @param consumes
* The list of media types that this parser consumes (e.g. "application/json" , "*/json" ).
*/
public UonParser(PropertyStore propertyStore, String...consumes) {
super(propertyStore, consumes);
this.ctx = createContext(UonParserContext.class);
}
@Override /* CoreObject */
public UonParserBuilder builder() {
return new UonParserBuilder(propertyStore);
}
/**
* Create a UON parser session for parsing parameter values.
*
* @return A new parser session.
*/
protected final UonParserSession createParameterSession() {
return new UonParserSession(ctx, createDefaultSessionArgs(), false);
}
@Override /* Parser */
public UonParserSession createSession(ParserSessionArgs args) {
return new UonParserSession(ctx, args);
}
}