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.uon; 014 015import org.apache.juneau.*; 016import org.apache.juneau.parser.*; 017import org.apache.juneau.urlencoding.*; 018 019/** 020 * Parses UON (a notation for URL-encoded query parameter values) text into POJO models. 021 * 022 * <h5 class='topic'>Media types</h5> 023 * 024 * Handles <code>Content-Type</code> types: <code><b>text/uon</b></code> 025 * 026 * <h5 class='topic'>Description</h5> 027 * 028 * This parser uses a state machine, which makes it very fast and efficient. 029 */ 030public class UonParser extends ReaderParser { 031 032 //------------------------------------------------------------------------------------------------------------------- 033 // Configurable properties 034 //------------------------------------------------------------------------------------------------------------------- 035 036 private static final String PREFIX = "UonParser."; 037 038 /** 039 * Configuration property: Decode <js>"%xx"</js> sequences. 040 * 041 * <h5 class='section'>Property:</h5> 042 * <ul> 043 * <li><b>Name:</b> <js>"UonParser.decoding.b"</js> 044 * <li><b>Data type:</b> <code>Boolean</code> 045 * <li><b>Default:</b> <jk>false</jk> for {@link UonParser}, <jk>true</jk> for {@link UrlEncodingParser} 046 * <li><b>Session-overridable:</b> <jk>true</jk> 047 * <li><b>Methods:</b> 048 * <ul> 049 * <li class='jm'>{@link UonParserBuilder#decoding(boolean)} 050 * <li class='jm'>{@link UonParserBuilder#decoding()} 051 * </ul> 052 * </ul> 053 * 054 * <h5 class='section'>Description:</h5> 055 * <p> 056 * Specify <jk>true</jk> if URI encoded characters should be decoded, <jk>false</jk> if they've already been decoded 057 * before being passed to this parser. 058 * 059 * <h5 class='section'>Example:</h5> 060 * <p class='bcode'> 061 * <jc>// Create a decoding UON parser.</jc> 062 * ReaderParser p = UonParser. 063 * .<jsm>create</jsm>() 064 * .decoding() 065 * .build(); 066 * 067 * <jc>// Same, but use property.</jc> 068 * ReaderParser p = UonParser. 069 * .<jsm>create</jsm>() 070 * .set(<jsf>UON_decoding</jsf>, <jk>true</jk>) 071 * .build(); 072 * 073 * <jc>// Produces: ["foo bar", "baz quz"].</jc> 074 * String[] foo = p.parse(<js>"@(foo%20bar,baz%20qux)"</js>, String[].<jk>class</jk>); 075 * </p> 076 */ 077 public static final String UON_decoding = PREFIX + "decoding.b"; 078 079 /** 080 * Configuration property: Validate end. 081 * 082 * <h5 class='section'>Property:</h5> 083 * <ul> 084 * <li><b>Name:</b> <js>"UonParser.validateEnd.b"</js> 085 * <li><b>Data type:</b> <code>Boolean</code> 086 * <li><b>Default:</b> <jk>false</jk> 087 * <li><b>Session-overridable:</b> <jk>true</jk> 088 * <li><b>Methods:</b> 089 * <ul> 090 * <li class='jm'>{@link UonParserBuilder#validateEnd(boolean)} 091 * <li class='jm'>{@link UonParserBuilder#validateEnd()} 092 * </ul> 093 * </ul> 094 * 095 * <h5 class='section'>Description:</h5> 096 * <p> 097 * If <jk>true</jk>, after parsing a POJO from the input, verifies that the remaining input in 098 * the stream consists of only comments or whitespace. 099 * 100 * <h5 class='section'>Example:</h5> 101 * <p class='bcode'> 102 * <jc>// Create a parser using strict mode.</jc> 103 * ReaderParser p = UonParser. 104 * .<jsm>create</jsm>() 105 * .validateEnd() 106 * .build(); 107 * 108 * <jc>// Same, but use property.</jc> 109 * ReaderParser p = UonParser. 110 * .<jsm>create</jsm>() 111 * .set(<jsf>UON_validateEnd</jsf>, <jk>true</jk>) 112 * .build(); 113 * 114 * <jc>// Should fail because input has multiple POJOs.</jc> 115 * String in = <js>"(foo=bar)(baz=qux)"</js>; 116 * MyBean myBean = p.parse(in, MyBean.<jk>class</jk>); 117 * </p> 118 */ 119 public static final String UON_validateEnd = PREFIX + "validateEnd.b"; 120 121 //------------------------------------------------------------------------------------------------------------------- 122 // Predefined instances 123 //------------------------------------------------------------------------------------------------------------------- 124 125 /** Reusable instance of {@link UonParser}, all default settings. */ 126 public static final UonParser DEFAULT = new UonParser(PropertyStore.DEFAULT); 127 128 /** Reusable instance of {@link UonParser} with decodeChars set to true. */ 129 public static final UonParser DEFAULT_DECODING = new UonParser.Decoding(PropertyStore.DEFAULT); 130 131 132 //------------------------------------------------------------------------------------------------------------------- 133 // Predefined subclasses 134 //------------------------------------------------------------------------------------------------------------------- 135 136 /** Default parser, decoding. */ 137 public static class Decoding extends UonParser { 138 139 /** 140 * Constructor. 141 * 142 * @param ps The property store containing all the settings for this object. 143 */ 144 public Decoding(PropertyStore ps) { 145 super(ps.builder().set(UON_decoding, true).build()); 146 } 147 } 148 149 150 //------------------------------------------------------------------------------------------------------------------- 151 // Instance 152 //------------------------------------------------------------------------------------------------------------------- 153 154 final boolean 155 decodeChars, validateEnd; 156 157 /** 158 * Constructor. 159 * 160 * @param ps 161 * The property store containing all the settings for this object. 162 */ 163 public UonParser(PropertyStore ps) { 164 this(ps, "text/uon"); 165 } 166 167 /** 168 * Constructor. 169 * 170 * @param ps 171 * The property store containing all the settings for this object. 172 * @param consumes 173 * The list of media types that this parser consumes (e.g. <js>"application/json"</js>, <js>"*​/json"</js>). 174 */ 175 public UonParser(PropertyStore ps, String...consumes) { 176 super(ps, consumes); 177 this.decodeChars = getBooleanProperty(UON_decoding, false); 178 this.validateEnd = getBooleanProperty(UON_validateEnd, false); 179 } 180 181 @Override /* Context */ 182 public UonParserBuilder builder() { 183 return new UonParserBuilder(getPropertyStore()); 184 } 185 186 /** 187 * Instantiates a new clean-slate {@link UonParserBuilder} object. 188 * 189 * <p> 190 * This is equivalent to simply calling <code><jk>new</jk> UonParserBuilder()</code>. 191 * 192 * <p> 193 * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies 194 * the settings of the object called on. 195 * 196 * @return A new {@link UonParserBuilder} object. 197 */ 198 public static UonParserBuilder create() { 199 return new UonParserBuilder(); 200 } 201 202 /** 203 * Create a UON parser session for parsing parameter values. 204 * 205 * @return A new parser session. 206 */ 207 protected final UonParserSession createParameterSession() { 208 return new UonParserSession(this, createDefaultSessionArgs(), false); 209 } 210 211 @Override /* Parser */ 212 public UonParserSession createSession(ParserSessionArgs args) { 213 return new UonParserSession(this, args); 214 } 215 216 @Override /* Context */ 217 public ObjectMap asMap() { 218 return super.asMap() 219 .append("UonParser", new ObjectMap() 220 .append("decodeChars", decodeChars) 221 ); 222 } 223}