X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2Ffs%2FFsContentProvider.java;h=9b1b9668303f066e2106de3aa4a296edcabbf55d;hb=54df376a9c2dd458a82eaa09bfbb718fe699dd0d;hp=e17384b942162d7cefbde3fde1fdb0682ad3674f;hpb=cc1dd97ebcc32e1bd754073ad23def182f460452;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContentProvider.java b/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContentProvider.java index e17384b94..9b1b96683 100644 --- a/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContentProvider.java +++ b/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContentProvider.java @@ -9,13 +9,14 @@ import java.nio.file.attribute.UserDefinedFileAttributeView; import java.util.Iterator; import java.util.Map; import java.util.NavigableMap; +import java.util.Objects; import java.util.TreeMap; import java.util.stream.Collectors; -import org.argeo.api.acr.Content; +import org.argeo.api.acr.ArgeoNamespace; import org.argeo.api.acr.ContentResourceException; -import org.argeo.api.acr.CrName; import org.argeo.api.acr.NamespaceUtils; +import org.argeo.api.acr.RuntimeNamespaceContext; import org.argeo.api.acr.spi.ContentProvider; import org.argeo.api.acr.spi.ProvidedContent; import org.argeo.api.acr.spi.ProvidedSession; @@ -24,23 +25,31 @@ import org.argeo.api.acr.spi.ProvidedSession; public class FsContentProvider implements ContentProvider { final static String XMLNS_ = "xmlns:"; - private final String mountPath; - private final Path rootPath; - private final boolean isRoot; + protected String mountPath; + protected Path rootPath; private NavigableMap prefixes = new TreeMap<>(); - public FsContentProvider(String mountPath, Path rootPath, boolean isRoot) { + public FsContentProvider(String mountPath, Path rootPath) { + Objects.requireNonNull(mountPath); + Objects.requireNonNull(rootPath); + this.mountPath = mountPath; this.rootPath = rootPath; - this.isRoot = isRoot; + // FIXME make it more robust initNamespaces(); } + protected FsContentProvider() { + + } + protected void initNamespaces() { try { UserDefinedFileAttributeView udfav = Files.getFileAttributeView(rootPath, UserDefinedFileAttributeView.class); + if (udfav == null) + return; for (String name : udfav.list()) { if (name.startsWith(XMLNS_)) { ByteBuffer buf = ByteBuffer.allocate(udfav.size(name)); @@ -53,10 +62,10 @@ public class FsContentProvider implements ContentProvider { } // defaults - addDefaultNamespace(udfav, CrName.CR_DEFAULT_PREFIX, CrName.CR_NAMESPACE_URI); - addDefaultNamespace(udfav, "basic", CrName.CR_NAMESPACE_URI); - addDefaultNamespace(udfav, "owner", CrName.CR_NAMESPACE_URI); - addDefaultNamespace(udfav, "posix", CrName.CR_NAMESPACE_URI); + addDefaultNamespace(udfav, ArgeoNamespace.CR_DEFAULT_PREFIX, ArgeoNamespace.CR_NAMESPACE_URI); + addDefaultNamespace(udfav, "basic", ArgeoNamespace.CR_NAMESPACE_URI); + addDefaultNamespace(udfav, "owner", ArgeoNamespace.CR_NAMESPACE_URI); + addDefaultNamespace(udfav, "posix", ArgeoNamespace.CR_NAMESPACE_URI); } catch (IOException e) { throw new RuntimeException("Cannot read namespaces from " + rootPath, e); } @@ -84,26 +93,30 @@ public class FsContentProvider implements ContentProvider { } } - - -@Override + + @Override public String getMountPath() { return mountPath; } - boolean isRoot(Path path) { + boolean isMountBase(Path path) { try { - return isRoot && Files.isSameFile(rootPath, path); + return Files.isSameFile(rootPath, path); } catch (IOException e) { throw new ContentResourceException(e); } } @Override - public ProvidedContent get(ProvidedSession session, String mountPath, String relativePath) { + public ProvidedContent get(ProvidedSession session, String relativePath) { return new FsContent(session, this, rootPath.resolve(relativePath)); } + @Override + public boolean exists(ProvidedSession session, String relativePath) { + return Files.exists(rootPath.resolve(relativePath)); + } + /* * NAMESPACE CONTEXT */ @@ -115,8 +128,19 @@ public class FsContentProvider implements ContentProvider { @Override public Iterator getPrefixes(String namespaceURI) { - return NamespaceUtils.getPrefixes((ns) -> prefixes.entrySet().stream().filter(e -> e.getValue().equals(ns)) - .map(Map.Entry::getKey).collect(Collectors.toUnmodifiableSet()), namespaceURI); + Iterator res = NamespaceUtils.getPrefixes((ns) -> prefixes.entrySet().stream() + .filter(e -> e.getValue().equals(ns)).map(Map.Entry::getKey).collect(Collectors.toUnmodifiableSet()), + namespaceURI); + if (!res.hasNext()) { + String prefix = RuntimeNamespaceContext.getNamespaceContext().getPrefix(namespaceURI); + if (prefix != null) { + registerPrefix(prefix, namespaceURI); + return getPrefixes(namespaceURI); + } else { + throw new IllegalArgumentException("Unknown namespace " + namespaceURI); + } + } + return res; } }