Example #1
0
  /** Implements {@link Files#readAttributes(Path, String, LinkOption...)}. */
  public ImmutableMap<String, Object> readAttributes(File file, String attributes) {
    String view = getViewName(attributes);
    List<String> attrs = getAttributeNames(attributes);

    if (attrs.size() > 1 && attrs.contains(ALL_ATTRIBUTES)) {
      // attrs contains * and other attributes
      throw new IllegalArgumentException("invalid attributes: " + attributes);
    }

    Map<String, Object> result = new HashMap<>();
    if (attrs.size() == 1 && attrs.contains(ALL_ATTRIBUTES)) {
      // for 'view:*' format, get all keys for all providers for the view
      AttributeProvider provider = providersByName.get(view);
      readAll(file, provider, result);

      for (String inheritedView : provider.inherits()) {
        AttributeProvider inheritedProvider = providersByName.get(inheritedView);
        readAll(file, inheritedProvider, result);
      }
    } else {
      // for 'view:attr1,attr2,etc'
      for (String attr : attrs) {
        result.put(attr, getAttribute(file, view, attr));
      }
    }

    return ImmutableMap.copyOf(result);
  }
Example #2
0
  private ImmutableMap<String, FileAttributeView> createInheritedViews(
      FileLookup lookup, AttributeProvider provider) {
    if (provider.inherits().isEmpty()) {
      return ImmutableMap.of();
    }

    Map<String, FileAttributeView> inheritedViews = new HashMap<>();
    createInheritedViews(lookup, provider, inheritedViews);
    return ImmutableMap.copyOf(inheritedViews);
  }
Example #3
0
  private void createInheritedViews(
      FileLookup lookup,
      AttributeProvider provider,
      Map<String, FileAttributeView> inheritedViews) {

    for (String inherited : provider.inherits()) {
      if (!inheritedViews.containsKey(inherited)) {
        AttributeProvider inheritedProvider = providersByName.get(inherited);
        FileAttributeView inheritedView =
            getFileAttributeView(lookup, inheritedProvider.viewType(), inheritedViews);

        inheritedViews.put(inherited, inheritedView);
      }
    }
  }
Example #4
0
  @Nullable
  private Object getAttributeInternal(File file, String view, String attribute) {
    AttributeProvider provider = providersByName.get(view);
    if (provider == null) {
      return null;
    }

    Object value = provider.get(file, attribute);
    if (value == null) {
      for (String inheritedView : provider.inherits()) {
        value = getAttributeInternal(file, inheritedView, attribute);
        if (value != null) {
          break;
        }
      }
    }

    return value;
  }
Example #5
0
  private void setAttributeInternal(
      File file, String view, String attribute, Object value, boolean create) {
    AttributeProvider provider = providersByName.get(view);

    if (provider != null) {
      if (provider.supports(attribute)) {
        provider.set(file, view, attribute, value, create);
        return;
      }

      for (String inheritedView : provider.inherits()) {
        AttributeProvider inheritedProvider = providersByName.get(inheritedView);
        if (inheritedProvider.supports(attribute)) {
          inheritedProvider.set(file, view, attribute, value, create);
          return;
        }
      }
    }

    throw new IllegalArgumentException("cannot set attribute '" + view + ":" + attribute + "'");
  }
Example #6
0
  private static void addMissingProviders(Map<String, AttributeProvider> providers) {
    Set<String> missingViews = new HashSet<>();
    for (AttributeProvider provider : providers.values()) {
      for (String inheritedView : provider.inherits()) {
        if (!providers.containsKey(inheritedView)) {
          missingViews.add(inheritedView);
        }
      }
    }

    if (missingViews.isEmpty()) {
      return;
    }

    // add any inherited views that were not listed directly
    for (String view : missingViews) {
      addStandardProvider(providers, view);
    }

    // in case any of the providers that were added themselves have missing views they inherit
    addMissingProviders(providers);
  }