Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced code generation #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/main/java/com/mickoo/xml/xsd2simplexml/GeneratedClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ public void addElement(JType type, String name, int minOccurs, boolean unbounded
jAnnotationUse.param("inline", true);
if(minOccurs == 0) {
jAnnotationUse.param("required", false);
addGetterSetter(jField, fieldName, type, false);
} else if(minOccurs == 1){
jAnnotationUse.param("required", false);
addGetterSetter(jField, fieldName, type, false);
} else if(minOccurs > 1){
jAnnotationUse.param("required", true);
}
addGetterSetter(jField, fieldName, type, false);

} else {

Expand All @@ -74,11 +73,10 @@ public void addElement(JType type, String name, int minOccurs, boolean unbounded
}
if(minOccurs == 0) {
jAnnotationUse.param("required", false);
addGetterSetter(jField, fieldName, type, false);
} else if(minOccurs == 1){
jAnnotationUse.param("required", false);
addGetterSetter(jField, fieldName, type, false);
jAnnotationUse.param("required", true);
}
addGetterSetter(jField, fieldName, type, false);
}

properties.add(fieldName);
Expand Down
69 changes: 45 additions & 24 deletions src/main/java/com/mickoo/xml/xsd2simplexml/SchemaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,36 @@ public void parse() throws Exception {
logger.info("Please verify the generated classes for compile errors and syntax issues.");
}

