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 .jsonschema .typeinfo ;
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 .jsonschema .JsonixJsonSchemaConstants ;
45+ import org .hisrc .jsonix .compilation .jsonschema .XmlSchemaJsonSchemaConstants ;
46+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .builtin .Base64BinaryTypeInfoProducer ;
47+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .builtin .BooleanTypeInfoProducer ;
48+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .builtin .DecimalTypeInfoProducer ;
49+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .builtin .DurationTypeInfoProducer ;
50+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .builtin .HexBinaryTypeInfoProducer ;
51+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .builtin .IntegerTypeInfoProducer ;
52+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .builtin .NormalizedStringTypeInfoProducer ;
53+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .builtin .QNameTypeInfoProducer ;
54+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .builtin .StringTypeInfoProducer ;
55+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .builtin .XMLGregorianCalendarTypeInfoProducer ;
56+ import org .hisrc .jsonix .xml .xsom .CollectSimpleTypeNamesVisitor ;
57+ import org .hisrc .xml .xsom .SchemaComponentAware ;
58+ import org .jvnet .jaxb2_commons .xml .bind .model .MBuiltinLeafInfo ;
59+ import org .jvnet .jaxb2_commons .xml .bind .model .MClassInfo ;
60+ import org .jvnet .jaxb2_commons .xml .bind .model .MClassRef ;
61+ import org .jvnet .jaxb2_commons .xml .bind .model .MEnumLeafInfo ;
62+ import org .jvnet .jaxb2_commons .xml .bind .model .MID ;
63+ import org .jvnet .jaxb2_commons .xml .bind .model .MIDREF ;
64+ import org .jvnet .jaxb2_commons .xml .bind .model .MIDREFS ;
65+ import org .jvnet .jaxb2_commons .xml .bind .model .MList ;
66+ import org .jvnet .jaxb2_commons .xml .bind .model .MTypeInfoVisitor ;
67+ import org .jvnet .jaxb2_commons .xml .bind .model .MWildcardTypeInfo ;
68+ import org .jvnet .jaxb2_commons .xml .bind .model .origin .MOriginated ;
69+ import org .jvnet .jaxb2_commons .xmlschema .XmlSchemaConstants ;
70+
71+ import com .sun .xml .xsom .XSComponent ;
72+
73+ public class CreateTypeInfoProducer <T , C extends T , O > implements MTypeInfoVisitor <T , C , TypeInfoProducer <T , C >> {
74+
75+ private static final String IDREFS_TYPE_INFO_NAME = "IDREFS" ;
76+ private static final String IDREF_TYPE_INFO_NAME = "IDREF" ;
77+ private static final String ID_TYPE_INFO_NAME = "ID" ;
78+
79+ private Map <QName , TypeInfoProducer <T , C >> XSD_TYPE_MAPPING = new HashMap <QName , TypeInfoProducer <T , C >>();
80+ {
81+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .ANYTYPE ,
82+ new BuiltinLeafInfoProducer <T , C , O >(XmlSchemaConstants .ANYTYPE ));
83+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .ANYSIMPLETYPE ,
84+ new BuiltinLeafInfoProducer <T , C , O >(XmlSchemaConstants .ANYSIMPLETYPE ));
85+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .STRING , new StringTypeInfoProducer <T , C , O >(XmlSchemaConstants .STRING ));
86+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .NORMALIZEDSTRING ,
87+ new NormalizedStringTypeInfoProducer <T , C , O >(XmlSchemaConstants .NORMALIZEDSTRING ));
88+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .TOKEN ,
89+ new NormalizedStringTypeInfoProducer <T , C , O >(XmlSchemaConstants .TOKEN ));
90+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .LANGUAGE ,
91+ new NormalizedStringTypeInfoProducer <T , C , O >(XmlSchemaConstants .LANGUAGE ));
92+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .NAME ,
93+ new NormalizedStringTypeInfoProducer <T , C , O >(XmlSchemaConstants .NAME ));
94+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .NCNAME ,
95+ new NormalizedStringTypeInfoProducer <T , C , O >(XmlSchemaConstants .NCNAME ));
96+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .ID , new BuiltinLeafInfoProducer <T , C , O >(XmlSchemaConstants .ID ));
97+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .ID , new BuiltinLeafInfoProducer <T , C , O >(XmlSchemaConstants .ID ));
98+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .IDREF , new BuiltinLeafInfoProducer <T , C , O >(XmlSchemaConstants .IDREF ));
99+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .IDREF , new BuiltinLeafInfoProducer <T , C , O >(XmlSchemaConstants .IDREF ));
100+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .IDREFS ,
101+ new BuiltinLeafInfoProducer <T , C , O >(XmlSchemaConstants .IDREFS ));
102+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .IDREFS ,
103+ new BuiltinLeafInfoProducer <T , C , O >(XmlSchemaConstants .IDREFS ));
104+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .ENTITY , new
105+ NormalizedStringTypeInfoProducer <T , C , O >(XmlSchemaConstants .ENTITY ));
106+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .ENTITIES , new
107+ NormalizedStringTypeInfoProducer <T , C , O >(XmlSchemaConstants .ENTITIES ));
108+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .NMTOKEN ,
109+ new NormalizedStringTypeInfoProducer <T , C , O >( XmlSchemaConstants .NMTOKEN ));
110+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .NMTOKENS ,
111+ new NormalizedStringTypeInfoProducer <T , C , O >(XmlSchemaConstants .NMTOKENS ));
112+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .BOOLEAN , new BooleanTypeInfoProducer <T , C , O >());
113+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .BASE64BINARY , new Base64BinaryTypeInfoProducer <T , C , O >());
114+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .HEXBINARY , new HexBinaryTypeInfoProducer <T , C , O >());
115+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .FLOAT ,
116+ new DecimalTypeInfoProducer <T , C , O >( XmlSchemaConstants .FLOAT ));
117+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .DECIMAL ,
118+ new DecimalTypeInfoProducer <T , C , O >( XmlSchemaConstants .DECIMAL ));
119+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .INTEGER ,
120+ new IntegerTypeInfoProducer <T , C , O >(XmlSchemaConstants .INTEGER ));
121+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .NONPOSITIVEINTEGER ,
122+ new IntegerTypeInfoProducer <T , C , O >( XmlSchemaConstants .NONPOSITIVEINTEGER ));
123+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .NEGATIVEINTEGER ,
124+ new IntegerTypeInfoProducer <T , C , O >( XmlSchemaConstants .NEGATIVEINTEGER ));
125+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .LONG ,
126+ new IntegerTypeInfoProducer <T , C , O >( XmlSchemaConstants .LONG ));
127+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .INT ,
128+ new IntegerTypeInfoProducer <T , C , O >( XmlSchemaConstants .INT ));
129+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .SHORT ,
130+ new IntegerTypeInfoProducer <T , C , O >( XmlSchemaConstants .SHORT ));
131+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .BYTE ,
132+ new IntegerTypeInfoProducer <T , C , O >( XmlSchemaConstants .BYTE ));
133+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .NONNEGATIVEINTEGER ,
134+ new IntegerTypeInfoProducer <T , C , O >(XmlSchemaConstants .NONNEGATIVEINTEGER ));
135+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .UNSIGNEDLONG ,
136+ new IntegerTypeInfoProducer <T , C , O >( XmlSchemaConstants .UNSIGNEDLONG ));
137+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .UNSIGNEDINT ,
138+ new IntegerTypeInfoProducer <T , C , O >( XmlSchemaConstants .UNSIGNEDINT ));
139+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .UNSIGNEDSHORT ,
140+ new IntegerTypeInfoProducer <T , C , O >( XmlSchemaConstants .UNSIGNEDSHORT ));
141+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .UNSIGNEDBYTE ,
142+ new IntegerTypeInfoProducer <T , C , O >( XmlSchemaConstants .UNSIGNEDBYTE ));
143+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .POSITIVEINTEGER ,
144+ new IntegerTypeInfoProducer <T , C , O >( XmlSchemaConstants .POSITIVEINTEGER ));
145+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .DOUBLE ,
146+ new DecimalTypeInfoProducer <T , C , O >(XmlSchemaConstants .DOUBLE ));
147+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .ANYURI ,
148+ new NormalizedStringTypeInfoProducer <T , C , O >( XmlSchemaConstants .ANYURI ));
149+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .ANYURI ,
150+ new NormalizedStringTypeInfoProducer <T , C , O >(XmlSchemaConstants .ANYURI ));
151+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .QNAME , new QNameTypeInfoProducer <T , C , O >());
152+ // XSD_TYPE_MAPPING.put(XmlSchemaConstants.NOTATION, new
153+ // BuiltinLeafInfoProducer<T, C, O>("Notation"));
154+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .DURATION , new DurationTypeInfoProducer <T , C , O >());
155+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .DATETIME ,
156+ new XMLGregorianCalendarTypeInfoProducer <T , C , O >( XmlSchemaConstants .DATETIME ));
157+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .TIME ,
158+ new XMLGregorianCalendarTypeInfoProducer <T , C , O >(XmlSchemaConstants .TIME ));
159+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .DATE ,
160+ new XMLGregorianCalendarTypeInfoProducer <T , C , O >( XmlSchemaConstants .DATE ));
161+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .GYEARMONTH ,
162+ new XMLGregorianCalendarTypeInfoProducer <T , C , O >( XmlSchemaConstants .GYEARMONTH ));
163+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .GYEAR ,
164+ new XMLGregorianCalendarTypeInfoProducer <T , C , O >(XmlSchemaConstants .GYEAR ));
165+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .GMONTHDAY ,
166+ new XMLGregorianCalendarTypeInfoProducer <T , C , O >(XmlSchemaConstants .GMONTHDAY ));
167+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .GDAY ,
168+ new XMLGregorianCalendarTypeInfoProducer <T , C , O >(XmlSchemaConstants .GDAY ));
169+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .GMONTH ,
170+ new XMLGregorianCalendarTypeInfoProducer <T , C , O >(XmlSchemaConstants .GMONTH ));
171+ XSD_TYPE_MAPPING .put (XmlSchemaConstants .CALENDAR , new BuiltinLeafInfoProducer <T , C , O >(
172+ JsonixJsonSchemaConstants .JSONIX_JSON_SCHEMA_ID , XmlSchemaConstants .xsd ("calendar" )));
173+ // XSD_TYPE_MAPPING.put(XmlSchemaConstants.CALENDAR, new
174+ // BuiltinLeafInfoProducer<T, C, O>("String"));
175+ }
176+
177+ private final MOriginated <O > originated ;
178+
179+ public CreateTypeInfoProducer (MOriginated <O > originated ) {
180+ Validate .notNull (originated );
181+ this .originated = originated ;
182+ }
183+
184+ public TypeInfoProducer <T , C > visitEnumLeafInfo (MEnumLeafInfo <T , C > info ) {
185+ return new EnumLeafInfoProducer <T , C >(info );
186+ }
187+
188+ public TypeInfoProducer <T , C > visitClassInfo (MClassInfo <T , C > info ) {
189+ return new ClassInfoProducer <T , C >(info );
190+ }
191+
192+ @ Override
193+ public TypeInfoProducer <T , C > visitClassRef (MClassRef <T , C > info ) {
194+ return new ClassRefProducer <T , C >(info );
195+ }
196+
197+ public TypeInfoProducer <T , C > visitList (MList <T , C > info ) {
198+ return new ListProducer <T , C >(info , info .getItemTypeInfo ().acceptTypeInfoVisitor (this ));
199+ }
200+
201+ public TypeInfoProducer <T , C > visitBuiltinLeafInfo (MBuiltinLeafInfo <T , C > info ) {
202+
203+ final O origin = this .originated .getOrigin ();
204+
205+ final List <QName > simpleTypeNames = new LinkedList <QName >();
206+ if (origin instanceof SchemaComponentAware ) {
207+ final XSComponent component = ((SchemaComponentAware ) origin ).getSchemaComponent ();
208+ if (component != null ) {
209+ final CollectSimpleTypeNamesVisitor visitor = new CollectSimpleTypeNamesVisitor ();
210+ component .visit (visitor );
211+ simpleTypeNames .addAll (visitor .getTypeNames ());
212+ }
213+ }
214+
215+ simpleTypeNames .add (info .getTypeName ());
216+
217+ for (QName candidateName : simpleTypeNames ) {
218+ final TypeInfoProducer <T , C > typeInfoProducer = XSD_TYPE_MAPPING .get (candidateName );
219+ if (typeInfoProducer != null ) {
220+ return typeInfoProducer ;
221+ }
222+ }
223+ return null ;
224+ }
225+
226+ public TypeInfoProducer <T , C > visitWildcardTypeInfo (MWildcardTypeInfo <T , C > info ) {
227+ throw new UnsupportedOperationException ();
228+ }
229+
230+ @ Override
231+ public TypeInfoProducer <T , C > visitID (MID <T , C > info ) {
232+ return new BuiltinLeafInfoProducer <T , C , O >(XmlSchemaConstants .ID );
233+ }
234+
235+ @ Override
236+ public TypeInfoProducer <T , C > visitIDREF (MIDREF <T , C > info ) {
237+ return new BuiltinLeafInfoProducer <T , C , O >(XmlSchemaConstants .IDREF );
238+ }
239+
240+ @ Override
241+ public TypeInfoProducer <T , C > visitIDREFS (MIDREFS <T , C > info ) {
242+ return new BuiltinLeafInfoProducer <T , C , O >(XmlSchemaConstants .IDREFS );
243+ }
244+ }
0 commit comments