- 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 (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);
- 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);
- }
-
- }
-
- /*
- * NAMESPACE CONTEXT
- */
-
- /*
- * SESSION
- */
-
- class CmsContentSession implements ProvidedSession {
- private Subject subject;
- private Locale locale;
-
- private CompletableFuture<ProvidedSession> closed = new CompletableFuture<>();
-
- public CmsContentSession(Subject subject, Locale locale) {
- this.subject = subject;
- this.locale = locale;
- }
-
- public void close() {
- closed.complete(this);
- }
-
- @Override
- public CompletionStage<ProvidedSession> onClose() {
- return closed.minimalCompletionStage();
- }
-
- @Override
- public Content get(String path) {
- Map.Entry<String, ContentProvider> entry = partitions.floorEntry(path);
- String mountPath = entry.getKey();
- ContentProvider provider = entry.getValue();
- String relativePath = path.substring(mountPath.length());
- if (relativePath.length() > 0 && relativePath.charAt(0) == '/')
- relativePath = relativePath.substring(1);
- return provider.get(CmsContentSession.this, mountPath, relativePath);
- }
-
- @Override
- public Subject getSubject() {
- return subject;
- }
-
- @Override
- public Locale getLocale() {
- return locale;
- }
-
- @Override
- public ProvidedRepository getRepository() {
- return CmsContentRepository.this;
- }
-
- /*
- * NAMESPACE CONTEXT
- */
-
- @Override
- public String getNamespaceURI(String prefix) {
- return NamespaceUtils.getNamespaceURI((p) -> prefixes.get(p), prefix);
- }
-
- @Override
- public Iterator<String> getPrefixes(String namespaceURI) {
- return NamespaceUtils.getPrefixes((ns) -> prefixes.entrySet().stream().filter(e -> e.getValue().equals(ns))
- .map(Map.Entry::getKey).collect(Collectors.toUnmodifiableSet()), namespaceURI);