+ this.isRoot = isRoot;
+ initNamespaces();
+ }
+
+ protected void initNamespaces() {
+ try {
+ UserDefinedFileAttributeView udfav = Files.getFileAttributeView(rootPath,
+ UserDefinedFileAttributeView.class);
+ for (String name : udfav.list()) {
+ if (name.startsWith(XMLNS_)) {
+ ByteBuffer buf = ByteBuffer.allocate(udfav.size(name));
+ udfav.read(name, buf);
+ buf.flip();
+ String namespace = StandardCharsets.UTF_8.decode(buf).toString();
+ String prefix = name.substring(XMLNS_.length());
+ prefixes.put(prefix, namespace);
+ }
+ }
+
+ // 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);
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot read namespaces from " + rootPath, e);
+ }
+
+ }
+
+ protected void addDefaultNamespace(UserDefinedFileAttributeView udfav, String prefix, String namespace)
+ throws IOException {
+ if (!prefixes.containsKey(prefix)) {
+ ByteBuffer bb = ByteBuffer.wrap(namespace.getBytes(StandardCharsets.UTF_8));
+ int size = udfav.write(XMLNS_ + prefix, bb);
+ prefixes.put(prefix, namespace);
+ }