org.exist.fluent
Class Document

java.lang.Object
  extended by org.exist.fluent.Resource
      extended by org.exist.fluent.NamedResource
          extended by org.exist.fluent.Document
Direct Known Subclasses:
XMLDocument

public class Document
extends NamedResource

A document from the database, either binary or XML. Note that querying a non-XML document is harmless, but will never return any results.

Author:
Piotr Kaminski

Nested Class Summary
static class Document.Event
          An event that concerns a document.
static interface Document.Listener
          Listener for events affecting documents.
 class Document.ListenersFacet
          The facet that gives access to a document's listeners.
static class Document.MetadataFacet
          The metadata facet for this document.
 
Method Summary
 String contentsAsString()
          Return the contents of this document interpreted as a string.
 Document copy(Folder destination, Name name)
          Copy this document to another collection, potentially changing the copy's name in the process.
 void delete()
          Delete this document from the database.
 boolean equals(Object o)
           
 void export(File destination)
          Export this document to the given file, overwriting it if it already exists.
 Folder folder()
          Return the folder that contains this document.
 int hashCode()
           
 long length()
          Return the length of this document, in bytes.
 Document.ListenersFacet listeners()
          Return the listeners facet for this document, used for adding and removing document listeners.
 Document.MetadataFacet metadata()
          Return the metadata facet for this resource, which lets you read and manipulate metadata such as ownership, access permissions, and creation/modification timestamps.
 void move(Folder destination, Name name)
          Move this document to another collection, potentially changing its name in the process.
 String name()
          Return the local filename of this document.
 String path()
          Return the full path of this document.
 String toString()
          Return a string representation of the reference to this document.
 void write(OutputStream stream)
          Copy the contents of the document to the given stream.
 XMLDocument xml()
          Cast this document to an XMLDocument, if possible.
 
Methods inherited from class org.exist.fluent.Resource
database, namespaceBindings, query
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

listeners

public Document.ListenersFacet listeners()
Return the listeners facet for this document, used for adding and removing document listeners.

Returns:
the listeners facet for this document

metadata

public Document.MetadataFacet metadata()
Description copied from class: NamedResource
Return the metadata facet for this resource, which lets you read and manipulate metadata such as ownership, access permissions, and creation/modification timestamps.

Specified by:
metadata in class NamedResource
Returns:
the metadata facet for this resource

xml

public XMLDocument xml()
Cast this document to an XMLDocument, if possible.

Returns:
this document cast as an XML document
Throws:
DatabaseException - if this document is not an XML document

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Return a string representation of the reference to this document. The representation will list the document's path, but will not include its contents.

Overrides:
toString in class Object
Returns:
a string representation of this document

name

public String name()
Return the local filename of this document. This name will never contain slashes ('/').

Specified by:
name in class NamedResource
Returns:
the local filename of this document

path

public String path()
Return the full path of this document. This is the path of its parent folder plus its filename.

Specified by:
path in class NamedResource
Returns:
the full path of this document

folder

public Folder folder()
Return the folder that contains this document.

Returns:
the folder that contains this document

length

public long length()
Return the length of this document, in bytes. For binary documents, this is the actual size of the file; for XML documents, this is the approximate amount of space that the document occupies in the database, and is unrelated to its serialized length.

Returns:
the length of this document, in bytes

delete

public void delete()
Delete this document from the database.

Specified by:
delete in class NamedResource

copy

public Document copy(Folder destination,
                     Name name)
Copy this document to another collection, potentially changing the copy's name in the process.

Specified by:
copy in class NamedResource
Parameters:
destination - the destination folder for the copy
name - the desired name for the copy
Returns:
the new copy of the document
See Also:
Name

move

public void move(Folder destination,
                 Name name)
Move this document to another collection, potentially changing its name in the process. This document will refer to the document in its new location after this method returns. You can easily use this method to move a document without changing its name (doc.move(newFolder, Name.keepCreate())) or to rename a document without changing its location (doc.move(doc.folder(), Name.create(newName))).

Specified by:
move in class NamedResource
Parameters:
destination - the destination folder for the move
name - the desired name for the moved document
See Also:
Name

contentsAsString

public String contentsAsString()
Return the contents of this document interpreted as a string. Binary documents are decoded using the default character encoding specified for the database.

Returns:
the contents of this document
Throws:
DatabaseException - if the encoding is not supported or some other unexpected IOException occurs
See Also:
Database.setDefaultCharacterEncoding(String)

export

public void export(File destination)
            throws IOException
Export this document to the given file, overwriting it if it already exists.

Parameters:
destination - the file to export to
Throws:
IOException - if the export failed due to an I/O error

write

public void write(OutputStream stream)
           throws IOException
Copy the contents of the document to the given stream. XML documents will use the default character encoding set for the database.

Parameters:
stream - the output stream to copy the document to
Throws:
IOException - in case of I/O problems; WARNING: I/O exceptions are currently logged and eaten by eXist, so they won't propagate to this layer!
See Also:
Database.setDefaultCharacterEncoding(String)


Copyright (C) All rights reserved.