Serializes this change event into a {@link BsonDocument}.
@return the serialized document.
176214
@Override
  public BsonDocument toBsonDocument() {
    final BsonDocument asDoc = new BsonDocument();
    asDoc.put(Fields.ID_FIELD, id);

    asDoc.put(Fields.OPERATION_TYPE_FIELD, new BsonString(getOperationType().toRemote()));

    final BsonDocument nsDoc = new BsonDocument();
    nsDoc.put(Fields.NS_DB_FIELD, new BsonString(ns.getDatabaseName()));
    nsDoc.put(Fields.NS_COLL_FIELD, new BsonString(getNamespace().getCollectionName()));
    asDoc.put(Fields.NS_FIELD, nsDoc);

    asDoc.put(Fields.DOCUMENT_KEY_FIELD, getDocumentKey());

    if (getFullDocument() != null && (getFullDocument() instanceof BsonValue)
        && ((BsonValue) getFullDocument()).isDocument()) {
      asDoc.put(Fields.FULL_DOCUMENT_FIELD, (BsonValue) getFullDocument());
    }

    if (getUpdateDescription() != null) {
      asDoc.put(Fields.UPDATE_DESCRIPTION_FIELD, getUpdateDescription().toBsonDocument());
    }

    asDoc.put(Fields.WRITE_PENDING_FIELD, new BsonBoolean(hasUncommittedWrites()));
    return asDoc;
  }
55308
private ChangeEvent<BsonDocument> getSynthesizedRemoteChangeEventForDocument(
      final CoreRemoteMongoCollection<BsonDocument> remoteColl,
      final BsonValue documentId
  ) {
    return getSynthesizedRemoteChangeEventForDocument(
        remoteColl.getNamespace(),
        documentId,
        remoteColl.find(getDocumentIdFilter(documentId)).first());
  }
69620
public <A extends Appendable> A document(A output) throws IOException {
        StringBuilder line = new StringBuilder();
        for (Field field: Beans.getKnownInstanceFields(_prototype)) {
            description d = field.getAnnotation(description.class);
            if (d == null) continue;

            placeholder p = field.getAnnotation(placeholder.class);
            String n = Strings.splitCamelCase(field.getName(), "-").toLowerCase();
            char key = getShortOptionKey(field);
            if ((field.getType() == Boolean.class || field.getType() == Boolean.TYPE) && _booleans.containsKey(key)) {
                line.append("  -").append(key).append(", --").append(n);
            } else {
                line.append("  --").append(n).append('=').append(p != null ? p.value() : "value");
            }
                if (line.length() < 16) {
                    for (int i = line.length(); i < 16; ++i) line.append(' ');
                    line.append(d.value());
                } else {
                    line.append("\n\t\t").append(d.value());
                }
            output.append(line.toString()).append('\n');
            line.setLength(0);
        }

        return output;
    }
28463
public CBlob toCBlob()
    {
        CBlob cBlob = new CBlob( getPath(), length, contentType );
        cBlob.setModificationDate( updated );
        
        return cBlob;
    }
990579
@Override
    public Document newDocument(Namespace namespace,
                                Collection<Statement> statements,
                                Collection<Bundle> bundles) {
	Document res = of.createDocument();
	res.setNamespace(namespace);
	res.getStatementOrBundle()
	   .addAll(statements);
	res.getStatementOrBundle()
	   .addAll(bundles);
	return res;
    }
693641
public static DocumentBuilder createDocumentBuilder() {
		final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		disableXXE(dbf);
		DocumentBuilder builder = null;
		try {
			builder = dbf.newDocumentBuilder();
		} catch (Exception e) {
			throw new UtilException(e, "Create xml document error!");
		}
		return builder;
	}
75900
private synchronized Document asXmlDocument() throws IOException {
    Document doc;
    try {
      doc =
        DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException pe) {
      throw new IOException(pe);
    }
    Element conf = doc.createElement("configuration");
    doc.appendChild(conf);
    conf.appendChild(doc.createTextNode("\n"));
    handleDeprecation(); //ensure properties is set and deprecation is handled
    for (Enumeration<Object> e = properties.keys(); e.hasMoreElements();) {
      String name = (String)e.nextElement();
      Object object = properties.get(name);
      String value = null;
      if (object instanceof String) {
        value = (String) object;
      }else {
        continue;
      }
      Element propNode = doc.createElement("property");
      conf.appendChild(propNode);

      Element nameNode = doc.createElement("name");
      nameNode.appendChild(doc.createTextNode(name));
      propNode.appendChild(nameNode);

      Element valueNode = doc.createElement("value");
      valueNode.appendChild(doc.createTextNode(value));
      propNode.appendChild(valueNode);

      if (updatingResource != null) {
        String[] sources = updatingResource.get(name);
        if(sources != null) {
          for(String s : sources) {
            Element sourceNode = doc.createElement("source");
            sourceNode.appendChild(doc.createTextNode(s));
            propNode.appendChild(sourceNode);
          }
        }
      }

      conf.appendChild(doc.createTextNode("\n"));
    }
    return doc;
  }
