| 1 |
|
package uk.co.badgersinfoil.asxsd.components; |
| 2 |
|
|
| 3 |
|
import org.eclipse.xsd.XSDConcreteComponent; |
| 4 |
|
import org.eclipse.xsd.XSDElementDeclaration; |
| 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.TypeDescriptor; |
| 10 |
|
import uk.co.badgersinfoil.asxsd.UnmarshalBuilder; |
| 11 |
|
import uk.co.badgersinfoil.metaas.dom.ASExpression; |
| 12 |
|
import uk.co.badgersinfoil.metaas.dom.StatementContainer; |
| 13 |
|
|
| 14 |
15 |
public class ElementDeclarationReferenceComponent extends AbstractMappingComponent { |
| 15 |
|
|
| 16 |
|
public void generateCode(CodegenContext context, |
| 17 |
|
XSDConcreteComponent component) |
| 18 |
|
{ |
| 19 |
11 |
XSDElementDeclaration resolvedElementDecl = resolve(component); |
| 20 |
11 |
CodegenRole role = context.getCurrentRole(); |
| 21 |
11 |
if (role == CodegenRole.UNMARSHAL) { |
| 22 |
1 |
StatementContainer block = context.getCurrentMethodCode(); |
| 23 |
1 |
String accessExpr = UnmarshalBuilder.getCurrentSourceExpr(context); |
| 24 |
1 |
MappingFunction conv = context.functionFor(resolvedElementDecl); |
| 25 |
1 |
block.addStmt("return "+conv.appliedTo(accessExpr)); |
| 26 |
1 |
} else if (role == CodegenRole.MARSHAL) { |
| 27 |
5 |
StatementContainer block = context.getCurrentMethodCode(); |
| 28 |
5 |
String accessExpr = MarshalBuilder.getCurrentSourceExpr(context); |
| 29 |
5 |
String targetParentExpr = MarshalBuilder.getCurrentTargetParentElementExpr(context); |
| 30 |
5 |
MappingFunction conv = context.functionFor(resolvedElementDecl); |
| 31 |
5 |
String converted = conv.appliedTo(accessExpr); |
| 32 |
5 |
block.addStmt(targetParentExpr+".appendChild("+converted+")"); |
| 33 |
5 |
} else if (role == CodegenRole.TYPE) { |
| 34 |
5 |
context.generateCode(resolve(component)); |
| 35 |
|
} else { |
| 36 |
0 |
throw new RuntimeException(getClass().getName()+" does not suppport operation generateCodeFor(role="+role+")"); |
| 37 |
|
} |
| 38 |
11 |
} |
| 39 |
|
|
| 40 |
|
public TypeDescriptor typeDescriptorFor(CodegenContext context, |
| 41 |
|
XSDConcreteComponent component) |
| 42 |
|
{ |
| 43 |
0 |
return context.typeDescriptorFor(resolve(component)); |
| 44 |
|
} |
| 45 |
|
|
| 46 |
|
public MappingFunction functionFor(CodegenContext context, XSDConcreteComponent component) { |
| 47 |
4 |
return context.functionFor(resolve(component)); |
| 48 |
|
} |
| 49 |
|
|
| 50 |
|
public String variableNameFor(CodegenContext context, |
| 51 |
|
XSDConcreteComponent component) |
| 52 |
|
{ |
| 53 |
9 |
return context.variableNameFor(resolve(component)); |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
public ASExpression createExpression(CodegenContext context, |
| 57 |
|
XSDConcreteComponent component) |
| 58 |
|
{ |
| 59 |
2 |
return context.createExpression(resolve(component)); |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
private static XSDElementDeclaration resolve(XSDConcreteComponent elementDeclaration) { |
| 63 |
31 |
return ((XSDElementDeclaration)elementDeclaration).getResolvedElementDeclaration(); |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
public boolean willAccept(XSDConcreteComponent component) { |
| 67 |
2339 |
return component instanceof XSDElementDeclaration |
| 68 |
|
&& ((XSDElementDeclaration)component).isElementDeclarationReference(); |
| 69 |
|
} |
| 70 |
|
} |