org.apache.olingo.commons.core.edm.primitivetype.EdmString Java Examples

The following examples show how to use org.apache.olingo.commons.core.edm.primitivetype.EdmString. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ODataXmlSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
static String replaceInvalidCharacters(EdmPrimitiveType expectedType,
    String value, Boolean isUniCode, String invalidCharacterReplacement) {
  if (!(expectedType instanceof EdmString)
      || invalidCharacterReplacement == null || isUniCode == null || !isUniCode) {
    return value;
  }
  String s = value;
  StringBuilder result = null;
  for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c <= 0x0020 && c != ' ' && c != '\n' && c != '\t' && c != '\r') {
      if (result == null) {
        result = new StringBuilder();
        result.append(s.substring(0, i));
      }
      result.append(invalidCharacterReplacement);
    } else if (result != null) {
      result.append(c);
    }
  }
  if (result == null) {
    return value;
  }
  return result.toString();
}
 
Example #2
Source File: FilterExpressionVisitor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public Object visitLiteral(Literal literal) throws ExpressionVisitException, ODataApplicationException {
  // To keep this tutorial simple, our filter expression visitor supports only Edm.Int32 and Edm.String
  // In real world scenarios it can be difficult to guess the type of an literal.
  // We can be sure, that the literal is a valid OData literal because the URI Parser checks 
  // the lexicographical structure
  
  // String literals start and end with an single quotation mark
  String literalAsString = literal.getText();
  if(literal.getType() instanceof EdmString) {
    String stringLiteral = "";
    if(literal.getText().length() > 2) {
      stringLiteral = literalAsString.substring(1, literalAsString.length() - 1);
    }
    
    return stringLiteral;
  } else {
    // Try to convert the literal into an Java Integer
    try {
      return Integer.parseInt(literalAsString);
    } catch(NumberFormatException e) {
      throw new ODataApplicationException("Only Edm.Int32 and Edm.String literals are implemented", 
          HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
    }
  }
}
 
Example #3
Source File: FilterExpressionVisitor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public Object visitLiteral(Literal literal) throws ExpressionVisitException, ODataApplicationException {
  // To keep this tutorial simple, our filter expression visitor supports only Edm.Int32 and Edm.String
  // In real world scenarios it can be difficult to guess the type of an literal.
  // We can be sure, that the literal is a valid OData literal because the URI Parser checks 
  // the lexicographical structure
  
  // String literals start and end with an single quotation mark
  String literalAsString = literal.getText();
  if(literal.getType() instanceof EdmString) {
    String stringLiteral = "";
    if(literal.getText().length() > 2) {
      stringLiteral = literalAsString.substring(1, literalAsString.length() - 1);
    }
    
    return stringLiteral;
  } else {
    // Try to convert the literal into an Java Integer
    try {
      return Integer.parseInt(literalAsString);
    } catch(NumberFormatException e) {
      throw new ODataApplicationException("Only Edm.Int32 and Edm.String literals are implemented", 
          HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
    }
  }
}
 
Example #4
Source File: FilterExpressionVisitor.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitLiteral(Literal literal) throws ExpressionVisitException, ODataApplicationException {
    // To keep this tutorial simple, our filter expression visitor supports
    // only Edm.Int32 and Edm.String
    // In real world scenarios it can be difficult to guess the type of an
    // literal.
    // We can be sure, that the literal is a valid OData literal because the
    // URI Parser checks
    // the lexicographical structure

    // String literals start and end with an single quotation mark
    String literalAsString = literal.getText();
    if (literal.getType() instanceof EdmString) {
        String stringLiteral = "";
        if (literal.getText().length() > 2) {
            stringLiteral = literalAsString.substring(1, literalAsString.length() - 1);
        }

        return stringLiteral;
    }

    // Try to convert the literal into an Java Integer
    try {
        return Integer.parseInt(literalAsString);
    } catch (NumberFormatException e) {
        throw new ODataApplicationException("Only Edm.Int32 and Edm.String literals are implemented",
            HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH, e);
    }
}
 
Example #5
Source File: ODataUtils.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
/**
 * This method creates access uri for the entity.
 *
 * @param baseURL      base URL
 * @param entity       entity
 * @param enitySetName entity Set Name
 * @param type         entity Type
 * @return Entity URI
 * @throws EdmPrimitiveTypeException
 */
public static String buildLocation(String baseURL, Entity entity, String enitySetName, EdmEntityType type)
        throws EdmPrimitiveTypeException {
    StringBuilder location = new StringBuilder();
    location.append(baseURL).append("/").append(enitySetName);
    int i = 0;
    boolean usename = type.getKeyPredicateNames().size() > 1;
    location.append("(");

    String value;
    for (Iterator var7 = type.getKeyPredicateNames().iterator(); var7.hasNext(); location.append(value)) {
        String key = (String) var7.next();
        if (i > 0) {
            location.append(",");
        }

        ++i;
        if (usename) {
            location.append(key).append("=");
        }

        String propertyType = entity.getProperty(key).getType();
        Object propertyValue = entity.getProperty(key).getValue();
        if (propertyType.startsWith("Edm.")) {
            propertyType = propertyType.substring(4);
        }

        EdmPrimitiveTypeKind kind = EdmPrimitiveTypeKind.valueOf(propertyType);
        EdmProperty property = type.getStructuralProperty(key);
        value = EdmPrimitiveTypeFactory.getInstance(kind)
                                       .valueToString(propertyValue, property.isNullable(), property.getMaxLength(),
                                                      property.getPrecision(), property.getScale(),
                                                      property.isUnicode());
        if (kind == EdmPrimitiveTypeKind.String) {
            value = EdmString.getInstance().toUriLiteral(Encoder.encode(value));
        }
    }

    location.append(")");
    return location.toString();
}
 
Example #6
Source File: EntityResponse.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static String buildLocation(String baseURL, Entity entity, String enitySetName, EdmEntityType type) 
    throws EdmPrimitiveTypeException {
  StringBuilder location = new StringBuilder();

  location.append(baseURL).append("/").append(enitySetName);
  
  int i = 0;
  boolean usename = type.getKeyPredicateNames().size() > 1;
  location.append("(");
  for (String key : type.getKeyPredicateNames()) {
    if (i > 0) {
      location.append(",");
    }
    i++;
    if (usename) {
      location.append(key).append("=");
    }
    
    EdmProperty property = (EdmProperty)type.getProperty(key);
    String propertyType = entity.getProperty(key).getType();
    Object propertyValue = entity.getProperty(key).getValue();
    
    if (propertyValue == null) {
      throw new EdmPrimitiveTypeException("The key value for property "+key+" is invalid; Key value cannot be null");
    }
    
    if(propertyType.startsWith("Edm.")) {
      propertyType = propertyType.substring(4);
    }
    EdmPrimitiveTypeKind kind = EdmPrimitiveTypeKind.valueOf(propertyType);
    String value =  EdmPrimitiveTypeFactory.getInstance(kind).valueToString(
        propertyValue, property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(), 
        property.isUnicode());
    if (kind == EdmPrimitiveTypeKind.String) {
        value = EdmString.getInstance().toUriLiteral(Encoder.encode(value));
    }
    location.append(value);
  }
  location.append(")");
  return location.toString();
}