Skip to content

Commit

Permalink
Ignore explicit declarations of reserved prefixes
Browse files Browse the repository at this point in the history
- added a new method to `Vocab` to get the base URI of a vocabulary
- In the prefix parsing utility of `VocabUtil`, check the URI associated
  with re-declared reserved prefix: an error is reported only if this
  URI is **not** the URI of the known vocabulary associated to the
  reserved prefix. In other words, explicit declaration of reserved
  prefixes is now allowed; trying to override a reserved prefix still
  raises an error.

Fixes #585
  • Loading branch information
rdeltour committed Oct 21, 2015
1 parent 8dccaca commit ebc1c75
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 19 deletions.
26 changes: 25 additions & 1 deletion src/main/java/com/adobe/epubcheck/vocab/AggregateVocab.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,38 @@
import java.util.List;

import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;

public class AggregateVocab implements Vocab
{

private final List<Vocab> vocabs;

private final String uri;

/**
* Returns a vocabulary composed of the union of the vocabularies given as
* parameter. The given vocabularies must have the same base URI.
*
* @param vocabs
* the vocabularies to aggregate.
* @return the aggregated vocabulary.
*/
public static Vocab of(Vocab... vocabs)
{
return new AggregateVocab(new ImmutableList.Builder<Vocab>().add(vocabs).build());
}

private AggregateVocab(List<Vocab> vocabs)
{
this.uri = (!vocabs.isEmpty()) ? Strings.nullToEmpty(vocabs.get(0).getURI()) : "";
for (Vocab vocab : vocabs)
{
if (!uri.equals(Strings.nullToEmpty(vocab.getURI())))
{
throw new IllegalArgumentException("Aggregated vocabs must share the same base URI");
}
}
this.vocabs = vocabs;
}

Expand All @@ -31,4 +49,10 @@ public Optional<Property> lookup(String name)
return Optional.absent();
}

@Override
public String getURI()
{
return uri;
}

}
29 changes: 19 additions & 10 deletions src/main/java/com/adobe/epubcheck/vocab/EnumVocab.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Maps.EntryTransformer;
Expand Down Expand Up @@ -39,6 +40,7 @@ public String apply(Enum<?> enumee)
};

private final Map<String, Property> index;
private final String uri;

/**
* Creates a new vocabulary backed by the given {@link Enum} class and with
Expand Down Expand Up @@ -70,18 +72,19 @@ public EnumVocab(final Class<P> clazz, final String base)
*/
public EnumVocab(final Class<P> clazz, final String base, final String prefix)
{
this.index = ImmutableMap.copyOf(Maps.transformEntries(
Maps.uniqueIndex(EnumSet.allOf(clazz), ENUM_TO_NAME),
new EntryTransformer<String, P, Property>()
{
this.uri = Strings.nullToEmpty(base);
this.index = ImmutableMap
.copyOf(Maps.transformEntries(Maps.uniqueIndex(EnumSet.allOf(clazz), ENUM_TO_NAME),
new EntryTransformer<String, P, Property>()
{

@Override
public Property transformEntry(String name, P enumee)
{
return Property.newFrom(name, base, prefix, enumee);
}
@Override
public Property transformEntry(String name, P enumee)
{
return Property.newFrom(name, base, prefix, enumee);
}

}));
}));
}

@Override
Expand All @@ -90,6 +93,12 @@ public Optional<Property> lookup(String name)
return Optional.fromNullable(index.get(name));
}

@Override
public String getURI()
{
return uri;
}

/**
* Returns an {@link Optional} containing the {@link Property} for the given
* enum item if it is defined in this vocabulary, or {@link Optional#absent()}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/adobe/epubcheck/vocab/UncheckedVocab.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
public final class UncheckedVocab implements Vocab
{

private String base;
private String prefix;
private final String base;
private final String prefix;

/**
* Creates a new unchecked vocabulary representing properties whose URIs start
Expand Down Expand Up @@ -44,4 +44,10 @@ public Optional<Property> lookup(String name)
return Optional.of(Property.newFrom(name, base, prefix));
}

@Override
public String getURI()
{
return base;
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/adobe/epubcheck/vocab/Vocab.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ public interface Vocab
* in this vocabulary.
*/
Optional<Property> lookup(String name);

/**
* Returns the base URI of this vocabulary.
*
* @return the base URI of this vocabulary.
*/
String getURI();
}
7 changes: 4 additions & 3 deletions src/main/java/com/adobe/epubcheck/vocab/VocabUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public static Optional<Property> parseProperty(String value, Map<String, Vocab>
Report report, EPUBLocation location)
{

return Optional.fromNullable(Iterables.get(
parseProperties(value, vocabs, false, report, location), 0, null));
return Optional.fromNullable(
Iterables.get(parseProperties(value, vocabs, false, report, location), 0, null));
}

/**
Expand Down Expand Up @@ -198,7 +198,8 @@ else if (forbidden.contains(uri))
}
else
{
if (predefined.containsKey(prefix))
if (predefined.containsKey(prefix)
&& !Strings.nullToEmpty(predefined.get(prefix).getURI()).equals(uri))
{
// re-declaration of reserved prefix
report.message(MessageId.OPF_007, location, prefix);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/adobe/epubcheck/opf/OPFCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public void testValidateDocumentItemNoMediaType()
@Test
public void testValidateRedeclaredReservedPrefixes()
{
Collections.addAll(expectedWarnings, MessageId.OPF_007, MessageId.OPF_007, MessageId.OPF_007b,
Collections.addAll(expectedWarnings, MessageId.OPF_007, MessageId.OPF_007b,
MessageId.OPF_007b);
// should generate 2 warnings (redeclaration of reserved prefixes and
// redeclaration of default vocab)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="uid"
prefix="media: http://should/not/redeclare
rendition: http://should/not/redeclare/too
rendition: http://www.idpf.org/vocab/rendition/#
foo: http://idpf.org/epub/vocab/package/#
bar: http://idpf.org/epub/vocab/package/link/#">

<!-- redeclaring 2 reserved prefixes -->
<!-- redeclaring 2 reserved prefixes:
- 'media' raises an error since it's redeclared to another vocab
- 'rendition' is ignored since it only re-declares the reserved vocab
-->

<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:identifier id="uid">urn:uuid:550e8400-e29b-41d4-a716-4466674412314</dc:identifier>
Expand Down

0 comments on commit ebc1c75

Please sign in to comment.