Loading…

Kryptel/Java

Targets class

Declaration

package com.kryptel.bslx;

public final class Targets {

  public static final int TARGET_UNKNOWN  = -1;
  public static final int TARGET_DEFAULT = 0;
  . . .

  public static final class TargetedPath

  public interface ITargetedPathEncoder


  public static final void SetEncoder(ITargetedPathEncoder enc)

  public static final String GetTargetName(int target)

  public static final int GetTargetCode(String target)

  public static final String GetTargetPath(int target) throws Exception

  public static final TargetedPath RecognizeTarget(String path) throws Exception
	
}

Description

Targets class handles standard folders that may map to different directories in different systems. For instance, the user documents folder resides in different places in different Windows versions. This class solves this problem by introducing a system-independent notation target + path.

Example

The user working on Windows XP backups a file report.doc residing in folder Reports in My Documents. The actual file path is C:\Documents and Settings\<user>\My Documents\Reports\report.doc. During backup Kryptel translates this path to a two-component structure consisting of an integer TARGET_DOCUMENTS and a string Reports\report.doc.

When the user restores this file after upgrading to Windows 7, the file will be correctly restored to C:\Users\<user>\Documents\Reports\report.doc.

TARGET_*

Integer constants representing targets (mostly Windows-specific ones). See the source code for the complete list.

TargetedPath

public static final class TargetedPath {
    public int target;
    public String path;
}

A helper class representing a targeted path as a pair target / subfolder. This class is used by RecognizeTarget to return the result.

ITargetedPathEncoder

public interface ITargetedPathEncoder {
    String GetTargetPath(int target) throws Exception;
    TargetedPath RecognizeTarget(String path) throws Exception;
}

See Customizing Targets for a discussion of this interface's purpose and usage.

SetEncoder

public static void SetEncoder(ITargetedPathEncoder enc)

See Customizing Targets for a discussion of this function's purpose and usage.

GetTargetName

public static final String GetTargetName(int target)

This function returns a user-friendly target name for a given target code (i.e. TARGET_* value). Like the target code, this name uniquely identifies the target.

GetTargetCode

public static final int GetTargetCode(String target)

For a given target user-friendly name return the corresponding target code (i.e. TARGET_* value).

GetTargetPath

public static final String GetTargetPath(int target) throws Exception

Converts a target code to the corresponding file system directory. For example, for TARGET_DOCUMENTS on Windows 7 the function should return C:\Users\<user>\Documents\.

The default function returns the target name (as returned by GetTargetName) followed by the path separator ('/' or '\' depending on the system). For TARGET_ASK_USER the function returns an empty string.

This function can be customized with user-supplied ITargetedPathEncoder. See Customizing Targets for a detailed discussion.

RecognizeTarget

public static final TargetedPath RecognizeTarget(String path) throws Exception

Converts the specified path to a two-component structure target + subdirectory. The default version returns TARGET_STORAGE_ROOT and the source path without leading path separator.

This function can be customized with user-supplied ITargetedPathEncoder. See Customizing Targets for a detailed discussion.

Customizing Targets

Functions GetTargetPath and RecognizeTarget are system-dependent and must be adapted to the target operating system. In order to add support for a new system define a class that extends interface ITargetedPathEncoder, create a class instance, and call SetEncoder with that instance as an argument. If an encoder is set, GetTargetPath and RecognizeTarget functions will redirect their calls to the user-supplied functions. In order to remove the encoder and switch back to the default implementation call SetEncoder with null argument.