You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Mark Kimsal <me...@gmail.com> on 2013/02/05 19:31:34 UTC

XWPF and custom properties duplicating on commit

I'm trying to update the values of custom properties in an XWPFDocument.
 If I do not commit() the properties after changing the values with
CTProperty.setLpwstr() then the values do not change.  If I commit the
properties after changing the value then I get duplicate XML tags in the
docProps/custom.xml and docProps/app.xml


Code:
    {
OPCPackage opc = OPCPackage.open(
  loader.getResourceAsStream("META-INF/"+template+".docx"));

    updateProperties(opc)

    XWPFDocument doc = new XWPFDocument( opc );
    // document bookmar stuff, but even commented out doesn't change
    // duplicate tag behavior
    doc.write(outputStream);
    opc.close();
    }

    private final void updateProperties(OPCPackage opc) {
        POIXMLProperties props = new POIXMLProperties(opc);
        CustomProperties customProperties = props.getCustomProperties();
  org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties
ctprops = customProperties.getUnderlyingProperties();
        List<CTProperty> propList = ctprops.getPropertyList();
        for(CTProperty ctp : propList) {
            System.out.println(ctp.getName());
            if (ctp.getName().equals("LEGAL NAME")) {
                ctp.setLpwstr("Hello World");
            }
        }
        props.commit();  //commenting out does not change docProps/*,
uncommenting gives duplicate tags
    }

Does anyone know how to get property changes to ripple up into the doc
without doing props.commit() ?

app.xml
<?xml version="1.0" encoding="UTF-8"?>
<Properties xmlns="
http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
xmlns:vt="
http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Template>Normal.dotm</Template><TotalTime>0</TotalTime><Pages>3</Pages><Words>1002</Words><Characters>5712</Characters><Application>Microsoft
Office
Word</Application><DocSecurity>0</DocSecurity><Lines>47</Lines><Paragraphs>13</Paragraphs><ScaleCrop>false</ScaleCrop><Company>
</Company><LinksUpToDate>false</LinksUpToDate><CharactersWithSpaces>6701</CharactersWithSpaces><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>14.0000</AppVersion></Properties><?xml
version="1.0" encoding="UTF-8"?>
<Properties xmlns="
http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
xmlns:vt="
http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Template>Normal.dotm</Template><TotalTime>0</TotalTime><Pages>3</Pages><Words>1002</Words><Characters>5712</Characters><Application>Microsoft
Office
Word</Application><DocSecurity>0</DocSecurity><Lines>47</Lines><Paragraphs>13</Paragraphs><ScaleCrop>false</ScaleCrop><Company>
</Company><LinksUpToDate>false</LinksUpToDate><CharactersWithSpaces>6701</CharactersWithSpaces><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>14.0000</AppVersion></Properties>

custom.xml
<?xml version="1.0" encoding="UTF-8"?>
<Properties xmlns="
http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
xmlns:vt="
http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><property
fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2"
name="COUNTRY"><vt:lpwstr>[COUNTRY]</vt:lpwstr></property><property
fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="LEGAL
NAME"><vt:lpwstr>Hello World</vt:lpwstr></property><property
fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4"
name="NAME"><vt:lpwstr>INSERT NAME</vt:lpwstr></property></Properties><?xml
version="1.0" encoding="UTF-8"?>
<Properties xmlns="
http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
xmlns:vt="
http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><property
fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2"
name="COUNTRY"><vt:lpwstr>[COUNTRY]</vt:lpwstr></property><property
fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="LEGAL
NAME"><vt:lpwstr>Hello World</vt:lpwstr></property><property
fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4"
name="NAME"><vt:lpwstr>INSERT NAME</vt:lpwstr></property></Properties>

Re: XWPF and custom properties duplicating on commit

Posted by Nick Burch <ap...@gagravarr.org>.
On Wed, 6 Feb 2013, Mark Kimsal wrote:
> If this is supposed to work, then I've found a bug.
>
> POIXMLProperties opcprops = new POIXMLProperties(opc)
> CustomerProperties cust = opcprops.getCustomProperties();
>
> //change custom properties, no committing
>
> XWPFDocument doc = new XWPFDocument(opc);
> doc.write(outputstream);

