| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
package uk.co.badgersinfoil.asxsd.components; |
| 6 |
|
|
| 7 |
|
import java.util.Iterator; |
| 8 |
|
import java.util.List; |
| 9 |
|
import org.eclipse.xsd.XSDCompositor; |
| 10 |
|
import org.eclipse.xsd.XSDConcreteComponent; |
| 11 |
|
import org.eclipse.xsd.XSDModelGroup; |
| 12 |
|
import org.eclipse.xsd.XSDParticle; |
| 13 |
|
import uk.co.badgersinfoil.asxsd.CodegenContext; |
| 14 |
|
import uk.co.badgersinfoil.asxsd.CodegenRole; |
| 15 |
|
import uk.co.badgersinfoil.asxsd.XSDUtils; |
| 16 |
|
import uk.co.badgersinfoil.metaas.ActionScriptFactory; |
| 17 |
|
import uk.co.badgersinfoil.metaas.dom.ASExpression; |
| 18 |
|
import uk.co.badgersinfoil.metaas.dom.StatementContainer; |
| 19 |
|
|
| 20 |
15 |
public class SequenceMappingComponent extends AbstractMappingComponent { |
| 21 |
|
|
| 22 |
|
public void generateCode(CodegenContext context, |
| 23 |
|
XSDConcreteComponent component) |
| 24 |
|
{ |
| 25 |
42 |
CodegenRole role = context.getCurrentRole(); |
| 26 |
42 |
if (role == CodegenRole.UNMARSHAL) { |
| 27 |
14 |
XSDModelGroup modelGroup = (XSDModelGroup)component; |
| 28 |
14 |
List particles = modelGroup.getParticles(); |
| 29 |
14 |
StatementContainer code = context.getCurrentMethodCode(); |
| 30 |
14 |
code.addComment(" process sequence,"); |
| 31 |
14 |
for (Iterator i=particles.iterator(); i.hasNext(); ) { |
| 32 |
66 |
XSDParticle part = (XSDParticle)i.next(); |
| 33 |
66 |
context.generateCode(part); |
| 34 |
66 |
} |
| 35 |
14 |
} else if (role == CodegenRole.MARSHAL) { |
| 36 |
14 |
XSDModelGroup modelGroup = (XSDModelGroup)component; |
| 37 |
14 |
List particles = modelGroup.getParticles(); |
| 38 |
14 |
StatementContainer code = context.getCurrentMethodCode(); |
| 39 |
14 |
code.addComment(" process sequence,"); |
| 40 |
14 |
for (Iterator i=particles.iterator(); i.hasNext(); ) { |
| 41 |
66 |
XSDParticle part = (XSDParticle)i.next(); |
| 42 |
66 |
context.generateCode(part); |
| 43 |
66 |
} |
| 44 |
14 |
} else if (role == CodegenRole.TYPE) { |
| 45 |
14 |
XSDModelGroup modelGroup = (XSDModelGroup)component; |
| 46 |
14 |
List particles = modelGroup.getParticles(); |
| 47 |
14 |
for (Iterator i=particles.iterator(); i.hasNext(); ) { |
| 48 |
66 |
XSDParticle part = (XSDParticle)i.next(); |
| 49 |
66 |
context.generateCode(part); |
| 50 |
66 |
} |
| 51 |
14 |
} else { |
| 52 |
0 |
throw new IllegalArgumentException(getClass().getName() + " does not support role "+role); |
| 53 |
|
} |
| 54 |
42 |
} |
| 55 |
|
|
| 56 |
|
public ASExpression createExpression(CodegenContext context, |
| 57 |
|
XSDConcreteComponent component) |
| 58 |
|
{ |
| 59 |
2 |
CodegenRole role = context.getCurrentRole(); |
| 60 |
2 |
XSDModelGroup modelGroup = (XSDModelGroup)component; |
| 61 |
2 |
ActionScriptFactory fact = context.getFactory(); |
| 62 |
2 |
List particles = modelGroup.getParticles(); |
| 63 |
2 |
if (role == CodegenRole.UNMARSHAL_DETECT) { |
| 64 |
1 |
if (particles.isEmpty()) { |
| 65 |
0 |
return fact.newExpression("false"); |
| 66 |
|
} |
| 67 |
1 |
Iterator i=particles.iterator(); |
| 68 |
1 |
ASExpression detect = context.createExpression((XSDParticle)i.next()); |
| 69 |
1 |
while (i.hasNext()) { |
| 70 |
1 |
XSDParticle part = (XSDParticle)i.next(); |
| 71 |
1 |
ASExpression rhs = context.createExpression(part); |
| 72 |
1 |
detect = fact.newOrExpression(detect, rhs); |
| 73 |
1 |
if (XSDUtils.isRequiredToAppear(part)) { |
| 74 |
|
|
| 75 |
|
|
| 76 |
1 |
break; |
| 77 |
|
} |
| 78 |
0 |
} |
| 79 |
1 |
return detect; |
| 80 |
|
} |
| 81 |
1 |
if (role == CodegenRole.MARSHAL_DETECT) { |
| 82 |
1 |
if (particles.isEmpty()) { |
| 83 |
0 |
return fact.newExpression("false"); |
| 84 |
|
} |
| 85 |
1 |
Iterator i = particles.iterator(); |
| 86 |
1 |
ASExpression detect = context.createExpression((XSDParticle)i.next()); |
| 87 |
1 |
while (i.hasNext()) { |
| 88 |
1 |
XSDParticle part = (XSDParticle)i.next(); |
| 89 |
1 |
ASExpression rhs = context.createExpression(part); |
| 90 |
1 |
detect = fact.newOrExpression(detect, rhs); |
| 91 |
1 |
if (XSDUtils.isRequiredToAppear(part)) { |
| 92 |
|
|
| 93 |
|
|
| 94 |
1 |
break; |
| 95 |
|
} |
| 96 |
0 |
} |
| 97 |
1 |
return detect; |
| 98 |
|
} |
| 99 |
0 |
return super.createExpression(context, component); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
public String createTypeNameFor(CodegenContext context, |
| 103 |
|
XSDConcreteComponent component) |
| 104 |
|
{ |
| 105 |
2 |
return context.createTypeNameFor(component.getContainer()) + "Sequence"; |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
public boolean willAccept(XSDConcreteComponent component) { |
| 109 |
1538 |
if (component instanceof XSDModelGroup) { |
| 110 |
46 |
XSDModelGroup modelGroup = (XSDModelGroup)component; |
| 111 |
46 |
return modelGroup.getCompositor() == XSDCompositor.SEQUENCE_LITERAL; |
| 112 |
|
} |
| 113 |
1492 |
return false; |
| 114 |
|
} |
| 115 |
|
} |