Coverage Report - uk.co.badgersinfoil.asxsd.BaseCodegenContext
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseCodegenContext
97% 
100% 
1.333
 
 1  
 /*
 2  
  * Copyright (c) 2007 David Holroyd
 3  
  */
 4  
 
 5  
 package uk.co.badgersinfoil.asxsd;
 6  
 
 7  
 import java.util.HashMap;
 8  
 import java.util.Map;
 9  
 import java.util.Stack;
 10  
 import org.eclipse.xsd.XSDConcreteComponent;
 11  
 import uk.co.badgersinfoil.metaas.ActionScriptFactory;
 12  
 import uk.co.badgersinfoil.metaas.ActionScriptProject;
 13  
 import uk.co.badgersinfoil.metaas.dom.ASCompilationUnit;
 14  
 import uk.co.badgersinfoil.metaas.dom.ASExpression;
 15  
 import uk.co.badgersinfoil.metaas.dom.ASMethod;
 16  
 import uk.co.badgersinfoil.metaas.dom.StatementContainer;
 17  
 
 18  
 public class BaseCodegenContext implements CodegenContext {
 19  
         private MappingRegistry registry;
 20  
         private ActionScriptFactory factory;
 21  
         private ActionScriptProject project;
 22  
         private UnmarshalBuilder unmarshalBuilder;
 23  
         private MarshalBuilder marshalBuilder;
 24  20
         private VariableNameGenerator varNameGen = new VariableNameGenerator();
 25  20
         private Map attrs = new HashMap();
 26  
         private TypeBuilder typeBuilder;
 27  20
         private Stack scopeStack = new Stack();
 28  20
         private MappingFunctionStore functionStore = new MappingFunctionStore();
 29  20
         private Map typeStore = new HashMap();
 30  2
         private static final String KEY_ROLE = BaseCodegenContext.class.getName() + ".role";
 31  1
         private static final String KEY_CODE = BaseCodegenContext.class.getName() + ".code";
 32  
 
 33  20
         public BaseCodegenContext(MappingRegistry registry, ActionScriptFactory factory, ActionScriptProject project) {
 34  20
                 this.registry = registry;
 35  20
                 this.factory = factory;
 36  20
                 this.project = project;
 37  20
         }
 38  
 
 39  
         public ActionScriptProject getProject() {
 40  20
                 return project;
 41  
         }
 42  
 
 43  
         public ActionScriptFactory getFactory() {
 44  41
                 return factory;
 45  
         }
 46  
 
 47  
         public MappingRegistry getRegistry() {
 48  0
                 return registry;
 49  
         }
 50  
         
 51  
         public TypeDescriptor typeDescriptorFor(XSDConcreteComponent component) {
 52  622
                 MappingComponent mapComp = componentFor(component);
 53  622
                 return mapComp.typeDescriptorFor(this, component);
 54  
         }
 55  
 
 56  
         public TypeDescriptor createTypeDescriptor(XSDConcreteComponent component) {
 57  59
                 MappingComponent mapComp = componentFor(component);
 58  59
                 pushAttrScope();
 59  59
                 setCurrentRole(CodegenRole.TYPE);
 60  59
                 TypeDescriptor result = mapComp.createTypeDescriptor(this, component);
 61  59
                 popAttrScope();
 62  59
                 return result;
 63  
         }
 64  
         
 65  
         public String createTypeNameFor(XSDConcreteComponent component) {
 66  43
                 MappingComponent mapComp = componentFor(component);
 67  43
                 String name = mapComp.createTypeNameFor(this, component);
 68  
                 // TODO: ensure name is unique within this context
 69  43
                 return name;
 70  
         }
 71  
 
 72  
         public CodegenRole getCurrentRole() {
 73  1388
                 return (CodegenRole)getAttribute(KEY_ROLE);
 74  
         }
 75  
 
 76  
         public void setCurrentRole(CodegenRole role) {
 77  131
                 setAttribute(KEY_ROLE, role);
 78  131
         }
 79  
 
 80  
         public void generateCode(XSDConcreteComponent component) {
 81  700
                 MappingComponent mapComp = componentFor(component);
 82  700
                 mapComp.generateCode(this, component);
 83  700
         }
 84  
 
 85  
         public MappingFunction functionFor(XSDConcreteComponent component) {
 86  448
                 MappingComponent mapComp = componentFor(component);
 87  448
                 return mapComp.functionFor(this, component);
 88  
         }
 89  
 
 90  
         public MappingFunction getOrCreateFunction(XSDConcreteComponent component) {
 91  186
                 CodegenRole role = getCurrentRole();
 92  186
                 MappingFunction convert = functionStore.get(role, component);
 93  186
                 if (convert == null) {
 94  
 //                        pushAttrScope();
 95  161
                         convert = createFunctionRef(component);
 96  161
                         functionStore.put(role, component, convert);
 97  
                         // now that we've got a ref to the method in the map,
 98  
                         // its safe to generate the method body without the
 99  
                         // potential for infinite recursion,
 100  161
                         ASMethod impl = createFunction(component);
 101  161
                         convert.setImplementation(impl);
 102  
 //                        popAttrScope();
 103  
                 }
 104  186
                 return convert;
 105  
         }
 106  
 
 107  
         public TypeDescriptor getOrCreateType(XSDConcreteComponent component) {
 108  308
                 TypeDescriptor typeDesc = (TypeDescriptor)typeStore.get(component);
 109  308
                 if (typeDesc == null) {
 110  57
                         pushAttrScope();
 111  57
                         typeDesc = createTypeDescriptor(component);
 112  57
                         typeStore.put(component, typeDesc);
 113  57
                         ASCompilationUnit unit = createType(component);
 114  57
                         typeDesc.setImplementation(unit);
 115  57
                         popAttrScope();
 116  
                 }
 117  308
                 return typeDesc;
 118  
         }
 119  
 
 120  
         private ASCompilationUnit createType(XSDConcreteComponent component) {
 121  57
                 MappingComponent mapComp = componentFor(component);
 122  57
                 return mapComp.createType(this, component);
 123  
         }
 124  
 
 125  
         public StatementContainer getCurrentMethodCode() {
 126  385
                 return (StatementContainer)getAttribute(KEY_CODE);
 127  
         }
 128  
         public void setCurrentMethodCode(StatementContainer code) {
 129  204
                 setAttribute(KEY_CODE, code);
 130  204
         }
 131  
 
 132  
         public MappingFunction createFunctionRef(XSDConcreteComponent component) {
 133  161
                 MappingComponent mapComp = componentFor(component);
 134  161
                 return mapComp.createFunctionRef(this, component);
 135  
         }
 136  
 
 137  
         public ASMethod createFunction(XSDConcreteComponent component) {
 138  161
                 MappingComponent mapComp = componentFor(component);
 139  161
                 return mapComp.createFunction(this, component);
 140  
         }
 141  
 
 142  
         public ASExpression createExpression(XSDConcreteComponent component) {
 143  44
                 MappingComponent mapComp = componentFor(component);
 144  44
                 return mapComp.createExpression(this, component);
 145  
         }
 146  
 
 147  
         private MappingComponent componentFor(XSDConcreteComponent component) {
 148  2632
                 if (component == null) {
 149  0
                         throw new IllegalArgumentException("component must not be null");
 150  
                 }
 151  2632
                 return registry.mappingComponentFor(component);
 152  
         }
 153  
 
 154  
         public void setUnmarshalBuilder(UnmarshalBuilder unmarshalBuilder) {
 155  15
                 this.unmarshalBuilder = unmarshalBuilder;
 156  15
         }
 157  
         public UnmarshalBuilder getUnmarshalBuilder() {
 158  232
                 return unmarshalBuilder;
 159  
         }
 160  
 
 161  
         public void setMarshalBuilder(MarshalBuilder marshalBuilder) {
 162  15
                 this.marshalBuilder = marshalBuilder;
 163  15
         }
 164  
         public MarshalBuilder getMarshalBuilder() {
 165  231
                 return marshalBuilder;
 166  
         }
 167  
 
 168  
         public void setTypeBuilder(TypeBuilder typeBuilder) {
 169  15
                 this.typeBuilder = typeBuilder;
 170  15
         }
 171  
         public TypeBuilder getTypeBuilder() {
 172  34
                 return typeBuilder;
 173  
         }
 174  
 
 175  
         public String variableNameFor(XSDConcreteComponent component) {
 176  337
                 MappingComponent mapComp = componentFor(component);
 177  337
                 return mapComp.variableNameFor(this, component);
 178  
         }
 179  
         
 180  
         public String toVariableName(String baseName) {
 181  319
                 return varNameGen.fieldName(baseName);
 182  
         }
 183  
 
 184  
         public void setAttribute(String key, Object value) {
 185  653
                 if (attrs.containsKey(key)) {
 186  1
                         throw new IllegalArgumentException(key+" is already mapped to "+attrs.get(key));
 187  
                 }
 188  652
                 attrs.put(key, value);
 189  652
         }
 190  
 
 191  
         public Object getAttribute(String key) {
 192  2291
                 Map scope = attrs;
 193  2291
                 int count = scopeStack.size();
 194  
                 while (true) {
 195  5098
                         if (scope.containsKey(key)) {
 196  2226
                                 return scope.get(key);
 197  
                         }
 198  2872
                         if (count == 0) {
 199  65
                                 return null;
 200  
                         }
 201  2807
                         scope = (Map)scopeStack.get(--count);
 202  
                 }
 203  
         }
 204  
 
 205  
         public void popAttrScope() {
 206  491
                 attrs = (Map)scopeStack.pop();
 207  490
         }
 208  
 
 209  
         public void pushAttrScope() {
 210  491
                 scopeStack.push(attrs);
 211  491
                 attrs = new HashMap();
 212  491
         }
 213  
 
 214  
         public MappingFunctionStore getMappingFunctionStore() {
 215  0
                 return functionStore;
 216  
         }
 217  
 }