| 1 |
|
package uk.co.badgersinfoil.asxsd.components; |
| 2 |
|
|
| 3 |
|
import org.eclipse.xsd.XSDAttributeDeclaration; |
| 4 |
|
import org.eclipse.xsd.XSDAttributeUse; |
| 5 |
|
import org.eclipse.xsd.XSDConcreteComponent; |
| 6 |
|
import uk.co.badgersinfoil.asxsd.CodegenContext; |
| 7 |
|
import uk.co.badgersinfoil.asxsd.CodegenRole; |
| 8 |
|
import uk.co.badgersinfoil.asxsd.DocUtils; |
| 9 |
|
import uk.co.badgersinfoil.asxsd.MarshalBuilder; |
| 10 |
|
import uk.co.badgersinfoil.asxsd.StringUtils; |
| 11 |
|
import uk.co.badgersinfoil.asxsd.TypeBuilder; |
| 12 |
|
import uk.co.badgersinfoil.asxsd.TypeDescriptor; |
| 13 |
|
import uk.co.badgersinfoil.metaas.dom.ASClassType; |
| 14 |
|
import uk.co.badgersinfoil.metaas.dom.ASField; |
| 15 |
|
import uk.co.badgersinfoil.metaas.dom.ASIfStatement; |
| 16 |
|
import uk.co.badgersinfoil.metaas.dom.StatementContainer; |
| 17 |
|
import uk.co.badgersinfoil.metaas.dom.Visibility; |
| 18 |
|
|
| 19 |
15 |
public class AttributeUseComponent extends AbstractMappingComponent { |
| 20 |
|
|
| 21 |
|
public boolean willAccept(XSDConcreteComponent component) { |
| 22 |
132 |
return component instanceof XSDAttributeUse; |
| 23 |
|
} |
| 24 |
|
|
| 25 |
|
public void generateCode(CodegenContext context, |
| 26 |
|
XSDConcreteComponent component) |
| 27 |
|
{ |
| 28 |
24 |
XSDAttributeUse attrUse = (XSDAttributeUse)component; |
| 29 |
24 |
CodegenRole role = context.getCurrentRole(); |
| 30 |
24 |
if (role == CodegenRole.UNMARSHAL) { |
| 31 |
8 |
context.pushAttrScope(); |
| 32 |
8 |
if (!attrUse.isRequired()) { |
| 33 |
7 |
StatementContainer code = context.getCurrentMethodCode(); |
| 34 |
7 |
String attrAccessExpr = "thisElement."+StringUtils.attrAccess(attrUse.getAttributeDeclaration()); |
| 35 |
7 |
ASIfStatement ifStmt = code.newIf(attrAccessExpr+".length() > 0"); |
| 36 |
7 |
context.setCurrentMethodCode(ifStmt); |
| 37 |
|
} |
| 38 |
8 |
XSDAttributeDeclaration attrDecl = attrUse.getAttributeDeclaration(); |
| 39 |
8 |
context.generateCode(attrDecl); |
| 40 |
8 |
context.popAttrScope(); |
| 41 |
8 |
} else if (role == CodegenRole.MARSHAL) { |
| 42 |
8 |
context.pushAttrScope(); |
| 43 |
8 |
if (!attrUse.isRequired()) { |
| 44 |
7 |
StatementContainer code = context.getCurrentMethodCode(); |
| 45 |
7 |
String sourceExpr = MarshalBuilder.getCurrentSourceExpr(context); |
| 46 |
7 |
String propertyNameExpr = sourceExpr+"."+context.variableNameFor(attrUse.getAttributeDeclaration()); |
| 47 |
7 |
ASIfStatement ifStmt = code.newIf(propertyNameExpr+" != null"); |
| 48 |
7 |
context.setCurrentMethodCode(ifStmt); |
| 49 |
|
} |
| 50 |
8 |
XSDAttributeDeclaration attrDecl = attrUse.getAttributeDeclaration(); |
| 51 |
8 |
context.generateCode(attrDecl); |
| 52 |
8 |
context.popAttrScope(); |
| 53 |
8 |
} else if (role == CodegenRole.TYPE) { |
| 54 |
8 |
XSDAttributeDeclaration attrDecl = attrUse.getAttributeDeclaration(); |
| 55 |
8 |
ASClassType clazz = TypeBuilder.getCurrentType(context); |
| 56 |
8 |
TypeDescriptor typeDesc = context.typeDescriptorFor(attrDecl.getTypeDefinition()); |
| 57 |
8 |
ASField field = clazz.newField(context.variableNameFor(attrDecl), Visibility.PUBLIC, |
| 58 |
|
typeDesc.getTypeName()); |
| 59 |
8 |
String doc = DocUtils.findDocumentation(attrDecl.getAnnotation()); |
| 60 |
8 |
if (doc == null) { |
| 61 |
8 |
if (typeDesc.isArray()) { |
| 62 |
1 |
field.setDescription(typeDesc.getDocumentation()); |
| 63 |
|
} |
| 64 |
|
} else { |
| 65 |
0 |
field.setDescription(doc); |
| 66 |
|
} |
| 67 |
8 |
} else { |
| 68 |
0 |
super.generateCode(context, component); |
| 69 |
|
} |
| 70 |
24 |
} |
| 71 |
|
} |