2

Im exposing REST services through use of Sprint MVC 4.0 framework and I try following Odata specification for the Query Parameters such as $filter, $search and $orderBy. Each of these contains expressions that I need to parse, build abstract syntax trees and validate. They are all retrieved as String.

I do not need all the constructions that are defined in the Odata grammer (http://docs.oasis-open.org/odata/odata/v4.0/cos01/abnf/odata-abnf-construction-rules.txt), I just pick the ones that are relevant for my uses cases (very few actually)

I would like some tip on how to parse and build the abstract tree in a easy way and if Odata4j might be used as a Utility library to do this job for me? I would like to avoid dragging bunch of new dependencies to odata4j, since I will only use small piece of the code.

Leeor
  • 19,260
  • 5
  • 56
  • 87
Ismar Slomic
  • 5,315
  • 6
  • 44
  • 63

1 Answers1

1

You can certainly use odata4j for building ASTs for query parameters. I've done that for exactly the purposes you cite. I split off the query parameters, and then split again on '&' to get parameters. For each of these I inspect the parameter name ($select, $filter, etc.) and then based on that use the corresponding OptionsQueryParser static method on the value, returning an number, or list, or AST specific to that query parameter. For expression ASTs, look at PrintExpressionVisitor and use that as a pattern for writing your own visitor to walk the AST.

Tom
  • 21
  • 1
  • 1
  • Tom, that sounds like something I'm looking for, too. Do you have any code you can share? – Steve Schmitt Apr 21 '14 at 19:04
  • I trust you can see the pattern here and complete it yourself. String queryString = uri.substring(uri.indexOf('?') + 1); for (String segment : queryString.split("&")) { String[] paramPair = segment.split("="); String name = paramPair[0].trim(); String value = paramPair[1].trim(); if (name.equals(OPTION_SELECT)) { select = OptionsQueryParser.parseSelect(value); } else if (name.equals(OPTION_FILTER)) { filter = OptionsQueryParser.parseFilter(value); } else if (name.equals(... – Tom May 01 '14 at 03:30
  • Thanks, Tom. Yes, that was the easy part. Now I'm trying to do the last part of your answer: walk the filter AST to build my query. – Steve Schmitt May 01 '14 at 07:41