src/java.management.rest/share/classes/com/oracle/jmx/remote/rest/json/parser/JsonParser.jj
author hb
Thu, 04 Jan 2018 14:39:04 +0530
branchjmx-rest-api
changeset 56006 352a4f213fc6
parent 56002 60ab3b595a8e
permissions -rw-r--r--
1. URL decoding of URL 2. MBean info availble in MBeanCollection page 3. MBeanCollection post supports objectname filtering 4. Tests now use reflection instead of Methodhndles 3. couple of bug fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
     1
/*
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
     4
 *
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    10
 *
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    15
 * accompanied this code).
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    16
 *
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    20
 *
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    23
 * questions.
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    24
 */
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    25
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    26
 /*
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    27
  * This is the JavaCC grammar file for a JSON parser. The productions are
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    28
  * based on syntax diagrams as specified at json.org
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    29
  * Every type of JSON is represent by a JSONElement. JSON Objects are mapped to
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    30
  * by LinkedHashMap via JSONObject. JSON Arrays are mapped to ArrayList via JSONArray
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    31
  * String, Number, Boolean and null are wrapped into a JSONPrimitive.
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    32
  * Control characters inside a string (JSONPrimitive(String)) are not escaped and are passed through
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    33
  * to ouput JSON string generated by toJsonString method of JSONElement.
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    34
  * Inside a String, only backslash ('\') and double quote ('"') characters
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    35
  * are escaped by a backslash, if they are not already escaped.
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    36
  * Applications consuming json string generated by JSON* objects must take care
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    37
  * to escape control characters within strings by prefixing with a backslash character,
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    38
  * if not already escaped.
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    39
  */
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    40
options {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    41
    STATIC=false;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    42
    ERROR_REPORTING=true;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    43
    JAVA_UNICODE_ESCAPE=true;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    44
    UNICODE_INPUT=true;
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    45
    IGNORE_CASE=true;
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    46
    CACHE_TOKENS=true;
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    47
//    DEBUG_PARSER=true;
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    48
//    DEBUG_LOOKAHEAD=true;
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
    49
//    DEBUG_TOKEN_MANAGER=true;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    50
}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    51
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    52
PARSER_BEGIN(JSONParser)
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    53
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    54
package com.oracle.jmx.remote.rest.json.parser;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    55
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    56
import java.io.StringReader;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    57
import com.oracle.jmx.remote.rest.json.*;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    58
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    59
public class JSONParser {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    60
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    61
    public JSONParser(String input) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    62
        this(new StringReader(input));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    63
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    64
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    65
    public JSONElement parse() throws ParseException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    66
        return jsonValue(); 
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    67
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    68
}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    69
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    70
PARSER_END(JSONParser)
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    71
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    72
SKIP: {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    73
    " " | "\b" | "\t" | "\n" | "\r" | "\f"
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    74
}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    75
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    76
TOKEN:{
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    77
   <INTEGER_LITERAL : (["-"])? (<DIGITS>)>
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    78
|   < FLOATING_POINT_LITERAL:
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    79
        <INTEGER_LITERAL> <FRAC>
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    80
|       <INTEGER_LITERAL> <EXPONENT>
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    81
|       <INTEGER_LITERAL> <FRAC> <EXPONENT>
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    82
    >
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    83
|   < #FRAC: "." <DIGITS>>
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    84
|   < #EXPONENT: ["e","E"] (["+","-"])? <DIGITS> >
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    85
|   < #DIGITS : (<DIGIT>)+>
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    86
|   < #DIGIT: ["0"-"9"]>
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    87
|   <QUOTED_STRING: "\"" ((~["\"","\\"]) | ("\\" ( ["n","t","b","r","f","\\","\""])))* "\"">
56006
352a4f213fc6 1. URL decoding of URL
hb
parents: 56002
diff changeset
    88
|   <BOOL_LITERAL : "true" | "false">
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    89
|   <NULL: "null">
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    90
}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    91
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    92
JSONElement jsonValue() : {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    93
	JSONElement x;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    94
}{
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    95
	( x = object()
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    96
	| x = list()
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    97
	| x = string()
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    98
	| x = number()
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    99
	| x = boolVal()
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   100
	| x = nullVal()
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   101
	)
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   102
	{ return x; }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   103
}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   104
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   105
JSONObject object() : {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   106
	final JSONObject jobject = new JSONObject();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   107
	JSONPrimitive key;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   108
	JSONElement value;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   109
}{
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   110
    "{"
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
   111
        (key = string())
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   112
        ":"
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   113
        value = jsonValue()
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   114
        { jobject.put((String)key.getValue(), value);}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   115
        (
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   116
                ","
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55985
diff changeset
   117
                (key = string())
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   118
                ":"
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   119
                value = jsonValue()
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   120
                { jobject.put((String)key.getValue(), value);}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   121
        )*
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   122
    "}"
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   123
    { return jobject; }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   124
}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   125
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   126
JSONArray list() : {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   127
    final JSONArray jarray = new JSONArray();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   128
    JSONElement value;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   129
}{
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   130
    "["
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   131
        value = jsonValue()
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   132
        { jarray.add(value);}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   133
        (
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   134
            ","
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   135
            value = jsonValue()
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   136
            { jarray.add(value); }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   137
        )*
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   138
    "]"
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   139
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   140
    { return jarray; }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   141
}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   142
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   143
JSONPrimitive nullVal(): {} {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   144
    <NULL>
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   145
    { return null; }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   146
}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   147
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   148
JSONPrimitive boolVal(): {}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   149
{
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   150
    <BOOL_LITERAL>
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   151
    {return new JSONPrimitive(Boolean.parseBoolean(token.image));}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   152
}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   153
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   154
JSONPrimitive number(): {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   155
    Token t;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   156
}{
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   157
    (t = <INTEGER_LITERAL> { return new JSONPrimitive(Long.parseLong(t.image));})
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   158
    | (t = <FLOATING_POINT_LITERAL> { return new JSONPrimitive(Double.parseDouble(t.image));})
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   159
}
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   160
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   161
JSONPrimitive string() : {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   162
    Token t;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   163
}{
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   164
    (t = <QUOTED_STRING>)
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   165
    { return new JSONPrimitive(t.image.substring(1,t.image.length()-1)); }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   166
}