Skip to content

Commit

Permalink
feat(JS-2477): recognize x-rechnung
Browse files Browse the repository at this point in the history
  • Loading branch information
welschsn committed Aug 19, 2024
1 parent d3c8c5c commit e7d28d8
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/main/java/org/jadice/filetype/matchers/XMLMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class XMLMatcher extends Matcher {

public static final String DOCUMENT_XML_VERSION_KEY = "document_xml_version";

public static final String X_RECHNUNG_KEY = "x_rechnung";

public static final int DEFAULT_MAX_ENTITY_EXPANSIONS = 20;
private static final String JAXP_ENTITY_EXPANSION_LIMIT_KEY = "jdk.xml.entityExpansionLimit";
private static volatile int MAX_ENTITY_EXPANSIONS = determineMaxEntityExpansions();
Expand Down Expand Up @@ -103,6 +105,13 @@ public class XMLMatcher extends Matcher {

private static SoftReference<SAXParserFactory> saxFactoryReference = new SoftReference<>(null);

private static final Map<String, String> X_RECHNUNG_ROOT_ELEMENT_XMLNS_PAIRS;
static {
X_RECHNUNG_ROOT_ELEMENT_XMLNS_PAIRS = new HashMap<>();
X_RECHNUNG_ROOT_ELEMENT_XMLNS_PAIRS.put("Invoice", "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
X_RECHNUNG_ROOT_ELEMENT_XMLNS_PAIRS.put("CrossIndustryInvoice", "urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100");
}

@Override
public boolean matches(final Context context) throws IOException {
try {
Expand All @@ -121,6 +130,10 @@ public boolean matches(final Context context) throws IOException {
if (handler.getEncoding() != null && !handler.getEncoding().isEmpty()) {
mimeType += ";charset=" + handler.getEncoding();
}
final boolean isXRechnung = matchesXRechnung(handler.getRootElementName(), handler.getNamespaceURI());
if (isXRechnung) {
mimeType += ";x-rechnung=true";
}

context.setProperty(MimeTypeAction.KEY, mimeType);
context.setProperty(ExtensionAction.KEY, "xml");
Expand All @@ -133,7 +146,10 @@ public boolean matches(final Context context) throws IOException {
// xml version: see
// http://sax.sourceforge.net/apidoc/org/xml/sax/package-summary.html#package_description
putIfPresent(DOCUMENT_XML_VERSION_KEY, handler.getXmlVersion(), xmlDetails);

if (isXRechnung) {
xmlDetails.put(X_RECHNUNG_KEY, true);
}

// Parser would have thrown a SAXException is this is no proper XML
return true;
} catch (ParserConfigurationException | SAXException e) {
Expand Down Expand Up @@ -373,4 +389,16 @@ private static int determineMaxEntityExpansions() {
}
return DEFAULT_MAX_ENTITY_EXPANSIONS;
}

/**
* Returns <code>true</code> if the XML root element and namespace match with those of an X-Rechnung
* standard.
*
* @param rootElement an XML root element
* @param namespaceURI an XML namespace
* @return <code>true</code> if input matches X-Rechnung standard
*/
public static boolean matchesXRechnung(String rootElement, String namespaceURI) {
return X_RECHNUNG_ROOT_ELEMENT_XMLNS_PAIRS.getOrDefault(rootElement, "").equals(namespaceURI);
}
}

0 comments on commit e7d28d8

Please sign in to comment.