aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/xsdc/cpp/CppCodeGenerator.java
blob: 9f9ef7fd035ce171916d48eb5c335acdd6969a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.xsdc.cpp;

import com.android.xsdc.CodeWriter;
import com.android.xsdc.FileSystem;
import com.android.xsdc.XmlSchema;
import com.android.xsdc.XsdConstants;
import com.android.xsdc.tag.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.namespace.QName;

public class CppCodeGenerator {
    private XmlSchema xmlSchema;
    private String pkgName;
    private Map<String, CppSimpleType> cppSimpleTypeMap;
    private CodeWriter cppFile;
    private CodeWriter headerFile;
    private boolean hasAttr;

    public CppCodeGenerator(XmlSchema xmlSchema, String pkgName)
            throws CppCodeGeneratorException {
        this.xmlSchema = xmlSchema;
        this.pkgName = pkgName;

        // class naming validation
        {
            Set<String> nameSet = new HashSet<>();
            nameSet.add("XmlParser");
            for (XsdType type : xmlSchema.getTypeMap().values()) {
                if ((type instanceof XsdComplexType) || (type instanceof XsdRestriction &&
                        ((XsdRestriction)type).getEnums() != null)) {
                    String name = Utils.toClassName(type.getName());
                    if (nameSet.contains(name)) {
                        throw new CppCodeGeneratorException(
                                String.format("duplicate class name : %s", name));
                    }
                    nameSet.add(name);
                    if (type instanceof XsdComplexType && !hasAttr) {
                        hasAttr = hasAttribute((XsdComplexType)type);
                    }
                }
            }
            for (XsdElement element : xmlSchema.getElementMap().values()) {
                XsdType type = element.getType();
                if (type.getRef() == null && type instanceof XsdComplexType) {
                    String name = Utils.toClassName(element.getName());
                    if (nameSet.contains(name)) {
                        throw new CppCodeGeneratorException(
                                String.format("duplicate class name : %s", name));
                    }
                    nameSet.add(name);
                    if (!hasAttr) {
                        hasAttr = hasAttribute((XsdComplexType)type);
                    }
                }
            }
        }

        cppSimpleTypeMap = new HashMap<>();
        for (XsdType type : xmlSchema.getTypeMap().values()) {
            if (type instanceof XsdSimpleType) {
                XsdType refType = new XsdType(null, new QName(type.getName()));
                parseSimpleType(refType, true);
            }
        }
    }

    public void print(FileSystem fs)
            throws CppCodeGeneratorException, IOException {
        // cpp file, headr file init
        String cppFileName = pkgName.replace(".", "_") + ".cpp";
        String hFileName =  pkgName.replace(".", "_") + ".h";
        cppFile =  new CodeWriter(fs.getPrintWriter(cppFileName));
        headerFile = new CodeWriter(fs.getPrintWriter("include/" + hFileName));

        String headerMacro = hFileName.toUpperCase().replace(".", "_");
        headerFile.printf("#ifndef %s\n", headerMacro);
        headerFile.printf("#define %s\n\n", headerMacro);
        headerFile.printf("#include <libxml/parser.h>\n");
        headerFile.printf("#include <libxml/xinclude.h>\n\n");
        headerFile.printf("#include <map>\n");
        headerFile.printf("#include <optional>\n");
        headerFile.printf("#include <string>\n");
        headerFile.printf("#include <vector>\n\n");

        cppFile.printf("#define LOG_TAG \"%s\"\n\n", pkgName);
        cppFile.printf("#include <android/log.h>\n");
        cppFile.printf("#include <android-base/strings.h>\n\n");
        cppFile.printf("#include <libxml/parser.h>\n");
        cppFile.printf("#include <libxml/xinclude.h>\n\n");
        cppFile.printf("#include \"%s\"\n\n", hFileName);

        List<String> namespace = new java.util.ArrayList<>();
        for (String token : pkgName.split("\\.")) {
            if (token.isEmpty()) {
                continue;
            }
            if (Character.isDigit(token.charAt(0))) {
                token = "_" + token;
            }
            namespace.add(token);
            headerFile.printf("namespace %s {\n", token);
            cppFile.printf("namespace %s {\n", token);
        }

        printPrototype();
        printXmlParser();

        for (XsdType type : xmlSchema.getTypeMap().values()) {
            if (type instanceof XsdComplexType) {
                String name = Utils.toClassName(type.getName());
                XsdComplexType complexType = (XsdComplexType) type;
                printClass(name, "", complexType);
            } else if (type instanceof XsdRestriction &&
                  ((XsdRestriction)type).getEnums() != null) {
                String name = Utils.toClassName(type.getName());
                XsdRestriction restrictionType = (XsdRestriction) type;
                printEnum(name, restrictionType);
            }
        }
        for (XsdElement element : xmlSchema.getElementMap().values()) {
            XsdType type = element.getType();
            if (type.getRef() == null && type instanceof XsdComplexType) {
                String name = Utils.toClassName(element.getName());
                XsdComplexType complexType = (XsdComplexType) type;
                printClass(name, "", complexType);
            }
        }

        Collections.reverse(namespace);
        for (String token : namespace) {
            headerFile.printf("} // %s\n", token);
            cppFile.printf("} // %s\n", token);
        }

        headerFile.printf("#endif // %s\n", headerMacro);
        cppFile.close();
        headerFile.close();
    }

