Skip to content

Commit 1d83c87

Browse files
committed
Issue #18.
1 parent fb6979b commit 1d83c87

File tree

6 files changed

+44
-26
lines changed

6 files changed

+44
-26
lines changed

basic/src/main/java/org/jvnet/jaxb2_commons/plugin/simplify/SimplifyPlugin.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.jvnet.jaxb2_commons.util.CustomizationUtils;
1616
import org.xml.sax.ErrorHandler;
1717

18+
import com.sun.codemodel.JJavaName;
1819
import com.sun.tools.xjc.model.CAdapter;
1920
import com.sun.tools.xjc.model.CAttributePropertyInfo;
2021
import com.sun.tools.xjc.model.CClassInfo;
@@ -33,6 +34,16 @@
3334

3435
public class SimplifyPlugin extends AbstractParameterizablePlugin {
3536

37+
private boolean usePluralForm = true;
38+
39+
public boolean isUsePluralForm() {
40+
return usePluralForm;
41+
}
42+
43+
public void setUsePluralForm(boolean usePluralForm) {
44+
this.usePluralForm = usePluralForm;
45+
}
46+
3647
@Override
3748
public String getOptionName() {
3849
return "Xsimplify";
@@ -240,8 +251,7 @@ private CElementPropertyInfo createElementPropertyInfo(final Model model,
240251
CReferencePropertyInfo property, CElement element,
241252
final CElementInfo elementInfo) {
242253
final CElementPropertyInfo elementPropertyInfo;
243-
final String propertyName = createPropertyName(model, element);
244-
254+
final String propertyName = createPropertyName(model, property, element);
245255
final CElementPropertyInfo originalPropertyInfo = elementInfo
246256
.getProperty();
247257
elementPropertyInfo = new CElementPropertyInfo(propertyName,
@@ -266,7 +276,7 @@ private CElementPropertyInfo createElementPropertyInfo(final Model model,
266276
CReferencePropertyInfo property, CElement element,
267277
final CClassInfo classInfo) {
268278
final CElementPropertyInfo elementPropertyInfo;
269-
final String propertyName = createPropertyName(model, element);
279+
final String propertyName = createPropertyName(model, property, element);
270280
elementPropertyInfo = new CElementPropertyInfo(propertyName,
271281
property.isCollection() ? CollectionMode.REPEATED_ELEMENT
272282
: CollectionMode.NOT_REPEATED, ID.NONE, null,
@@ -296,7 +306,7 @@ private CElementPropertyInfo createElementPropertyInfo(final Model model,
296306
//
297307
private CReferencePropertyInfo createReferencePropertyInfo(
298308
final Model model, CReferencePropertyInfo property, CElement element) {
299-
final String propertyName = createPropertyName(model, element);
309+
final String propertyName = createPropertyName(model, property, element);
300310
final CReferencePropertyInfo referencePropertyInfo = new CReferencePropertyInfo(
301311
propertyName, property.isCollection(), /* required */false,/* mixed */
302312
false, element.getSchemaComponent(),
@@ -320,7 +330,7 @@ private CReferencePropertyInfo createContentReferencePropertyInfo(
320330

321331
private CElementPropertyInfo createElementPropertyInfo(final Model model,
322332
CElementPropertyInfo property, CTypeRef typeRef) {
323-
final String propertyName = createPropertyName(model, typeRef);
333+
final String propertyName = createPropertyName(model, property, typeRef);
324334
boolean required = false;
325335
final CElementPropertyInfo elementPropertyInfo = new CElementPropertyInfo(
326336
propertyName,
@@ -337,7 +347,8 @@ private CElementPropertyInfo createElementPropertyInfo(final Model model,
337347
return elementPropertyInfo;
338348
}
339349

340-
private String createPropertyName(final Model model, CElement element) {
350+
private String createPropertyName(final Model model,
351+
CPropertyInfo propertyInfo, CElement element) {
341352
final String localPart;
342353
if (element instanceof CClassRef) {
343354
final CClassRef classRef = (CClassRef) element;
@@ -348,13 +359,20 @@ private String createPropertyName(final Model model, CElement element) {
348359
}
349360
final String propertyName = model.getNameConverter().toPropertyName(
350361
localPart);
351-
return propertyName;
362+
return pluralizeIfNecessary(propertyInfo, propertyName);
352363
}
353364

354-
private String createPropertyName(final Model model, CTypeRef element) {
365+
private String createPropertyName(final Model model,
366+
CPropertyInfo propertyInfo, CTypeRef element) {
355367
final String propertyName = model.getNameConverter().toPropertyName(
356368
element.getTagName().getLocalPart());
357-
return propertyName;
369+
return pluralizeIfNecessary(propertyInfo, propertyName);
370+
}
371+
372+
private String pluralizeIfNecessary(CPropertyInfo propertyInfo,
373+
final String propertyName) {
374+
return (propertyInfo.isCollection() && isUsePluralForm())? JJavaName
375+
.getPluralForm(propertyName) : propertyName;
358376
}
359377

360378
}

tests/simplify-01/src/test/java/org/jvnet/jaxb2_commons/plugin/simplify/tests01/Gh1Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class Gh1Test {
1616
public void compiles()
1717
{
1818
final Gh1 item = new Gh1();
19-
item.getA();
20-
item.getB();
19+
item.getAS();
20+
item.getBS();
2121
item.getMixedContent();
2222

2323
}
@@ -26,8 +26,8 @@ public void compiles()
2626
public void contextIsSuccessfullyCreated() throws JAXBException {
2727
final JAXBContext context = JAXBContext.newInstance(Gh1.class);
2828
final Gh1 value = new Gh1();
29-
value.getA().add("a");
30-
value.getB().add(2);
29+
value.getAS().add("a");
30+
value.getBS().add(2);
3131
value.getMixedContent().add("Test");
3232

3333
final StringWriter sw = new StringWriter();

tests/simplify-01/src/test/java/org/jvnet/jaxb2_commons/plugin/simplify/tests01/Gh4Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public void setUp() throws Exception {
1919
@Test
2020
public void compiles() {
2121
final SimplifyReferencesPropertyAsElementPropertyType item = new SimplifyReferencesPropertyAsElementPropertyType();
22-
item.getBase();
23-
item.getBaseElement();
22+
item.getBases();
23+
item.getBaseElements();
2424

2525
}
2626

@@ -36,7 +36,7 @@ public void unmarshalls() throws Exception {
3636
"simplifyReferencesPropertyAsElementProperty.xml")))
3737
.getValue();
3838

39-
Assert.assertEquals(3, value.getBase().size());
40-
Assert.assertEquals(3, value.getBaseElement().size());
39+
Assert.assertEquals(3, value.getBases().size());
40+
Assert.assertEquals(3, value.getBaseElements().size());
4141
}
4242
}

tests/simplify-01/src/test/java/org/jvnet/jaxb2_commons/plugin/simplify/tests01/Gh5Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public void setUp() throws Exception {
1919
@Test
2020
public void compiles() {
2121
final SimplifyReferencesPropertyAsReferencePropertyType item = new SimplifyReferencesPropertyAsReferencePropertyType();
22-
item.getBase();
23-
item.getBaseElement();
22+
item.getBases();
23+
item.getBaseElements();
2424
}
2525

2626
@Test
@@ -35,7 +35,7 @@ public void unmarshalls() throws Exception {
3535
"simplifyReferencesPropertyAsReferenceProperty.xml")))
3636
.getValue();
3737

38-
Assert.assertEquals(3, value.getBase().size());
39-
Assert.assertEquals(3, value.getBaseElement().size());
38+
Assert.assertEquals(3, value.getBases().size());
39+
Assert.assertEquals(3, value.getBaseElements().size());
4040
}
4141
}

tests/simplify-01/src/test/java/org/jvnet/jaxb2_commons/plugin/simplify/tests01/Gh6Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public void setUp() throws Exception {
1919
@Test
2020
public void compiles() {
2121
final SimplifyElementsPropertyAsElementPropertyType item = new SimplifyElementsPropertyAsElementPropertyType();
22-
item.getInt();
23-
item.getString();
22+
item.getInts();
23+
item.getStrings();
2424
}
2525

2626
public void testElementsPropertyAsElementPropertyType() throws Exception {
@@ -31,7 +31,7 @@ public void testElementsPropertyAsElementPropertyType() throws Exception {
3131
getClass().getResourceAsStream("simplifyElementsPropertyAsElementProperty.xml")))
3232
.getValue();
3333

34-
Assert.assertEquals(3, value.getString().size());
35-
Assert.assertEquals(3, value.getInt().size());
34+
Assert.assertEquals(3, value.getStrings().size());
35+
Assert.assertEquals(3, value.getInts().size());
3636
}
3737
}

tests/simplify-01/src/test/java/org/jvnet/jaxb2_commons/plugin/simplify/tests01/RunSimplifyPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void compilesSchema() throws Exception {
2828
URL binding = getClass().getResource("/binding.xjb");
2929
final String[] arguments = new String[] { "-xmlschema",
3030
schema.toExternalForm(), "-b", binding.toExternalForm(), "-d",
31-
"target/generated-sources/xjc", "-extension", "-Xsimplify" };
31+
"target/generated-sources/xjc", "-extension", "-Xsimplify"};
3232

3333
Options options = new Options();
3434
options.parseArguments(arguments);

0 commit comments

Comments
 (0)