| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
package uk.co.badgersinfoil.asxsd; |
| 6 |
|
|
| 7 |
|
import java.util.HashMap; |
| 8 |
|
import java.util.Iterator; |
| 9 |
|
import java.util.List; |
| 10 |
|
import java.util.Map; |
| 11 |
|
import org.eclipse.xsd.XSDComplexTypeDefinition; |
| 12 |
|
import org.eclipse.xsd.XSDElementDeclaration; |
| 13 |
|
import org.eclipse.xsd.XSDNamedComponent; |
| 14 |
|
import org.eclipse.xsd.XSDSchema; |
| 15 |
|
import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 16 |
|
import org.eclipse.xsd.XSDTypeDefinition; |
| 17 |
|
import uk.co.badgersinfoil.metaas.ActionScriptProject; |
| 18 |
|
import uk.co.badgersinfoil.metaas.ActionScriptFactory; |
| 19 |
|
import uk.co.badgersinfoil.metaas.dom.ASClassType; |
| 20 |
|
import uk.co.badgersinfoil.metaas.dom.ASCompilationUnit; |
| 21 |
|
import uk.co.badgersinfoil.metaas.dom.ASConstants; |
| 22 |
|
import uk.co.badgersinfoil.metaas.dom.ASField; |
| 23 |
|
import uk.co.badgersinfoil.metaas.dom.ASMethod; |
| 24 |
|
import uk.co.badgersinfoil.metaas.dom.Visibility; |
| 25 |
|
|
| 26 |
|
|
| 27 |
|
public class MarshalBuilder { |
| 28 |
|
|
| 29 |
|
static final String KEY_MARSHAL_CURRENT_METHOD_CODE = "uk.co.badgersinfoil.asxsd.marshal.currentMethod.code"; |
| 30 |
|
static final String KEY_MARSHAL_CURRENT_SOURCE_EXPR = "uk.co.badgersinfoil.asxsd.marshal.currentSource.expr"; |
| 31 |
|
static final String KEY_MARSHAL_CURRENT_TARGET_PARENTELEMENT_EXPR = "uk.co.badgersinfoil.asxsd.marshal.currentTargetParentElement.expr"; |
| 32 |
|
|
| 33 |
15 |
private Map converterClassesByNamespace = new HashMap(); |
| 34 |
|
private ActionScriptProject project; |
| 35 |
|
|
| 36 |
|
private CodegenContext context; |
| 37 |
|
|
| 38 |
15 |
public MarshalBuilder(ActionScriptProject project, CodegenContext context) { |
| 39 |
15 |
this.project = project; |
| 40 |
15 |
this.context = context; |
| 41 |
15 |
} |
| 42 |
|
|
| 43 |
|
public void processSchema(XSDSchema schema) { |
| 44 |
15 |
List types = schema.getTypeDefinitions(); |
| 45 |
15 |
context.pushAttrScope(); |
| 46 |
15 |
context.setCurrentRole(CodegenRole.MARSHAL); |
| 47 |
15 |
for (Iterator i = types.iterator(); i.hasNext(); ) { |
| 48 |
19 |
XSDTypeDefinition typeDef = (XSDTypeDefinition)i.next(); |
| 49 |
19 |
if (typeDef instanceof XSDComplexTypeDefinition) { |
| 50 |
17 |
XSDComplexTypeDefinition ctype = (XSDComplexTypeDefinition)typeDef; |
| 51 |
|
|
| 52 |
17 |
context.functionFor(ctype); |
| 53 |
|
} |
| 54 |
19 |
} |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
15 |
context.popAttrScope(); |
| 61 |
15 |
} |
| 62 |
|
|
| 63 |
|
public void buildMethodParams(ASMethod meth, XSDElementDeclaration elementDecl) { |
| 64 |
6 |
TypeDescriptor typeDesc = context.typeDescriptorFor(elementDecl); |
| 65 |
6 |
if (elementDecl.getType() instanceof XSDComplexTypeDefinition) { |
| 66 |
4 |
meth.addParam("thisObject", typeDesc.getTypeName()); |
| 67 |
|
} else { |
| 68 |
2 |
meth.addParam("thisValue", typeDesc.getTypeName()); |
| 69 |
|
} |
| 70 |
6 |
} |
| 71 |
|
|
| 72 |
|
public void buildMethodParams(ASMethod meth, XSDTypeDefinition typeDef) { |
| 73 |
73 |
TypeDescriptor typeDesc = context.typeDescriptorFor(typeDef); |
| 74 |
73 |
if (typeDef instanceof XSDComplexTypeDefinition) { |
| 75 |
16 |
meth.addParam("_result", "XML"); |
| 76 |
16 |
meth.addParam("thisObject", typeDesc.getTypeName()); |
| 77 |
|
} else { |
| 78 |
57 |
meth.addParam("thisValue", typeDesc.getTypeName()); |
| 79 |
|
} |
| 80 |
73 |
} |
| 81 |
|
|
| 82 |
|
public String methodNameFor(XSDTypeDefinition typeDef) { |
| 83 |
|
String stub; |
| 84 |
146 |
if (XSDUtils.isAnonymous(typeDef)) { |
| 85 |
2 |
XSDNamedComponent container = (XSDNamedComponent)typeDef.getContainer(); |
| 86 |
2 |
stub = container.getName(); |
| 87 |
2 |
} else { |
| 88 |
144 |
stub = typeDef.getName(); |
| 89 |
|
} |
| 90 |
146 |
return "marshal" + StringUtils.sanitize(stub); |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
public String methodNameFor(XSDElementDeclaration elementDecl) { |
| 94 |
12 |
String name = StringUtils.sanitize(elementDecl.getName()); |
| 95 |
12 |
return "marshal" + name; |
| 96 |
|
} |
| 97 |
|
|
| 98 |
|
public static String xmlReturnTypeFor(XSDTypeDefinition typeDef) { |
| 99 |
89 |
if (typeDef instanceof XSDComplexTypeDefinition) { |
| 100 |
32 |
return "XML"; |
| 101 |
|
} |
| 102 |
57 |
if (typeDef instanceof XSDSimpleTypeDefinition) { |
| 103 |
57 |
return ASConstants.TYPE_STRING; |
| 104 |
|
} |
| 105 |
0 |
throw new IllegalArgumentException(typeDef.getClass().getName()); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
public ASCompilationUnit getClassForNamespace(String namespace) { |
| 109 |
160 |
ASCompilationUnit result = (ASCompilationUnit)converterClassesByNamespace.get(namespace); |
| 110 |
160 |
if (result == null) { |
| 111 |
25 |
result = createClassForNamespace(namespace); |
| 112 |
25 |
converterClassesByNamespace.put(namespace, result); |
| 113 |
|
} |
| 114 |
160 |
return result; |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
private ASCompilationUnit createClassForNamespace(String namespace) { |
| 118 |
25 |
ASCompilationUnit cu = project.newClass((StringUtils.toPackageName(namespace)+".Marshaler")); |
| 119 |
25 |
ASClassType clazz = (ASClassType)cu.getType(); |
| 120 |
25 |
clazz.setDescription("Automatically generated functions for turning Objects into XML"); |
| 121 |
25 |
ASField ns = clazz.newField("NAMESPACE", Visibility.PUBLIC, "Namespace"); |
| 122 |
25 |
ns.setConst(true); |
| 123 |
25 |
ns.setStatic(true); |
| 124 |
25 |
ns.setInitializer("new Namespace("+ActionScriptFactory.str(namespace)+")"); |
| 125 |
25 |
return cu; |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
public static String getCurrentSourceExpr(CodegenContext context) { |
| 129 |
182 |
String result = (String)context.getAttribute(KEY_MARSHAL_CURRENT_SOURCE_EXPR); |
| 130 |
182 |
if (result == null) { |
| 131 |
0 |
throw new IllegalStateException("no source expression defined"); |
| 132 |
|
} |
| 133 |
182 |
return result; |
| 134 |
|
} |
| 135 |
|
public static void setCurrentSourceExpr(CodegenContext context, String expr) { |
| 136 |
153 |
context.setAttribute(KEY_MARSHAL_CURRENT_SOURCE_EXPR, expr); |
| 137 |
153 |
} |
| 138 |
|
|
| 139 |
|
public static String getCurrentTargetParentElementExpr(CodegenContext context) { |
| 140 |
71 |
return (String)context.getAttribute(KEY_MARSHAL_CURRENT_TARGET_PARENTELEMENT_EXPR); |
| 141 |
|
} |
| 142 |
|
public static void setCurrentTargetParentElementExpr(CodegenContext context, String expr) { |
| 143 |
17 |
context.setAttribute(KEY_MARSHAL_CURRENT_TARGET_PARENTELEMENT_EXPR, expr); |
| 144 |
17 |
} |
| 145 |
|
|
| 146 |
|
public static void warn(String msg) { |
| 147 |
0 |
System.err.println(msg); |
| 148 |
0 |
} |
| 149 |
|
} |