    private void printEnum(String name, XsdRestriction restrictionType)
            throws CppCodeGeneratorException {
        headerFile.printf("enum class %s {\n", name);
        cppFile.printf("const std::map<std::string, %s> %sString {\n", name, name);
        List<XsdEnumeration> enums = restrictionType.getEnums();

        for (XsdEnumeration tag : enums) {
            headerFile.printf("%s,\n", Utils.toEnumName(tag.getValue()));
            cppFile.printf("{ \"%s\", %s::%s },\n", tag.getValue(), name,
                    Utils.toEnumName(tag.getValue()));
        }
        headerFile.printf("UNKNOWN\n};\n\n");
        cppFile.printf("};\n\n");

        cppFile.printf("static %s stringTo%s(std::string value) {\n"
                + "auto enumValue =  %sString.find(value);\n"
                + "return enumValue == %sString.end() ? %s::UNKNOWN : enumValue->second;\n"
                + "}\n\n", name, name, name, name, name);
    }


    private void printPrototype() throws CppCodeGeneratorException {
        for (XsdType type : xmlSchema.getTypeMap().values()) {
            if (type instanceof XsdComplexType) {
                String name = Utils.toClassName(type.getName());
                headerFile.printf("class %s;\n", name);
            }
        }
        for (XsdElement element : xmlSchema.getElementMap().values()) {
            XsdType type = element.getType();
            if (type.getRef() == null && type instanceof XsdComplexType) {
                String name = Utils.toClassName(element.getName());
                headerFile.printf("class %s;\n", name);
            }
        }
    }

