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 static org.apache.juneau.common.internal.StringUtils.*;
016
017import org.apache.juneau.common.internal.*;
018
019/**
020 * Utility methods for the UON and UrlEncoding serializers and parsers.
021 *
022 * <h5 class='section'>See Also:</h5><ul>
023 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.UonDetails">UON Details</a>
024 * </ul>
025 */
026public final class UonUtils {
027
028   private static final AsciiSet needsQuoteChars = AsciiSet.create("),=\n\t\r\b\f ");
029   private static final AsciiSet maybeNeedsQuotesFirstChar = AsciiSet.create("),=\n\t\r\b\f tfn+-.#0123456789");
030
031   /**
032    * Returns <jk>true</jk> if the specified string needs to be quoted per UON notation.
033    *
034    * <p>
035    * For example, strings that start with '(' or '@' or look like boolean or numeric values need to be quoted.
036    *
037    * @param s The string to test.
038    * @return <jk>true</jk> if the specified string needs to be quoted per UON notation.
039    */
040   public static boolean needsQuotes(String s) {
041      char c0 = s.isEmpty() ? 0 : s.charAt(0);
042      return (
043         s.isEmpty()
044         || c0 == '@'
045         || c0 == '('
046         || needsQuoteChars.contains(s)
047         || (
048            maybeNeedsQuotesFirstChar.contains(c0)
049            && (
050               "true".equals(s)
051               || "false".equals(s)
052               || "null".equals(s)
053               || isNumeric(s)
054            )
055         )
056      );
057   }
058}