Page 1 of 1

Remove table not working in word (weblogic 12c and java 1.8)

PostPosted: Sat Aug 05, 2017 3:19 am
by SamFrnd
Hi,

I am using the below method to remove a table from the word document using docx4j3.2.2. This code works fine in weblogic 11g and java 1.6.

However, I execute the same code in weblogic 12c and java 1.8 and the code does not remove the table. Also, there is no error or exception thrown by the code. The weblogic is installed on UNIX server.

Can you please help.

Tbl hdrTbl = TableUtil.getInstance().getFirstTable(factory, pkg);
if (hdrTbl != null) {
loggerUtil.logTrace("Removing the first table from the document");
pkg.getMainDocumentPart().getContent().remove(hdrTbl.getParent());
}

public Tbl getFirstTable(ObjectFactory factory, WordprocessingMLPackage pkg) {
Tbl headerTable = null;
loggerUtil.logTrace("Start of method getFirstTable");
try {
List<Object> tbllst = getAllElementFromObject(pkg.getMainDocumentPart().getContents(), Tbl.class);
if (tbllst.size() > 0) {
Tbl currentTable = (Tbl) tbllst.get(0);
List<Object> rows = getAllElementFromObject(currentTable,Tr.class);
for (int i = 0; i < rows.size(); i++) {
Tr tr = (Tr) rows.get(i);
List<Object> cells = tr.getContent();
for (Object o2 : cells) {
Tc tc = null;
String value = "";
if (o2 instanceof javax.xml.bind.JAXBElement
&& ((JAXBElement<?>) o2).getDeclaredType().getName().equals("org.docx4j.wml.Tc")) {
tc = (org.docx4j.wml.Tc) ((JAXBElement<?>) o2).getValue();
value = tc.getContent().get(0).toString();
if (value.contains(
Constant.SAMPLE_DATE)) {
loggerUtil.logTrace("Document first table contains 'SAMPLE DATE'");
return currentTable;
}
}
}
}
}
} catch (Exception e) {
loggerUtil.logError("Error in method getFirstTable", e);
e.printStackTrace();
}
loggerUtil.logTrace("End of method getFirstTable");

Re: Remove table not working in word (weblogic 12c and java

PostPosted: Sat Aug 05, 2017 9:21 am
by jason
What is getAllElementFromObject?

It probably isn't finding the Tbl if JAXB has wrapped it in a JAXBElement. It should also test XmlUtils.unwrap

Re: Remove table not working in word (weblogic 12c and java

PostPosted: Sat Aug 05, 2017 4:10 pm
by SamFrnd
Hi Jason,

getAllElementFromObject returns all the tables in the document. As you can see from the below traces, I have printed the document content.

Can you tell me what is the difference in values for hdrTable.getContent() on 12c and 11g.

12c

javax.xml.bind.JAXBElement@2616e298 is the table in the document. I use hdrTbl.getContent() to remove the table. However, when I print that it gives me the value as org.docx4j.wml.Body@29c86d77.

11g

javax.xml.bind.JAXBElement@277e277e is the table in the document. hdrTbl.getContent() is used as the parameter to remove the table. when I print that it gives me javax.xml.bind.JAXBElement@277e277e. Hence, the remove is working correctly in this version.


Weblogic 12C


printing the document content - [javax.xml.bind.JAXBElement@2616e298....content...

>CheckIn/6 08.04 20:29:05.499 IdcServer-30 - custom:PNP Checkin - printing the content - c.toString() - hdrTbl - org.docx4j.wml.Tbl@52f894a5

>CheckIn/6 08.04 20:29:05.499 IdcServer-30 - custom:PNP Checkin - printing the content - hdrTbl.getParent().toString - org.docx4j.wml.Body@29c86d77

Weblogic 11g

>CheckIn/6 08.04 22:51:36.309 IdcServer-19 - custom:PNP Checkin - printing the content - [javax.xml.bind.JAXBElement@277e277e,...content...
>CheckIn/6 08.04 22:51:36.310 IdcServer-19 - custom:PNP Checkin - printing - hdrTbl.toString() - hdrTbl - org.docx4j.wml.Tbl@27b227b2
>CheckIn/6 08.04 22:51:36.310 IdcServer-19 - custom:PNP Checkin - printing - hdrTbl.getParent().toString -javax.xml.bind.JAXBElement@277e277e

private static List<Object> getAllElementFromObject(Object obj,Class<?> toSearch) {
loggerUtil.logTrace("Starting method getAllElementFromObject");
List<Object> result = new ArrayList<Object>();
if (obj instanceof JAXBElement)
obj = ((JAXBElement<?>) obj).getValue();
if (obj.getClass().equals(toSearch))
result.add(obj);
else if (obj instanceof ContentAccessor) {
List<?> children = ((ContentAccessor) obj).getContent();
for (Object child : children) {
result.addAll(getAllElementFromObject(child, toSearch));
}
}
loggerUtil.logTrace("Ending method getAllElementFromObject");
return result;
}

Re: Remove table not working in word (weblogic 12c and java

PostPosted: Sat Aug 05, 2017 10:35 pm
by jason
Where the table is wrapped in a JAXBElement, your method returns the table, not the JAXBElement wrapper.

You then try to remove the parent (?!) of whatever is returned. Don't you want to remove the table itself (ie the Tbl object, or the JAXBElement containing it).

To remove a JAXBElement containing a table, you either need to return the JAXBElement from getAllElementFromObject, so you can remove it from the list using remove, OR you need to iterate through the list, and when you encounter a JAXBElement, look to see if it is the one containing the Tbl object of interest.

Re: Remove table not working in word (weblogic 12c and java

PostPosted: Mon Aug 07, 2017 11:02 am
by SamFrnd
Yes, I want to remove the table from the document. Yes, the getAllElementFromObject() provides me the table list from the word document.

Something like this...[org.docx4j.wml.Tbl@4fa8bebb, org.docx4j.wml.Tbl@5773f83d].

In my old weblogic environment (11g, java 1.6), table.getParent() gives me the following "javax.xml.bind.JAXBElement@277e277e". I use this value to remove the table from the word document.

However, in my current weblogic environment (12c, java 1.8), table.getParent() returns me the following "org.docx4j.wml.Body@29c86d77". Can you let me know how can I get the jaxbelement wrapper from getAllElementFromObject() method. I did try the changing that to

(obj instanceof javax.xml.bind.JAXBElement<?>) obj).getValue() - That gave me the same value as above. Tried using the XMLUtils.unwrap(table) but that did not provide me the required jaxbelement wrapper.

Any help on will be greatly appreciated.

Thank you.

Re: Remove table not working in word (weblogic 12c and java

PostPosted: Tue Aug 08, 2017 9:18 pm
by jason
Here is the simplest fix:

1. fix your code to remove the correct object: instead of pkg.getMainDocumentPart().getContent().remove(hdrTbl.getParent()), do pkg.getMainDocumentPart().getContent().remove(hdrTbl)

2. in your getAllElementFromObject, where the tbl is wrapped in a JAXBElement, return the JAXBElement object (ie not just the tbl object).

Where the method says "if (obj instanceof JAXBElement)", that's your JAXBElement object right there (until the next line points obj at something else, so change that to say objValue, but leave result.add(obj) unchanged)