DefaultFieldParser implements FieldParser { @Override public FieldMap parse( String fields ) { List<String> prefixList = Lists.newArrayList(); FieldMap fieldMap = new FieldMap(); StringBuilder builder = new StringBuilder(); String[] fieldSplit = fields.split( "" ); for ( int i = 0; i < fieldSplit.length; i++ ) { String c = fieldSplit[i]; if ( (c.equals( ":" ) && fieldSplit[i + 1].equals( ":" )) || c.equals( "~" ) || c.equals( "|" ) ) { boolean insideParameters = false; for ( ; i < fieldSplit.length; i++ ) { c = fieldSplit[i]; if ( StringUtils.isAlphanumeric( c ) || c.equals( ":" ) || c.equals( "~" ) || c.equals( "|" ) ) { builder.append( c ); } else if ( c.equals( "(" ) ) { insideParameters = true; builder.append( c ); } else if ( insideParameters && c.equals( ";" ) ) { builder.append( c ); } else if ( (insideParameters && c.equals( ")" )) ) { builder.append( c ); break; } else if ( c.equals( "," ) || ( c.equals( "[" ) && !insideParameters ) ) { i--; break; } } } else if ( c.equals( "," ) ) { putInMap( fieldMap, joinedWithPrefix( builder, prefixList ) ); builder = new StringBuilder(); } else if ( c.equals( "[" ) || c.equals( "(" ) ) { prefixList.add( builder.toString() ); builder = new StringBuilder(); } else if ( c.equals( "]" ) || c.equals( ")" ) ) { if ( !builder.toString().isEmpty() ) { putInMap( fieldMap, joinedWithPrefix( builder, prefixList ) ); } prefixList.remove( prefixList.size() - 1 ); builder = new StringBuilder(); } else if ( StringUtils.isAlphanumeric( c ) || c.equals( "*" ) || c.equals( ":" ) || c.equals( ";" ) || c.equals( "~" ) || c.equals( "!" ) || c.equals( "|" ) || c.equals( "{" ) || c.equals( "}" ) ) { builder.append( c ); } } if ( !builder.toString().isEmpty() ) { putInMap( fieldMap, joinedWithPrefix( builder, prefixList ) ); } return fieldMap; }  @Override FieldMap parse( String fields ); @Override List<String> modifyFilter( Collection<String> fields, Collection<String> excludeFields );  }
@Test public void parseWithTransformer() { final FieldMap fieldMap = parser.parse( "id,organisationUnits~pluck" ); Assert.assertEquals( 2, fieldMap.size() ); Assert.assertTrue( fieldMap.containsKey( "id" ) ); Assert.assertTrue( fieldMap.containsKey( "organisationUnits~pluck" ) ); }
@Test public void parseWithTransformerArgAndFields() { final FieldMap fieldMap = parser.parse( "id,organisationUnits~pluck(name)[id,name]" ); Assert.assertEquals( 2, fieldMap.size() ); Assert.assertTrue( fieldMap.containsKey( "id" ) ); Assert.assertTrue( fieldMap.containsKey( "organisationUnits~pluck(name)" ) ); final FieldMap innerFieldMap = fieldMap.get( "organisationUnits~pluck(name)" ); Assert.assertTrue( innerFieldMap.containsKey( "id" ) ); Assert.assertTrue( innerFieldMap.containsKey( "name" ) ); }