Adapt to MS Windows
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / fs / FsContentProvider.java
index 59a4d8deb5616f12c6b1b24c61406726f3f121b2..47cd64af343dc2aec976cb554b82be229c6547ad 100644 (file)
@@ -13,35 +13,45 @@ import java.util.Objects;
 import java.util.TreeMap;
 import java.util.stream.Collectors;
 
+import javax.xml.namespace.QName;
+
+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;
+import org.argeo.cms.util.OS;
 
 /** Access a file system as a {@link ContentProvider}. */
 public class FsContentProvider implements ContentProvider {
-       final static String XMLNS_ = "xmlns:";
 
        protected String mountPath;
        protected Path rootPath;
 
        private NavigableMap<String, String> prefixes = new TreeMap<>();
 
+       private final boolean isNtfs;
+       private final String XMLNS_;
+
        public FsContentProvider(String mountPath, Path rootPath) {
                Objects.requireNonNull(mountPath);
                Objects.requireNonNull(rootPath);
 
                this.mountPath = mountPath;
                this.rootPath = rootPath;
+
+               this.isNtfs = OS.LOCAL.isMSWindows();
+               this.XMLNS_ = isNtfs ? "xmlns%3A" : "xmlns:";
+
                // FIXME make it more robust
                initNamespaces();
        }
 
        protected FsContentProvider() {
-
+               this.isNtfs = OS.LOCAL.isMSWindows();
+               this.XMLNS_ = isNtfs ? "xmlns%3A" : "xmlns:";
        }
 
        protected void initNamespaces() {
@@ -62,10 +72,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);
                }
@@ -117,6 +127,27 @@ public class FsContentProvider implements ContentProvider {
                return Files.exists(rootPath.resolve(relativePath));
        }
 
+       /*
+        * ATTRIBUTE NAMES
+        */
+       /**
+        * Make sure that the prefixed name is compatible with the underlying file
+        * system for file names/attributes (NTFS does not accept :)
+        */
+       String toFsPrefixedName(QName key) {
+               return isNtfs ? NamespaceUtils.toPrefixedName(this, key).replace(":", "%3A")
+                               : NamespaceUtils.toPrefixedName(this, key);
+       }
+
+       /**
+        * PArse a prefixed name which is compatible with the underlying file system for
+        * file names/attributes (NTFS does not accept :)
+        */
+       QName fromFsPrefixedName(String name) {
+               return isNtfs ? NamespaceUtils.parsePrefixedName(this, name.replace("%3A", ":"))
+                               : NamespaceUtils.parsePrefixedName(this, name);
+       }
+
        /*
         * NAMESPACE CONTEXT
         */