Nope, I'd expect that to break. You should either work entirely on the OPC 
layer (without touching POIXMLDocument based classes like XWPFDocument), 
or at the POIXMLDocument level (eg ask XWPFDocument for the properties 
objects to change)

Your code ends up with two different objects holding an in-memory 
representation of the properties, and changes to one aren't see by the 
other. At write-out time, one will win, one will loose!

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: XWPF and custom properties duplicating on commit

Posted by Mark Kimsal <me...@gmail.com>.
If this is supposed to work, then I've found a bug.

POIXMLProperties opcprops = new POIXMLProperties(opc)
CustomerProperties cust = opcprops.getCustomProperties();

//change custom properties, no committing

XWPFDocument doc = new XWPFDocument(opc);
doc.write(outputstream);



On Tue, Feb 5, 2013 at 6:54 PM, Nick Burch <ap...@gagravarr.org> wrote:

> On Tue, 5 Feb 2013, Mark Kimsal wrote:
>
>> I think I figured it out after browsing through the source.  You should
>> not make your own POIXMLProperties from the OPC package if you are dealing
>> with an XWPFDocument, you should use doc.getProperties().  This is because
>> when the document is written out, it calls its own
>> getProperties().commit().  If the doc has never called its own
>> getProperties() before, it makes a new one out of the opc package.
>>
>
> You should generally either work entirely at the OPCPackage layer, or at
> the POIXMLDocument layer. As you've found, if you do some things at the
> POIXMLDocument level and go around adding new things to the package,
> POIXMLDocument won't know and may well stomp on things when it writes back
> out again... POIXMLDocument should be fine with anything that was there
> when it was opened, but if you're playing at the low level and high level
> at the same time you need to make sure you don't accidently introduce your
> own pseudo race condition in a single thread!
>
>
>  I'm not sure if this behavior makes sense or not, shouldn't modifications
>> to the properties though getUnderlyingProperties make changes via
>> references which would propagate back up and affect any newly create
>> POIXMLProperties created from the same OPC reference?
>>
>
> I'm not quite sure what you're saying, but if you have two
> POIXMLProperties instances open on the same OPCPackage, then both will have
> an in-memory representation. If you make conflicting changes to the two,
> only one will win at write-out time, as you've found!
>
> (If it's something else, then you might be best to write a small unit test
> that shows up the problem)
>
> Nick
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.**org<us...@poi.apache.org>
> For additional commands, e-mail: user-help@poi.apache.org
>
>

Re: XWPF and custom properties duplicating on commit

Posted by Nick Burch <ap...@gagravarr.org>.
On Tue, 5 Feb 2013, Mark Kimsal wrote:
> I think I figured it out after browsing through the source.  You should 
> not make your own POIXMLProperties from the OPC package if you are 
> dealing with an XWPFDocument, you should use doc.getProperties().  This 
> is because when the document is written out, it calls its own 
> getProperties().commit().  If the doc has never called its own 
> getProperties() before, it makes a new one out of the opc package.

You should generally either work entirely at the OPCPackage layer, or at 
the POIXMLDocument layer. As you've found, if you do some things at the 
POIXMLDocument level and go around adding new things to the package, 
POIXMLDocument won't know and may well stomp on things when it writes back 
out again... POIXMLDocument should be fine with anything that was there 
when it was opened, but if you're playing at the low level and high level 
at the same time you need to make sure you don't accidently introduce 
your own pseudo race condition in a single thread!

> I'm not sure if this behavior makes sense or not, shouldn't 
> modifications to the properties though getUnderlyingProperties make 
> changes via references which would propagate back up and affect any 
> newly create POIXMLProperties created from the same OPC reference?

I'm not quite sure what you're saying, but if you have two 
POIXMLProperties instances open on the same OPCPackage, then both will 
have an in-memory representation. If you make conflicting changes to the 
two, only one will win at write-out time, as you've found!

(If it's something else, then you might be best to write a small unit test 
that shows up the problem)

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: XWPF and custom properties duplicating on commit

Posted by Mark Kimsal <me...@gmail.com>.
I think I figured it out after browsing through the source.  You should not
make your own POIXMLProperties from the OPC package if you are dealing with
an XWPFDocument, you should use doc.getProperties().  This is because when
the document is written out, it calls its own getProperties().commit().  If
the doc has never called its own getProperties() before, it makes a new one
out of the opc package.

