Skip to content

Commit e8c0526

Browse files
committed
Working on #67.
1 parent 354ccc2 commit e8c0526

23 files changed

+739
-333
lines changed

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

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/**
2+
* Jsonix is a JavaScript library which allows you to convert between XML
3+
* and JavaScript object structures.
4+
*
5+
* Copyright (c) 2010 - 2014, Alexey Valikov, Highsource.org
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without modification,
9+
* are permitted provided that the following conditions are met:
10+
*
11+
* * Redistributions of source code must retain the above copyright notice, this
12+
* list of conditions and the following disclaimer.
13+
*
14+
* * Redistributions in binary form must reproduce the above copyright notice, this
15+
* list of conditions and the following disclaimer in the documentation and/or
16+
* other materials provided with the distribution.
17+
*
18+
* * Neither the name of the copyright holder nor the names of its
19+
* contributors may be used to endorse or promote products derived from
20+
* this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
26+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
*/
33+
34+
package org.hisrc.jsonix.compilation.mapping;
35+
36+
import java.util.HashMap;
37+
import java.util.LinkedList;
38+
import java.util.List;
39+
import java.util.Map;
40+
41+
import javax.xml.namespace.QName;
42+
43+
import org.apache.commons.lang3.Validate;
44+
import org.hisrc.jsonix.compilation.typeinfo.BuiltinLeafInfoCompiler;
45+
import org.hisrc.jsonix.compilation.typeinfo.EnumLeafInfoCompiler;
46+
import org.hisrc.jsonix.compilation.typeinfo.ListCompiler;
47+
import org.hisrc.jsonix.compilation.typeinfo.PackagedTypeInfoCompiler;
48+
import org.hisrc.jsonix.compilation.typeinfo.TypeInfoCompiler;
49+
import org.hisrc.jsonix.compilation.typeinfo.builtin.Base64BinaryTypeInfoCompiler;
50+
import org.hisrc.jsonix.compilation.typeinfo.builtin.BooleanTypeInfoCompiler;
51+
import org.hisrc.jsonix.compilation.typeinfo.builtin.DecimalTypeInfoCompiler;
52+
import org.hisrc.jsonix.compilation.typeinfo.builtin.DurationTypeInfoCompiler;
53+
import org.hisrc.jsonix.compilation.typeinfo.builtin.HexBinaryTypeInfoCompiler;
54+
import org.hisrc.jsonix.compilation.typeinfo.builtin.IntegerTypeInfoCompiler;
55+
import org.hisrc.jsonix.compilation.typeinfo.builtin.QNameTypeInfoCompiler;
56+
import org.hisrc.jsonix.compilation.typeinfo.builtin.StringTypeInfoCompiler;
57+
import org.hisrc.jsonix.compilation.typeinfo.builtin.XMLGregorianCalendarTypeInfoCompiler;
58+
import org.hisrc.jsonix.xml.xsom.CollectEnumerationValuesVisitor;
59+
import org.hisrc.jsonix.xml.xsom.CollectSimpleTypeNamesVisitor;
60+
import org.hisrc.xml.xsom.SchemaComponentAware;
61+
import org.jvnet.jaxb2_commons.xml.bind.model.MBuiltinLeafInfo;
62+
import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo;
63+
import org.jvnet.jaxb2_commons.xml.bind.model.MClassRef;
64+
import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo;
65+
import org.jvnet.jaxb2_commons.xml.bind.model.MID;
66+
import org.jvnet.jaxb2_commons.xml.bind.model.MIDREF;
67+
import org.jvnet.jaxb2_commons.xml.bind.model.MIDREFS;
68+
import org.jvnet.jaxb2_commons.xml.bind.model.MList;
69+
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfoVisitor;
70+
import org.jvnet.jaxb2_commons.xml.bind.model.MWildcardTypeInfo;
71+
import org.jvnet.jaxb2_commons.xml.bind.model.origin.MOriginated;
72+
import org.jvnet.jaxb2_commons.xmlschema.XmlSchemaConstants;
73+
74+
import com.sun.xml.xsom.XSComponent;
75+
import com.sun.xml.xsom.XmlString;
76+
77+
public class CreateTypeInfoCompilerTypeInfoVisitor<T, C extends T, O>
78+
implements MTypeInfoVisitor<T, C, TypeInfoCompiler<T, C>> {
79+
80+
private static final String IDREFS_TYPE_INFO_NAME = "IDREFS";
81+
private static final String IDREF_TYPE_INFO_NAME = "IDREF";
82+
private static final String ID_TYPE_INFO_NAME = "ID";
83+
84+
private Map<QName, TypeInfoCompiler<T, C>> XSD_TYPE_MAPPING = new HashMap<QName, TypeInfoCompiler<T, C>>();
85+
{
86+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYTYPE, new BuiltinLeafInfoCompiler<T, C, O>("AnyType"));
87+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYSIMPLETYPE, new BuiltinLeafInfoCompiler<T, C, O>("AnySimpleType"));
88+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.STRING, new StringTypeInfoCompiler<T, C, O>("String"));
89+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NORMALIZEDSTRING,
90+
new StringTypeInfoCompiler<T, C, O>("NormalizedString"));
91+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.TOKEN, new StringTypeInfoCompiler<T, C, O>("Token"));
92+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.LANGUAGE, new StringTypeInfoCompiler<T, C, O>("Language"));
93+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NAME, new StringTypeInfoCompiler<T, C, O>("Name"));
94+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NCNAME, new StringTypeInfoCompiler<T, C, O>("NCName"));
95+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ID, new StringTypeInfoCompiler<T, C, O>("ID"));
96+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ID, new StringTypeInfoCompiler<T, C, O>("String"));
97+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREF, new StringTypeInfoCompiler<T, C, O>("IDREF"));
98+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREF, new StringTypeInfoCompiler<T, C, O>("String"));
99+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREFS, new StringTypeInfoCompiler<T, C, O>("IDREFS"));
100+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREFS, new StringTypeInfoCompiler<T, C, O>("Strings"));
101+
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.ENTITY, new
102+
// BuiltinLeafInfoCompiler<T, C, O>("Entity"));
103+
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.ENTITIES, new
104+
// BuiltinLeafInfoCompiler<T, C, O>("Entities"));
105+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NMTOKEN, new StringTypeInfoCompiler<T, C, O>("NMToken"));
106+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NMTOKENS, new StringTypeInfoCompiler<T, C, O>("NMTokens"));
107+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.BOOLEAN, new BooleanTypeInfoCompiler<T, C, O>());
108+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.BASE64BINARY, new Base64BinaryTypeInfoCompiler<T, C, O>());
109+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.HEXBINARY, new HexBinaryTypeInfoCompiler<T, C, O>());
110+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.FLOAT, new DecimalTypeInfoCompiler<T, C, O>("Float"));
111+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DECIMAL, new DecimalTypeInfoCompiler<T, C, O>("Decimal"));
112+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.INTEGER, new IntegerTypeInfoCompiler<T, C, O>("Integer"));
113+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NONPOSITIVEINTEGER,
114+
new IntegerTypeInfoCompiler<T, C, O>("NonPositiveInteger"));
115+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NEGATIVEINTEGER,
116+
new IntegerTypeInfoCompiler<T, C, O>("NegativeInteger"));
117+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.LONG, new IntegerTypeInfoCompiler<T, C, O>("Long"));
118+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.INT, new IntegerTypeInfoCompiler<T, C, O>("Int"));
119+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.SHORT, new IntegerTypeInfoCompiler<T, C, O>("Short"));
120+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.BYTE, new IntegerTypeInfoCompiler<T, C, O>("Byte"));
121+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.NONNEGATIVEINTEGER,
122+
new IntegerTypeInfoCompiler<T, C, O>("NonNegativeInteger"));
123+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDLONG, new IntegerTypeInfoCompiler<T, C, O>("UnsignedLong"));
124+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDINT, new IntegerTypeInfoCompiler<T, C, O>("UnsignedInt"));
125+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDSHORT, new IntegerTypeInfoCompiler<T, C, O>("UnsignedShort"));
126+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDBYTE, new IntegerTypeInfoCompiler<T, C, O>("UnsignedByte"));
127+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.POSITIVEINTEGER,
128+
new IntegerTypeInfoCompiler<T, C, O>("PositiveInteger"));
129+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DOUBLE, new DecimalTypeInfoCompiler<T, C, O>("Double"));
130+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYURI, new StringTypeInfoCompiler<T, C, O>("AnyURI"));
131+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYURI, new StringTypeInfoCompiler<T, C, O>("String"));
132+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.QNAME, new QNameTypeInfoCompiler<T, C, O>());
133+
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.NOTATION, new
134+
// BuiltinLeafInfoCompiler<T, C, O>("Notation"));
135+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DURATION, new DurationTypeInfoCompiler<T, C, O>());
136+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DATETIME,
137+
new XMLGregorianCalendarTypeInfoCompiler<T, C, O>("DateTime"));
138+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.TIME, new XMLGregorianCalendarTypeInfoCompiler<T, C, O>("Time"));
139+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.DATE, new XMLGregorianCalendarTypeInfoCompiler<T, C, O>("Date"));
140+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GYEARMONTH,
141+
new XMLGregorianCalendarTypeInfoCompiler<T, C, O>("GYearMonth"));
142+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GYEAR, new XMLGregorianCalendarTypeInfoCompiler<T, C, O>("GYear"));
143+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GMONTHDAY,
144+
new XMLGregorianCalendarTypeInfoCompiler<T, C, O>("GMonthDay"));
145+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GDAY, new XMLGregorianCalendarTypeInfoCompiler<T, C, O>("GDay"));
146+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.GMONTH, new XMLGregorianCalendarTypeInfoCompiler<T, C, O>("GMonth"));
147+
XSD_TYPE_MAPPING.put(XmlSchemaConstants.CALENDAR, new BuiltinLeafInfoCompiler<T, C, O>("Calendar"));
148+
// XSD_TYPE_MAPPING.put(XmlSchemaConstants.CALENDAR, new
149+
// BuiltinLeafInfoCompiler<T, C, O>("String"));
150+
}
151+
152+
private final MOriginated<O> originated;
153+
154+
public CreateTypeInfoCompilerTypeInfoVisitor(MOriginated<O> originated) {
155+
Validate.notNull(originated);
156+
this.originated = originated;
157+
}
158+
159+
public TypeInfoCompiler<T, C> visitEnumLeafInfo(MEnumLeafInfo<T, C> info) {
160+
return new EnumLeafInfoCompiler<T, C>(info, info.getBaseTypeInfo().acceptTypeInfoVisitor(this));
161+
}
162+
163+
public TypeInfoCompiler<T, C> visitClassInfo(MClassInfo<T, C> info) {
164+
return new PackagedTypeInfoCompiler<T, C>(info);
165+
}
166+
167+
@Override
168+
public TypeInfoCompiler<T, C> visitClassRef(MClassRef<T, C> info) {
169+
return new PackagedTypeInfoCompiler<T, C>(info);
170+
}
171+
172+
public TypeInfoCompiler<T, C> visitList(MList<T, C> info) {
173+
return new ListCompiler<T, C>(info, info.getItemTypeInfo().acceptTypeInfoVisitor(this));
174+
}
175+
176+
public TypeInfoCompiler<T, C> visitBuiltinLeafInfo(MBuiltinLeafInfo<T, C> info) {
177+
178+
final O origin = this.originated.getOrigin();
179+
180+
final List<QName> simpleTypeNames = new LinkedList<QName>();
181+
final List<XmlString> enumerationValues = new LinkedList<XmlString>();
182+
if (origin instanceof SchemaComponentAware) {
183+
final XSComponent component = ((SchemaComponentAware) origin).getSchemaComponent();
184+
if (component != null) {
185+
final CollectSimpleTypeNamesVisitor visitor = new CollectSimpleTypeNamesVisitor();
186+
component.visit(visitor);
187+
simpleTypeNames.addAll(visitor.getTypeNames());
188+
189+
final CollectEnumerationValuesVisitor collectEnumerationValuesVisitor = new CollectEnumerationValuesVisitor();
190+
component.visit(collectEnumerationValuesVisitor);
191+
enumerationValues.addAll(collectEnumerationValuesVisitor.getValues());
192+
if (!enumerationValues.isEmpty()) {
193+
System.out.println(enumerationValues);
194+
}
195+
}
196+
}
197+
198+
simpleTypeNames.add(info.getTypeName());
199+
200+
for (QName candidateName : simpleTypeNames) {
201+
final TypeInfoCompiler<T, C> typeInfoCompiler = XSD_TYPE_MAPPING.get(candidateName);
202+
if (typeInfoCompiler != null) {
203+
return typeInfoCompiler;
204+
}
205+
}
206+
return null;
207+
}
208+
209+
public TypeInfoCompiler<T, C> visitWildcardTypeInfo(MWildcardTypeInfo<T, C> info) {
210+
// TODO ????
211+
return null;
212+
}
213+
214+
@Override
215+
public TypeInfoCompiler<T, C> visitID(MID<T, C> info) {
216+
return new BuiltinLeafInfoCompiler<T, C, O>(ID_TYPE_INFO_NAME);
217+
}
218+
219+
@Override
220+
public TypeInfoCompiler<T, C> visitIDREF(MIDREF<T, C> info) {
221+
return new BuiltinLeafInfoCompiler<T, C, O>(IDREF_TYPE_INFO_NAME);
222+
}
223+
224+
@Override
225+
public TypeInfoCompiler<T, C> visitIDREFS(MIDREFS<T, C> info) {
226+
return new BuiltinLeafInfoCompiler<T, C, O>(IDREFS_TYPE_INFO_NAME);
227+
}
228+
}

0 commit comments

Comments
 (0)