com.adobe.epubcheck.vocab.EnumVocab Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of epubcheck Show documentation
Show all versions of epubcheck Show documentation
EpubCheck is a tool to validate IDPF EPUB files. It can detect many types of errors in EPUB.
OCF container structure, OPF and OPS mark-up, and internal reference consistency are checked.
EpubCheck can be run as a standalone command-line tool, installed as a Java server-side web application
or used as a Java library.
package com.adobe.epubcheck.vocab;
import java.util.EnumSet;
import java.util.Map;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Maps.EntryTransformer;
/**
* A {@link Vocab} implementation that is backed by an {@link Enum}.
*
*
* Property names will be computed from {@link Enum} constant names by applying
* the following transformation:
*
*
* - The name is converted to lower case
* - The underscore character (
'_'
) is replaced by the hyphen
* character ('-'
).
*
*
* @author Romain Deltour
*
*/
public final class EnumVocab implements Vocab
{
public final static Function, String> ENUM_TO_NAME = new Function, String>()
{
@Override
public String apply(Enum> enumee)
{
return enumee.toString().toLowerCase().replace('_', '-');
}
};
private final Map index;
/**
* Creates a new vocabulary backed by the given {@link Enum} class and with
* properties having the common URI stem base
. Properties of the
* created vocabulary will have an empty prefix (in other words, this creates
* a default vocabulary).
*
* @param clazz
* the enumeration backing this vocabulary.
* @param base
* the common stem URI of properties in this vocabulary.
*/
public > EnumVocab(final Class clazz, final String base)
{
this(clazz, base, null);
}
/**
* Creates a new vocabulary backed by the given {@link Enum} class and with
* properties having the common URI stem base
and prefix
* prefix
*
* @param clazz
* the enumeration backing this vocabulary.
* @param base
* the common stem URI of properties in this vocabulary.
* @param prefix
* the common prefix of properties in this vocabulary.
*/
public > EnumVocab(final Class clazz, final String base, final String prefix)
{
this.index = ImmutableMap.copyOf(Maps.transformEntries(
Maps.uniqueIndex(EnumSet.allOf(clazz), ENUM_TO_NAME),
new EntryTransformer()
{
@Override
public Property transformEntry(String name, T enumee)
{
return Property.newFrom(name, base, prefix, enumee);
}
}));
}
@Override
public Optional lookup(String name)
{
return Optional.fromNullable(index.get(name));
}
}