Page 1 of 1

clone the R of docx4j!!!!

PostPosted: Sat Oct 31, 2009 5:33 am
by kevin Feng
I add the Cloneable of interface into the R and the Text than override the method of clone both of them,
because I want a deep Clone of the R so that I can change one Text of the R but the other is not be changed,
however it is not work.they also quote the same object(for example the rPr or the runContent in the R)

the clone method in Text Class:
Code: Select all
    public Text clone() throws CloneNotSupportedException{
       return (Text)super.clone();
    }


the clone method in R Class:
Code: Select all
    public R clone() throws CloneNotSupportedException{
       R r= (R)super.clone();
       Text text = new Text();
      for (Object o2 : runContent) {
         if (o2 instanceof javax.xml.bind.JAXBElement) {
            // directly to Text.
            if (((JAXBElement) o2).getDeclaredType().getName().equals("org.docx4j.wml.Text")) {
               org.docx4j.wml.Text t = (org.docx4j.wml.Text) ((JAXBElement) o2).getValue();
               text = t.clone();
            }
         }
      }
      for (int i=0;i<r.getRunContent().size();i++){
         if (r.getRunContent().get(i) instanceof javax.xml.bind.JAXBElement) {
            // directly to Text.
            if (((JAXBElement) r.getRunContent().get(i)).getDeclaredType().getName().equals("org.docx4j.wml.Text")) {
               ((JAXBElement) r.getRunContent().get(i)).setValue(text);
            }
         }
      }
       return r;
    }


has any problem?? what I forget to do??

Re: clone the R of docx4j!!!!

PostPosted: Sat Oct 31, 2009 5:57 am
by jason
Hi, and welcome.

In general I'd suggest you use the deepCopy method in our XmlUtils class.

[comment on code deleted]

cheers .. Jason

Re: clone the R of docx4j!!!!

PostPosted: Sat Oct 31, 2009 6:25 am
by kevin Feng
Thank you for your reply :)

The first loop In my code,I think it mean that I clone the Text Object from the original r then put it into a temp Object of Text.
In the second loop, I loop the clone r to put the temp Object of Text into the Text of clone r.

Re: clone the R of docx4j!!!!

PostPosted: Sat Oct 31, 2009 8:11 am
by jason
As far as copying runContent is concerned, shouldn't it be more like:

Code: Select all
      public R clone() throws CloneNotSupportedException{
             R r= (R)super.clone();
             r.runContent = new ArrayList<Object>();
             Text text = new Text();
            for (Object o2 : runContent) {
               if (o2 instanceof javax.xml.bind.JAXBElement) {
                  // directly to Text.
                  if (((JAXBElement) o2).getDeclaredType().getName().equals("org.docx4j.wml.Text")) {
                     org.docx4j.wml.Text t = (org.docx4j.wml.Text) ((JAXBElement) o2).getValue();
                     text = t.clone();
                     r.getRunContent().add(text);
                  }
               }
            }


Edit: Note the line:

Code: Select all
r.runContent = new ArrayList<Object>();

Re: clone the R of docx4j!!!!

PostPosted: Sat Oct 31, 2009 11:53 am
by kevin Feng
yes , you are right.

I use the the clone method of the XmlUtils class. it works..

thank you very much jason...