| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
package uk.co.badgersinfoil.asxsd.components; |
| 6 |
|
|
| 7 |
|
import org.eclipse.xsd.XSDConcreteComponent; |
| 8 |
|
import org.eclipse.xsd.XSDElementDeclaration; |
| 9 |
|
import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 10 |
|
import org.eclipse.xsd.XSDTypeDefinition; |
| 11 |
|
import uk.co.badgersinfoil.asxsd.BasicMappingFunction; |
| 12 |
|
import uk.co.badgersinfoil.asxsd.CodegenContext; |
| 13 |
|
import uk.co.badgersinfoil.asxsd.CodegenRole; |
| 14 |
|
import uk.co.badgersinfoil.asxsd.MappingFunction; |
| 15 |
|
import uk.co.badgersinfoil.asxsd.MarshalBuilder; |
| 16 |
|
import uk.co.badgersinfoil.asxsd.StringUtils; |
| 17 |
|
import uk.co.badgersinfoil.asxsd.TypeBuilder; |
| 18 |
|
import uk.co.badgersinfoil.asxsd.TypeDescriptor; |
| 19 |
|
import uk.co.badgersinfoil.asxsd.UnmarshalBuilder; |
| 20 |
|
import uk.co.badgersinfoil.asxsd.XSDUtils; |
| 21 |
|
import uk.co.badgersinfoil.metaas.dom.ASArg; |
| 22 |
|
import uk.co.badgersinfoil.metaas.dom.ASClassType; |
| 23 |
|
import uk.co.badgersinfoil.metaas.dom.ASCompilationUnit; |
| 24 |
|
import uk.co.badgersinfoil.metaas.dom.ASConstants; |
| 25 |
|
import uk.co.badgersinfoil.metaas.dom.ASMethod; |
| 26 |
|
import uk.co.badgersinfoil.metaas.dom.StatementContainer; |
| 27 |
|
import uk.co.badgersinfoil.metaas.dom.Visibility; |
| 28 |
|
|
| 29 |
15 |
public class ArrayTypeDeclarationComponent extends AbstractMappingComponent { |
| 30 |
|
|
| 31 |
|
public TypeDescriptor createTypeDescriptor(CodegenContext context, |
| 32 |
|
XSDConcreteComponent component) |
| 33 |
|
{ |
| 34 |
3 |
XSDTypeDefinition typeDef = (XSDTypeDefinition)component; |
| 35 |
3 |
XSDElementDeclaration listElement = XSDUtils.getElementIfContainerForList(typeDef); |
| 36 |
3 |
TypeDescriptor elementType = context.typeDescriptorFor(listElement); |
| 37 |
3 |
String doc = "Elements of type {@link " + elementType.getTypeName()+"}"; |
| 38 |
3 |
return new TypeDescriptor(ASConstants.TYPE_ARRAY, true, doc, component); |
| 39 |
|
} |
| 40 |
|
|
| 41 |
|
public ASCompilationUnit createType(CodegenContext context, |
| 42 |
|
XSDConcreteComponent component) |
| 43 |
|
{ |
| 44 |
|
|
| 45 |
1 |
return null; |
| 46 |
|
} |
| 47 |
|
|
| 48 |
|
public boolean willAccept(XSDConcreteComponent component) { |
| 49 |
2380 |
return component instanceof XSDTypeDefinition |
| 50 |
|
&& XSDUtils.getElementIfContainerForList((XSDTypeDefinition)component)!=null; |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
public void generateCode(CodegenContext context, final XSDConcreteComponent component) { |
| 54 |
3 |
CodegenRole role = context.getCurrentRole(); |
| 55 |
3 |
if (role == CodegenRole.TYPE) { |
| 56 |
0 |
ASClassType clazz = TypeBuilder.getCurrentType(context); |
| 57 |
0 |
clazz.setDescription("Gah! This type should not have been generated"); |
| 58 |
0 |
} else if (role == CodegenRole.MARSHAL) { |
| 59 |
1 |
StatementContainer block = context.getCurrentMethodCode(); |
| 60 |
1 |
block.addStmt("default xml namespace = NAMESPACE"); |
| 61 |
1 |
XSDElementDeclaration listElement = XSDUtils.getElementIfContainerForList((XSDTypeDefinition)component); |
| 62 |
1 |
String elementType = context.typeDescriptorFor(listElement).getTypeName(); |
| 63 |
1 |
String fieldAccess = "_result"; |
| 64 |
1 |
String childVarName = "_child"; |
| 65 |
1 |
StatementContainer loop = block.newForEachIn("var "+childVarName+":"+elementType, "thisObject"); |
| 66 |
1 |
String access = "_"+context.variableNameFor(listElement); |
| 67 |
1 |
loop.addStmt("var "+access + ":XML = <"+listElement.getName()+"/>"); |
| 68 |
1 |
if (listElement.getType() instanceof XSDSimpleTypeDefinition) { |
| 69 |
1 |
MappingFunction conv = context.functionFor(listElement); |
| 70 |
1 |
String converted = conv.appliedTo(childVarName); |
| 71 |
1 |
loop.addStmt(access + ".appendChild("+converted+")"); |
| 72 |
1 |
loop.addStmt(fieldAccess + ".appendChild("+access+")"); |
| 73 |
1 |
} else { |
| 74 |
0 |
MappingFunction conv = context.functionFor(listElement); |
| 75 |
0 |
String converted = conv.appliedTo(access + ", " + childVarName); |
| 76 |
0 |
loop.addStmt(converted); |
| 77 |
0 |
loop.addStmt(fieldAccess + ".appendChild("+access+")"); |
| 78 |
|
} |
| 79 |
1 |
block.addStmt("return _result"); |
| 80 |
1 |
} else if (role == CodegenRole.UNMARSHAL) { |
| 81 |
2 |
StatementContainer block = context.getCurrentMethodCode(); |
| 82 |
2 |
block.addStmt("default xml namespace = NAMESPACE"); |
| 83 |
2 |
TypeDescriptor typeDesc = context.createTypeDescriptor(component); |
| 84 |
2 |
block.addStmt("var _result:"+typeDesc.getTypeName()+" = new "+typeDesc.getTypeName()+"()"); |
| 85 |
2 |
XSDElementDeclaration listElement = XSDUtils.getElementIfContainerForList((XSDTypeDefinition)component); |
| 86 |
2 |
block.addStmt("var _children:XMLList = thisElement.elements()"); |
| 87 |
2 |
StatementContainer loop = block.newForEachIn("var _child:XML", "_children"); |
| 88 |
2 |
MappingFunction conv = context.functionFor(listElement); |
| 89 |
|
String access; |
| 90 |
2 |
if (listElement.getType() instanceof XSDSimpleTypeDefinition) { |
| 91 |
2 |
access = "_child.text()"; |
| 92 |
|
} else { |
| 93 |
0 |
access = "_child"; |
| 94 |
|
} |
| 95 |
2 |
String converted = conv.appliedTo(access); |
| 96 |
2 |
loop.addStmt("_result.push("+converted+")"); |
| 97 |
2 |
block.addStmt("return _result"); |
| 98 |
2 |
} else { |
| 99 |
0 |
throw new IllegalArgumentException(getClass().getName() + " does not support generateCode(role="+role+")"); |
| 100 |
|
} |
| 101 |
3 |
} |
| 102 |
|
|
| 103 |
|
public MappingFunction createFunctionRef(CodegenContext context, |
| 104 |
|
XSDConcreteComponent component) |
| 105 |
|
{ |
| 106 |
2 |
XSDTypeDefinition typeDef = (XSDTypeDefinition)component; |
| 107 |
2 |
CodegenRole role = context.getCurrentRole(); |
| 108 |
2 |
if (role == CodegenRole.MARSHAL) { |
| 109 |
1 |
MarshalBuilder builder = context.getMarshalBuilder(); |
| 110 |
1 |
ASCompilationUnit unit = builder.getClassForNamespace(typeDef.getTargetNamespace()); |
| 111 |
1 |
ASClassType clazz = (ASClassType)unit.getType(); |
| 112 |
1 |
String name = StringUtils.sanitize(typeDef.getName()); |
| 113 |
1 |
String methodName = "marshal" + name; |
| 114 |
|
String qname; |
| 115 |
1 |
if (unit.getPackageName() != null) { |
| 116 |
1 |
qname = unit.getPackageName()+"."+clazz.getName()+"."+methodName; |
| 117 |
|
} else { |
| 118 |
0 |
qname = clazz.getName()+"."+methodName; |
| 119 |
|
} |
| 120 |
1 |
return new BasicMappingFunction(qname); |
| 121 |
|
} |
| 122 |
1 |
if (role == CodegenRole.UNMARSHAL) { |
| 123 |
1 |
UnmarshalBuilder builder = context.getUnmarshalBuilder(); |
| 124 |
1 |
ASCompilationUnit unit = builder.getClassForNamespace(typeDef.getTargetNamespace()); |
| 125 |
1 |
ASClassType clazz = (ASClassType)unit.getType(); |
| 126 |
1 |
String name = StringUtils.sanitize(typeDef.getName()); |
| 127 |
1 |
String methodName = "unmarshal" + name; |
| 128 |
|
String qname; |
| 129 |
1 |
if (unit.getPackageName() != null) { |
| 130 |
1 |
qname = unit.getPackageName()+"."+clazz.getName()+"."+methodName; |
| 131 |
|
} else { |
| 132 |
0 |
qname = clazz.getName()+"."+methodName; |
| 133 |
|
} |
| 134 |
1 |
return new BasicMappingFunction(qname); |
| 135 |
|
} |
| 136 |
0 |
return super.createFunctionRef(context, component); |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
public ASMethod createFunction(CodegenContext context, |
| 140 |
|
XSDConcreteComponent component) |
| 141 |
|
{ |
| 142 |
2 |
XSDTypeDefinition typeDef = (XSDTypeDefinition)component; |
| 143 |
2 |
CodegenRole role = context.getCurrentRole(); |
| 144 |
2 |
if (role == CodegenRole.MARSHAL) { |
| 145 |
1 |
MarshalBuilder builder = context.getMarshalBuilder(); |
| 146 |
1 |
ASCompilationUnit unit = builder.getClassForNamespace(typeDef.getTargetNamespace()); |
| 147 |
1 |
ASClassType clazz = (ASClassType)unit.getType(); |
| 148 |
1 |
String name = StringUtils.sanitize(typeDef.getName()); |
| 149 |
1 |
String methodName = "marshal" + name; |
| 150 |
1 |
TypeDescriptor typeDesc = context.typeDescriptorFor(typeDef); |
| 151 |
1 |
ASMethod meth = clazz.newMethod(methodName, Visibility.PUBLIC, "XML"); |
| 152 |
1 |
ASArg resultArg = meth.addParam("_result", "XML"); |
| 153 |
1 |
ASArg arg = meth.addParam("thisObject", typeDesc.getTypeName()); |
| 154 |
1 |
meth.setStatic(true); |
| 155 |
1 |
context.pushAttrScope(); |
| 156 |
1 |
context.setCurrentMethodCode(meth); |
| 157 |
1 |
UnmarshalBuilder.setCurrentSourceExpr(context, arg.getName()); |
| 158 |
1 |
context.generateCode(typeDef); |
| 159 |
1 |
context.popAttrScope(); |
| 160 |
1 |
return meth; |
| 161 |
|
} |
| 162 |
1 |
if (role == CodegenRole.UNMARSHAL) { |
| 163 |
1 |
UnmarshalBuilder builder = context.getUnmarshalBuilder(); |
| 164 |
1 |
ASCompilationUnit unit = builder.getClassForNamespace(typeDef.getTargetNamespace()); |
| 165 |
1 |
ASClassType clazz = (ASClassType)unit.getType(); |
| 166 |
1 |
String name = StringUtils.sanitize(typeDef.getName()); |
| 167 |
1 |
String methodName = "unmarshal" + name; |
| 168 |
1 |
TypeDescriptor typeDesc = context.typeDescriptorFor(typeDef); |
| 169 |
1 |
ASMethod meth = clazz.newMethod(methodName, Visibility.PUBLIC, typeDesc.getTypeName()); |
| 170 |
1 |
ASArg arg = meth.addParam("thisElement", "XML"); |
| 171 |
1 |
String doc = "a element of type "+typeDef.getURI(); |
| 172 |
1 |
arg.setDescription(doc); |
| 173 |
1 |
meth.setStatic(true); |
| 174 |
1 |
context.pushAttrScope(); |
| 175 |
1 |
context.setCurrentMethodCode(meth); |
| 176 |
1 |
UnmarshalBuilder.setCurrentSourceExpr(context, arg.getName()); |
| 177 |
1 |
context.generateCode(typeDef); |
| 178 |
1 |
context.popAttrScope(); |
| 179 |
1 |
return meth; |
| 180 |
|
} |
| 181 |
0 |
return super.createFunction(context, component); |
| 182 |
|
} |
| 183 |
|
} |