Notices, XPath and other tools

Besides the SDK, the library has a few tools that eForms applications often need. The first one is usually the starting point of an application: it reads which SDK version a notice uses, so that the application can choose everything else. The others read XML safely, and work with the XPath expressions of the fields and nodes.

Reading a notice

Before an application can process a notice, it needs to know which SDK version and which notice subtype the notice uses. The class NoticeDocument reads these facts from the XML of a notice:

NoticeDocument notice = new NoticeDocument(Path.of("cn_24_multilingual.xml"));

notice.getEformsSdkVersion(); // "2.0"
notice.getNoticeSubType();    // "16"
notice.getPrimaryLanguage();  // "ENG"
notice.getOtherLanguages();   // [BUL, CES, DAN, ...]

The SDK version comes from the element cbc:CustomizationID, without its prefix eforms-sdk-. Give it to the SdkDownloader, the SdkResourceLoader and the factories, so that the notice is processed with its own version of the SDK. The notice subtype comes from cbc:SubTypeCode, and the languages from cbc:NoticeLanguageCode and cac:AdditionalNoticeLanguage. When the SDK version or the notice subtype is missing, the method throws a RuntimeException.

NoticeDocument finds these elements by the prefixes that eForms notices use, such as cbc:. It does not read a notice that uses other prefixes for the same namespaces. getXmlContents() gives the whole XML of the notice, for the rest of your processing.

Reading XML safely

An XML document can contain references to other files or to network addresses, which a parser would load while it reads the document. A notice received from outside can use this to read files of your server. The class SafeDocumentBuilder creates parsers that never load such references, following the recommendations of OWASP:

DocumentBuilder builder = SafeDocumentBuilder.buildSafeDocumentBuilderStrict();
Document document = builder.parse(Path.of("notice.xml").toFile());

buildSafeDocumentBuilderStrict() refuses any document that has a document type declaration (<!DOCTYPE). buildSafeDocumentBuilderAllowDoctype() accepts such a declaration but loads nothing from it. NoticeDocument uses the second one.

Working with XPath

Each field and node of the SDK has an absolute XPath. An application that generates code from the SDK often needs to take these paths apart, or to write one path relative to another. The class XPathProcessor does this for the paths of the SDK.

parse takes a path apart into its steps:

XPathInfo path = XPathProcessor.parse("/*/cac:ProcurementProjectLot[cbc:ID/@schemeName='Lot']/cbc:ID");

path.isAbsolute();                         // true
path.getSteps().get(1).getStepText();      // "cac:ProcurementProjectLot"
path.getSteps().get(1).getPredicateText(); // "[cbc:ID/@schemeName='Lot']"
path.hasPredicate("schemeName='Lot'");     // true

For the path of an attribute, isAttribute() is true, getAttributeName() gives the name of the attribute, and getPathToLastElement() gives the path of the element that carries it. You can also obtain this from a field directly, with SdkField.getXpathInfo().

contextualize writes a path relative to another one, the context. The result is the path that you would write inside an XSLT template or an XPath predicate whose context is the first path:

XPathProcessor.contextualize("/*/cac:ProcurementProject", "/*/cac:ProcurementProject/cbc:Name");
// "cbc:Name"

XPathProcessor.contextualize("/*/cac:ProcurementProjectLot", "/*/cac:ProcurementProject/cbc:Name");
// "../cac:ProcurementProject/cbc:Name"

join does the opposite: it appends a relative path to another path. Where the relative path starts by going up (..), the result can be shortened, and you choose how far:

String lot = "/*/cac:ProcurementProjectLot";
String name = "../cac:ProcurementProject/cbc:Name";

XPathProcessor.join(lot, name, Simplification.NONE);
// "/*/cac:ProcurementProjectLot/../cac:ProcurementProject/cbc:Name"

XPathProcessor.join(lot, name, Simplification.PRESERVE_PREDICATES);
// "/*/cac:ProcurementProject/cbc:Name"

The shorter path does not always mean the same thing. The long one selects nothing when the notice has no lot, because it passes through the lot. The short one selects the name in all cases. With NONE, the meaning is kept. With PRESERVE_PREDICATES, a step is removed only when it has no predicate, so that no condition is lost. With FULL, a step is removed even with its predicate.

XPathProcessor is made for the paths that the SDK uses. It is not a general tool for rewriting any XPath expression.

Genericode codelists

The SDK publishes its codelists in the Genericode format (.gc files). GenericodeTools.getMarshaller() gives a reader and writer for these files, which the SdkCodelistRepository uses (see The codelists). Use it when you need more of a codelist file than its codes, such as its other columns.