Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.cxf.jaxrs.ext.search.odata;
 
 import java.util.List;
 import java.util.Map;
 
 
 public class ODataParser<T> extends AbstractSearchConditionParser<T> {
     private final FilterParser parser;
     
     private static class TypedProperty {
         private final TypeInfo typeInfo;
         private final String propertyName;
         
         TypedProperty(final TypeInfo typeInfofinal String propertyName) {
             this. = typeInfo;
             this. = propertyName;
         }
     }
     
     private static class TypedValue {
         private final Object value;
         private final String literal;    
         private final Class< ? > typeClass;
         
         TypedValue(final Class< ? > typeClassfinal String literalfinal Object value) {
             this. = literal;
             this. = value;
             this. = typeClass;
         }
     }
     
     private class FilterExpressionVisitor implements ExpressionVisitor {
         private final T condition;
         
         FilterExpressionVisitor(final T condition) {
             this. = condition;
         }
 
         @Override
         public Object visitFilterExpression(FilterExpression filterExpressionString expressionString
                 Object expression) {
             return expression;
         }
 
         @Override
        @SuppressWarnings("unchecked")
        public Object visitBinary(BinaryExpression binaryExpressionBinaryOperator operator
                Object leftSideObject rightSide) {
            
            // AND / OR operate on search conditions
            if (operator == . || operator == .) {
                if (leftSide instanceof SearchCondition && rightSide instanceof SearchCondition) {
                    final ListSearchCondition< T > > conditions = new ArrayListSearchCondition< T > >(2);
                    conditions.add((SearchCondition< T >)leftSide);
                    conditions.add((SearchCondition< T >)rightSide);
                    
                    if (operator == .) {
                        return new AndSearchCondition< T >(conditions);
                    } else if (operator == .) {
                        return new OrSearchCondition< T >(conditions);
                    }
                } else {
                    throw new SearchParseException(
                        "Unsupported binary operation arguments (SearchCondition expected): " 
                            + leftSide + ", " + rightSide);
                }
            }
            // Property could be either on left side (Name eq 'Tom') or 
            // right side ('Tom' eq Name)
            TypedValue value = null;
            TypedProperty property = null;
            
            if (leftSide instanceof TypedProperty && rightSide instanceof TypedValue) {
                property = (TypedProperty)leftSide;
                value = (TypedValue)rightSide;
            } else if (rightSide instanceof TypedProperty && leftSide instanceof TypedValue) {
                property = (TypedProperty)rightSide;
                value = (TypedValue)leftSide;
            } else {
                throw new SearchParseException(
                    "Unsupported binary operation arguments (TypedValue or TypedProperty expected): " 
                        + leftSide + ", " + rightSide);
            }
                       
            ConditionType conditionType = null;
            switch (operator) {
            case :    
                conditionType = .;
                break;
            case :
                conditionType = .;
                break;
            case :
                conditionType = .;
                break;
            case :
                conditionType = .;
                break;
            case :
                conditionType = .;
                break;                
            case :
                conditionType = .;
                break;                
            default:
                throw new SearchParseException("Unsupported binary operation: " + operator);
            }
            
            Object typedValue = null;
            // If property type and value type are compatible, just use them
            if (property.typeInfo.getWrappedTypeClass().isAssignableFrom(value.typeClass)) {
                typedValue = value.value;
            } else { // Property type and value type are not compatible and convert / cast are required
                String valueStr = value.literal;
                if (isDecodeQueryValues()) {
                    valueStr = UrlUtils.urlDecode(valueStr);
                }
                typedValue = parseType(property.propertyNamenullnullproperty.propertyName
                    property.typeInfovalueStr);
            }
            
            final CollectionCheckInfo checkInfo = property.typeInfo.getCollectionCheckInfo();
            if (checkInfo != null) {
                return new CollectionCheckCondition< T >(property.propertyNametypedValue
                    property.typeInfo.getGenericType(), conditionTypecheckInfo);
            }
                        
            return new PrimitiveSearchCondition< T >(property.propertyNametypedValue,  
                property.typeInfo.getGenericType(), conditionType);
        }
        @Override
        public Object visitLiteral(LiteralExpression literalEdmLiteral edmLiteral) {            
            try {
                final EdmSimpleType type = edmLiteral.getType();  
                
                final Object value = type.valueOfString(edmLiteral.getLiteral(), 
                    .nulltype.getDefaultType());
                
                return new TypedValue(type.getDefaultType(), edmLiteral.getLiteral(), value); 
            } catch (EdmSimpleTypeException ex) {
                throw new SearchParseException("Failed to convert literal to a typed form: " + literalex);
            }
        }
        
        @Override
        public Object visitProperty(PropertyExpression propertyExpressionString uriLiteralEdmTyped edmProperty) {
            String setter = getActualSetterName(uriLiteral);
            final TypeInfo typeInfo = ODataParser.this.getTypeInfo(setternull);
            return new TypedProperty(typeInfosetter);
        }
        @Override
        public Object visitMethod(MethodExpression methodExpressionMethodOperator methodList<Objectparameters) {
            throw new SearchParseException("Unsupported operation visitMethod: " + methodExpression 
                + "," + method + "," + parameters);
        }
        @Override
        public Object visitMember(MemberExpression memberExpressionObject pathObject property) {
            throw new SearchParseException("Unsupported operation visitMember: " 
                + memberExpression + "," + path + "," + property);            
        }
        @Override
        public Object visitUnary(UnaryExpression unaryExpressionUnaryOperator operatorObject operand) {
            throw new SearchParseException("Unsupported operation visitUnary: " + unaryExpression 
                + "," + operator + "," + operand);
        }
        
        @Override
        public Object visitOrderByExpression(OrderByExpression orderByExpressionString expressionString,
                List<Objectorders) {
            throw new SearchParseException("Unsupported operation visitOrderByExpression: " 
                + orderByExpression + "," + expressionString + "," + orders);
        }
        @Override
        public Object visitOrder(OrderExpression orderExpressionObject filterResultSortOrder sortOrder) {
            throw new SearchParseException("Unsupported operation visitOrder: " + orderExpression 
                + "," + filterResult + "," + sortOrder);
        }        
    }
    
    
Creates OData parser.

Parameters:
conditionClass - class of T used to create condition objects. Class T must have accessible no-arguments constructor and complementary setters to these used in OData $filter expressions.
    public ODataParser(final Class< T > conditionClass) {    
        this(conditionClass, Collections.<StringString>emptyMap());
    }
    
    
Creates OData parser.

Parameters:
tclass - class of T used to create condition objects in built syntax tree. Class T must have accessible no-arg constructor and complementary setters to these used in OData $filter expressions.
contextProperties
    public ODataParser(Class<T> tclassMap<StringStringcontextProperties) {
        this(tclasscontextPropertiesnull);
    }
    
    
Creates OData parser.

Parameters:
tclass - class of T used to create condition objects in built syntax tree. Class T must have accessible no-arg constructor and complementary setters to these used in OData $filter expressions.
contextProperties
    public ODataParser(Class<T> tclass
                      Map<StringStringcontextProperties,
                      Map<StringStringbeanProperties) {
        super(tclasscontextPropertiesbeanProperties);
        
        this. = new FilterParserImpl(null);
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public SearchCondition<T> parse(String searchExpressionthrows SearchParseException {
        try {
            final T condition = .newInstance();
            final FilterExpression expression = .parseFilterString(searchExpression);
            final FilterExpressionVisitor visitor = new FilterExpressionVisitor(condition);            
            return (SearchCondition< T >)expression.accept(visitor);            
        } catch (ODataMessageException ex) {
            throw new SearchParseException(ex);
        } catch (ODataApplicationException ex) {
            throw new SearchParseException(ex);
        } catch (InstantiationException ex) {
            throw new SearchParseException(ex);
        } catch (IllegalAccessException ex) {
            throw new SearchParseException(ex);
        }
    }
New to GrepCode? Check out our FAQ X