public Object objectDone() {
    Object o = super.objectDone();
    BSONObject b = (BSONObject) o;

    if (!_lastArray) {
      if (b.containsField("$oid")) {
        o = new ObjectId((String) b.get("$oid"));
        if (!isStackEmpty()) {
          gotObjectId(_lastName, (ObjectId) o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$date")) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
        o = format.parse((String) b.get("$date"), new ParsePosition(0));
        if (!isStackEmpty()) {
          cur().put(_lastName, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$regex")) {
        o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
        if (!isStackEmpty()) {
          cur().put(_lastName, o);
        } else {
          setRoot(o);
        }
      }
    }

    return o;
  }
  public Object objectDone() {
    String name = curName();
    Object o = super.objectDone();
    BSONObject b = (BSONObject) o;

    // override the object if it's a special type
    if (!_lastArray) {
      if (b.containsField("$oid")) {
        o = new ObjectId((String) b.get("$oid"));
        if (!isStackEmpty()) {
          gotObjectId(name, (ObjectId) o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$date")) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        GregorianCalendar calendar = new GregorianCalendar(new SimpleTimeZone(0, "GMT"));
        format.setCalendar(calendar);
        String txtdate = (String) b.get("$date");
        o = format.parse(txtdate, new ParsePosition(0));
        if (o == null) {
          // try older format with no ms
          format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
          format.setCalendar(calendar);
          o = format.parse(txtdate, new ParsePosition(0));
        }
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$regex")) {
        o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$ts")) {
        Long ts = ((Number) b.get("$ts")).longValue();
        Long inc = ((Number) b.get("$inc")).longValue();
        o = new BSONTimestamp(ts.intValue(), inc.intValue());
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$code")) {
        if (b.containsField("$scope")) {
          o = new CodeWScope((String) b.get("$code"), (DBObject) b.get("$scope"));
        } else {
          o = new Code((String) b.get("$code"));
        }
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$ref")) {
        o = new DBRef(null, (String) b.get("$ref"), b.get("$id"));
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$minKey")) {
        o = new MinKey();
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$maxKey")) {
        o = new MaxKey();
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$uuid")) {
        o = UUID.fromString((String) b.get("$uuid"));
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      }
    }
    return o;
  }
Ejemplo n.º 3
0
 protected Object getElementValue(ElementRecord record) {
   switch (record.type) {
     case BSON.EOO:
     case BSON.UNDEFINED:
     case BSON.NULL:
       return null;
     case BSON.MAXKEY:
       return new MaxKey();
     case BSON.MINKEY:
       return new MinKey();
     case BSON.BOOLEAN:
       return (_input.get(record.valueOffset) != 0);
     case BSON.NUMBER_INT:
       return _input.getInt(record.valueOffset);
     case BSON.TIMESTAMP:
       int inc = _input.getInt(record.valueOffset);
       int time = _input.getInt(record.valueOffset + 4);
       return new BSONTimestamp(time, inc);
     case BSON.DATE:
       return new Date(_input.getLong(record.valueOffset));
     case BSON.NUMBER_LONG:
       return _input.getLong(record.valueOffset);
     case BSON.NUMBER:
       return Double.longBitsToDouble(_input.getLong(record.valueOffset));
     case BSON.OID:
       return new ObjectId(
           _input.getIntBE(record.valueOffset),
           _input.getIntBE(record.valueOffset + 4),
           _input.getIntBE(record.valueOffset + 8));
     case BSON.SYMBOL:
       return new Symbol(_input.getUTF8String(record.valueOffset));
     case BSON.CODE:
       return new Code(_input.getUTF8String(record.valueOffset));
     case BSON.STRING:
       return _input.getUTF8String(record.valueOffset);
     case BSON.CODE_W_SCOPE:
       int strsize = _input.getInt(record.valueOffset + 4);
       String code = _input.getUTF8String(record.valueOffset + 4);
       BSONObject scope =
           (BSONObject)
               _callback.createObject(_input.array(), record.valueOffset + 4 + 4 + strsize);
       return new CodeWScope(code, scope);
     case BSON.REF:
       int csize = _input.getInt(record.valueOffset);
       String ns = _input.getCString(record.valueOffset + 4);
       int oidOffset = record.valueOffset + csize + 4;
       ObjectId oid =
           new ObjectId(
               _input.getIntBE(oidOffset),
               _input.getIntBE(oidOffset + 4),
               _input.getIntBE(oidOffset + 8));
       return _callback.createDBRef(ns, oid);
     case BSON.OBJECT:
       return _callback.createObject(_input.array(), record.valueOffset);
     case BSON.ARRAY:
       return _callback.createArray(_input.array(), record.valueOffset);
     case BSON.BINARY:
       return readBinary(record.valueOffset);
     case BSON.REGEX:
       int n = sizeCString(record.valueOffset);
       String pattern = _input.getCString(record.valueOffset);
       String flags = _input.getCString(record.valueOffset + n);
       return Pattern.compile(pattern, BSON.regexFlags(flags));
     default:
       throw new BSONException(
           "Invalid type " + record.type + " for field " + getElementFieldName(record.offset));
   }
 }