    private void printClass(String name, String nameScope, XsdComplexType complexType)
            throws CppCodeGeneratorException {
        assert name != null;
        // need element, attribute name duplicate validation?

        String baseName = getBaseName(complexType);
        CppSimpleType valueType = (complexType instanceof XsdSimpleContent) ?
                getValueType((XsdSimpleContent) complexType, false) : null;

        headerFile.printf("class %s ", name);

        if (baseName != null) {
            headerFile.printf(": public %s {\n", baseName);
        } else {
            headerFile.println("{");
        }

        // parse types for elements and attributes
        List<CppType> elementTypes = new ArrayList<>();
        for (XsdElement element : complexType.getElements()) {
            CppType cppType;
            XsdElement elementValue = resolveElement(element);
            if (element.getRef() == null && element.getType().getRef() == null
                    && element.getType() instanceof XsdComplexType) {
                // print inner class for anonymous types
                headerFile.printf("public:\n");
                String innerName = Utils.toClassName(getElementName(element));
                XsdComplexType innerType = (XsdComplexType) element.getType();
                printClass(innerName, nameScope + name + "::", innerType);
                headerFile.println();
                cppType = new CppComplexType(nameScope + name + "::"+ innerName);
            } else {
                cppType = parseType(elementValue.getType(), getElementName(elementValue));
            }
            elementTypes.add(cppType);
        }
        List<CppSimpleType> attributeTypes = new ArrayList<>();
        for (XsdAttribute attribute : complexType.getAttributes()) {
            XsdType type = resolveAttribute(attribute).getType();
            attributeTypes.add(parseSimpleType(type, false));
        }

        // print member variables

        headerFile.printf("private:\n");
        for (int i = 0; i < elementTypes.size(); ++i) {
            CppType type = elementTypes.get(i);
            XsdElement element = complexType.getElements().get(i);
            XsdElement elementValue = resolveElement(element);
            String typeName = element.isMultiple() || type instanceof CppComplexType ?
                    String.format("std::vector<%s>", type.getName()) : type.getName();
            headerFile.printf("%s %s;\n", typeName,
                    Utils.toVariableName(getElementName(elementValue)));
        }
        for (int i = 0; i < attributeTypes.size(); ++i) {
            CppType type = attributeTypes.get(i);
            XsdAttribute attribute = resolveAttribute(complexType.getAttributes().get(i));
            headerFile.printf("%s %s;\n", type.getName(),
                    Utils.toVariableName(attribute.getName()));
        }
        if (valueType != null) {
            headerFile.printf("%s value;\n", valueType.getName());
        }

        // print getters and setters

        headerFile.printf("public:\n");
        for (int i = 0; i < elementTypes.size(); ++i) {
            CppType type = elementTypes.get(i);
            XsdElement element = complexType.getElements().get(i);
            XsdElement elementValue = resolveElement(element);
            printGetterAndSetter(nameScope + name, type,
                    Utils.toVariableName(getElementName(elementValue)),
                    type instanceof CppComplexType ? true : element.isMultiple(),
                    type instanceof CppComplexType ? false : ((CppSimpleType)type).isList());
        }
        for (int i = 0; i < attributeTypes.size(); ++i) {
            CppType type = attributeTypes.get(i);
            XsdAttribute attribute = resolveAttribute(complexType.getAttributes().get(i));
            printGetterAndSetter(nameScope + name, type,
                    Utils.toVariableName(attribute.getName()), false, false);
        }
        if (valueType != null) {
            printGetterAndSetter(nameScope + name, valueType, "value", false, false);
        }

        printParser(name, nameScope, complexType);
        headerFile.println("};\n");
    }

    private void printParser(String name, String nameScope, XsdComplexType complexType)
            throws CppCodeGeneratorException {
        CppSimpleType baseValueType = (complexType instanceof XsdSimpleContent) ?
                getValueType((XsdSimpleContent) complexType, true) : null;
        List<XsdElement> allElements = new ArrayList<>();
        List<XsdAttribute> allAttributes = new ArrayList<>();
        stackComponents(complexType, allElements, allAttributes);

        // parse types for elements and attributes
        List<CppType> allElementTypes = new ArrayList<>();
        for (XsdElement element : allElements) {
            XsdElement elementValue = resolveElement(element);
            CppType cppType = parseType(elementValue.getType(), elementValue.getName());
            allElementTypes.add(cppType);
        }
        List<CppSimpleType> allAttributeTypes = new ArrayList<>();
        for (XsdAttribute attribute : allAttributes) {
            XsdType type = resolveAttribute(attribute).getType();
            allAttributeTypes.add(parseSimpleType(type, false));
        }

        String fullName = nameScope + name;
        headerFile.printf("static %s read(xmlNode *root);\n", fullName, Utils.lowerize(name));
        cppFile.printf("\n%s %s::read(xmlNode *root) {\n", fullName, fullName);

        cppFile.printf("%s instance;\n std::string raw;\n", fullName, fullName);

        for (int i = 0; i < allAttributes.size(); ++i) {
            CppType type = allAttributeTypes.get(i);
            XsdAttribute attribute = resolveAttribute(allAttributes.get(i));
            String variableName = Utils.toVariableName(attribute.getName());
            cppFile.printf("raw = getXmlAttribute(root, \"%s\");\n", attribute.getName());
            cppFile.printf("if (raw != \"\") {\n");
            cppFile.print(type.getParsingExpression());
            cppFile.printf("instance.set%s(value);\n}\n", Utils.capitalize(variableName));
        }

        if (baseValueType != null) {
            cppFile.printf("auto xmlValue = make_xmlUnique(xmlNodeListGetString("
                    + "root->doc, root->xmlChildrenNode, 1));\n"
                    + "if (xmlValue != nullptr) {\n"
                    + "raw = reinterpret_cast<const char*>(xmlValue.get());\n");

            cppFile.print(baseValueType.getParsingExpression());
            cppFile.printf("instance.setValue(value);\n");
            cppFile.printf("}\n");
        } else if (!allElements.isEmpty()) {
            cppFile.print("for (xmlNode *child = root->xmlChildrenNode; child != nullptr;"
                    + " child = child->next) {\n");
            for (int i = 0; i < allElements.size(); ++i) {
                CppType type = allElementTypes.get(i);
                XsdElement element = allElements.get(i);
                XsdElement elementValue = resolveElement(element);
                String variableName = Utils.toVariableName(getElementName(elementValue));
                if (i != 0) cppFile.printf("} else ");
                cppFile.printf("if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>");
                cppFile.printf("(\"%s\"))) {\n", elementValue.getName());

                if (type instanceof CppSimpleType) {
                    cppFile.printf("auto xmlValue = make_xmlUnique(xmlNodeListGetString(");
                    cppFile.printf("child->doc, child->xmlChildrenNode, 1));\n");
                    cppFile.printf("if (xmlValue == nullptr) {\ncontinue;\n}\n");
                    cppFile.printf("raw = reinterpret_cast<const char*>(xmlValue.get());\n");
                }

                cppFile.print(type.getParsingExpression());

                if (element.isMultiple() || type instanceof CppComplexType) {
                    cppFile.printf("instance.get%s().push_back(std::move(value));\n",
                            Utils.capitalize(variableName));
                } else {
                    cppFile.printf("instance.set%s(value);\n", Utils.capitalize(variableName));
                }
            }
            cppFile.printf("}\n}\n");
        }
        cppFile.printf("return instance;\n"
                + "}\n");
    }