protected JType getJType(JCodeModel codeModel, String type) {
if ("string".equals(type) || "anyURI".equals(type) || "time".equals(type)) {
return codeModel._ref(String.class);
} else if ("int".equals(type) || "integer".equals(type) || "byte".equals(type) || "negativeInteger".equals(type) || "nonNegativeInteger".equals(type) || "nonPositiveInteger".equals(type) || "positiveInteger".equals(type) || "short".equals(type) || "unsignedInt".equals(type) || "unsignedShort".equals(type) || "byte".equals("unsignedByte")) {
return codeModel._ref(Integer.class);
} else if ("long".equals(type) || "unsignedLong".equals(type)) {
return codeModel._ref(Long.class);
} else if ("decimal".equals(type)) {
return codeModel._ref(Double.class);
} else if ("boolean".equals(type)) {
return codeModel._ref(Boolean.class);
protected JType getJType(JCodeModel codeModel, String type, boolean canUsePrimitiveType) {
switch (type) {
case "int":
case "integer":
case "byte":
case "negativeInteger":
case "nonNegativeInteger":
case "nonPositiveInteger":
case "positiveInteger":
case "short":
case "unsignedInt":
case "unsignedShort":
case "unsignedByte":
return canUsePrimitiveType ? codeModel._ref(int.class) : codeModel._ref(Integer.class);
case "long":
case "unsignedLong":
return canUsePrimitiveType ? codeModel._ref(long.class) : codeModel._ref(Long.class);
case "decimal":
return canUsePrimitiveType ? codeModel._ref(double.class) : codeModel._ref(Double.class);
case "boolean":
return canUsePrimitiveType ? codeModel._ref(boolean.class) : codeModel._ref(Boolean.class);
case "time":
case "date":
case "dateTime":
return codeModel._ref(Date.class);
case "string":
case "anyURI":
default:
return codeModel._ref(String.class);
}

return codeModel._ref(String.class);
}

static class ParseContext {
Expand Down Expand Up @@ -116,6 +132,7 @@ static class SimpleTypeRestriction {
public String minLength = null;
public String[] pattern = null;
public String totalDigits = null;
public String baseType;

public String toString() {
String enumValues = "";
Expand Down Expand Up @@ -147,11 +164,14 @@ public String toString() {
}
}

protected SimpleTypeRestriction getRestrictions(XSSimpleType xsSimpleType) {
protected SimpleTypeRestriction getRestrictions(XSSimpleType xsSimpleType) {
SimpleTypeRestriction simpleTypeRestriction = new SimpleTypeRestriction();
simpleTypeRestriction.baseType = xsSimpleType.getName();
XSRestrictionSimpleType restriction = xsSimpleType.asRestriction();
if (restriction != null) {

if (!xsSimpleType.getTargetNamespace().equals("http://www.w3.org/2001/XMLSchema")) {
simpleTypeRestriction.baseType = restriction.getBaseType().asSimpleType().getName();
}
Vector<String> enumeration = new Vector<String>();
Vector<String> pattern = new Vector<String>();

Expand Down Expand Up @@ -214,12 +234,15 @@ protected void processParticle(XSParticle particle, ParseContext parseContext) t
protected void processGroup(XSModelGroup modelGroup, ParseContext parseContext) throws Exception {
logger.info(parseContext.indent + "[Start of " + modelGroup.getCompositor() + parseContext.getOccurs() + "]");
for (XSParticle particle : modelGroup.getChildren()) {
if (modelGroup.getCompositor() == XSModelGroup.Compositor.CHOICE) {
particle.setMinOccurs(BigInteger.ZERO);
}
ParseContext newParseContext = new ParseContext();
newParseContext.indent = parseContext.indent + "\t";
newParseContext.path = parseContext.path;
newParseContext.currentClass = parseContext.currentClass;
particle.setMaxOccurs(BigInteger.valueOf(parseContext.maxOccurs.intValue()));
particle.setMinOccurs(BigInteger.valueOf(parseContext.minOccurs.intValue()));
//particle.setMaxOccurs(BigInteger.valueOf(parseContext.maxOccurs.intValue()));
//particle.setMinOccurs(BigInteger.valueOf(parseContext.minOccurs.intValue()));
processParticle(particle, newParseContext);
}
logger.info(parseContext.indent + "[End of " + modelGroup.getCompositor() + "]");
Expand All @@ -239,8 +262,8 @@ protected void processComplexType(XSComplexType complexType, ParseContext parseC
if (xsSimpleType != null) {
processText("value", xsSimpleType, parseContext);
}
Collection<? extends XSAttributeUse> c = complexType.getAttributeUses();
Iterator<? extends XSAttributeUse> i = c.iterator();

Iterator<? extends XSAttributeUse> i = complexType.iterateAttributeUses();
while(i.hasNext()) {
XSAttributeUse attUse = i.next();
processAttribute(attUse, parseContext);
Expand Down Expand Up @@ -296,11 +319,9 @@ private void addSimpleType(String name, XSSimpleType simpleType, ParseContext pa

//add simple element with primitive property to class

JType jType = getJType(parseContext.currentClass.codeModel, simpleType.getName());
if(jType == null && restrictions.pattern != null) {
jType = parseContext.currentClass.codeModel.ref(String.class);
}

boolean canUsePrimitiveType = parseContext.minOccurs == 1 && parseContext.maxOccurs == 1;
JType jType = getJType(parseContext.currentClass.codeModel, restrictions.baseType, canUsePrimitiveType);

parseContext.currentClass.addElement(
jType,
name,
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/sun/xml/xsom/impl/ComplexTypeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,17 @@ public Collection<XSAttributeUse> getAttributeUses() {

if( baseType==null ) return super.getAttributeUses();

// TODO: this is fairly inefficient
Map<UName,XSAttributeUse> uses = new HashMap<UName, XSAttributeUse>();
for( XSAttributeUse a : baseType.getAttributeUses())
uses.put(new UName(a.getDecl()),a);

uses.keySet().removeAll(prohibitedAtts);
List<XSAttributeUse> uses = new ArrayList<XSAttributeUse>();
for( XSAttributeUse a : baseType.getAttributeUses()) {
if (!prohibitedAtts.contains(a.getDecl())) {
uses.add(a);
}
}

for( XSAttributeUse a : super.getAttributeUses())
uses.put(new UName(a.getDecl()),a);
uses.add(a);

return uses.values();
return uses;
}


Expand Down