I'm not sure if this behavior makes sense or not, shouldn't modifications
to the properties though getUnderlyingProperties make changes via
references which would propagate back up and affect any newly create
POIXMLProperties created from the same OPC reference?


On Tue, Feb 5, 2013 at 1:31 PM, Mark Kimsal <me...@gmail.com> wrote:

> I'm trying to update the values of custom properties in an XWPFDocument.
>  If I do not commit() the properties after changing the values with
> CTProperty.setLpwstr() then the values do not change.  If I commit the
> properties after changing the value then I get duplicate XML tags in the
> docProps/custom.xml and docProps/app.xml
>
>
> Code:
>     {
> OPCPackage opc = OPCPackage.open(
>   loader.getResourceAsStream("META-INF/"+template+".docx"));
>
>     updateProperties(opc)
>
>     XWPFDocument doc = new XWPFDocument( opc );
>     // document bookmar stuff, but even commented out doesn't change
>     // duplicate tag behavior
>     doc.write(outputStream);
>     opc.close();
>     }
>
>     private final void updateProperties(OPCPackage opc) {
>         POIXMLProperties props = new POIXMLProperties(opc);
>         CustomProperties customProperties = props.getCustomProperties();
>   org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties
> ctprops = customProperties.getUnderlyingProperties();
>         List<CTProperty> propList = ctprops.getPropertyList();
>         for(CTProperty ctp : propList) {
>             System.out.println(ctp.getName());
>             if (ctp.getName().equals("LEGAL NAME")) {
>                 ctp.setLpwstr("Hello World");
>             }
>         }
>         props.commit();  //commenting out does not change docProps/*,
> uncommenting gives duplicate tags
>     }
>
> Does anyone know how to get property changes to ripple up into the doc
> without doing props.commit() ?
>
> app.xml
> <?xml version="1.0" encoding="UTF-8"?>
> <Properties xmlns="
> http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
> xmlns:vt="
> http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Template>Normal.dotm</Template><TotalTime>0</TotalTime><Pages>3</Pages><Words>1002</Words><Characters>5712</Characters><Application>Microsoft
> Office
> Word</Application><DocSecurity>0</DocSecurity><Lines>47</Lines><Paragraphs>13</Paragraphs><ScaleCrop>false</ScaleCrop><Company>
> </Company><LinksUpToDate>false</LinksUpToDate><CharactersWithSpaces>6701</CharactersWithSpaces><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>14.0000</AppVersion></Properties><?xml
> version="1.0" encoding="UTF-8"?>
> <Properties xmlns="
> http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
> xmlns:vt="
> http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Template>Normal.dotm</Template><TotalTime>0</TotalTime><Pages>3</Pages><Words>1002</Words><Characters>5712</Characters><Application>Microsoft
> Office
> Word</Application><DocSecurity>0</DocSecurity><Lines>47</Lines><Paragraphs>13</Paragraphs><ScaleCrop>false</ScaleCrop><Company>
> </Company><LinksUpToDate>false</LinksUpToDate><CharactersWithSpaces>6701</CharactersWithSpaces><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>14.0000</AppVersion></Properties>
>
> custom.xml
> <?xml version="1.0" encoding="UTF-8"?>
> <Properties xmlns="
> http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
> xmlns:vt="
> http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><property
> fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2"
> name="COUNTRY"><vt:lpwstr>[COUNTRY]</vt:lpwstr></property><property
> fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="LEGAL
> NAME"><vt:lpwstr>Hello World</vt:lpwstr></property><property
> fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4"
> name="NAME"><vt:lpwstr>INSERT NAME</vt:lpwstr></property></Properties><?xml
> version="1.0" encoding="UTF-8"?>
> <Properties xmlns="
> http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
> xmlns:vt="
> http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><property
> fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2"
> name="COUNTRY"><vt:lpwstr>[COUNTRY]</vt:lpwstr></property><property
> fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="LEGAL
> NAME"><vt:lpwstr>Hello World</vt:lpwstr></property><property
> fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4"
> name="NAME"><vt:lpwstr>INSERT NAME</vt:lpwstr></property></Properties>
>
>