Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wml shapes: @XmlAnyElement causes ClassCastException across modules with org.eclipse.persistence.oxm.XMLBinder #602

Open
Nishanth-GH opened this issue Jan 10, 2025 · 1 comment

Comments

@Nishanth-GH
Copy link

Nishanth-GH commented Jan 10, 2025

I am trying to read a docx file and replace the author names with a constant string. This is the code in my main function.

import jakarta.xml.bind.JAXBException;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.CTTrackChange;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;


public class App {

    public static void main(String[] args) throws IOException, Docx4JException {
        try {
            // Replace "path/to/your/document.docx" with the actual file path
            String inputFilePath = "src/main/java/shapes.docx";
            String outputFilePath = "output_no_user_info.docx";

            // Load the document
            WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(new File(inputFilePath));

            // Remove user information
            removeUserInfo(wmlPackage);

            // Save the modified document
            FileOutputStream fos = new FileOutputStream(outputFilePath);
            wmlPackage.save(fos);
            fos.close();

            System.out.println("User information removed successfully. Saved as: " + outputFilePath);

        } catch (Docx4JException | JAXBException e) {
            e.printStackTrace();
            System.err.println("Error processing the document: " + e.getMessage());
        }
    }

    public static void removeUserInfo(WordprocessingMLPackage wmlPackage) throws Docx4JException, JAXBException {
        MainDocumentPart documentPart = wmlPackage.getMainDocumentPart();
        List<Object> trackedChanges = documentPart.getJAXBNodesViaXPath("//w:ins | //w:del", true);

        for (Object change : trackedChanges) {
            if (change instanceof CTTrackChange) {
                CTTrackChange trackChange = (CTTrackChange) change;
                trackChange.setAuthor("External");
            }
        }
    }
}

pom.xml

 <dependencies>
    <dependency>
      <groupId>org.docx4j</groupId>
      <artifactId>docx4j-core</artifactId>
      <version>11.4.9</version>
    </dependency>
    <dependency>
      <groupId>org.docx4j</groupId>
      <artifactId>docx4j-JAXB-MOXy</artifactId>
      <version>11.4.5</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>org.eclipse.persistence.moxy</artifactId>
      <version>4.0.2</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>
        <configuration>
          <source>11</source>
          <target>11</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

The error i'm observing is the below shared one, and it's coming up when there are any shapes in the document. I see CTWordprocessingShape and org.eclipse.persistence.internal.oxm.Root are different classes to be cast and code is breaking in below line of AbstractMarshalRecordImpl.

Root xr = (Root) originalObject;
Link to code for AbstractMarshalRecordImpl:- https://github.com/payara/patched-src-eclipselink/blob/master/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/oxm/record/AbstractMarshalRecordImpl.java

Exception in thread "main" java.lang.ClassCastException: class org.docx4j.com.microsoft.schemas.office.word.x2010.wordprocessingShape.CTWordprocessingShape cannot be cast to class org.eclipse.persistence.internal.oxm.Root (org.docx4j.com.microsoft.schemas.office.word.x2010.wordprocessingShape.CTWordprocessingShape and org.eclipse.persistence.internal.oxm.Root are in unnamed module of loader 'app')
    at org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecordImpl.addXsiTypeAndClassIndicatorIfRequired(AbstractMarshalRecordImpl.java:211)
    at org.eclipse.persistence.oxm.record.XMLRecord.addXsiTypeAndClassIndicatorIfRequired(XMLRecord.java:406)
    at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.buildIntoNestedRow(XMLObjectBuilder.java:462)
    at org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping.buildCompositeRow(XMLAnyCollectionMapping.java:683)
    at org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping.writeFromObjectIntoRow(XMLAnyCollectionMapping.java:554)
    at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.writeOutMappings(XMLObjectBuilder.java:349)
    at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.buildIntoNestedRow(XMLObjectBuilder.java:465)
    at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.buildIntoNestedRow(XMLObjectBuilder.java:445)
    at org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping.buildCompositeRowForDescriptor(XMLCompositeObjectMapping.java:466)
    at org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping.buildCompositeRow(XMLCompositeObjectMapping.java:447)
    at org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping.writeSingleValue(XMLCompositeObjectMapping.java:680)
    at org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping.writeFromObjectIntoRow(XMLCompositeObjectMapping.java:645)
    at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.writeOutMappings(XMLObjectBuilder.java:349)
    at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.buildIntoNestedRow(XMLObjectBuilder.java:465)
    at org.eclipse.persistence.internal.oxm.XMLObjectBuilder.buildIntoNestedRow(XMLObjectBuilder.java:445)
    at org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping.buildCompositeRowForDescriptor(XMLCompositeObjectMapping.java:466)

I'm fairly new to library and please let me know if there is any work around to bypass the issue and why this issue is happening.
I tried using ReferenceImpl instead of Moxy, ReferenceImpl doesn't cause the issue.

@plutext plutext changed the title DocX4j processing with Moxy is failing for a document with shapes wml shapes: @XmlAnyElement causes ClassCastException across modules with org.eclipse.persistence.oxm.XMLBinder Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants