Page 1 of 1

cannot save to outputstream or ByteArrayOutputStream

PostPosted: Fri Jun 02, 2017 6:48 am
by fwu
hello,

Im trying to save a docx file to an outputstream or a ByteArrayOutputStream. Im using the save method like this. Note that Im able to load the file, get the title and cusotm properties. Only the save doesnt works.

Code: Select all
WordprocessingMLPackage wordMLPackage=null;

wordMLPackage = WordprocessingMLPackage.load(is);

OutputStream os=null;
            
wordMLPackage.save(os);



I also tried like this:

Code: Select all
ByteArrayOutputStream memoryStream=null;

WordprocessingMLPackage wordMLPackage=null;

wordMLPackage = WordprocessingMLPackage.load(is);
            
wordMLPackage.save(memoryStream);



they are both failling. Worst than that I dont get any error, although Im using try catch including the generic exception catch. Only a general error on the Alfresco side.

What can be happening here?

ps: im using docx4j-3.2.1.jar

thank you

fwu

Re: cannot save to outputstream or ByteArrayOutputStream

PostPosted: Fri Jun 02, 2017 8:12 am
by jason
In both your examples, you are setting the output stream to null. Really?

Re: cannot save to outputstream or ByteArrayOutputStream

PostPosted: Fri Jun 02, 2017 9:00 am
by fwu
hello,

No, that is not sequencial code. I set it to null before the try block.

I then do this, for instance, for the memorystream:

memoryStream = new ByteArrayOutputStream();

I dont know if I cant get the error due to the fact that I needed to remove/delete the jcl-over-slf4j-1.7.5.jar. Nevertheless, all is working with the exception of the save method. I dont and I beleive I cant save to a file (Im inside Alfresco workflow code where I dont have direct access to the file system). That is why Im using a bytearray or outputstream.

regards,

fwu

Re: cannot save to outputstream or ByteArrayOutputStream

PostPosted: Fri Jun 02, 2017 11:26 am
by fwu
hello,

More info:

When I look to the load method I get this:

WordprocessingMLPackage org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(InputStream is) throws Docx4JException

But when I look to the save method I get this:

void org.docx4j.openpackaging.packages.OpcPackage.save(OutputStream outStream) throws Docx4JException

So, the same object points to different packages? How can this be?


Also,

I get one docx4j error before loading the docx (please note that I can load the docx without any problem):

ERROR [docx4j.jaxb.NamespacePrefixMapperUtils] [http-apr-8080-exec-2] name: com.sun.xml.internal.bind.namespacePrefixMapper value: org.docx4j.jaxb.NamespacePrefixMapperSunInternal@7e2e27ae .. trying RI.

Can this be a problem for the save method?

Is there any other way of saving a previously open file to a memoery stream?

thank you

Re: cannot save to outputstream or ByteArrayOutputStream

PostPosted: Fri Jun 02, 2017 6:28 pm
by jason
Seems like you are having problems with some basic Java concepts.

Please post a very simple complete example of how you are trying to save to a ByteArrayOutputStream; then demonstrate it didn't work. (Hint: toByteArray)

Re: cannot save to outputstream or ByteArrayOutputStream

PostPosted: Fri Jun 02, 2017 8:35 pm
by fwu
hello

here is the



Code: Select all
InputStream is = null;
ByteArrayOutputStream memoryStream=null;
ContentReader oWORDContent=null;

WordprocessingMLPackage wordMLPackage=null;

try {

memoryStream = new ByteArrayOutputStream();

// Get the document from the repo
NodeRef oNodeRef = new NodeRef("workspace","SpacesStore",docRef);
         
oWORDContent = getReader(oNodeRef);

// Get content into InputStream
is = oWORDContent.getContentInputStream();

   try {
              
   wordMLPackage = WordprocessingMLPackage.load(is);
   

//custom properties
org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart = wordMLPackage.getDocPropsCustomPart();

// DO things here regarding the properties...


System.out.println("before save");

// save to a memory stream
wordMLPackage.save(memoryStream);

System.out.println("after save");

}
catch (Docx4JException e) {

   System.out.println("error1" );

   } catch (Exception e) {
      System.out.println("error2" );
   
   }

// do something here with bytearray... cant reach this

}
         
         catch (IOException e)
         {
            System.out.println("error3");
           
           
   }
         catch(Exception e){
            System.out.println("error4");
           
         }

         finally
         {
       System.out.println("I reach this...");
}



So, I can load the document, get the properties, but I cant save the document. The code doesnt enter in any catch block of the fourth, but it reach the finally block.

thank you jason for your time

Re: cannot save to outputstream or ByteArrayOutputStream

PostPosted: Fri Jun 02, 2017 9:18 pm
by fwu
debug info:


ERROR [docx4j.jaxb.NamespacePrefixMapperUtils] [http-apr-8080-exec-9] name: com.sun.xml.internal.bind.namespacePrefixMapper value: org.docx4j.jaxb.NamespacePrefixMapperSunInternal@3332d32c .. trying RI.

before save
I reach this...

Re: cannot save to outputstream or ByteArrayOutputStream

PostPosted: Fri Jun 02, 2017 10:42 pm
by fwu
hello,

It is an error and not an exception.

Here it is:

java.lang.NoSuchMethodError: org.docx4j.openpackaging.packages.WordprocessingMLPackage.save(Ljava/io/OutputStream;)V
at org.alfresco.extension.wordtoolkit.service.WORDToolkitServiceImpl.mergeDocProperties


So, im calling a method from an object that doesnt have that method. But Im able to do that in development time. However, I can see at development time that the method exists only in another package.

the load method come from here:

WordprocessingMLPackage org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(InputStream is) throws Docx4JException


the save comes from here:

void org.docx4j.openpackaging.packages.OpcPackage.save(OutputStream outStream) throws Docx4JException


So Im able to call both method in development time using the same object, but they come from different packages.

Also, in my pom I only have a dependecy to version 3.2.1. however my compilation will get that version and the 2.8.1 version. Are these problems connected?

Re: cannot save to outputstream or ByteArrayOutputStream

PostPosted: Fri Jun 02, 2017 11:44 pm
by jason
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
import java.io.ByteArrayOutputStream;

import org.docx4j.Docx4J;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public class test1 {

        public static void main(String[] args) throws Docx4JException {

                WordprocessingMLPackage pkg = WordprocessingMLPackage.createPackage();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                Docx4J.save(pkg, baos);
                System.out.println(baos.toByteArray().length);
        }
}

 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


outputs 5454.

fwu wrote:n my pom I only have a dependecy to version 3.2.1. however my compilation will get that version and the 2.8.1 version. Are these problems connected?


You must ensure you only have 1 version of docx4j on your classpath

Re: cannot save to outputstream or ByteArrayOutputStream

PostPosted: Sat Jun 03, 2017 2:19 am
by fwu
jason,

After deleting the old file it works. I thought I had tested that before. Nevertheless, Im not able to avoid the deployment of the old version even using provided in the dependence...

thank you