    private void printGetterAndSetter(String name, CppType type, String variableName,
            boolean isMultiple, boolean isMultipleType) {
        String typeName = isMultiple ? String.format("std::vector<%s>", type.getName())
                : type.getName();

        headerFile.printf("%s& get%s();\n", typeName, Utils.capitalize(variableName));

        cppFile.println();
        cppFile.printf("%s& %s::get%s() {\n"
                + "return %s;\n}\n",
                typeName, name, Utils.capitalize(variableName), variableName);

        if (isMultiple || isMultipleType) {
            String elementTypeName = type instanceof CppComplexType ? type.getName() :
                    ((CppSimpleType)type).getTypeName();
            if (elementTypeName.equals("bool")) {
                headerFile.printf("%s getFirst%s();\n",
                        elementTypeName, Utils.capitalize(variableName));
                cppFile.println();
                cppFile.printf("%s %s::getFirst%s() {\n"
                        + "if (%s.empty()) {\n"
                        + "return false;\n"
                        + "}\n"
                        + "return %s[0];\n"
                        + "}\n",
                        elementTypeName, name, Utils.capitalize(variableName), variableName,
                        variableName);
            } else {
                headerFile.printf("%s* getFirst%s();\n",
                        elementTypeName, Utils.capitalize(variableName));
                cppFile.println();
                cppFile.printf("%s* %s::getFirst%s() {\n"
                        + "if (%s.empty()) {\n"
                        + "return nullptr;\n"
                        + "}\n"
                        + "return &%s[0];\n"
                        + "}\n",
                        elementTypeName, name, Utils.capitalize(variableName), variableName,
                        variableName);
            }
        }

        if (isMultiple) return;
        headerFile.printf("void set%s(%s);\n", Utils.capitalize(variableName), typeName);
        cppFile.println();
        cppFile.printf("void %s::set%s(%s %s) {\n"
                + "this->%s = std::move(%s);\n"
                + "}\n",
                name, Utils.capitalize(variableName), typeName, variableName,
                variableName, variableName);
    }

