コード例 #1
0
  /**
   * Returns the charset explicitly set by the user for the given resource, or <code>null</code>. If
   * no setting exists for the given resource and <code>recurse</code> is <code>true</code>, every
   * parent up to the workspace root will be checked until a charset setting can be found.
   *
   * @param resourcePath the path for the resource
   * @param recurse whether the parent should be queried
   * @return the charset setting for the given resource
   */
  public String getCharsetFor(IPath resourcePath, boolean recurse) {
    Assert.isLegal(resourcePath.segmentCount() >= 1);
    IProject project = workspace.getRoot().getProject(resourcePath.segment(0));

    Preferences prefs = getPreferences(project, false, false);
    Preferences derivedPrefs = getPreferences(project, false, true);

    if (prefs == null && derivedPrefs == null)
      // no preferences found - for performance reasons, short-circuit
      // lookup by falling back to workspace's default setting
      return recurse ? ResourcesPlugin.getEncoding() : null;

    return internalGetCharsetFor(prefs, derivedPrefs, resourcePath, recurse);
  }
コード例 #2
0
  private String internalGetCharsetFor(
      Preferences prefs, Preferences derivedPrefs, IPath resourcePath, boolean recurse) {
    String charset = null;

    // try to find the encoding in regular and then derived preferences
    if (prefs != null) charset = prefs.get(getKeyFor(resourcePath), null);
    // derivedPrefs may be not null, only if #isDerivedEncodingStoredSeparately returns true
    // so the explicit check against #isDerivedEncodingStoredSeparately is not required
    if (charset == null && derivedPrefs != null)
      charset = derivedPrefs.get(getKeyFor(resourcePath), null);

    if (!recurse) return charset;

    while (charset == null && resourcePath.segmentCount() > 1) {
      resourcePath = resourcePath.removeLastSegments(1);
      // try to find the encoding in regular and then derived preferences
      if (prefs != null) charset = prefs.get(getKeyFor(resourcePath), null);
      if (charset == null && derivedPrefs != null)
        charset = derivedPrefs.get(getKeyFor(resourcePath), null);
    }

    // ensure we default to the workspace encoding if none is found
    return charset == null ? ResourcesPlugin.getEncoding() : charset;
  }