- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setNamespaceAware(true);
- factory.setXIncludeAware(true);
- // factory.setSchema(schema);
-
- DocumentBuilder dBuilder = factory.newDocumentBuilder();
- dBuilder.setErrorHandler(new ErrorHandler() {
-
- @Override
- public void warning(SAXParseException exception) throws SAXException {
- }
-
- @Override
- public void fatalError(SAXParseException exception) throws SAXException {
- }
-
- @Override
- public void error(SAXParseException exception) throws SAXException {
- log.error(exception);
-
- }
- });
-
- Document document;
- if (path != null && Files.exists(path)) {
- InputSource inputSource = new InputSource(path.toAbsolutePath().toUri().toString());
- inputSource.setEncoding(StandardCharsets.UTF_8.name());
- // TODO public id as well?
- document = dBuilder.parse(inputSource);
- } else {
- document = dBuilder.newDocument();
-// Element root = document.createElementNS(CrName.ROOT.getNamespaceURI(),
-// CrName.ROOT.get().toPrefixedString());
- Element root = document.createElement(CrName.ROOT.get().toPrefixedString());
- // root.setAttribute("xmlns", "");
- root.setAttribute("xmlns:" + CrName.CR_DEFAULT_PREFIX, CrName.CR_NAMESPACE_URI);
- document.appendChild(root);
-
- // write it
- TransformerFactory transformerFactory = TransformerFactory.newInstance();
- Transformer transformer = transformerFactory.newTransformer();
- DOMSource source = new DOMSource(document);
- if (path != null)
- try (Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
- StreamResult result = new StreamResult(writer);
- transformer.transform(source, result);
- }
- }
-
- DomContentProvider contentProvider = new DomContentProvider(document);
- addProvider("/", contentProvider);
- } catch (DOMException | ParserConfigurationException | SAXException | IOException
- | TransformerFactoryConfigurationError | TransformerException e) {
- throw new IllegalStateException("Cannot init ACR root " + path, e);