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

Document type detection #65

Closed
GediminasVaistai opened this issue Sep 13, 2024 · 3 comments
Closed

Document type detection #65

GediminasVaistai opened this issue Sep 13, 2024 · 3 comments

Comments

@GediminasVaistai
Copy link

GediminasVaistai commented Sep 13, 2024

Hello,

Are there any functions that can detect the type of document I pass into "readXMLDOM" and automatically set the schema (e.g., invoice, catalogue)? Currently, I have to manually specify the document type before reading it (e.g., UBL21Marshaller.catalogue()).

// Read
final Document aDoc = DOMReader.readXMLDOM (new ClassPathResource (sFilename),
new DOMReaderSettings ().setSchema (UBL21Marshaller.catalogue ().getSchema ()));
assertNotNull (sFilename, aDoc);
final CatalogueType aUBLObject = UBL21Marshaller.catalogue ().read (aDoc);
assertNotNull (sFilename, aUBLObject);

  // Validate
  IErrorList aErrors = UBL21Marshaller.catalogue ().validate (aUBLObject);
@phax phax self-assigned this Sep 13, 2024
@phax phax added the question label Sep 13, 2024
phax added a commit that referenced this issue Sep 13, 2024
@phax
Copy link
Owner

phax commented Sep 13, 2024

Yes, that would be nice, but unfortunately each marshaller is tightly bound to a specific set of XML Schemas.
As the UBL2xMarshaller classes are also able to directly read from a file, stream or resource.
There is a an enumeration EUBL21DocumentType (also available for other UBL versions), that lists all the contained document types. With a little trickery you can easily identify the root element local name and the root element namespace URI required to use it. I assume that would already help you.
Please let me know if it does, so that I can add it to the other versions as well and release a an update afterwards

@GediminasVaistai
Copy link
Author

Thank you!
The two new methods you created helped me identify and handle the document type I was trying to pass.
I tried to compare the enumeration EUBL21DocumentType (root element's local name, root element's namespace URI) with the document I passed in.
And when I found matching pairs, I used this code to get a schema and used it in the readXMLDOM() method to validate the document.
( XMLSchemaCache.getInstanceOfClassLoader(e.getClass().getClassLoader()).getSchema(e.getAllXSDResources()) | where e is a EUBL21DocumentType )

@phax
Copy link
Owner

phax commented Sep 17, 2024

Okay great. So I will the 2 methods for all UBL versions and create an updated release 9.0.3

phax added a commit that referenced this issue Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants