Esempio n. 1
0
  /**
   * Writes the given value to the JSON writer. currently the following conversions are done:
   *
   * <table>
   * <tr>
   * <th>JSR Property Type</th>
   * <th>JSON Value Type</th>
   * </tr>
   * <tr>
   * <td>BINARY</td>
   * <td>always 0 as long</td>
   * </tr>
   * <tr>
   * <td>DATE</td>
   * <td>converted date string as defined by ECMA</td>
   * </tr>
   * <tr>
   * <td>BOOLEAN</td>
   * <td>boolean</td>
   * </tr>
   * <tr>
   * <td>LONG</td>
   * <td>long</td>
   * </tr>
   * <tr>
   * <td>DOUBLE</td>
   * <td>double</td>
   * </tr>
   * <tr>
   * <td><i>all other</li>
   * </td>
   * <td>string</td>
   * </tr>
   * </table>
   *
   * <sup>1</sup> Currently not implemented and uses 0 as default.
   *
   * @param w json writer
   * @param v value to dump
   */
  protected void dumpValue(JSONWriter w, Value v)
      throws ValueFormatException, IllegalStateException, RepositoryException, JSONException {

    switch (v.getType()) {
      case PropertyType.BINARY:
        w.value(0);
        break;

      case PropertyType.DATE:
        w.value(format(v.getDate()));
        break;

      case PropertyType.BOOLEAN:
        w.value(v.getBoolean());
        break;

      case PropertyType.LONG:
        w.value(v.getLong());
        break;

      case PropertyType.DOUBLE:
        w.value(v.getDouble());

        break;
      default:
        w.value(v.getString());
    }
  }
Esempio n. 2
0
  @Test
  public void testNextWithLiteralDouble() throws Exception {
    when(mockValue.getType()).thenReturn(PropertyType.DOUBLE);
    when(mockValue.getDouble()).thenReturn(1.0);
    final QuerySolution solution = testObj.next();

    assertEquals(1.0, solution.get("a").asLiteral().getDouble(), 0.1);
  }
Esempio n. 3
0
 /** Tests conversion from Date type to Double type. */
 public void testGetDouble() throws RepositoryException {
   Value val = PropertyUtil.getValue(prop);
   double d = val.getDouble();
   long mili = val.getDate().getTimeInMillis();
   assertEquals(
       "Conversion from a Date value to a Double value "
           + "returns a different number of miliseconds.",
       mili,
       (long) d);
 }
 /**
  * Utility method used to convert a JCR {@link Value} object into a valid graph property value.
  *
  * @param value the JCR value; may be null
  * @param jcrProperty the JCR property, used to access the session (if needed)
  * @return the graph representation of the value
  * @throws RepositoryException if there is an error working with the session
  */
 public Object convert(Value value, javax.jcr.Property jcrProperty) throws RepositoryException {
   if (value == null) return null;
   try {
     switch (value.getType()) {
       case javax.jcr.PropertyType.BINARY:
         return factories.getBinaryFactory().create(value.getBinary().getStream(), 0);
       case javax.jcr.PropertyType.BOOLEAN:
         return factories.getBooleanFactory().create(value.getBoolean());
       case javax.jcr.PropertyType.DATE:
         return factories.getDateFactory().create(value.getDate());
       case javax.jcr.PropertyType.DOUBLE:
         return factories.getDoubleFactory().create(value.getDouble());
       case javax.jcr.PropertyType.LONG:
         return factories.getLongFactory().create(value.getLong());
       case javax.jcr.PropertyType.NAME:
         return factories.getNameFactory().create(value.getString());
       case javax.jcr.PropertyType.PATH:
         return factories.getPathFactory().create(value.getString());
       case javax.jcr.PropertyType.REFERENCE:
         return factories.getReferenceFactory().create(value.getString());
       case javax.jcr.PropertyType.STRING:
         return factories.getStringFactory().create(value.getString());
       case javax.jcr.PropertyType.UNDEFINED:
         // We don't know what type of values these are, but we need to convert any value
         // that depends on namespaces (e.g., names and paths) into Graph objects.
         try {
           // So first try a name ...
           return factories.getNameFactory().create(value.getString());
         } catch (ValueFormatException e) {
           // Wasn't a name, so try a path ...
           try {
             return factories.getPathFactory().create(value.getString());
           } catch (ValueFormatException e2) {
             // Wasn't a path, so finally treat as a string ...
             return factories.getStringFactory().create(value.getString());
           }
         }
     }
   } catch (ValueFormatException e) {
     // There was an error converting the JCR value into the appropriate graph value
     String typeName = javax.jcr.PropertyType.nameFromValue(value.getType());
     throw new RepositoryException(
         JcrConnectorI18n.errorConvertingJcrValueOfType.text(value.getString(), typeName));
   }
   return null;
 }
Esempio n. 5
0
 protected void assertCanObtainValue(Value value, int expectedType) throws Exception {
   switch (expectedType) {
     case PropertyType.BINARY:
       Binary binary = value.getBinary();
       try {
         InputStream stream = binary.getStream();
         assertThat(stream, is(notNullValue()));
         try {
           stream.read();
         } finally {
           stream.close();
         }
       } finally {
         binary.dispose();
       }
       break;
     case PropertyType.BOOLEAN:
       assertThat(value.getBoolean() || !value.getBoolean(), is(true));
       break;
     case PropertyType.DATE:
       Calendar cal = value.getDate();
       assertThat(cal, is(notNullValue()));
       break;
     case PropertyType.DOUBLE:
       double doubleValue = value.getDouble();
       assertThat(doubleValue < 0.0d || doubleValue >= -1.0d, is(true));
       break;
     case PropertyType.LONG:
       long longValue = value.getLong();
       assertThat(longValue < 0L || longValue >= 0L, is(true));
       break;
     case PropertyType.NAME:
       context.getValueFactories().getNameFactory().create(value.getString());
       break;
     case PropertyType.PATH:
       context.getValueFactories().getPathFactory().create(value.getString());
       break;
     case PropertyType.REFERENCE:
       UUID uuid = context.getValueFactories().getUuidFactory().create(value.getString());
       assertThat(uuid, is(notNullValue()));
       break;
     case PropertyType.STRING:
       value.getString();
       break;
   }
 }
 private static Object stringValue(Value value)
     throws ValueFormatException, IllegalStateException, RepositoryException {
   switch (value.getType()) {
     case PropertyType.STRING:
     case PropertyType.NAME:
     case PropertyType.REFERENCE:
     case PropertyType.PATH:
       return value.getString();
     case PropertyType.BOOLEAN:
       return value.getBoolean();
     case PropertyType.LONG:
       return value.getLong();
     case PropertyType.DOUBLE:
       return value.getDouble();
     case PropertyType.DATE:
       return DateUtils.iso8601(value.getDate());
     default:
       return value.toString();
   }
 }