| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
package uk.co.badgersinfoil.asxsd.components; |
| 6 |
|
|
| 7 |
|
import org.eclipse.xsd.XSDConcreteComponent; |
| 8 |
|
import org.eclipse.xsd.XSDNamedComponent; |
| 9 |
|
import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 10 |
|
import uk.co.badgersinfoil.asxsd.BasicMappingFunction; |
| 11 |
|
import uk.co.badgersinfoil.asxsd.CodegenContext; |
| 12 |
|
import uk.co.badgersinfoil.asxsd.CodegenRole; |
| 13 |
|
import uk.co.badgersinfoil.asxsd.MappingFunction; |
| 14 |
|
import uk.co.badgersinfoil.asxsd.MarshalBuilder; |
| 15 |
|
import uk.co.badgersinfoil.asxsd.StringUtils; |
| 16 |
|
import uk.co.badgersinfoil.asxsd.TypeDescriptor; |
| 17 |
|
import uk.co.badgersinfoil.asxsd.UnmarshalBuilder; |
| 18 |
|
import uk.co.badgersinfoil.asxsd.XSDUtils; |
| 19 |
|
import uk.co.badgersinfoil.metaas.dom.ASArg; |
| 20 |
|
import uk.co.badgersinfoil.metaas.dom.ASClassType; |
| 21 |
|
import uk.co.badgersinfoil.metaas.dom.ASCompilationUnit; |
| 22 |
|
import uk.co.badgersinfoil.metaas.dom.ASMethod; |
| 23 |
|
import uk.co.badgersinfoil.metaas.dom.StatementContainer; |
| 24 |
|
import uk.co.badgersinfoil.metaas.dom.Visibility; |
| 25 |
|
|
| 26 |
214 |
public abstract class AbstractSimpleTypeComponent extends AbstractMappingComponent { |
| 27 |
|
|
| 28 |
|
public TypeDescriptor typeDescriptorFor(CodegenContext context, |
| 29 |
|
XSDConcreteComponent component) |
| 30 |
|
{ |
| 31 |
437 |
XSDSimpleTypeDefinition simpleType = (XSDSimpleTypeDefinition)component; |
| 32 |
437 |
return typeDescriptorFor(context, simpleType); |
| 33 |
|
} |
| 34 |
|
|
| 35 |
|
protected TypeDescriptor typeDescriptorFor(CodegenContext context, |
| 36 |
|
XSDSimpleTypeDefinition simpleType) |
| 37 |
|
{ |
| 38 |
187 |
return super.typeDescriptorFor(context, simpleType); |
| 39 |
|
} |
| 40 |
|
|
| 41 |
|
public TypeDescriptor createTypeDescriptor(CodegenContext context, |
| 42 |
|
XSDConcreteComponent component) |
| 43 |
|
{ |
| 44 |
33 |
XSDSimpleTypeDefinition simpleType = (XSDSimpleTypeDefinition)component; |
| 45 |
33 |
return createTypeDescriptor(context, simpleType); |
| 46 |
|
} |
| 47 |
|
|
| 48 |
|
public ASCompilationUnit createType(CodegenContext context, |
| 49 |
|
XSDConcreteComponent component) |
| 50 |
|
{ |
| 51 |
33 |
return null; |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
public void generateCode(CodegenContext context, |
| 55 |
|
XSDConcreteComponent component) |
| 56 |
|
{ |
| 57 |
62 |
CodegenRole role = context.getCurrentRole(); |
| 58 |
62 |
if (role == CodegenRole.UNMARSHAL) { |
| 59 |
31 |
StatementContainer block = context.getCurrentMethodCode(); |
| 60 |
31 |
MappingFunction fn = context.functionFor(component); |
| 61 |
31 |
String converted = fn.appliedTo("thisValue"); |
| 62 |
31 |
block.addStmt("return "+converted); |
| 63 |
31 |
} else if (role == CodegenRole.MARSHAL) { |
| 64 |
31 |
StatementContainer block = context.getCurrentMethodCode(); |
| 65 |
31 |
MappingFunction fn = context.functionFor(component); |
| 66 |
31 |
String converted = fn.appliedTo("thisValue"); |
| 67 |
31 |
block.addStmt("return "+converted); |
| 68 |
31 |
} else { |
| 69 |
0 |
super.generateCode(context, component); |
| 70 |
|
} |
| 71 |
62 |
} |
| 72 |
|
|
| 73 |
|
protected abstract TypeDescriptor createTypeDescriptor(CodegenContext context, |
| 74 |
|
XSDSimpleTypeDefinition simpleType); |
| 75 |
|
|
| 76 |
|
public MappingFunction createFunctionRef(CodegenContext context, |
| 77 |
|
XSDConcreteComponent component) |
| 78 |
|
{ |
| 79 |
115 |
XSDSimpleTypeDefinition simpleType = (XSDSimpleTypeDefinition)component; |
| 80 |
115 |
CodegenRole role = context.getCurrentRole(); |
| 81 |
115 |
if (role == CodegenRole.UNMARSHAL) { |
| 82 |
58 |
UnmarshalBuilder builder = context.getUnmarshalBuilder(); |
| 83 |
58 |
ASCompilationUnit unit = builder.getClassForNamespace(simpleType.getTargetNamespace()); |
| 84 |
58 |
ASClassType clazz = (ASClassType)unit.getType(); |
| 85 |
58 |
String methodName = unmarshalMethodNameFor(simpleType); |
| 86 |
|
String qname; |
| 87 |
58 |
if (unit.getPackageName() != null) { |
| 88 |
58 |
qname = unit.getPackageName()+"."+clazz.getName()+"."+methodName; |
| 89 |
|
} else { |
| 90 |
0 |
qname = clazz.getName()+"."+methodName; |
| 91 |
|
} |
| 92 |
58 |
return new BasicMappingFunction(qname); |
| 93 |
|
} |
| 94 |
57 |
if (role == CodegenRole.MARSHAL) { |
| 95 |
57 |
MarshalBuilder builder = context.getMarshalBuilder(); |
| 96 |
57 |
ASCompilationUnit unit = builder.getClassForNamespace(simpleType.getTargetNamespace()); |
| 97 |
57 |
ASClassType clazz = (ASClassType)unit.getType(); |
| 98 |
57 |
String methodName = builder.methodNameFor(simpleType); |
| 99 |
|
String qname; |
| 100 |
57 |
if (unit.getPackageName() != null) { |
| 101 |
57 |
qname = unit.getPackageName()+"."+clazz.getName()+"."+methodName; |
| 102 |
|
} else { |
| 103 |
0 |
qname = clazz.getName()+"."+methodName; |
| 104 |
|
} |
| 105 |
57 |
return new BasicMappingFunction(qname); |
| 106 |
|
} |
| 107 |
0 |
throw new IllegalArgumentException(getClass().getName() + " does not support role "+role); |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
private String unmarshalMethodNameFor(XSDSimpleTypeDefinition simpleType) { |
| 111 |
|
String stub; |
| 112 |
116 |
if (XSDUtils.isAnonymous(simpleType)) { |
| 113 |
2 |
XSDNamedComponent container = (XSDNamedComponent)simpleType.getContainer(); |
| 114 |
2 |
stub = container.getName(); |
| 115 |
2 |
} else { |
| 116 |
114 |
stub = simpleType.getName(); |
| 117 |
|
} |
| 118 |
116 |
return "unmarshal" + StringUtils.sanitize(stub); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
public ASMethod createFunction(CodegenContext context, |
| 122 |
|
XSDConcreteComponent component) |
| 123 |
|
{ |
| 124 |
115 |
XSDSimpleTypeDefinition simpleType = (XSDSimpleTypeDefinition)component; |
| 125 |
115 |
CodegenRole role = context.getCurrentRole(); |
| 126 |
115 |
if (role == CodegenRole.UNMARSHAL) { |
| 127 |
58 |
UnmarshalBuilder builder = context.getUnmarshalBuilder(); |
| 128 |
58 |
ASCompilationUnit unit = builder.getClassForNamespace(simpleType.getTargetNamespace()); |
| 129 |
58 |
ASClassType clazz = (ASClassType)unit.getType(); |
| 130 |
58 |
TypeDescriptor typeDesc = context.typeDescriptorFor(simpleType); |
| 131 |
58 |
String methodName = unmarshalMethodNameFor(simpleType); |
| 132 |
58 |
ASMethod meth = clazz.newMethod(methodName, Visibility.PUBLIC, typeDesc.getTypeName()); |
| 133 |
58 |
ASArg arg = meth.addParam("thisValue", "String"); |
| 134 |
58 |
String doc = "a value of type "+simpleType.getURI(); |
| 135 |
58 |
arg.setDescription(doc); |
| 136 |
58 |
meth.setStatic(true); |
| 137 |
58 |
context.pushAttrScope(); |
| 138 |
58 |
UnmarshalBuilder.setCurrentSourceExpr(context, arg.getName()); |
| 139 |
58 |
context.setCurrentMethodCode(meth); |
| 140 |
58 |
context.generateCode(simpleType); |
| 141 |
58 |
context.popAttrScope(); |
| 142 |
58 |
return meth; |
| 143 |
|
} |
| 144 |
57 |
if (role == CodegenRole.MARSHAL) { |
| 145 |
57 |
MarshalBuilder builder = context.getMarshalBuilder(); |
| 146 |
57 |
ASCompilationUnit unit = builder.getClassForNamespace(simpleType.getTargetNamespace()); |
| 147 |
57 |
ASClassType clazz = (ASClassType)unit.getType(); |
| 148 |
57 |
String typeName = MarshalBuilder.xmlReturnTypeFor(simpleType); |
| 149 |
57 |
String methodName = builder.methodNameFor(simpleType); |
| 150 |
57 |
ASMethod meth = clazz.newMethod(methodName, Visibility.PUBLIC, typeName); |
| 151 |
57 |
builder.buildMethodParams(meth, simpleType); |
| 152 |
57 |
meth.setStatic(true); |
| 153 |
57 |
context.pushAttrScope(); |
| 154 |
57 |
MarshalBuilder.setCurrentSourceExpr(context, "thisValue"); |
| 155 |
57 |
context.setCurrentMethodCode(meth); |
| 156 |
57 |
context.generateCode(simpleType); |
| 157 |
57 |
context.popAttrScope(); |
| 158 |
57 |
return meth; |
| 159 |
|
} |
| 160 |
0 |
return super.createFunction(context, component); |
| 161 |
|
} |
| 162 |
|
|
| 163 |
|
public boolean willAccept(XSDConcreteComponent component) { |
| 164 |
16385 |
return component instanceof XSDSimpleTypeDefinition |
| 165 |
|
&& willAcceptType((XSDSimpleTypeDefinition)component); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
protected abstract boolean willAcceptType(XSDSimpleTypeDefinition simpleType); |
| 169 |
|
} |