Skip to content

Commit 8875d33

Browse files
committed
Working of #67.
1 parent 00de8db commit 8875d33

File tree

3 files changed

+185
-211
lines changed

3 files changed

+185
-211
lines changed

compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/CreateTypeInfoDeclarationVisitor.java

Lines changed: 77 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.apache.commons.lang3.Validate;
4444
import org.hisrc.jscm.codemodel.JSCodeModel;
4545
import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression;
46-
import org.hisrc.jscm.codemodel.expression.JSObjectLiteral;
4746
import org.hisrc.jsonix.definition.Modules;
4847
import org.hisrc.jsonix.naming.Naming;
4948
import org.hisrc.jsonix.xml.xsom.CollectSimpleTypeNamesVisitor;
@@ -56,147 +55,108 @@
5655
import org.jvnet.jaxb2_commons.xml.bind.model.MIDREF;
5756
import org.jvnet.jaxb2_commons.xml.bind.model.MIDREFS;
5857
import org.jvnet.jaxb2_commons.xml.bind.model.MList;
59-
import org.jvnet.jaxb2_commons.xml.bind.model.MPackagedTypeInfo;
6058
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfoVisitor;
6159
import org.jvnet.jaxb2_commons.xml.bind.model.MWildcardTypeInfo;
6260
import org.jvnet.jaxb2_commons.xml.bind.model.origin.MOriginated;
6361
import org.jvnet.jaxb2_commons.xmlschema.XmlSchemaConstants;
6462

63+
import com.sun.javadoc.Type;
6564
import com.sun.xml.xsom.XSComponent;
6665

