1 package org
.argeo
.cms
.acr
.fs
;
3 import java
.io
.IOException
;
4 import java
.nio
.ByteBuffer
;
5 import java
.nio
.charset
.StandardCharsets
;
6 import java
.nio
.file
.Files
;
7 import java
.nio
.file
.Path
;
8 import java
.nio
.file
.attribute
.UserDefinedFileAttributeView
;
9 import java
.util
.Iterator
;
11 import java
.util
.NavigableMap
;
12 import java
.util
.TreeMap
;
13 import java
.util
.stream
.Collectors
;
15 import org
.argeo
.api
.acr
.Content
;
16 import org
.argeo
.api
.acr
.ContentResourceException
;
17 import org
.argeo
.api
.acr
.CrName
;
18 import org
.argeo
.api
.acr
.NamespaceUtils
;
19 import org
.argeo
.api
.acr
.spi
.ContentProvider
;
20 import org
.argeo
.api
.acr
.spi
.ProvidedContent
;
21 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
23 /** Access a file system as a {@link ContentProvider}. */
24 public class FsContentProvider
implements ContentProvider
{
25 final static String XMLNS_
= "xmlns:";
27 private final String mountPath
;
28 private final Path rootPath
;
29 private final boolean isRoot
;
31 private NavigableMap
<String
, String
> prefixes
= new TreeMap
<>();
33 public FsContentProvider(String mountPath
, Path rootPath
, boolean isRoot
) {
34 this.mountPath
= mountPath
;
35 this.rootPath
= rootPath
;
40 protected void initNamespaces() {
42 UserDefinedFileAttributeView udfav
= Files
.getFileAttributeView(rootPath
,
43 UserDefinedFileAttributeView
.class);
44 for (String name
: udfav
.list()) {
45 if (name
.startsWith(XMLNS_
)) {
46 ByteBuffer buf
= ByteBuffer
.allocate(udfav
.size(name
));
47 udfav
.read(name
, buf
);
49 String namespace
= StandardCharsets
.UTF_8
.decode(buf
).toString();
50 String prefix
= name
.substring(XMLNS_
.length());
51 prefixes
.put(prefix
, namespace
);
56 addDefaultNamespace(udfav
, CrName
.CR_DEFAULT_PREFIX
, CrName
.CR_NAMESPACE_URI
);
57 addDefaultNamespace(udfav
, "basic", CrName
.CR_NAMESPACE_URI
);
58 addDefaultNamespace(udfav
, "owner", CrName
.CR_NAMESPACE_URI
);
59 addDefaultNamespace(udfav
, "posix", CrName
.CR_NAMESPACE_URI
);
60 } catch (IOException e
) {
61 throw new RuntimeException("Cannot read namespaces from " + rootPath
, e
);
66 protected void addDefaultNamespace(UserDefinedFileAttributeView udfav
, String prefix
, String namespace
)
68 if (!prefixes
.containsKey(prefix
)) {
69 ByteBuffer bb
= ByteBuffer
.wrap(namespace
.getBytes(StandardCharsets
.UTF_8
));
70 udfav
.write(XMLNS_
+ prefix
, bb
);
71 prefixes
.put(prefix
, namespace
);
75 public void registerPrefix(String prefix
, String namespace
) {
76 if (prefixes
.containsKey(prefix
))
77 prefixes
.remove(prefix
);
79 UserDefinedFileAttributeView udfav
= Files
.getFileAttributeView(rootPath
,
80 UserDefinedFileAttributeView
.class);
81 addDefaultNamespace(udfav
, prefix
, namespace
);
82 } catch (IOException e
) {
83 throw new RuntimeException("Cannot register namespace " + prefix
+ " " + namespace
+ " on " + rootPath
, e
);
90 public String
getMountPath() {
94 boolean isRoot(Path path
) {
96 return isRoot
&& Files
.isSameFile(rootPath
, path
);
97 } catch (IOException e
) {
98 throw new ContentResourceException(e
);
103 public ProvidedContent
get(ProvidedSession session
, String mountPath
, String relativePath
) {
104 return new FsContent(session
, this, rootPath
.resolve(relativePath
));
112 public String
getNamespaceURI(String prefix
) {
113 return NamespaceUtils
.getNamespaceURI((p
) -> prefixes
.get(p
), prefix
);
117 public Iterator
<String
> getPrefixes(String namespaceURI
) {
118 return NamespaceUtils
.getPrefixes((ns
) -> prefixes
.entrySet().stream().filter(e
-> e
.getValue().equals(ns
))
119 .map(Map
.Entry
::getKey
).collect(Collectors
.toUnmodifiableSet()), namespaceURI
);