#47, #60 - Added RelProvider implementation pluralizing using the Evo Inflector algorithm.

See http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html for details of the algorithm.
This commit is contained in:
Oliver Gierke
2013-04-15 23:18:29 +02:00
parent 254339c3b2
commit 6acf17e730
5 changed files with 102 additions and 1 deletions

View File

@@ -66,6 +66,7 @@
<jsonpath.version>0.8.1</jsonpath.version>
<minidevjson.version>1.1.1</minidevjson.version>
<slf4j.version>1.7.2</slf4j.version>
<evo.version>1.0.1</evo.version>
<bundlor.failOnWarnings>true</bundlor.failOnWarnings>
</properties>
@@ -150,6 +151,13 @@
<version>${jsonpath.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.atteo</groupId>
<artifactId>evo-inflector</artifactId>
<version>${evo.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>

View File

@@ -41,6 +41,7 @@ import org.springframework.hateoas.core.AnnotationRelProvider;
import org.springframework.hateoas.core.DefaultLinkDiscoverer;
import org.springframework.hateoas.core.DefaultRelProvider;
import org.springframework.hateoas.core.DelegatingRelProvider;
import org.springframework.hateoas.core.EvoInflectorRelProvider;
import org.springframework.hateoas.hal.HalLinkDiscoverer;
import org.springframework.hateoas.hal.Jackson1HalModule;
import org.springframework.hateoas.hal.Jackson2HalModule;
@@ -71,6 +72,7 @@ class HypermediaSupportBeanDefinitionRegistrar implements ImportBeanDefinitionRe
private static final boolean JACKSON2_PRESENT = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper",
null);
private static final boolean JSONPATH_PRESENT = ClassUtils.isPresent("com.jayway.jsonpath.JsonPath", null);
private static final boolean EVO_PRESENT = ClassUtils.isPresent("org.atteo.evo.inflector.English", null);
private final ImportBeanDefinitionRegistrar linkBuilderBeanDefinitionRegistrar = new LinkBuilderBeanDefinitionRegistrar();
@@ -114,7 +116,8 @@ class HypermediaSupportBeanDefinitionRegistrar implements ImportBeanDefinitionRe
*/
private static void registerRelProviderPluginRegistryAndDelegate(BeanDefinitionRegistry registry) {
RootBeanDefinition defaultRelProviderBeanDefinition = new RootBeanDefinition(DefaultRelProvider.class);
Class<?> defaultRelProviderType = EVO_PRESENT ? EvoInflectorRelProvider.class : DefaultRelProvider.class;
RootBeanDefinition defaultRelProviderBeanDefinition = new RootBeanDefinition(defaultRelProviderType);
registry.registerBeanDefinition("defaultRelProvider", defaultRelProviderBeanDefinition);
RootBeanDefinition annotationRelProviderBeanDefinition = new RootBeanDefinition(AnnotationRelProvider.class);

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.core;
import org.atteo.evo.inflector.English;
import org.springframework.hateoas.RelProvider;
/**
* {@link RelProvider} implementation using the Evo Inflector implementation of an algorithmic approach to English
* plurals.
*
* @see http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html
* @author Oliver Gierke
*/
public class EvoInflectorRelProvider extends DefaultRelProvider {
/*
* (non-Javadoc)
* @see org.springframework.hateoas.core.DefaultRelProvider#getCollectionResourceRelFor(java.lang.Class)
*/
@Override
public String getCollectionResourceRelFor(Class<?> type) {
return English.plural(getSingleResourceRelFor(type));
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.core;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.hateoas.RelProvider;
/**
* Unit tests for {@link EvoInflectorRelProvider}.
*
* @author Oliver Gierke
*/
public class EvoInflectorRelProviderUnitTest {
RelProvider provider = new EvoInflectorRelProvider();
@Test
public void buildsCollectionRelCorrectly() {
assertRels(City.class, "city", "cities");
assertRels(Person.class, "person", "persons");
}
private void assertRels(Class<?> type, String singleRel, String collectionRel) {
assertThat(provider.getSingleResourceRelFor(type), is(singleRel));
assertThat(provider.getCollectionResourceRelFor(type), is(collectionRel));
}
static class Person {
}
static class City {
}
}

View File

@@ -10,5 +10,6 @@ Import-Template:
javax.xml.bind.*;version="0",
net.minidev.json.*;version="${minidevjson.version:[=.=.=,+1.0.0)}";resolution:=optional,
org.aopalliance.*;version="[1.0.0,2.0.0)";resolution:=optional,
org.atteo.evo.inflector.*;version="${evo.version:[=.=.=,+1.0.0)}";resolution:=optional,
org.springframework.*;version="${spring.version:[=.=.=,+1.0.0)}";resolution:=optional,
org.codehaus.jackson.*;version="${jackson1.version:[=.=.=,+1.0.0)}";resolution:=optional