| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
package uk.co.badgersinfoil.asxsd.components; |
| 6 |
|
|
| 7 |
|
import org.eclipse.xsd.XSDElementDeclaration; |
| 8 |
|
import org.eclipse.xsd.XSDParticle; |
| 9 |
|
import org.eclipse.xsd.XSDParticleContent; |
| 10 |
|
import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 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.TypeDescriptor; |
| 16 |
|
import uk.co.badgersinfoil.asxsd.UnmarshalBuilder; |
| 17 |
|
import uk.co.badgersinfoil.metaas.dom.StatementContainer; |
| 18 |
|
|
| 19 |
|
|
| 20 |
16 |
public class SinglyOccuringParticleComponent extends AbstractParticleComponent { |
| 21 |
|
|
| 22 |
|
protected TypeDescriptor typeDescriptorFor(CodegenContext context, XSDParticle particle) { |
| 23 |
|
|
| 24 |
1 |
XSDParticleContent partContent = particle.getContent(); |
| 25 |
1 |
return context.typeDescriptorFor(partContent); |
| 26 |
|
} |
| 27 |
|
|
| 28 |
|
protected boolean willAcceptParticle(XSDParticle particle) { |
| 29 |
261 |
return particle.getMaxOccurs() == 1 |
| 30 |
|
&& particle.getMinOccurs() == 1 |
| 31 |
|
&& particle.getContent() instanceof XSDElementDeclaration; |
| 32 |
|
} |
| 33 |
|
|
| 34 |
|
protected void generateCodeFor(CodegenContext context, |
| 35 |
|
XSDParticle particle) |
| 36 |
|
{ |
| 37 |
192 |
CodegenRole role = context.getCurrentRole(); |
| 38 |
192 |
if (role == CodegenRole.UNMARSHAL) { |
| 39 |
64 |
XSDElementDeclaration elementDecl = (XSDElementDeclaration)particle.getContent(); |
| 40 |
64 |
StatementContainer block = context.getCurrentMethodCode(); |
| 41 |
64 |
String fieldAccess = "_result." + context.variableNameFor(elementDecl); |
| 42 |
64 |
String accessExpr = UnmarshalBuilder.getCurrentSourceExpr(context); |
| 43 |
64 |
if (elementDecl.getResolvedElementDeclaration().getTypeDefinition() instanceof XSDSimpleTypeDefinition) { |
| 44 |
60 |
accessExpr += ".text()"; |
| 45 |
|
} |
| 46 |
64 |
MappingFunction unmarshaler = context.functionFor(particle.getContent()); |
| 47 |
64 |
block.addStmt(fieldAccess + " = " + unmarshaler.appliedTo(accessExpr)); |
| 48 |
64 |
} else if (role == CodegenRole.MARSHAL) { |
| 49 |
64 |
XSDElementDeclaration elementDecl = (XSDElementDeclaration)particle.getContent(); |
| 50 |
64 |
String propertyName = context.variableNameFor(elementDecl); |
| 51 |
64 |
String sourceExpr = MarshalBuilder.getCurrentSourceExpr(context); |
| 52 |
64 |
String accessExpr = sourceExpr+"."+propertyName; |
| 53 |
64 |
context.pushAttrScope(); |
| 54 |
64 |
MarshalBuilder.setCurrentSourceExpr(context, accessExpr); |
| 55 |
64 |
context.generateCode(elementDecl); |
| 56 |
64 |
context.popAttrScope(); |
| 57 |
64 |
} else if (role == CodegenRole.TYPE) { |
| 58 |
64 |
context.generateCode(particle.getContent()); |
| 59 |
|
} else { |
| 60 |
0 |
throw new RuntimeException(getClass().getName()+" does not suppport operation generateCodeFor(role="+role+")"); |
| 61 |
|
} |
| 62 |
192 |
} |
| 63 |
|
} |