Skip to content

Commit

Permalink
Issues #50, #40, #33.
Browse files Browse the repository at this point in the history
  • Loading branch information
highsource committed Sep 12, 2015
1 parent 5e4641b commit 2c979f4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.hisrc.jsonix.compilation.mapping;

import java.math.BigInteger;

import javax.xml.namespace.QName;

import org.apache.commons.lang3.Validate;
Expand All @@ -9,6 +11,8 @@
import org.hisrc.jscm.codemodel.expression.JSMemberExpression;
import org.hisrc.jscm.codemodel.expression.JSObjectLiteral;
import org.hisrc.jsonix.naming.Naming;
import org.hisrc.jsonix.xml.xsom.ParticleMultiplicityCounter;
import org.hisrc.xml.xsom.XSFunctionApplier;
import org.jvnet.jaxb2_commons.xml.bind.model.MAnyAttributePropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MAnyElementPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MAttributePropertyInfo;
Expand All @@ -29,12 +33,16 @@
import org.jvnet.jaxb2_commons.xml.bind.model.MWrappable;
import org.jvnet.jaxb2_commons.xml.bind.model.util.DefaultTypeInfoVisitor;

import com.sun.tools.xjc.model.Multiplicity;

final class PropertyInfoVisitor<T, C extends T> implements
MPropertyInfoVisitor<T, C, JSObjectLiteral> {

private final JSCodeModel codeModel;
private final MappingCompiler<T, C> mappingCompiler;
private final Naming naming;
private final XSFunctionApplier<Multiplicity> multiplicityCounter = new XSFunctionApplier<Multiplicity>(
ParticleMultiplicityCounter.INSTANCE);

public PropertyInfoVisitor(MappingCompiler<T, C> mappingCompiler) {
Validate.notNull(mappingCompiler);
Expand All @@ -47,6 +55,28 @@ private void createPropertyInfoOptions(MPropertyInfo<T, C> propertyInfo,
JSObjectLiteral options) {
options.append(naming.name(),
this.codeModel.string(propertyInfo.getPrivateName()));

final Multiplicity multiplicity = multiplicityCounter
.apply(propertyInfo.getOrigin());
if (multiplicity != null) {
if (multiplicity.min != null
&& !BigInteger.ZERO.equals(multiplicity.min)) {
options.append(naming.required(), this.codeModel._boolean(true));
}
if (propertyInfo.isCollection()) {
if (multiplicity.min != null) {
if (!BigInteger.ONE.equals(multiplicity.min)) {
options.append(naming.minOccurs(), this.codeModel
.integer(multiplicity.min.longValue()));
}
}
if (multiplicity.max != null) {
options.append(naming.maxOccurs(), this.codeModel
.integer(multiplicity.max.longValue()));
}
}
}

if (propertyInfo.isCollection()) {
options.append(naming.collection(), this.codeModel._boolean(true));
}
Expand Down
19 changes: 19 additions & 0 deletions compiler/src/main/java/org/hisrc/jsonix/naming/CompactNaming.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public class CompactNaming implements Naming {
public static final String ELEMENT_REF = "er";
public static final String ELEMENT_REFS = "ers";

public static final String REQUIRED = "rq";
public static final String MINOCCURS = "mno";
public static final String MAXOCCURS = "mxo";

public static final String NAMING_NAME = "compact";

public CompactNaming() {
Expand Down Expand Up @@ -245,4 +249,19 @@ public String elementInfos() {
public String typeInfos() {
return TYPE_INFOS;
}

@Override
public String required() {
return REQUIRED;
}

@Override
public String minOccurs() {
return MINOCCURS;
}

@Override
public String maxOccurs() {
return MAXOCCURS;
}
}
5 changes: 5 additions & 0 deletions compiler/src/main/java/org/hisrc/jsonix/naming/Naming.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@ public interface Naming {

String localPart();

String required();

String minOccurs();

String maxOccurs();
}
25 changes: 22 additions & 3 deletions compiler/src/main/java/org/hisrc/jsonix/naming/StandardNaming.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class StandardNaming implements Naming {
public static final String LIST = "list"; // li
public static final String CLASS_INFO = "classInfo"; // ci
public static final String LOCAL_NAME = "localName"; // ln
public static final String TYPE_NAME = "typeName"; // tn
public static final String TYPE_NAME = "typeName"; // tn
public static final String BASE_TYPE_INFO = "baseTypeInfo"; // base
public static final String PROPERTY_INFOS = "propertyInfos"; // ps
public static final String ENUM_INFO = "enumInfo"; // ei
Expand Down Expand Up @@ -43,6 +43,10 @@ public class StandardNaming implements Naming {
public static final String ELEMENT_REFS = "elementRefs";// rfs
public static final String COLLECTION = "collection";// col

public static final String REQUIRED = "required";
public static final String MINOCCURS = "minOccurs";
public static final String MAXOCCURS = "maxOccurs";

public static final String NAMING_NAME = "standard";

public StandardNaming() {
Expand All @@ -68,7 +72,7 @@ public String list() {
public String localName() {
return LOCAL_NAME;
}

@Override
public String typeName() {
return TYPE_NAME;
Expand Down Expand Up @@ -216,7 +220,7 @@ public String localPart() {
public String dependencies() {
return DEPENDENCIES;
}

@Override
public String targetNamespaceURI() {
return TARGET_NAMESPACE_URI;
Expand All @@ -242,4 +246,19 @@ public String typeInfos() {
return TYPE_INFOS;
}

@Override
public String required() {
return REQUIRED;
}

@Override
public String minOccurs() {
return MINOCCURS;
}

@Override
public String maxOccurs() {
return MAXOCCURS;
}

}

0 comments on commit 2c979f4

Please sign in to comment.