    private void printXmlParser() throws CppCodeGeneratorException {
        cppFile.printf("template <class T>\n"
                + "constexpr void (*xmlDeleter)(T* t);\n"
                + "template <>\nconstexpr auto xmlDeleter<xmlDoc> = xmlFreeDoc;\n"
                + "template <>\nauto xmlDeleter<xmlChar> = [](xmlChar *s) { xmlFree(s); };\n\n"
                + "template <class T>\n"
                + "constexpr auto make_xmlUnique(T *t) {\n"
                + "auto deleter = [](T *t) { xmlDeleter<T>(t); };\n"
                + "return std::unique_ptr<T, decltype(deleter)>{t, deleter};\n"
                + "}\n\n");

        if (hasAttr) {
            cppFile.printf("static std::string getXmlAttribute"
                    + "(const xmlNode *cur, const char *attribute) {\n"
                    + "auto xmlValue = make_xmlUnique(xmlGetProp(cur, "
                    + "reinterpret_cast<const xmlChar*>(attribute)));\n"
                    + "if (xmlValue == nullptr) {\n"
                    + "return \"\";\n"
                    + "}\n"
                    + "std::string value(reinterpret_cast<const char*>(xmlValue.get()));\n"
                    + "return value;\n"
                    + "}\n\n");
        }

        String className = Utils.toClassName(pkgName);

        boolean isMultiRootElement = xmlSchema.getElementMap().values().size() > 1;
        for (XsdElement element : xmlSchema.getElementMap().values()) {
            CppType cppType = parseType(element.getType(), element.getName());
            String elementName = element.getName();
            String VariableName = Utils.toVariableName(elementName);
            String typeName = cppType instanceof CppSimpleType ? cppType.getName() :
                    Utils.toClassName(cppType.getName());

            headerFile.printf("std::optional<%s> read%s(const char* configFile);\n\n",
                    typeName, isMultiRootElement ? Utils.capitalize(typeName) : "");
            cppFile.printf("std::optional<%s> read%s(const char* configFile) {\n",
                    typeName, isMultiRootElement ? Utils.capitalize(typeName) : "");
            cppFile.printf("auto doc = make_xmlUnique(xmlParseFile(configFile));\n"
                    + "if (doc == nullptr) {\n"
                    + "return std::nullopt;\n"
                    + "}\n"
                    + "xmlNodePtr child = xmlDocGetRootElement(doc.get());\n"
                    + "if (child == NULL) {\n"
                    + "return std::nullopt;\n"
                    + "}\n\n"
                    + "if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>"
                    + "(\"%s\"))) {\n",
                    elementName);

            if (cppType instanceof CppSimpleType) {
                cppFile.printf("%s value = getXmlAttribute(child, \"%s\");\n",
                        elementName, elementName);
            } else {
                cppFile.printf(cppType.getParsingExpression());
            }
            cppFile.printf("return value;\n}\n");
            cppFile.printf("return std::nullopt;\n");
            cppFile.printf("}\n\n");
        }
    }

    private String getElementName(XsdElement element) {
        if (element instanceof XsdChoice) {
            return element.getName() + "_optional";
        } else if (element instanceof XsdAll) {
            return element.getName() + "_all";
        }
        return element.getName();
    }

    private void stackComponents(XsdComplexType complexType, List<XsdElement> elements,
            List<XsdAttribute> attributes) throws CppCodeGeneratorException {
        if (complexType.getBase() != null) {
            QName baseRef = complexType.getBase().getRef();
            if (baseRef != null && !baseRef.getNamespaceURI().equals(XsdConstants.XSD_NAMESPACE)) {
                XsdType parent = getType(baseRef.getLocalPart());
                if (parent instanceof XsdComplexType) {
                    stackComponents((XsdComplexType) parent, elements, attributes);
                }
            }
        }
        elements.addAll(complexType.getElements());
        attributes.addAll(complexType.getAttributes());
    }

    private String getBaseName(XsdComplexType complexType) throws CppCodeGeneratorException {
        if (complexType.getBase() == null) return null;
        if (complexType.getBase().getRef().getNamespaceURI().equals(XsdConstants.XSD_NAMESPACE)) {
            return null;
        }
        XsdType base = getType(complexType.getBase().getRef().getLocalPart());
        if (base instanceof XsdComplexType) {
            return Utils.toClassName(base.getName());
        }
        return null;
    }

    private CppSimpleType getValueType(XsdSimpleContent simpleContent, boolean traverse)
            throws CppCodeGeneratorException {
        assert simpleContent.getBase() != null;
        QName baseRef = simpleContent.getBase().getRef();
        assert baseRef != null;
        if (baseRef.getNamespaceURI().equals(XsdConstants.XSD_NAMESPACE)) {
            return predefinedType(baseRef.getLocalPart());
        } else {
            XsdType parent = getType(baseRef.getLocalPart());
            if (parent instanceof XsdSimpleType) {
                return parseSimpleTypeReference(baseRef, false);
            }
            if (!traverse) return null;
            if (parent instanceof XsdSimpleContent) {
                return getValueType((XsdSimpleContent) parent, true);
            } else {
                throw new CppCodeGeneratorException(
                        String.format("base not simple : %s", baseRef.getLocalPart()));
            }
        }
    }

    private CppType parseType(XsdType type, String defaultName) throws CppCodeGeneratorException {
        if (type.getRef() != null) {
            String name = type.getRef().getLocalPart();
            if (type.getRef().getNamespaceURI().equals(XsdConstants.XSD_NAMESPACE)) {
                return predefinedType(name);
            } else {
                XsdType typeValue = getType(name);
                if (typeValue instanceof XsdSimpleType) {
                    return parseSimpleTypeReference(type.getRef(), false);
                }
                return parseType(typeValue, name);
            }
        }
        if (type instanceof XsdComplexType) {
            return new CppComplexType(Utils.toClassName(defaultName));
        } else if (type instanceof XsdSimpleType) {
            return parseSimpleTypeValue((XsdSimpleType) type, false);
        } else {
            throw new CppCodeGeneratorException(
                    String.format("unknown type name : %s", defaultName));
        }
    }

    private CppSimpleType parseSimpleType(XsdType type, boolean traverse)
            throws CppCodeGeneratorException {
        if (type.getRef() != null) {
            return parseSimpleTypeReference(type.getRef(), traverse);
        } else {
            return parseSimpleTypeValue((XsdSimpleType) type, traverse);
        }
    }

    private CppSimpleType parseSimpleTypeReference(QName typeRef, boolean traverse)
            throws CppCodeGeneratorException {
        assert typeRef != null;
        String typeName = typeRef.getLocalPart();
        if (typeRef.getNamespaceURI().equals(XsdConstants.XSD_NAMESPACE)) {
            return predefinedType(typeName);
        }
        if (cppSimpleTypeMap.containsKey(typeName)) {
            return cppSimpleTypeMap.get(typeName);
        } else if (traverse) {
            XsdSimpleType simpleType = getSimpleType(typeName);
            CppSimpleType ret = parseSimpleTypeValue(simpleType, true);
            cppSimpleTypeMap.put(typeName, ret);
            return ret;
        } else {
            throw new CppCodeGeneratorException(String.format("unknown type name : %s", typeName));
        }
    }

    private CppSimpleType parseSimpleTypeValue(XsdSimpleType simpleType, boolean traverse)
            throws CppCodeGeneratorException {
        if (simpleType instanceof XsdList) {
            XsdList list = (XsdList) simpleType;
            return parseSimpleType(list.getItemType(), traverse).newListType();
        } else if (simpleType instanceof XsdRestriction) {
            // we don't consider any restrictions.
            XsdRestriction restriction = (XsdRestriction) simpleType;
            if (restriction.getEnums() != null) {
                String name = Utils.toClassName(restriction.getName());
                return new CppSimpleType(name, "stringTo" + name + "(%s)", false);
            }
            return parseSimpleType(restriction.getBase(), traverse);
        } else if (simpleType instanceof XsdUnion) {
            // unions are almost always interpreted as java.lang.String
            // Exceptionally, if any of member types of union are 'list', then we interpret it as
            // List<String>
            XsdUnion union = (XsdUnion) simpleType;
            for (XsdType memberType : union.getMemberTypes()) {
                if (parseSimpleType(memberType, traverse).isList()) {
                    return new CppSimpleType("std::string", "%s", true);
                }
            }
            return new CppSimpleType("std::string", "%s", false);
        } else {
            // unreachable
            throw new IllegalStateException("unknown simple type");
        }
    }

