From 6acf17e73061c2948bc79fbb2dcf39722ddabfc1 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 15 Apr 2013 23:18:29 +0200 Subject: [PATCH] #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. --- pom.xml | 8 +++ ...ermediaSupportBeanDefinitionRegistrar.java | 5 +- .../hateoas/core/EvoInflectorRelProvider.java | 38 ++++++++++++++ .../core/EvoInflectorRelProviderUnitTest.java | 51 +++++++++++++++++++ template.mf | 1 + 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/springframework/hateoas/core/EvoInflectorRelProvider.java create mode 100644 src/test/java/org/springframework/hateoas/core/EvoInflectorRelProviderUnitTest.java diff --git a/pom.xml b/pom.xml index 20e38448..9f4021fe 100644 --- a/pom.xml +++ b/pom.xml @@ -66,6 +66,7 @@ 0.8.1 1.1.1 1.7.2 + 1.0.1 true @@ -150,6 +151,13 @@ ${jsonpath.version} true + + + org.atteo + evo-inflector + ${evo.version} + true + org.slf4j diff --git a/src/main/java/org/springframework/hateoas/config/HypermediaSupportBeanDefinitionRegistrar.java b/src/main/java/org/springframework/hateoas/config/HypermediaSupportBeanDefinitionRegistrar.java index 817e12f2..9ea1128d 100644 --- a/src/main/java/org/springframework/hateoas/config/HypermediaSupportBeanDefinitionRegistrar.java +++ b/src/main/java/org/springframework/hateoas/config/HypermediaSupportBeanDefinitionRegistrar.java @@ -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); diff --git a/src/main/java/org/springframework/hateoas/core/EvoInflectorRelProvider.java b/src/main/java/org/springframework/hateoas/core/EvoInflectorRelProvider.java new file mode 100644 index 00000000..d46e7b0f --- /dev/null +++ b/src/main/java/org/springframework/hateoas/core/EvoInflectorRelProvider.java @@ -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)); + } +} diff --git a/src/test/java/org/springframework/hateoas/core/EvoInflectorRelProviderUnitTest.java b/src/test/java/org/springframework/hateoas/core/EvoInflectorRelProviderUnitTest.java new file mode 100644 index 00000000..fb7dc831 --- /dev/null +++ b/src/test/java/org/springframework/hateoas/core/EvoInflectorRelProviderUnitTest.java @@ -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 { + + } +} diff --git a/template.mf b/template.mf index 07dae951..abccb7d2 100644 --- a/template.mf +++ b/template.mf @@ -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