[daisy] Daisy detachment
Bruno Dumon
bruno at outerthought.org
Tue Jul 11 02:50:49 CDT 2006
On Tue, 2006-07-11 at 03:03 -0400, tim_cranfield at goldenboat.net wrote:
> In the detachment project we need to be able to persist a
> Document offline. It was suggested that we use an XML file
> written with document.getXml(), and a separate file for
> anything else useful like branch and language names.
> http://lists.cocoondev.org/pipermail/daisy/2006-July/004380.html
>
> Looking at the methods for DocumentDocument, I'm wondering if
> and how its possible to set fields, links and parts.
Not sure if I understand the question, is it something like this you're
looking for?
DocumentDocument documentDoc = DocumentDocument.Factory.newInstance();
DocumentDocument.Document documentXml = documentDoc.addNewDocument();
FieldsDocument.Fields fieldsXml = documentXml.addNewFields();
FieldDocument.Field fieldXml = fieldsXml.addNewField();
fieldXml.setTypeId(123);
fieldXml.addNewString().setStringValue("boe");
(for multivalue-fields, you would call addNewString multiple times)
Or another way to do the same:
FieldDocument.Field anotherFieldXml = FieldDocument.Field.Factory.newInstance();
anotherFieldXml.setTypeId(124);
anotherFieldXml.addNewLong().setLongValue(23232);
FieldDocument.Field[] myFields = new FieldDocument.Field[1];
myFields[0] = anotherFieldXml;
fieldsXml.setFieldArray(myFields);
so here we first created the fields individually (each
XmlBeans-generated object has a "Factory" inner class via which you
create new objects of that type, rather than using the constructor), and
then set the array of fields on the parent fields object.
If you're writing everything in one method, the first approach can as
well be used, the second approach is useful if you're separating things
over multiple methods.
This is what is done in the Daisy implementation: see
DocumentVariantImpl.addXml(DocumentDocument.Document documentXml)
>
> Another issue I'm having is with unzipping detachment files. I
> think the problem is with my zip method :
>
> public static File zipDetachment(String dir2zip,
> ZipOutputStream zos){
> //create a new File object based on the directory we have to
> zip
> File zipDir = new File(dir2zip);
> zipDir.mkdir();
> try {
>
> //get a listing of the directory content
> String[] dirList = zipDir.list();
> byte[] readBuffer = new byte[2156];
> int bytesIn = 0;
> //loop through dirList, and zip the files
> for(int i=0; i<dirList.length; i++) {
> File f = new File(zipDir, dirList[i]);
> if(f.isDirectory()) {
> //if the File object is a directory, call
> this
> //function again to add its content
> recursively
> String filePath = f.getPath();
> zipDetachment(filePath, zos);
> //loop again
> continue;
> }
> //if we reached here, the File object f was not
> a directory
> //create a FileInputStream on top of f
> FileInputStream fis = new FileInputStream(f);
> //create a new zip entry
> ZipEntry anEntry = new ZipEntry(f.getPath());
> //place the zip entry in the ZipOutputStream
> object
> zos.putNextEntry(anEntry);
> //now write the content of the file to the
> ZipOutputStream
> while((bytesIn = fis.read(readBuffer)) != -1){
> zos.write(readBuffer, 0, bytesIn);
> fis.close();
> }
> zos.close();
> }
>
> } catch(Exception e) {
> //handle exception
> }
> return zipDir;
> }
>
> When I list the contents of the zipped file:
>
> entries = zipFile.entries();
>
> while(entries.hasMoreElements()) {
> ZipEntry entry = (ZipEntry)entries.nextElement();
> System.out.println(entry.getName());
> }
>
> The directories are not listed and the paths are printed
> instead of the name. This does not happen when I use the above
> code on other zip files. It may be an issue with the file
> separator character, I have tried various things without
> success.
I may be looking over it, but I never see a call to zos.closeEntry().
This needs to be called after writing the data for each entry, and thus
before writing the next entry.
There are some other things I find confusing:
while((bytesIn = fis.read(readBuffer)) != -1){
zos.write(readBuffer, 0, bytesIn);
fis.close();
}
The stream from which you are reading is closed inside the loop which
reads the data from it. Can't be the intention, I guess.
And after that you call zos.close(); before everything is written to the
zos. The zos.close() should come after the for-loop I think.
Hope this helps.
--
Bruno Dumon http://outerthought.org/
Outerthought - Open Source, Java & XML Competence Support Center
bruno at outerthought.org bruno at apache.org
More information about the daisy
mailing list