55744
function()
	{
		var doc = null;
		
		if (document.implementation && document.implementation.createDocument)
		{
			doc = document.implementation.createDocument('', '', null);
		}
		else if (window.ActiveXObject)
		{
			doc = new ActiveXObject('Microsoft.XMLDOM');
	 	}
	 	
	 	return doc;
	}
891921
public Document serviceToDocument() {
        final AtomService service = this;

        final Document doc = new Document();
        final Element root = new Element("service", ATOM_PROTOCOL);
        doc.setRootElement(root);
        final List<Workspace> spaces = service.getWorkspaces();
        for (final Workspace space : spaces) {
            root.addContent(space.workspaceToElement());
        }
        return doc;
    }
368703
public ChangeEvent<DocumentT> withoutUncommittedWrites() {
    return new ChangeEvent<>(this.getId(),
        this.getOperationType(),
        this.getFullDocument(),
        this.getNamespace(),
        this.getDocumentKey(),
        this.getUpdateDescription(),
        false);
  }
261645
public Document serialiseToDocument(final Object obj)
	{
		final Document document = DOMUtils.createDocumentBuilder().newDocument();

		serialise(obj, document);

		return document;
	}
635569
public Document doc() throws IOException {
		// if the stream has already been read, use the text format
		InputSource is;
		if (document == null) {
			if (text == null) {
				is = new InputSource(inputStream);
				is.setEncoding(getCharSet().name());
			} else {
				is = new InputSource(new StringReader(text));
			}
			try {
				document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			}
			inputStream.close();
		}
		return document;
	}
419335
void storeNextEvent() {
    try {
      if (currentStream != null && currentStream.isOpen()) {
        final StitchEvent<ChangeEvent<BsonDocument>> event = currentStream.nextEvent();
        if (event == null) {
          return;
        }

        if (event.getError() != null) {
          throw event.getError();
        }

        if (event.getData() == null) {
          return;
        }

        logger.debug(String.format(Locale.US,
            "NamespaceChangeStreamListener::stream ns=%s event found: op=%s id=%s",
            nsConfig.getNamespace(), event.getData().getOperationType(), event.getData().getId()));
        nsLock.writeLock().lockInterruptibly();
        try {
          events.put(BsonUtils.getDocumentId(event.getData().getDocumentKey()), event.getData());
        } finally {
          nsLock.writeLock().unlock();
        }

        for (final Callback<ChangeEvent<BsonDocument>, Object> watcher : watchers) {
          watcher.onComplete(OperationResult.successfulResultOf(event.getData()));
        }
      }
    } catch (final InterruptedException | IOException ex) {
      logger.info(String.format(
          Locale.US,
          "NamespaceChangeStreamListener::stream ns=%s interrupted on "
              + "fetching next event: %s",
          nsConfig.getNamespace(),
          ex));
      logger.info("stream END – INTERRUPTED");
      Thread.currentThread().interrupt();
    } catch (final Exception ex) {
      // TODO: Emit error through DataSynchronizer as an ifc
      logger.error(String.format(
          Locale.US,
          "NamespaceChangeStreamListener::stream ns=%s exception on fetching next event: %s",
          nsConfig.getNamespace(),
          ex), ex);
      logger.info("stream END – EXCEPTION");
      final boolean wasInterrupted = Thread.currentThread().isInterrupted();
      this.close();
      if (wasInterrupted) {
        Thread.currentThread().interrupt();
      }
    }
  }
1031441
public static BsonDocument copyOfDocument(final BsonDocument document) {
    final BsonDocument newDocument = new BsonDocument();
    for (final Map.Entry<String, BsonValue> kv : document.entrySet()) {
      newDocument.put(kv.getKey(), kv.getValue());
    }
    return newDocument;
  }
63309
public Blob update() {
    // [START update]
    Map<String, String> newMetadata = new HashMap<>();
    newMetadata.put("key", "value");
    blob.toBuilder().setMetadata(null).build().update();
    Blob updatedBlob = blob.toBuilder().setMetadata(newMetadata).build().update();
    // [END update]
    return updatedBlob;
  }
1172343
private static BsonDocument withNewVersion(
      final BsonDocument document,
      final BsonDocument newVersion
  ) {
    final BsonDocument newDocument = BsonUtils.copyOfDocument(document);
    newDocument.put(DOCUMENT_VERSION_FIELD, newVersion);
    return newDocument;
  }
797860
public static UpdateDescription fromBsonDocument(final BsonDocument document) {
    keyPresent(Fields.UPDATED_FIELDS_FIELD, document);
    keyPresent(Fields.REMOVED_FIELDS_FIELD, document);

    final BsonArray removedFieldsArr =
        document.getArray(Fields.REMOVED_FIELDS_FIELD);
    final Set<String> removedFields = new HashSet<>(removedFieldsArr.size());
    for (final BsonValue field : removedFieldsArr) {
      removedFields.add(field.asString().getValue());
    }

    return new UpdateDescription(document.getDocument(Fields.UPDATED_FIELDS_FIELD), removedFields);
  }