67-
final class CreateTypeInfoDeclarationVisitor<T, C extends T, O> implements
68-
MTypeInfoVisitor<T, C, JSAssignmentExpression> {
66+
public class CreateTypeInfoDeclarationVisitor<T, C extends T, O>
67+
implements MTypeInfoVisitor<T, C, TypeInfoCompiler<T, C>> {
6968

7069
private static final String IDREFS_TYPE_INFO_NAME = "IDREFS";
7170
private static final String IDREF_TYPE_INFO_NAME = "IDREF";
7271
private static final String ID_TYPE_INFO_NAME = "ID";
7372

74-
private static Map<QName, String> XSD_TYPE_MAPPING = new HashMap<QName, String>();
73+
private Map<QName, TypeInfoCompiler<T, C>> XSD_TYPE_MAPPING = new HashMap<QName, TypeInfoCompiler<T, C>>();
7574
{
76-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYTYPE, "AnyType");
77-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYSIMPLETYPE, "AnySimpleType");
78-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.STRING, "String");
79-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NORMALIZEDSTRING,
80-
"NormalizedString");
81-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.TOKEN, "Token");
82-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.LANGUAGE, "Language");
83-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NAME, "Name");
84-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NCNAME, "NCName");
85-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ID, "ID");
86-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ID, "String");
87-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREF, "IDREF");
88-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREF, "String");
89-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREFS, "IDREFS");
90-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREFS, "Strings");
91-
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.ENTITY, "Entity");
92-
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.ENTITIES, "Entities");
93-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NMTOKEN, "NMToken");
94-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NMTOKENS, "NMTokens");
95-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.BOOLEAN, "Boolean");
96-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.BASE64BINARY, "Base64Binary");
97-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.HEXBINARY, "HexBinary");
98-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.FLOAT, "Float");
99-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DECIMAL, "Decimal");
100-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.INTEGER, "Integer");
101-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NONPOSITIVEINTEGER,
102-
"NonPositiveInteger");
103-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NEGATIVEINTEGER,
104-
"NegativeInteger");
105-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.LONG, "Long");
106-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.INT, "Int");
107-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.SHORT, "Short");
108-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.BYTE, "Byte");
109-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NONNEGATIVEINTEGER,
110-
"NonNegativeInteger");
111-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDLONG, "UnsignedLong");
112-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDINT, "UnsignedInt");
113-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDSHORT, "UnsignedShort");
114-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDBYTE, "UnsignedByte");
115-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.POSITIVEINTEGER,
116-
"PositiveInteger");
117-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DOUBLE, "Double");
118-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYURI, "AnyURI");
119-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYURI, "String");
120-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.QNAME, "QName");
121-
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.NOTATION, "Notation");
122-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DURATION, "Duration");
123-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DATETIME, "DateTime");
124-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.TIME, "Time");
125-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DATE, "Date");
126-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GYEARMONTH, "GYearMonth");
127-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GYEAR, "GYear");
128-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GMONTHDAY, "GMonthDay");
129-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GDAY, "GDay");
130-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GMONTH, "GMonth");
131-
XSD_TYPE_MAPPING.put(XmlSchemaConstants.CALENDAR, "Calendar");
132-
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.CALENDAR, "String");
75+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYTYPE, new BuiltinLeafInfoCompiler<T, C, O>("AnyType"));
76+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYSIMPLETYPE, new BuiltinLeafInfoCompiler<T, C, O>("AnySimpleType"));
77+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.STRING, new BuiltinLeafInfoCompiler<T, C, O>("String"));
78+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NORMALIZEDSTRING, new BuiltinLeafInfoCompiler<T, C, O>("NormalizedString"));
79+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.TOKEN, new BuiltinLeafInfoCompiler<T, C, O>("Token"));
80+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.LANGUAGE, new BuiltinLeafInfoCompiler<T, C, O>("Language"));
81+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NAME, new BuiltinLeafInfoCompiler<T, C, O>("Name"));
82+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NCNAME, new BuiltinLeafInfoCompiler<T, C, O>("NCName"));
83+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ID, new BuiltinLeafInfoCompiler<T, C, O>("ID"));
84+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ID, new BuiltinLeafInfoCompiler<T, C, O>("String"));
85+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREF, new BuiltinLeafInfoCompiler<T, C, O>("IDREF"));
86+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREF, new BuiltinLeafInfoCompiler<T, C, O>("String"));
87+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREFS, new BuiltinLeafInfoCompiler<T, C, O>("IDREFS"));
88+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREFS, new BuiltinLeafInfoCompiler<T, C, O>("Strings"));
89+
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.ENTITY, new BuiltinLeafInfoCompiler<T, C, O>("Entity"));
90+
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.ENTITIES, new BuiltinLeafInfoCompiler<T, C, O>("Entities"));
91+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NMTOKEN, new BuiltinLeafInfoCompiler<T, C, O>("NMToken"));
92+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NMTOKENS, new BuiltinLeafInfoCompiler<T, C, O>("NMTokens"));
93+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.BOOLEAN, new BuiltinLeafInfoCompiler<T, C, O>("Boolean"));
94+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.BASE64BINARY, new BuiltinLeafInfoCompiler<T, C, O>("Base64Binary"));
95+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.HEXBINARY, new BuiltinLeafInfoCompiler<T, C, O>("HexBinary"));
96+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.FLOAT, new BuiltinLeafInfoCompiler<T, C, O>("Float"));
97+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DECIMAL, new BuiltinLeafInfoCompiler<T, C, O>("Decimal"));
98+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.INTEGER, new BuiltinLeafInfoCompiler<T, C, O>("Integer"));
99+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NONPOSITIVEINTEGER, new BuiltinLeafInfoCompiler<T, C, O>("NonPositiveInteger"));
100+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NEGATIVEINTEGER, new BuiltinLeafInfoCompiler<T, C, O>("NegativeInteger"));
101+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.LONG, new BuiltinLeafInfoCompiler<T, C, O>("Long"));
102+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.INT, new BuiltinLeafInfoCompiler<T, C, O>("Int"));
103+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.SHORT, new BuiltinLeafInfoCompiler<T, C, O>("Short"));
104+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.BYTE, new BuiltinLeafInfoCompiler<T, C, O>("Byte"));
105+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NONNEGATIVEINTEGER, new BuiltinLeafInfoCompiler<T, C, O>("NonNegativeInteger"));
106+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDLONG, new BuiltinLeafInfoCompiler<T, C, O>("UnsignedLong"));
107+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDINT, new BuiltinLeafInfoCompiler<T, C, O>("UnsignedInt"));
108+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDSHORT, new BuiltinLeafInfoCompiler<T, C, O>("UnsignedShort"));
109+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDBYTE, new BuiltinLeafInfoCompiler<T, C, O>("UnsignedByte"));
110+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.POSITIVEINTEGER, new BuiltinLeafInfoCompiler<T, C, O>("PositiveInteger"));
111+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DOUBLE, new BuiltinLeafInfoCompiler<T, C, O>("Double"));
112+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYURI, new BuiltinLeafInfoCompiler<T, C, O>("AnyURI"));
113+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYURI, new BuiltinLeafInfoCompiler<T, C, O>("String"));
114+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.QNAME, new BuiltinLeafInfoCompiler<T, C, O>("QName"));
115+
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.NOTATION, new BuiltinLeafInfoCompiler<T, C, O>("Notation"));
116+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DURATION, new BuiltinLeafInfoCompiler<T, C, O>("Duration"));
117+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DATETIME, new BuiltinLeafInfoCompiler<T, C, O>("DateTime"));
118+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.TIME, new BuiltinLeafInfoCompiler<T, C, O>("Time"));
119+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DATE, new BuiltinLeafInfoCompiler<T, C, O>("Date"));
120+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GYEARMONTH, new BuiltinLeafInfoCompiler<T, C, O>("GYearMonth"));
121+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GYEAR, new BuiltinLeafInfoCompiler<T, C, O>("GYear"));
122+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GMONTHDAY, new BuiltinLeafInfoCompiler<T, C, O>("GMonthDay"));
123+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GDAY, new BuiltinLeafInfoCompiler<T, C, O>("GDay"));
124+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GMONTH, new BuiltinLeafInfoCompiler<T, C, O>("GMonth"));
125+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.CALENDAR, new BuiltinLeafInfoCompiler<T, C, O>("Calendar"));
126+
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.CALENDAR, new BuiltinLeafInfoCompiler<T, C, O>("String"));
133127
}
134128

