public class ConfigurableFilenamePolicy extends Object implements IFilenamePolicy
IFilenamePolicy
that is user-customizable by providing a pattern
string.
A pattern string is a string containing patterns enclosed in curly
braces. Each pattern starts with a variable and is optionally
followed by one or more options, each introduced by a vertical bar (
|
). Please note that all whitespace is significant.
{author|fallback|20}-{title|sep=,}.{uri}.{ext}
contains the
variables author with the options fallback
and
20
, the variable title with the option
sep=,
, and the variables uri and ext, each
without any option.
Variable | Supported Options | Description |
---|---|---|
author | fallback , sep= String,
Number, raw |
The object's author. This tries to find the nearest work object in the
aggregation tree and extracts its author or authors.
If the |
title | sep= String, Number, raw |
The object's title or titles. |
uri | — | The object's TextGrid URI. This only includes the scheme-specific part. |
ext | — | A filename extension that is suitable for the object's MIME type, or
dat if none found. This does not include a leading dot.
|
* | pre= String (Default . ),
post= String |
A filename disambiguation pattern, only inserted if required. If filename
disambiguation is on (setUniqueFilenames(boolean) ),
getFilename(IAggregationEntry) will first generate a filename
candidate with this pattern expanding to the empty string. If this filename
has already been used for a different entry, it will re-run the
filename generation with this pattern expanding to the empty string for the
first object resolving to the candidate and to
prefix + n-1 + postfix for every other object. I.e.
for three XML documents by Goethe and the pattern
{author}*.{ext} you will get Goethe.xml ,
Goethe.1.xml and Goethe.2.xml .
Instead of {*} without options you can also simply write
* .
|
{uri}
or *
or you risk to get te same
filename for different objects!
Using ConfigurableFilenamePolicy.Builder.subPolicy(String, ConfigurableFilenamePolicy)
it is
possible to define a variable that expands to the result of applying a
complete configurable policy to the given object. These subpatterns all take
the option /
, which appends a slash if and only if the subpolicy
expands to a non-empty string that does not already end in a slash.
As a special case, a policy that has ConfigurableFilenamePolicy.Builder.isParent()
set,
automatically has a subpolicy called parent
that delegates
to the policy itself, so you can build recursive path structures.
Here is a typical setup using subpolicies:
ConfigurableFilenamePolicy parentPolicy = ConfigurableFilenamePolicy .builder("{parent|/}{title}") .isParent() .build(); ConfigurableFilenamePolicy filenamePolicy = ConfigurableFilenamePolicy .builder("{parent|/}{author}-{title}.{uri}.{ext}") .subPolicy("parent", parentPolicy) .build(); ConfigurableFilenamePolicy metaPolicy = ConfigurableFilenamePolicy .builder(".meta/{filename}.meta") .subPolicy("parent", parentPolicy) .subPolicy("filename", filenamePolicy) .build();
Number | If you pass any non-negative non-zero integer number as an option, the expanded value of the variable will be trimmed after at most Number characters. Trimming occurs after all other processing steps for the variable. |
raw |
Insert the result of this variable as-is, without character sanitization.
If you do not include this option, the result of the metadata-based variables will be transcribed from its original characters to a safe subset of US-ASCII characters in order to be safe from all kinds of encoding and filename issues. This tries to do something sensible with, e.g., umlauts and non-latin scripts. |
sep= String |
If present and the respective metadata field contains multiple values, use all values, joined together with the given separator String. Otherwise, only use the first value. |
fallback |
See at the corresponding variable descriptions. |
Modifier and Type | Class and Description |
---|---|
static class |
ConfigurableFilenamePolicy.Builder |
protected static interface |
ConfigurableFilenamePolicy.Segment
A segment represents a part of a filename pattern.
|
Modifier | Constructor and Description |
---|---|
protected |
ConfigurableFilenamePolicy() |
Modifier and Type | Method and Description |
---|---|
static ConfigurableFilenamePolicy.Builder |
builder(String pattern) |
com.google.common.base.Optional<URI> |
getBase(IAggregationEntry entry)
Tries to generate a base URI for the given aggregation entry.
|
URI |
getFilename(IAggregationEntry entry)
Generates a file name for the aggregation entry described by the given
metadata.
|
URI |
getFilename(IAggregationEntry entry,
boolean asParent)
Deprecated.
Use
getFilename(IAggregationEntry) instead |
String |
getFilename(info.textgrid.namespaces.metadata.core._2010.ObjectType object,
boolean asParent)
Generates a file name candidate for the object described by the given
metadata.
|
boolean |
isUniqueFilenames() |
ConfigurableFilenamePolicy |
setUniqueFilenames(boolean uniqueFilenames)
Guarantee a unique object to filename mapping.
|
String |
translate(String source)
Creates a valid file name part for the given string.
|
public String translate(String source)
IFilenamePolicy
translate
in interface IFilenamePolicy
source
- The string that should be translatedpublic String getFilename(info.textgrid.namespaces.metadata.core._2010.ObjectType object, boolean asParent)
IFilenamePolicy
getFilename
in interface IFilenamePolicy
object
- The object with the metadata for which to generate a filenameasParent
- If true, the generated file name should be the parent for a
different filename. Implementors might, e.g., want to leave
out an extension.@Deprecated public URI getFilename(IAggregationEntry entry, boolean asParent)
getFilename(IAggregationEntry)
insteadIFilenamePolicy
getFilename
in interface IFilenamePolicy
asParent
- is not supported by ConfigurableFilenamePolicy
IFilenamePolicy.getFilename(ObjectType, boolean)
public URI getFilename(IAggregationEntry entry)
IFilenamePolicy
getFilename
in interface IFilenamePolicy
IFilenamePolicy.getFilename(ObjectType, boolean)
public com.google.common.base.Optional<URI> getBase(IAggregationEntry entry)
IFilenamePolicy
getBase
in interface IFilenamePolicy
public boolean isUniqueFilenames()
public ConfigurableFilenamePolicy setUniqueFilenames(boolean uniqueFilenames)
uniqueFilenames
- , default value is true
public static ConfigurableFilenamePolicy.Builder builder(String pattern)
Copyright © 2015 TextGrid. All Rights Reserved.