| 1 |
|
package uk.co.badgersinfoil.asxsd.components; |
| 2 |
|
|
| 3 |
|
import org.eclipse.xsd.XSDConcreteComponent; |
| 4 |
|
import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 5 |
|
import org.eclipse.xsd.XSDVariety; |
| 6 |
|
import uk.co.badgersinfoil.asxsd.CodegenContext; |
| 7 |
|
import uk.co.badgersinfoil.asxsd.CodegenRole; |
| 8 |
|
import uk.co.badgersinfoil.asxsd.MappingFunction; |
| 9 |
|
import uk.co.badgersinfoil.asxsd.MarshalBuilder; |
| 10 |
|
import uk.co.badgersinfoil.asxsd.TypeDescriptor; |
| 11 |
|
import uk.co.badgersinfoil.asxsd.UnmarshalBuilder; |
| 12 |
|
import uk.co.badgersinfoil.metaas.dom.ASIfStatement; |
| 13 |
|
import uk.co.badgersinfoil.metaas.dom.StatementContainer; |
| 14 |
|
|
| 15 |
15 |
public class ListComponent extends AbstractSimpleTypeComponent { |
| 16 |
|
|
| 17 |
|
public TypeDescriptor typeDescriptorFor(CodegenContext context, |
| 18 |
|
XSDSimpleTypeDefinition listType) |
| 19 |
|
{ |
| 20 |
15 |
return context.getOrCreateType(listType); |
| 21 |
|
} |
| 22 |
|
|
| 23 |
|
public TypeDescriptor createTypeDescriptor(CodegenContext context, |
| 24 |
|
XSDSimpleTypeDefinition listType) |
| 25 |
|
{ |
| 26 |
5 |
XSDSimpleTypeDefinition itemType = listType.getItemTypeDefinition(); |
| 27 |
5 |
TypeDescriptor itemTypeDesc = context.typeDescriptorFor(itemType); |
| 28 |
5 |
String docs = "Items of type "+itemTypeDesc.getTypeName()+" ("+itemType.getURI()+")"; |
| 29 |
5 |
return new TypeDescriptor("Array", true, docs, listType); |
| 30 |
|
} |
| 31 |
|
|
| 32 |
|
public void generateCode(CodegenContext context, |
| 33 |
|
XSDConcreteComponent component) |
| 34 |
|
{ |
| 35 |
10 |
XSDSimpleTypeDefinition listType = (XSDSimpleTypeDefinition)component; |
| 36 |
10 |
XSDSimpleTypeDefinition itemType = listType.getItemTypeDefinition(); |
| 37 |
10 |
CodegenRole role = context.getCurrentRole(); |
| 38 |
10 |
if (role == CodegenRole.UNMARSHAL) { |
| 39 |
5 |
StatementContainer block = context.getCurrentMethodCode(); |
| 40 |
5 |
String srcExpr = UnmarshalBuilder.getCurrentSourceExpr(context); |
| 41 |
5 |
block.addStmt("var _tmp = new Array()"); |
| 42 |
5 |
StatementContainer loop = block.newForEachIn("var listItem", srcExpr+".split(\" \")"); |
| 43 |
5 |
MappingFunction fn = context.functionFor(itemType); |
| 44 |
5 |
String converted = fn.appliedTo("listItem"); |
| 45 |
5 |
loop.addStmt("_tmp.push("+converted+")"); |
| 46 |
5 |
block.newReturn("_tmp"); |
| 47 |
5 |
} else if (role == CodegenRole.MARSHAL) { |
| 48 |
5 |
StatementContainer block = context.getCurrentMethodCode(); |
| 49 |
5 |
String srcExpr = MarshalBuilder.getCurrentSourceExpr(context); |
| 50 |
5 |
block.addStmt("var _tmp = null"); |
| 51 |
5 |
StatementContainer loop = block.newForEachIn("var listItem", srcExpr); |
| 52 |
5 |
MappingFunction fn = context.functionFor(itemType); |
| 53 |
5 |
String converted = fn.appliedTo("listItem"); |
| 54 |
5 |
ASIfStatement ifFirst = loop.newIf("_tmp == null"); |
| 55 |
5 |
ifFirst.addStmt("_tmp = " + converted); |
| 56 |
5 |
ifFirst.getElse().addStmt("_tmp += \" \" + "+converted); |
| 57 |
5 |
block.newReturn("_tmp"); |
| 58 |
5 |
} else { |
| 59 |
0 |
super.generateCode(context, component); |
| 60 |
|
} |
| 61 |
10 |
} |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
public boolean willAcceptType(XSDSimpleTypeDefinition simpleType) { |
| 66 |
1056 |
return simpleType.getVariety() == XSDVariety.LIST_LITERAL; |
| 67 |
|
} |
| 68 |
|
} |