135-
private final JSCodeModel codeModel;
136-
private final Modules<T, C> mappingNameResolver;
137-
private final Naming naming;
138-
private final String mappingName;
139129
private final MOriginated<O> originated;
140130

141-
CreateTypeInfoDeclarationVisitor(MappingCompiler<T, C> mappingCompiler,
142-
MOriginated<O> originated) {
143-
Validate.notNull(mappingCompiler);
131+
CreateTypeInfoDeclarationVisitor(MOriginated<O> originated) {
144132
Validate.notNull(originated);
145-
this.mappingNameResolver = mappingCompiler.getModules();
146-
this.codeModel = mappingCompiler.getCodeModel();
147-
this.naming = mappingCompiler.getNaming();
148-
this.mappingName = mappingCompiler.getMapping().getMappingName();
149133
this.originated = originated;
150134
}
151135

152-
private JSAssignmentExpression createTypeInfoDeclaration(
153-
MPackagedTypeInfo<T, C> info) {
154-
final String typeInfoMappingName = this.mappingNameResolver
155-
.getMappingName(info.getPackageInfo().getPackageName());
156-
final String spaceName = typeInfoMappingName.equals(this.mappingName) ? ""
157-
: typeInfoMappingName;
158-
final String typeInfoName = spaceName
159-
+ "."
160-
+ info.getContainerLocalName(MappingCompiler.DEFAULT_SCOPED_NAME_DELIMITER);
161-
return this.codeModel.string(typeInfoName);
162-
163-
}
164-
165-
public JSAssignmentExpression visitEnumLeafInfo(MEnumLeafInfo<T, C> info) {
166-
return createTypeInfoDeclaration(info);
136+
public TypeInfoCompiler<T, C> visitEnumLeafInfo(MEnumLeafInfo<T, C> info) {
137+
return new PackagedTypeInfoCompiler<T, C>(info);
167138
}
168139

169-
public JSAssignmentExpression visitClassInfo(MClassInfo<T, C> info) {
170-
return createTypeInfoDeclaration(info);
140+
public TypeInfoCompiler<T, C> visitClassInfo(MClassInfo<T, C> info) {
141+
return new PackagedTypeInfoCompiler<T, C>(info);
171142
}
172143

173144
@Override
174-
public JSAssignmentExpression visitClassRef(MClassRef<T, C> info) {
175-
return createTypeInfoDeclaration(info);
145+
public TypeInfoCompiler<T, C> visitClassRef(MClassRef<T, C> info) {
146+
return new PackagedTypeInfoCompiler<T, C>(info);
176147
}
177148

178-
public JSAssignmentExpression visitList(MList<T, C> info) {
179-
final JSObjectLiteral list = this.codeModel.object();
180-
list.append(naming.type(), this.codeModel.string(naming.list()));
181-
final JSAssignmentExpression typeInfoDeclaration = info
182-
.getItemTypeInfo().acceptTypeInfoVisitor(this);
183-
if (!typeInfoDeclaration
184-
.acceptExpressionVisitor(new CheckValueStringLiteralExpressionVisitor(
185-
"String"))) {
186-
list.append(naming.baseTypeInfo(), typeInfoDeclaration);
187-
}
188-
return list;
149+
public TypeInfoCompiler<T, C> visitList(MList<T, C> info) {
150+
return new ListCompiler<T, C>(info, info.getItemTypeInfo().acceptTypeInfoVisitor(this));
189151
}
190152

191-
public JSAssignmentExpression visitBuiltinLeafInfo(
192-
MBuiltinLeafInfo<T, C> info) {
153+
public TypeInfoCompiler<T, C> visitBuiltinLeafInfo(MBuiltinLeafInfo<T, C> info) {
193154

194155
final O origin = this.originated.getOrigin();
195156

196157
final List<QName> simpleTypeNames = new LinkedList<QName>();
197158
if (origin instanceof SchemaComponentAware) {
198-
final XSComponent component = ((SchemaComponentAware) origin)
199-
.getSchemaComponent();
159+
final XSComponent component = ((SchemaComponentAware) origin).getSchemaComponent();
200160
if (component != null) {
201161
final CollectSimpleTypeNamesVisitor visitor = new CollectSimpleTypeNamesVisitor();
202162
component.visit(visitor);
@@ -207,32 +167,31 @@ public JSAssignmentExpression visitBuiltinLeafInfo(
207167
simpleTypeNames.add(info.getTypeName());
208168

209169
for (QName candidateName : simpleTypeNames) {
210-
final String name = XSD_TYPE_MAPPING.get(candidateName);
211-
if (name != null) {
212-
return this.codeModel.string(name);
170+
final TypeInfoCompiler<T, C> typeInfoCompiler = XSD_TYPE_MAPPING.get(candidateName);
171+
if (typeInfoCompiler != null) {
172+
return typeInfoCompiler;
213173
}
214174
}
215175
return null;
216176
}
217177

218-
public JSAssignmentExpression visitWildcardTypeInfo(
219-
MWildcardTypeInfo<T, C> info) {
178+
public TypeInfoCompiler<T, C> visitWildcardTypeInfo(MWildcardTypeInfo<T, C> info) {
220179
// TODO ????
221180
return null;
222181
}
223182

224183
@Override
225-
public JSAssignmentExpression visitID(MID<T, C> info) {
226-
return this.codeModel.string(ID_TYPE_INFO_NAME);
184+
public TypeInfoCompiler<T, C> visitID(MID<T, C> info) {
185+
return new BuiltinLeafInfoCompiler<T, C, O>(ID_TYPE_INFO_NAME);
227186
}
228187

229188
@Override
230-
public JSAssignmentExpression visitIDREF(MIDREF<T, C> info) {
231-
return this.codeModel.string(IDREF_TYPE_INFO_NAME);
189+
public TypeInfoCompiler<T, C> visitIDREF(MIDREF<T, C> info) {
190+
return new BuiltinLeafInfoCompiler<T, C, O>(IDREF_TYPE_INFO_NAME);
232191
}
233192

234193
@Override
235-
public JSAssignmentExpression visitIDREFS(MIDREFS<T, C> info) {
236-
return this.codeModel.string(IDREFS_TYPE_INFO_NAME);
194+
public TypeInfoCompiler<T, C> visitIDREFS(MIDREFS<T, C> info) {
195+
return new BuiltinLeafInfoCompiler<T, C, O>(IDREFS_TYPE_INFO_NAME);
237196
}
238197
}

0 commit comments

Comments
 (0)