Database Reference
In-Depth Information
File
A file resource in Milton is equivalent to a resource in eXist. Milton does not
differentiate between XML files and binary files in eXist; to Milton they are
all just files.
From a host we can retrieve resources, and we can use those resources to find
subresources (see Example 13-2 ).
Example 13-2. Retrieving a resource from eXist using Milton
final Resource resource = host . child ( "my-collection" );
if ( resource != null ) {
if ( resource instanceof Folder ) {
//resource is a Folder, i.e. collection in eXist
final Folder folder = (( Folder ) resource );
//TODO you do something with the Folder here
} else if ( resource instanceof File ) {
//resource is a File, i.e. resource in eXist
final File file = (( File ) resource );
//TODO you do something with the File here
}
}
Path
In Milton, the Path object encapsulates a path to a resource. The path may be
either absolute from the root path, or relative to an existing resource.
We can construct paths that are relative to other paths and then execute opera‐
tions relating to those paths (see Example 13-3 ).
Example 13-3. Creating the collection /db/my-collection/apples/pears in eXist with
Milton
Path rootPath = host . path ();
Path pathPears = Path . path ( rootPath , "my-collection/apples/pears" );
Folder pears = host . getOrCreateFolder ( pathPears , true );
Examples. The source code of two small complete examples of using Milton from Java
to store a file and retrieve a file is included in the folder chapters/integration/webdav-
client of the book-code Git repository (see “Getting the Source Code” on page 15 ).
To compile the examples, enter the webdav-client folder and run mvn package .
You can then execute the StoreApp example like so:
java -jar webdav-client-store/target/webdav-client-store-1.0-example.jar
Search WWH ::




Custom Search