Skip to content

Commit 6acf17e

Browse files
committed
#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.
1 parent 254339c commit 6acf17e

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<jsonpath.version>0.8.1</jsonpath.version>
6767
<minidevjson.version>1.1.1</minidevjson.version>
6868
<slf4j.version>1.7.2</slf4j.version>
69+
<evo.version>1.0.1</evo.version>
6970
<bundlor.failOnWarnings>true</bundlor.failOnWarnings>
7071
</properties>
7172

@@ -150,6 +151,13 @@
150151
<version>${jsonpath.version}</version>
151152
<optional>true</optional>
152153
</dependency>
154+
155+
<dependency>
156+
<groupId>org.atteo</groupId>
157+
<artifactId>evo-inflector</artifactId>
158+
<version>${evo.version}</version>
159+
<optional>true</optional>
160+
</dependency>
153161

154162
<dependency>
155163
<groupId>org.slf4j</groupId>

src/main/java/org/springframework/hateoas/config/HypermediaSupportBeanDefinitionRegistrar.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.hateoas.core.DefaultLinkDiscoverer;
4242
import org.springframework.hateoas.core.DefaultRelProvider;
4343
import org.springframework.hateoas.core.DelegatingRelProvider;
44+
import org.springframework.hateoas.core.EvoInflectorRelProvider;
4445
import org.springframework.hateoas.hal.HalLinkDiscoverer;
4546
import org.springframework.hateoas.hal.Jackson1HalModule;
4647
import org.springframework.hateoas.hal.Jackson2HalModule;
@@ -71,6 +72,7 @@ class HypermediaSupportBeanDefinitionRegistrar implements ImportBeanDefinitionRe
7172
private static final boolean JACKSON2_PRESENT = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper",
7273
null);
7374
private static final boolean JSONPATH_PRESENT = ClassUtils.isPresent("com.jayway.jsonpath.JsonPath", null);
75+
private static final boolean EVO_PRESENT = ClassUtils.isPresent("org.atteo.evo.inflector.English", null);
7476

7577
private final ImportBeanDefinitionRegistrar linkBuilderBeanDefinitionRegistrar = new LinkBuilderBeanDefinitionRegistrar();
7678

@@ -114,7 +116,8 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
114116
*/
115117
private static void registerRelProviderPluginRegistryAndDelegate(BeanDefinitionRegistry registry) {
116118

117-
RootBeanDefinition defaultRelProviderBeanDefinition = new RootBeanDefinition(DefaultRelProvider.class);
119+
Class<?> defaultRelProviderType = EVO_PRESENT ? EvoInflectorRelProvider.class : DefaultRelProvider.class;
120+
RootBeanDefinition defaultRelProviderBeanDefinition = new RootBeanDefinition(defaultRelProviderType);
118121
registry.registerBeanDefinition("defaultRelProvider", defaultRelProviderBeanDefinition);
119122

120123
RootBeanDefinition annotationRelProviderBeanDefinition = new RootBeanDefinition(AnnotationRelProvider.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas.core;
17+
18+
import org.atteo.evo.inflector.English;
19+
import org.springframework.hateoas.RelProvider;
20+
21+
/**
22+
* {@link RelProvider} implementation using the Evo Inflector implementation of an algorithmic approach to English
23+
* plurals.
24+
*
25+
* @see http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html
26+
* @author Oliver Gierke
27+
*/
28+
public class EvoInflectorRelProvider extends DefaultRelProvider {
29+
30+
/*
31+
* (non-Javadoc)
32+
* @see org.springframework.hateoas.core.DefaultRelProvider#getCollectionResourceRelFor(java.lang.Class)
33+
*/
34+
@Override
35+
public String getCollectionResourceRelFor(Class<?> type) {
36+
return English.plural(getSingleResourceRelFor(type));
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas.core;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
import org.springframework.hateoas.RelProvider;
23+
24+
/**
25+
* Unit tests for {@link EvoInflectorRelProvider}.
26+
*
27+
* @author Oliver Gierke
28+
*/
29+
public class EvoInflectorRelProviderUnitTest {
30+
31+
RelProvider provider = new EvoInflectorRelProvider();
32+
33+
@Test
34+
public void buildsCollectionRelCorrectly() {
35+
assertRels(City.class, "city", "cities");
36+
assertRels(Person.class, "person", "persons");
37+
}
38+
39+
private void assertRels(Class<?> type, String singleRel, String collectionRel) {
40+
assertThat(provider.getSingleResourceRelFor(type), is(singleRel));
41+
assertThat(provider.getCollectionResourceRelFor(type), is(collectionRel));
42+
}
43+
44+
static class Person {
45+
46+
}
47+
48+
static class City {
49+
50+
}
51+
}

template.mf

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ Import-Template:
1010
javax.xml.bind.*;version="0",
1111
net.minidev.json.*;version="${minidevjson.version:[=.=.=,+1.0.0)}";resolution:=optional,
1212
org.aopalliance.*;version="[1.0.0,2.0.0)";resolution:=optional,
13+
org.atteo.evo.inflector.*;version="${evo.version:[=.=.=,+1.0.0)}";resolution:=optional,
1314
org.springframework.*;version="${spring.version:[=.=.=,+1.0.0)}";resolution:=optional,
1415
org.codehaus.jackson.*;version="${jackson1.version:[=.=.=,+1.0.0)}";resolution:=optional

0 commit comments

Comments
 (0)