    private XsdElement resolveElement(XsdElement element) throws CppCodeGeneratorException {
        if (element.getRef() == null) return element;
        String name = element.getRef().getLocalPart();
        XsdElement ret = xmlSchema.getElementMap().get(name);
        if (ret != null) return ret;
        throw new CppCodeGeneratorException(String.format("no element named : %s", name));
    }

    private XsdAttribute resolveAttribute(XsdAttribute attribute)
            throws CppCodeGeneratorException {
        if (attribute.getRef() == null) return attribute;
        String name = attribute.getRef().getLocalPart();
        XsdAttribute ret = xmlSchema.getAttributeMap().get(name);
        if (ret != null) return ret;
        throw new CppCodeGeneratorException(String.format("no attribute named : %s", name));
    }

    private XsdType getType(String name) throws CppCodeGeneratorException {
        XsdType type = xmlSchema.getTypeMap().get(name);
        if (type != null) return type;
        throw new CppCodeGeneratorException(String.format("no type named : %s", name));
    }

    private XsdSimpleType getSimpleType(String name) throws CppCodeGeneratorException {
        XsdType type = getType(name);
        if (type instanceof XsdSimpleType) return (XsdSimpleType) type;
        throw new CppCodeGeneratorException(String.format("not a simple type : %s", name));
    }

    private boolean hasAttribute(XsdComplexType complexType) throws CppCodeGeneratorException {
        if (complexType.getAttributes().size() > 0) {
            return true;
        }
        boolean results = false;
        for (XsdElement element : complexType.getElements()) {
            XsdElement elementValue = resolveElement(element);
            if (element.getRef() == null && element.getType().getRef() == null
                    && element.getType() instanceof XsdComplexType) {
                results = hasAttribute((XsdComplexType) element.getType());
                if (results) {
                    return results;
                }
            }
        }
        return results;
    }

    private static CppSimpleType predefinedType(String name) throws CppCodeGeneratorException {
        switch (name) {
            case "string":
            case "token":
            case "normalizedString":
            case "language":
            case "ENTITY":
            case "ID":
            case "Name":
            case "NCName":
            case "NMTOKEN":
            case "anyURI":
            case "anyType":
            case "QName":
            case "NOTATION":
            case "IDREF":
                return new CppSimpleType("std::string", "%s", false);
            case "ENTITIES":
            case "NMTOKENS":
            case "IDREFS":
                return new CppSimpleType("std::string", "%s", true);
            case "date":
            case "dateTime":
            case "time":
            case "gDay":
            case "gMonth":
            case "gYear":
            case "gMonthDay":
            case "gYearMonth":
            case "duration":
                return new CppSimpleType("std::string", "%s", false);
            case "decimal":
                return new CppSimpleType("double", "std::stod(%s)", false);
            case "integer":
            case "negativeInteger":
            case "nonNegativeInteger":
            case "positiveInteger":
            case "nonPositiveInteger":
                return new CppSimpleType("long long", "std::stoll(%s)", false);
            case "unsignedLong":
                return new CppSimpleType("unsigned long long", "std::stoull(%s)", false);
            case "long":
                return new CppSimpleType("long long", "std::stoll(%s)", false);
            case "unsignedInt":
                return new CppSimpleType("unsigned int",
                        "static_cast<unsigned int>(stoul(%s))", false);
            case "int":
                return new CppSimpleType("int", "std::stoi(%s)", false);
            case "unsignedShort":
                return new CppSimpleType("unsigned short",
                        "static_cast<unsigned short>(std::stoi(%s))", false);
            case "short":
                return new CppSimpleType("short", "static_cast<short>(std::stoi(%s))", false);
            case "unsignedByte":
                return new CppSimpleType("unsigned char",
                        "static_cast<unsigned char>(std::stoi(%s))", false);
            case "byte":
                return new CppSimpleType("char", "static_cast<char>(std::stoi(%s))", false);
            case "boolean":
                return new CppSimpleType("bool", "%s == \"true\"", false);
            case "double":
                return new CppSimpleType("double", "std::stod(%s)", false);
            case "float":
                return new CppSimpleType("float", "std::stof(%s)", false);
            case "base64Binary":
            case "hexBinary":
                return new CppSimpleType("std::string", "%s", false);
        }
        throw new CppCodeGeneratorException("unknown xsd predefined type : " + name);
    }
}