| 1 |
|
package uk.co.badgersinfoil.asxsd.components; |
| 2 |
|
|
| 3 |
|
import org.eclipse.xsd.XSDAttributeDeclaration; |
| 4 |
|
import org.eclipse.xsd.XSDConcreteComponent; |
| 5 |
|
import uk.co.badgersinfoil.asxsd.CodegenContext; |
| 6 |
|
import uk.co.badgersinfoil.asxsd.CodegenRole; |
| 7 |
|
import uk.co.badgersinfoil.asxsd.MappingFunction; |
| 8 |
|
import uk.co.badgersinfoil.asxsd.MarshalBuilder; |
| 9 |
|
import uk.co.badgersinfoil.asxsd.StringUtils; |
| 10 |
|
import uk.co.badgersinfoil.asxsd.UnmarshalBuilder; |
| 11 |
|
import uk.co.badgersinfoil.metaas.dom.StatementContainer; |
| 12 |
|
|
| 13 |
|
|
| 14 |
15 |
public class AttributeDeclarationComponent extends AbstractMappingComponent { |
| 15 |
|
|
| 16 |
|
public boolean willAccept(XSDConcreteComponent component) { |
| 17 |
108 |
return component instanceof XSDAttributeDeclaration; |
| 18 |
|
} |
| 19 |
|
|
| 20 |
|
public void generateCode(CodegenContext context, |
| 21 |
|
XSDConcreteComponent component) |
| 22 |
|
{ |
| 23 |
16 |
XSDAttributeDeclaration attrDecl = (XSDAttributeDeclaration)component; |
| 24 |
16 |
CodegenRole role = context.getCurrentRole(); |
| 25 |
16 |
if (role == CodegenRole.UNMARSHAL) { |
| 26 |
8 |
StatementContainer code = context.getCurrentMethodCode(); |
| 27 |
8 |
String sourceExpr = UnmarshalBuilder.getCurrentSourceExpr(context); |
| 28 |
8 |
String attrAccessExpr = sourceExpr+"."+StringUtils.attrAccess(attrDecl); |
| 29 |
8 |
MappingFunction function = context.functionFor(attrDecl.getType()); |
| 30 |
8 |
String converted = function.appliedTo(attrAccessExpr); |
| 31 |
8 |
code.addStmt("_result."+context.variableNameFor(attrDecl)+" = "+converted); |
| 32 |
8 |
} else if (role == CodegenRole.MARSHAL) { |
| 33 |
8 |
StatementContainer code = context.getCurrentMethodCode(); |
| 34 |
8 |
String sourceExpr = MarshalBuilder.getCurrentSourceExpr(context); |
| 35 |
8 |
String propertyNameExpr = sourceExpr+"."+context.variableNameFor(attrDecl); |
| 36 |
8 |
MappingFunction function = context.functionFor(attrDecl.getType()); |
| 37 |
8 |
String converted = function.appliedTo(propertyNameExpr); |
| 38 |
8 |
code.addStmt("_result."+StringUtils.attrAccess(attrDecl)+" = "+converted); |
| 39 |
8 |
} else { |
| 40 |
0 |
super.generateCode(context, component); |
| 41 |
|
} |
| 42 |
16 |
} |
| 43 |
|
|
| 44 |
|
public String variableNameFor(CodegenContext context, |
| 45 |
|
XSDConcreteComponent component) |
| 46 |
|
{ |
| 47 |
|
|
| 48 |
31 |
XSDAttributeDeclaration attrDecl = (XSDAttributeDeclaration)component; |
| 49 |
31 |
return context.toVariableName(attrDecl.getName()); |
| 50 |
|
} |
| 51 |
|
} |