Search in sources :

Example 11 with NamespaceMap

use of org.apache.ws.commons.schema.utils.NamespaceMap in project convertigo by convertigo.

the class JsonSchemaUtils method getOasSchema.

protected static JSONObject getOasSchema(XmlSchemaCollection xmlSchemaCollection, XmlSchema xmlSchema, String oasDirUrl, boolean isOas2) {
    final NamespaceMap nsMap = (NamespaceMap) xmlSchemaCollection.getNamespaceContext();
    final JSONObject jsonSchema = new JSONObject();
    try {
        String prefix = nsMap.getPrefix(xmlSchema.getTargetNamespace());
        jsonSchema.put("id", oasDirUrl + prefix + ".jsonschema#");
        jsonSchema.put("ns", xmlSchema.getTargetNamespace());
        jsonSchema.put("definitions", new JSONObject());
        new XmlSchemaWalker(false, false) {

            final Map<String, JSONObject> refs = new HashMap<String, JSONObject>(50);

            final JSONObject definitions = jsonSchema.getJSONObject("definitions");

            JSONObject parent = definitions;

            JSONObject root = null;

            private boolean isGlobal(JSONObject jParent) {
                if (jParent != null) {
                    return jParent.equals(definitions);
                }
                return false;
            }

            private JSONObject getRefObject(String ref) throws JSONException {
                JSONObject refObject = refs.get(ref);
                if (refObject != null) {
                    if (!refObject.has("value") && !refObject.equals(root)) {
                        handle(refObject);
                    }
                    if (refObject.has("value")) {
                        JSONObject value = refObject.getJSONObject("value");
                        if (value.has("type") && !value.getString("type").equals("object")) {
                            JSONObject ob = new JSONObject();
                            copyOKeys(value, ob);
                            return ob;
                        }
                    }
                }
                return new JSONObject().put("$ref", ref);
            }

            private String normalize(String key) {
                return key.replaceAll("[^a-zA-Z0-9]", "x");
            }

            private void addGlobalObject(JSONObject jParent, JSONObject jElement, String key) throws JSONException {
                if (jParent != null) {
                    jParent.put(normalize(key), jElement);
                    refs.put(jElement.getString("objKey"), jElement);
                }
            }

            private String toOasType(String baseType) {
                String oasType = baseType;
                switch(baseType) {
                    case "NMTOKEN":
                    case "token":
                    case "IDREF":
                    case "ID":
                        oasType = "string";
                        break;
                    case "dateTime":
                        oasType = "string";
                        break;
                    case "decimal":
                        oasType = "number";
                        break;
                    case "byte":
                    case "int":
                    case "short":
                    case "nonNegativeInteger":
                    case "positiveInteger":
                        oasType = "integer";
                        break;
                }
                return oasType;
            }

            private String getDefinitionRef(QName rname) {
                if (rname != null) {
                    String local = rname.getLocalPart();
                    String ns = rname.getNamespaceURI();
                    return oasDirUrl + nsMap.getPrefix(ns) + ".jsonschema#/definitions/" + normalize(local);
                }
                return null;
            }

            private void addChild(JSONObject jParent, JSONObject jElement) throws JSONException {
                if (!jParent.has("children")) {
                    jParent.put("children", new JSONArray());
                }
                jParent.getJSONArray("children").put(jElement);
            }

            private void handleElement(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
                if (!jsonOb.has("type")) {
                    jsonOb.put("type", "object");
                }
                if (!jsonOb.has("required")) {
                    jsonOb.put("required", new JSONArray());
                }
                if (!jsonOb.has("properties")) {
                    jsonOb.put("properties", new JSONObject());
                }
                JSONArray required = jsonOb.getJSONArray("required");
                JSONObject properties = jsonOb.getJSONObject("properties");
                String propertyName = jsonChild.getString("name");
                long minOccurs = jsonChild.has("minOccurs") ? jsonChild.getLong("minOccurs") : 0;
                long maxOccurs = jsonChild.has("maxOccurs") ? jsonChild.getLong("maxOccurs") : minOccurs;
                boolean isRequired = Math.min(minOccurs, minItems) > 0;
                boolean isArray = maxOccurs > 1 || maxItems > 1;
                JSONObject property = new JSONObject();
                // fill required
                if (isRequired) {
                    if (required.join(",").indexOf(propertyName) == -1) {
                        required.put(propertyName);
                    }
                } else if (!isArray) {
                    property.put("nullable", true);
                }
                // fill properties
                if (isArray) {
                    property.put("type", "array");
                    property.put("minItems", Math.min(minOccurs, minItems));
                    property.put("maxItems", Math.max(maxOccurs, maxItems));
                    property.put("items", new JSONObject());
                }
                JSONObject prop = isArray ? property.getJSONObject("items") : property;
                if (jsonChild.has("value")) {
                    JSONObject value = jsonChild.getJSONObject("value");
                    @SuppressWarnings("unchecked") Iterator<String> it = value.keys();
                    while (it.hasNext()) {
                        String pkey = it.next();
                        if (!pkey.isEmpty()) {
                            prop.put(pkey, value.get(pkey));
                        }
                    }
                } else if (jsonChild.has("children")) {
                    JSONArray children = jsonChild.getJSONArray("children");
                    for (int i = 0; i < children.length(); i++) {
                        JSONObject child = children.getJSONObject(i);
                        if (!child.has("objType")) {
                            @SuppressWarnings("unchecked") Iterator<String> it = child.keys();
                            while (it.hasNext()) {
                                String pkey = it.next();
                                if (!pkey.isEmpty()) {
                                    prop.put(pkey, child.get(pkey));
                                }
                            }
                        }
                    }
                }
                if (!isRequired && prop.has("$ref")) {
                    JSONArray allOf = new JSONArray().put(new JSONObject().put("$ref", prop.remove("$ref")));
                    prop.put("nullable", true).put("allOf", allOf);
                }
                properties.put(propertyName, property);
                // handle children
                if (jsonChild.has("children")) {
                    JSONArray children = jsonChild.getJSONArray("children");
                    for (int i = 0; i < children.length(); i++) {
                        JSONObject child = children.getJSONObject(i);
                        if (child.has("objType")) {
                            handleChild(prop.has("type") || prop.has("$ref") ? jsonOb : prop, child);
                        }
                    }
                }
            }

            private void handleAttribute(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
                if (!jsonOb.has("required")) {
                    jsonOb.put("required", new JSONArray());
                }
                if (!jsonOb.has("properties")) {
                    jsonOb.put("properties", new JSONObject());
                }
                JSONObject attrObject = null;
                if (jsonOb.getJSONObject("properties").has("attr")) {
                    attrObject = jsonOb.getJSONObject("properties").getJSONObject("attr");
                } else {
                    attrObject = new JSONObject().put("type", "object").put("required", new JSONArray()).put("properties", new JSONObject());
                    jsonOb.getJSONObject("properties").put("attr", attrObject);
                }
                JSONArray required = attrObject.getJSONArray("required");
                JSONObject properties = attrObject.getJSONObject("properties");
                String attrName = jsonChild.getString("name");
                long minOccurs = jsonChild.has("minOccurs") ? jsonChild.getLong("minOccurs") : 0;
                long maxOccurs = jsonChild.has("maxOccurs") ? jsonChild.getLong("maxOccurs") : minOccurs;
                boolean isRequired = minOccurs > 0;
                boolean isArray = maxOccurs > 1;
                // fill required
                if (isRequired) {
                    if (required.join(",").indexOf(attrName) == -1) {
                        required.put(attrName);
                        // if attribute is required then attr object is required also
                        if (jsonOb.getJSONArray("required").join(",").indexOf("attr") == -1) {
                            jsonOb.getJSONArray("required").put("attr");
                        }
                    }
                }
                // fill properties
                JSONObject attribute = new JSONObject();
                if (isArray) {
                    attribute.put("type", "array");
                    attribute.put("minItems", minOccurs);
                    attribute.put("maxItems", maxOccurs);
                    attribute.put("items", new JSONObject());
                }
                JSONObject attr = isArray ? attribute.getJSONObject("items") : attribute;
                if (jsonChild.has("value")) {
                    JSONObject value = jsonChild.getJSONObject("value");
                    copyOKeys(value, attr);
                } else if (jsonChild.has("children")) {
                    JSONArray children = jsonChild.getJSONArray("children");
                    for (int i = 0; i < children.length(); i++) {
                        JSONObject child = children.getJSONObject(i);
                        if (!child.has("objType")) {
                            copyOKeys(child, attr);
                        }
                    }
                }
                properties.put(attrName, attribute);
                // handle children
                if (jsonChild.has("children")) {
                    JSONArray children = jsonChild.getJSONArray("children");
                    for (int i = 0; i < children.length(); i++) {
                        JSONObject child = children.getJSONObject(i);
                        if (child.has("objType")) {
                            handleChild(attr, child);
                        }
                    }
                }
            }

            private void handleChoice(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
                // note: oneOf with complex inline model is baddly supported: minTems forced to 0L
                long minOccurs = jsonChild.has("minOccurs") ? jsonChild.getLong("minOccurs") : 0;
                long maxOccurs = jsonChild.has("maxOccurs") ? jsonChild.getLong("maxOccurs") : minOccurs;
                // handle children
                if (jsonChild.has("children")) {
                    JSONArray children = jsonChild.getJSONArray("children");
                    for (int i = 0; i < children.length(); i++) {
                        JSONObject child = children.getJSONObject(i);
                        // minTems forced to 0L
                        handleChild(jsonOb, child, 0L, Math.max(maxOccurs, maxItems));
                    }
                }
            }

            private void handleUnion(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
                if (jsonChild.has("children")) {
                    JSONArray children = jsonChild.getJSONArray("children");
                    for (int i = 0; i < children.length(); i++) {
                        JSONObject child = children.getJSONObject(i);
                        handleChild(jsonOb, child, minItems, maxItems);
                    }
                }
            }

            private void handleExtension(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
                boolean hasAllOf = jsonOb.has("allOf");
                if (!hasAllOf) {
                    jsonOb.put("allOf", new JSONArray());
                }
                JSONArray allOf = jsonOb.getJSONArray("allOf");
                // handle children
                if (jsonChild.has("children")) {
                    JSONArray children = jsonChild.getJSONArray("children");
                    for (int i = 0; i < children.length(); i++) {
                        JSONObject child = children.getJSONObject(i);
                        JSONObject ob = new JSONObject();
                        handleChild(ob, child);
                        merge(ob);
                        allOf.put(ob);
                    }
                }
                merge(jsonOb);
            }

            private void handleSequence(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
                long minOccurs = jsonChild.has("minOccurs") ? jsonChild.getLong("minOccurs") : 0;
                long maxOccurs = jsonChild.has("maxOccurs") ? jsonChild.getLong("maxOccurs") : minOccurs;
                boolean hasAllOf = jsonOb.has("allOf");
                if (!hasAllOf) {
                    jsonOb.put("allOf", new JSONArray());
                }
                JSONArray allOf = jsonOb.getJSONArray("allOf");
                // handle children
                if (jsonChild.has("children")) {
                    JSONArray children = jsonChild.getJSONArray("children");
                    for (int i = 0; i < children.length(); i++) {
                        JSONObject child = children.getJSONObject(i);
                        JSONObject ob = new JSONObject();
                        handleChild(ob, child, Math.min(minOccurs, minItems), Math.max(maxOccurs, maxItems));
                        merge(ob);
                        allOf.put(ob);
                    }
                    merge(jsonOb);
                }
            }

            private void merge(JSONObject jsonOb) throws JSONException {
                if (jsonOb.has("allOf")) {
                    JSONArray allOf = jsonOb.getJSONArray("allOf");
                    int len = allOf.length();
                    if (len > 0) {
                        JSONObject merge = new JSONObject().put("type", "object").put("required", new JSONArray()).put("properties", new JSONObject());
                        List<JSONObject> merged = new ArrayList<JSONObject>();
                        List<JSONObject> others = new ArrayList<JSONObject>();
                        for (int i = 0; i < len; i++) {
                            JSONObject ob = allOf.getJSONObject(i);
                            if (ob.has("description") && ob.length() == 1) {
                                merge.put("description", ob.getString("description"));
                                merged.add(ob);
                            }
                            if (ob.has("properties")) {
                                copyOKeys(ob.getJSONObject("properties"), merge.getJSONObject("properties"));
                            }
                            if (ob.has("required")) {
                                copyAKeys(ob.getJSONArray("required"), merge.getJSONArray("required"));
                            }
                            if (!ob.has("properties") && !ob.has("required")) {
                                others.add(ob);
                            } else {
                                merged.add(ob);
                            }
                        }
                        for (JSONObject ob : merged) {
                            allOf.remove(ob);
                        }
                        if (merged.size() > 0) {
                            allOf.put(merge);
                        }
                        // merge in parent
                        if (allOf.length() == 1) {
                            JSONObject ob = allOf.getJSONObject(0);
                            if (jsonOb.has("properties")) {
                                if (ob.has("description") && ob.length() == 1) {
                                    jsonOb.put("description", ob.getString("description"));
                                }
                                if (ob.has("properties")) {
                                    copyOKeys(ob.getJSONObject("properties"), jsonOb.getJSONObject("properties"));
                                }
                                if (ob.has("required")) {
                                    copyAKeys(ob.getJSONArray("required"), jsonOb.getJSONArray("required"));
                                }
                                jsonOb.remove("allOf");
                            } else {
                                copyOKeys(ob, jsonOb);
                                jsonOb.remove("allOf");
                            }
                        } else {
                            ;
                        }
                    }
                }
            }

            private void handleChild(JSONObject jsonOb, JSONObject jsonChild) throws JSONException {
                handleChild(jsonOb, jsonChild, 1L, 1L);
            }

            private void handleChild(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
                if (jsonChild.has("objType")) {
                    String objType = jsonChild.getString("objType");
                    switch(objType) {
                        case "elementType":
                            handleElement(jsonOb, jsonChild, minItems, maxItems);
                            break;
                        case "attributeType":
                            handleAttribute(jsonOb, jsonChild, minItems, maxItems);
                            break;
                        case "choiceType":
                            handleChoice(jsonOb, jsonChild, minItems, maxItems);
                            break;
                        case "sequenceType":
                            handleSequence(jsonOb, jsonChild, minItems, maxItems);
                            break;
                        case "simpleUnionType":
                            handleUnion(jsonOb, jsonChild, minItems, maxItems);
                            break;
                        case "complexContentExtensionType":
                            handleExtension(jsonOb, jsonChild, minItems, maxItems);
                            break;
                        case "allType":
                        case "groupType":
                        case "complexType":
                        case "simpleRestrictionType":
                        case "simpleType":
                            if (jsonChild.has("children")) {
                                JSONArray children = jsonChild.getJSONArray("children");
                                for (int i = 0; i < children.length(); i++) {
                                    handleChild(jsonOb, (JSONObject) children.get(i), minItems, maxItems);
                                }
                            } else {
                                System.out.println("ObjType: " + objType + " has no children");
                            }
                            break;
                        default:
                            System.out.println("Unhandled objType: " + objType);
                            break;
                    }
                } else {
                    copyOKeys(jsonChild, jsonOb);
                }
                // remove pattern for integer (not supported)
                if (jsonOb.has("type") && jsonOb.has("pattern")) {
                    if (!jsonOb.getString("type").equals("string")) {
                        jsonOb.remove("pattern");
                    }
                }
            }

            private void copyOKeys(JSONObject from, JSONObject to) throws JSONException {
                @SuppressWarnings("unchecked") Iterator<String> it = from.keys();
                while (it.hasNext()) {
                    String pkey = it.next();
                    if (!pkey.isEmpty()) {
                        to.put(pkey, from.get(pkey));
                    }
                }
            }

            private void copyAKeys(JSONArray from, JSONArray to) throws JSONException {
                for (int i = 0; i < from.length(); i++) {
                    to.put(from.get(i));
                }
            }

            private void handleRefs(Object ob) {
                try {
                    if (ob instanceof JSONObject) {
                        JSONObject jsonOb = (JSONObject) ob;
                        if (jsonOb.has("$ref")) {
                            String ref = jsonOb.getString("$ref");
                            JSONObject refObject = getRefObject(ref);
                            if (!refObject.has("$ref")) {
                                jsonOb.remove("$ref");
                                copyOKeys(refObject, jsonOb);
                            }
                        }
                        @SuppressWarnings("unchecked") Iterator<String> it = jsonOb.keys();
                        while (it.hasNext()) {
                            String pkey = it.next();
                            handleRefs(jsonOb.get(pkey));
                        }
                        if (jsonOb.has("allOf")) {
                            merge(jsonOb);
                        }
                    } else if (ob instanceof JSONArray) {
                        JSONArray jsonArray = (JSONArray) ob;
                        for (int i = 0; i < jsonArray.length(); i++) {
                            handleRefs(jsonArray.get(i));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            private void handle(JSONObject jParent) {
                try {
                    if (jParent != null) {
                        if (!jParent.has("value")) {
                            JSONObject value = new JSONObject();
                            jParent.put("value", value);
                            JSONObject jsonOb = jParent.getJSONObject("value");
                            if (jParent.has("children")) {
                                JSONArray children = jParent.getJSONArray("children");
                                int len = children.length();
                                for (int i = 0; i < len; i++) {
                                    handleChild(jsonOb, (JSONObject) children.get(i));
                                    merge(jsonOb);
                                }
                            } else {
                            // 
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Engine.logEngine.warn("(JSonSchemaUtils) Unexpected exception while generating jsonchema models", e);
                }
            }

            @Override
            protected void walkChoice(XmlSchema xmlSchema, XmlSchemaChoice obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                JSONObject jElement = new JSONObject();
                try {
                    jElement.put("objType", "choiceType").put("minOccurs", obj.getMinOccurs()).put("maxOccurs", obj.getMaxOccurs());
                    addChild(jParent, jElement);
                    parent = jElement;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkChoice(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkGroup(XmlSchema xmlSchema, XmlSchemaGroup obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                QName qname = obj.getName();
                JSONObject jElement = new JSONObject();
                try {
                    if (isGlobal(jParent)) {
                        if (qname != null) {
                            jElement.put("objType", "groupType");
                            String id = jsonSchema.getString("id");
                            jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
                            jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
                            addGlobalObject(jParent, jElement, qname.getLocalPart());
                            parent = jElement;
                            root = jElement;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkGroup(xmlSchema, obj);
                if (isGlobal(jParent)) {
                // handle(jElement);
                }
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkGroupRef(XmlSchema xmlSchema, XmlSchemaGroupRef obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                QName refName = obj.getRefName();
                long minOccurs = obj.getMinOccurs();
                long maxOccurs = obj.getMaxOccurs();
                JSONObject jElement = new JSONObject();
                try {
                    if (refName != null) {
                        String ref = getDefinitionRef(refName);
                        if (ref != null) {
                            JSONObject jRef = null;
                            if (maxOccurs > 1) {
                                if (ref.indexOf("xsd.jsonschema") != -1) {
                                    jRef = new JSONObject().put("type", "array").put("items", new JSONObject().put("type", toOasType(refName.getLocalPart()))).put("minItems", minOccurs);
                                } else {
                                    JSONObject refObject = getRefObject(ref);
                                    jRef = new JSONObject().put("type", "array").put("items", refObject).put("minItems", minOccurs);
                                }
                            } else {
                                if (ref.indexOf("xsd.jsonschema") != -1) {
                                    jRef = new JSONObject().put("type", toOasType(refName.getLocalPart()));
                                    if (minOccurs == 0) {
                                        jRef.put("nullable", "true");
                                    }
                                } else {
                                    JSONObject refObject = getRefObject(ref);
                                    if (minOccurs == 0) {
                                        jRef = new JSONObject().put("nullable", "true").put("allOf", new JSONArray().put(refObject));
                                    } else {
                                        jRef = refObject;
                                    }
                                }
                            }
                            jElement.put("objType", "groupType").put("minOccurs", minOccurs).put("maxOccurs", maxOccurs).put("name", refName.getLocalPart());
                            addChild(jElement, jRef);
                            addChild(jParent, jElement);
                            parent = jElement;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkGroupRef(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkAll(XmlSchema xmlSchema, XmlSchemaAll obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                JSONObject jElement = new JSONObject();
                try {
                    jElement.put("objType", "allType").put("minOccurs", obj.getMinOccurs()).put("maxOccurs", obj.getMaxOccurs());
                    addChild(jParent, jElement);
                    parent = jElement;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkAll(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkSequence(XmlSchema xmlSchema, XmlSchemaSequence obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                JSONObject jElement = new JSONObject();
                try {
                    jElement.put("objType", "sequenceType").put("minOccurs", obj.getMinOccurs()).put("maxOccurs", obj.getMaxOccurs());
                    addChild(jParent, jElement);
                    parent = jElement;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkSequence(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkElement(XmlSchema xmlSchema, XmlSchemaElement obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                String name = obj.getName();
                QName qname = obj.getQName();
                QName refName = obj.getRefName();
                QName typeName = obj.getSchemaTypeName();
                XmlSchemaType xmlSchemaType = obj.getSchemaType();
                long minOccurs = obj.getMinOccurs();
                long maxOccurs = obj.getMaxOccurs();
                JSONObject jElement = new JSONObject();
                try {
                    if (isGlobal(jParent)) {
                        jElement.put("objType", "elementType");
                    } else {
                        jElement.put("objType", "elementType").put("minOccurs", minOccurs).put("maxOccurs", maxOccurs);
                    }
                    if (refName == null && typeName == null) {
                    // pass though
                    } else {
                        QName rname = refName != null ? refName : (typeName != null ? typeName : (xmlSchemaType != null ? xmlSchemaType.getQName() : new QName("")));
                        String ref = getDefinitionRef(rname);
                        if (ref != null) {
                            if (name.isEmpty()) {
                                name = rname.getLocalPart();
                            }
                            if (ref.indexOf("xsd.jsonschema") != -1) {
                                addChild(jElement, new JSONObject().put("type", toOasType(rname.getLocalPart())));
                            } else {
                                JSONObject refObject = getRefObject(ref);
                                addChild(jElement, refObject);
                            }
                        }
                    }
                    jElement.put("name", name);
                    if (isGlobal(jParent)) {
                        if (qname != null) {
                            String id = jsonSchema.getString("id");
                            jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
                            jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
                        }
                        addGlobalObject(jParent, jElement, name);
                        parent = jElement;
                        root = jElement;
                    } else {
                        addChild(jParent, jElement);
                        parent = jElement;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkElement(xmlSchema, obj);
                if (isGlobal(jParent)) {
                // handle(jElement);
                }
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkAny(XmlSchema xmlSchema, XmlSchemaAny obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                JSONObject jElement = new JSONObject();
                try {
                    JSONObject value = new JSONObject().put("type", "string").put("description", "any element");
                    jElement.put("objType", "elementType").put("minOccurs", 1).put("maxOccurs", 1).put("name", "any").put("value", value);
                    jParent.put("additionalProperties", true);
                    addChild(jParent, jElement);
                    parent = jElement;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkAny(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkAnyAttribute(XmlSchema xmlSchema, XmlSchemaAnyAttribute obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                JSONObject jElement = new JSONObject();
                try {
                    JSONObject value = new JSONObject().put("type", "string").put("xml", new JSONObject().put("attribute", true)).put("description", "any attribute");
                    jElement.put("objType", "attributeType").put("minOccurs", 0).put("name", "any").put("value", value);
                    addChild(jParent, jElement);
                    parent = jElement;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkAnyAttribute(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkAppInfo(XmlSchema xmlSchema, XmlSchemaAppInfo item) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                try {
                    String description = "";
                    NodeList nodeList = item.getMarkup();
                    for (int i = 0; i < nodeList.getLength(); i++) {
                        Node node = nodeList.item(i);
                        if (node.getNodeType() == Node.TEXT_NODE) {
                            description += node.getNodeValue();
                        }
                    }
                    if (!description.isEmpty()) {
                        addChild(jParent, new JSONObject().put("title", description));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkAppInfo(xmlSchema, item);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkDocumentation(XmlSchema xmlSchema, XmlSchemaDocumentation item) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                try {
                    String description = "";
                    NodeList nodeList = item.getMarkup();
                    for (int i = 0; i < nodeList.getLength(); i++) {
                        Node node = nodeList.item(i);
                        if (node.getNodeType() == Node.TEXT_NODE) {
                            description += node.getNodeValue();
                        }
                    }
                    if (!description.isEmpty()) {
                        addChild(jParent, new JSONObject().put("description", description));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkDocumentation(xmlSchema, item);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkAttribute(XmlSchema xmlSchema, XmlSchemaAttribute obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                String name = obj.getName();
                QName qname = obj.getQName();
                QName refName = obj.getRefName();
                QName typeName = obj.getSchemaTypeName();
                XmlSchemaSimpleType xmlSchemaSimpleType = obj.getSchemaType();
                boolean isRequired = obj.getUse().equals(XmlSchemaUtils.attributeUseRequired);
                JSONObject jElement = new JSONObject();
                try {
                    jElement.put("objType", "attributeType").put("minOccurs", isRequired ? 1 : 0);
                    if (refName == null && typeName == null) {
                    // pass through
                    } else {
                        QName rname = refName != null ? refName : (typeName != null ? typeName : (xmlSchemaSimpleType != null ? xmlSchemaSimpleType.getQName() : new QName("")));
                        String ref = getDefinitionRef(rname);
                        if (ref != null) {
                            if (name.isEmpty()) {
                                name = rname.getLocalPart();
                            }
                            JSONObject value = new JSONObject();
                            if (ref.indexOf("xsd.jsonschema") != -1) {
                                value.put("type", toOasType(rname.getLocalPart()));
                                value.put("xml", new JSONObject().put("attribute", true));
                            } else {
                                JSONObject refObject = getRefObject(ref);
                                copyOKeys(refObject, value);
                                value.put("xml", new JSONObject().put("attribute", true));
                            }
                            addChild(jElement, value);
                        }
                    }
                    jElement.put("name", name);
                    if (isGlobal(jParent)) {
                        if (qname != null) {
                            String id = jsonSchema.getString("id");
                            jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
                            jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
                        }
                        addGlobalObject(jParent, jElement, name);
                        parent = jElement;
                        root = jElement;
                    } else {
                        addChild(jParent, jElement);
                        parent = jElement;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkAttribute(xmlSchema, obj);
                if (isGlobal(jParent)) {
                // handle(jElement);
                }
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkAttributeGroup(XmlSchema xmlSchema, XmlSchemaAttributeGroup obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                QName qname = obj.getName();
                JSONObject jElement = new JSONObject();
                try {
                    if (isGlobal(jParent)) {
                        if (qname != null) {
                            jElement.put("objType", "attributeGroupType");
                            String id = jsonSchema.getString("id");
                            jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
                            jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
                            addGlobalObject(jParent, jElement, qname.getLocalPart());
                            parent = jElement;
                            root = jElement;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkAttributeGroup(xmlSchema, obj);
                if (isGlobal(jParent)) {
                // handle(jElement);
                }
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkAttributeGroupRef(XmlSchema xmlSchema, XmlSchemaAttributeGroupRef obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                QName refName = obj.getRefName();
                JSONObject jElement = new JSONObject();
                try {
                    if (refName != null) {
                        String ref = getDefinitionRef(refName);
                        if (ref != null) {
                            jElement.put("objType", "attributeGroupType").put("name", refName.getLocalPart());
                            addChild(jElement, new JSONObject().put("$ref", ref));
                            addChild(jParent, jElement);
                            parent = jElement;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkAttributeGroupRef(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkSimpleContent(XmlSchema xmlSchema, XmlSchemaSimpleContent obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                QName qname = null;
                XmlSchemaContent xmlSchemaContent = obj.getContent();
                if (xmlSchemaContent instanceof XmlSchemaSimpleContentRestriction) {
                    qname = ((XmlSchemaSimpleContentRestriction) xmlSchemaContent).getBaseTypeName();
                } else if (xmlSchemaContent instanceof XmlSchemaSimpleContentExtension) {
                    qname = ((XmlSchemaSimpleContentExtension) xmlSchemaContent).getBaseTypeName();
                }
                JSONObject jElement = new JSONObject();
                try {
                    if (qname != null) {
                        String ref = getDefinitionRef(qname);
                        if (ref != null) {
                            if (ref.indexOf("xsd.jsonschema") != -1) {
                                jElement.put("objType", "elementType").put("minOccurs", 1).put("maxOccurs", 1).put("name", "text").put("value", new JSONObject().put("type", toOasType(qname.getLocalPart())));
                            } else {
                                JSONObject refObject = getRefObject(ref);
                                jElement.put("objType", "elementType").put("minOccurs", 1).put("maxOccurs", 1).put("name", "text").put("value", refObject);
                            }
                            addChild(jParent, jElement);
                            parent = jElement;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkSimpleContent(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkSimpleType(XmlSchema xmlSchema, XmlSchemaSimpleType obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                QName qname = obj.getQName();
                QName bname = obj.getBaseSchemaTypeName();
                JSONObject jElement = new JSONObject();
                try {
                    jElement.put("objType", "simpleType");
                    if (bname != null) {
                        String ref = getDefinitionRef(bname);
                        if (ref != null) {
                            if (ref.indexOf("xsd.jsonschema") != -1) {
                                jElement.put("value", new JSONObject().put("type", toOasType(bname.getLocalPart())));
                            } else {
                                JSONObject refObject = getRefObject(ref);
                                jElement.put("value", refObject);
                            }
                        }
                    }
                    if (isGlobal(jParent)) {
                        if (qname != null) {
                            String id = jsonSchema.getString("id");
                            jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
                            jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
                        }
                        addGlobalObject(jParent, jElement, obj.getName());
                        parent = jElement;
                        root = jElement;
                    } else {
                        addChild(jParent, jElement);
                        parent = jElement;
                    }
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
                super.walkSimpleType(xmlSchema, obj);
                if (isGlobal(jParent)) {
                // handle(jElement);
                }
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkSimpleTypeRestriction(XmlSchema xmlSchema, XmlSchemaSimpleTypeRestriction obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                QName qname = obj.getBaseTypeName();
                JSONObject jElement = new JSONObject();
                try {
                    if (qname != null) {
                        String ref = getDefinitionRef(qname);
                        if (ref != null) {
                            jElement.put("objType", "simpleRestrictionType");
                            if (ref.indexOf("xsd.jsonschema") != -1) {
                                addChild(jElement, new JSONObject().put("type", toOasType(qname.getLocalPart())));
                            } else {
                                JSONObject refObject = getRefObject(ref);
                                addChild(jElement, refObject);
                            }
                            addChild(jParent, jElement);
                            parent = jElement;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkSimpleTypeRestriction(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkSimpleTypeUnion(XmlSchema xmlSchema, XmlSchemaSimpleTypeUnion obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                JSONObject jElement = new JSONObject();
                try {
                    jElement.put("objType", "simpleUnionType");
                    QName[] members = obj.getMemberTypesQNames();
                    if (members != null) {
                        for (QName qname : members) {
                            String ref = getDefinitionRef(qname);
                            if (ref.indexOf("xsd.jsonschema") != -1) {
                                addChild(jElement, new JSONObject().put("type", toOasType(qname.getLocalPart())));
                            } else {
                                parent = jElement;
                                walkByTypeName(xmlSchema, qname);
                            }
                        }
                    } else {
                        parent = jElement;
                        super.walkSimpleTypeUnion(xmlSchema, obj);
                    }
                    addChild(jParent, jElement);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkFacets(XmlSchema xmlSchema, XmlSchemaObjectCollection facets) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                JSONArray array = new JSONArray();
                boolean arrayWithDuplicates = false;
                for (int i = 0; i < facets.getCount(); i++) {
                    XmlSchemaFacet facet = (XmlSchemaFacet) facets.getItem(i);
                    Object value = facet.getValue();
                    try {
                        JSONObject jFacet = new JSONObject();
                        if (facet instanceof XmlSchemaEnumerationFacet) {
                            if ("".equals(value)) {
                                jFacet.put("minLength", 0);
                                array.put(value);
                                array.put(JSONObject.NULL);
                            } else {
                                if (array.join(",").toLowerCase().indexOf(String.valueOf(value).toLowerCase()) != -1) {
                                    arrayWithDuplicates = true;
                                }
                                array.put(value);
                            }
                            if (i < facets.getCount() - 1) {
                                continue;
                            }
                        } else if (facet instanceof XmlSchemaPatternFacet) {
                            jFacet.put("pattern", value);
                        } else if (facet instanceof XmlSchemaLengthFacet) {
                            jFacet.put("length", Integer.valueOf(value.toString(), 10));
                        } else if (facet instanceof XmlSchemaMinLengthFacet) {
                            jFacet.put("minLength", Integer.valueOf(value.toString(), 10));
                        } else if (facet instanceof XmlSchemaMaxLengthFacet) {
                            jFacet.put("maxLength", Integer.valueOf(value.toString(), 10));
                        } else if (facet instanceof XmlSchemaMaxExclusiveFacet) {
                            jFacet.put("maximum", Integer.valueOf(value.toString(), 10)).put("exclusiveMaximum", true);
                        } else if (facet instanceof XmlSchemaMaxInclusiveFacet) {
                            jFacet.put("maximum", Integer.valueOf(value.toString(), 10)).put("exclusiveMaximum", false);
                        } else if (facet instanceof XmlSchemaMinExclusiveFacet) {
                            jFacet.put("minimum", Integer.valueOf(value.toString(), 10)).put("exclusiveMinimum", true);
                        } else if (facet instanceof XmlSchemaMinInclusiveFacet) {
                            jFacet.put("minimum", Integer.valueOf(value.toString(), 10)).put("exclusiveMinimum", false);
                        } else if (facet instanceof XmlSchemaTotalDigitsFacet) {
                            jFacet.put("maxLength", Integer.valueOf(value.toString(), 10));
                        }
                        if (facet instanceof XmlSchemaEnumerationFacet) {
                            if (array.length() > 0) {
                                jFacet.put("nullable", true);
                                if (facets.getCount() > 1) {
                                    if (arrayWithDuplicates) {
                                        jFacet.put("pattern", array.join("|"));
                                    } else {
                                        jFacet.put("enum", array);
                                    }
                                }
                            }
                        }
                        if (jFacet.length() > 0) {
                            addChild(jParent, jFacet);
                        }
                        parent = jFacet;
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkSimpleTypeList(XmlSchema xmlSchema, XmlSchemaSimpleTypeList obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                QName qname = obj.getItemTypeName();
                JSONObject jElement = new JSONObject();
                try {
                    if (qname != null) {
                        String ref = getDefinitionRef(qname);
                        if (ref != null) {
                            JSONObject value = new JSONObject();
                            if (ref.indexOf("xsd.jsonschema") != -1) {
                                value.put("type", "array").put("items", new JSONObject().put("type", toOasType(qname.getLocalPart())));
                            } else {
                                JSONObject refObject = getRefObject(ref);
                                value.put("type", "array").put("items", refObject);
                            }
                            jElement.put("objType", "simpleListType").put("value", value);
                            addChild(jParent, jElement);
                            parent = jElement;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkSimpleTypeList(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkComplexContentExtension(XmlSchema xmlSchema, XmlSchemaComplexContentExtension obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                QName baseTypeName = obj.getBaseTypeName();
                JSONObject jElement = new JSONObject();
                try {
                    if (baseTypeName != null) {
                        String ref = getDefinitionRef(baseTypeName);
                        if (ref != null) {
                            jElement.put("objType", "complexContentExtensionType");
                            addChild(jElement, new JSONObject().put("$ref", ref));
                            addChild(jParent, jElement);
                            parent = jElement;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkComplexContentExtension(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkComplexContentRestriction(XmlSchema xmlSchema, XmlSchemaComplexContentRestriction obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                QName baseTypeName = obj.getBaseTypeName();
                JSONObject jElement = new JSONObject();
                try {
                    if (baseTypeName != null) {
                        String ref = getDefinitionRef(baseTypeName);
                        if (ref != null) {
                            jElement.put("objType", "complexContentRestrictionType");
                            addChild(jElement, new JSONObject().put("$ref", ref));
                            addChild(jParent, jElement);
                            parent = jElement;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkComplexContentRestriction(xmlSchema, obj);
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walkComplexType(XmlSchema xmlSchema, XmlSchemaComplexType obj) {
                JSONObject jParent = parent;
                JSONObject jRoot = root;
                QName qname = obj.getQName();
                JSONObject jElement = new JSONObject();
                try {
                    jElement.put("objType", "complexType");
                    if (obj.isMixed()) {
                        JSONObject jText = new JSONObject();
                        jText.put("objType", "elementType").put("name", "text").put("minOccurs", 0).put("maxOccurs", 1).put("value", new JSONObject().put("description", "the mixed content string").put("type", "string"));
                        addChild(jElement, jText);
                    }
                    if (isGlobal(jParent)) {
                        if (qname != null) {
                            String id = jsonSchema.getString("id");
                            jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
                            jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
                        }
                        addGlobalObject(jParent, jElement, obj.getName());
                        parent = jElement;
                        root = jElement;
                    } else {
                        addChild(jParent, jElement);
                        parent = jElement;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.walkComplexType(xmlSchema, obj);
                if (isGlobal(jParent)) {
                // handle(jElement);
                }
                parent = jParent;
                root = jRoot;
            }

            @Override
            protected void walk(XmlSchema xmlSchema) {
                XmlSchemaObjectCollection items = xmlSchema.getItems();
                for (Iterator<XmlSchemaObject> i = GenericUtils.cast(items.getIterator()); i.hasNext(); ) {
                    XmlSchemaObject obj = i.next();
                    if (obj instanceof XmlSchemaInclude) {
                        walkInclude(xmlSchema, (XmlSchemaInclude) obj);
                    } else if (obj instanceof XmlSchemaImport) {
                        walkImport(xmlSchema, (XmlSchemaImport) obj);
                    }
                }
                // walk simple types first
                for (Iterator<XmlSchemaType> i = GenericUtils.cast(xmlSchema.getSchemaTypes().getValues()); i.hasNext(); ) {
                    XmlSchemaObject obj = i.next();
                    if (obj instanceof XmlSchemaSimpleType) {
                        walk(xmlSchema, obj);
                    }
                }
                // walk others
                for (Iterator<XmlSchemaType> i = GenericUtils.cast(xmlSchema.getSchemaTypes().getValues()); i.hasNext(); ) {
                    XmlSchemaObject obj = i.next();
                    if (!(obj instanceof XmlSchemaSimpleType)) {
                        walk(xmlSchema, obj);
                    }
                }
                for (Iterator<XmlSchemaObject> i = GenericUtils.cast(items.getIterator()); i.hasNext(); ) {
                    XmlSchemaObject obj = i.next();
                    if (!(obj instanceof XmlSchemaInclude || obj instanceof XmlSchemaImport || obj instanceof XmlSchemaType)) {
                        walk(xmlSchema, obj);
                    }
                }
                List<String> toRemove = new ArrayList<String>();
                @SuppressWarnings("unchecked") Iterator<String> it = definitions.keys();
                while (it.hasNext()) {
                    String pkey = it.next();
                    try {
                        JSONObject jsonOb = definitions.getJSONObject(pkey);
                        handle(jsonOb);
                        JSONObject value = jsonOb.getJSONObject("value");
                        if (value.has("properties") || value.has("allOf") || value.has("$ref")) {
                            jsonOb.put("type", "object");
                            handleRefs(value);
                        }
                        copyOKeys(value, jsonOb);
                        if (jsonOb.has("type")) {
                            if (!jsonOb.getString("type").equals("object")) {
                                toRemove.add(pkey);
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                for (String pkey : toRemove) {
                    definitions.remove(pkey);
                }
                boolean debug = false;
                if (!debug) {
                    try {
                        @SuppressWarnings("unchecked") Iterator<String> ita = definitions.keys();
                        while (ita.hasNext()) {
                            JSONObject jsonOb = definitions.getJSONObject(ita.next());
                            jsonOb.remove("objKey");
                            jsonOb.remove("objType");
                            jsonOb.remove("QName");
                            jsonOb.remove("children");
                            jsonOb.remove("name");
                            jsonOb.remove("maxOccurs");
                            jsonOb.remove("minOccurs");
                            jsonOb.remove("value");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                refs.clear();
            }
        }.walk(xmlSchema);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return jsonSchema;
}
Also used : HashMap(java.util.HashMap) Node(org.w3c.dom.Node) XmlSchemaSimpleTypeRestriction(org.apache.ws.commons.schema.XmlSchemaSimpleTypeRestriction) XmlSchemaSimpleTypeUnion(org.apache.ws.commons.schema.XmlSchemaSimpleTypeUnion) XmlSchemaSimpleTypeList(org.apache.ws.commons.schema.XmlSchemaSimpleTypeList) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) XmlSchemaSimpleTypeList(org.apache.ws.commons.schema.XmlSchemaSimpleTypeList) XmlSchemaFacet(org.apache.ws.commons.schema.XmlSchemaFacet) XmlSchemaMinInclusiveFacet(org.apache.ws.commons.schema.XmlSchemaMinInclusiveFacet) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) NodeList(org.w3c.dom.NodeList) XmlSchemaAttributeGroup(org.apache.ws.commons.schema.XmlSchemaAttributeGroup) XmlSchemaAttribute(org.apache.ws.commons.schema.XmlSchemaAttribute) XmlSchemaSimpleContentRestriction(org.apache.ws.commons.schema.XmlSchemaSimpleContentRestriction) XmlSchemaTotalDigitsFacet(org.apache.ws.commons.schema.XmlSchemaTotalDigitsFacet) XmlSchemaGroup(org.apache.ws.commons.schema.XmlSchemaGroup) XmlSchemaMaxInclusiveFacet(org.apache.ws.commons.schema.XmlSchemaMaxInclusiveFacet) JSONObject(org.codehaus.jettison.json.JSONObject) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaMinLengthFacet(org.apache.ws.commons.schema.XmlSchemaMinLengthFacet) XmlSchemaAll(org.apache.ws.commons.schema.XmlSchemaAll) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) XmlSchemaComplexContentRestriction(org.apache.ws.commons.schema.XmlSchemaComplexContentRestriction) XmlSchemaInclude(org.apache.ws.commons.schema.XmlSchemaInclude) JSONObject(org.codehaus.jettison.json.JSONObject) XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) XmlSchemaAnyAttribute(org.apache.ws.commons.schema.XmlSchemaAnyAttribute) XmlSchemaMaxLengthFacet(org.apache.ws.commons.schema.XmlSchemaMaxLengthFacet) XmlSchemaAttributeGroupRef(org.apache.ws.commons.schema.XmlSchemaAttributeGroupRef) XmlSchemaSimpleContent(org.apache.ws.commons.schema.XmlSchemaSimpleContent) XmlSchemaPatternFacet(org.apache.ws.commons.schema.XmlSchemaPatternFacet) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) Iterator(java.util.Iterator) XmlSchemaChoice(org.apache.ws.commons.schema.XmlSchemaChoice) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) XmlSchemaMinExclusiveFacet(org.apache.ws.commons.schema.XmlSchemaMinExclusiveFacet) XmlSchemaEnumerationFacet(org.apache.ws.commons.schema.XmlSchemaEnumerationFacet) XmlSchemaAppInfo(org.apache.ws.commons.schema.XmlSchemaAppInfo) XmlSchemaObjectCollection(org.apache.ws.commons.schema.XmlSchemaObjectCollection) XmlSchemaComplexContentExtension(org.apache.ws.commons.schema.XmlSchemaComplexContentExtension) XmlSchemaSimpleContentExtension(org.apache.ws.commons.schema.XmlSchemaSimpleContentExtension) QName(javax.xml.namespace.QName) JSONArray(org.codehaus.jettison.json.JSONArray) JSONException(org.codehaus.jettison.json.JSONException) XmlSchemaGroupRef(org.apache.ws.commons.schema.XmlSchemaGroupRef) XmlSchemaDocumentation(org.apache.ws.commons.schema.XmlSchemaDocumentation) XmlSchemaAny(org.apache.ws.commons.schema.XmlSchemaAny) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) JSONException(org.codehaus.jettison.json.JSONException) XmlSchemaMaxExclusiveFacet(org.apache.ws.commons.schema.XmlSchemaMaxExclusiveFacet) XmlSchemaContent(org.apache.ws.commons.schema.XmlSchemaContent) XmlSchemaLengthFacet(org.apache.ws.commons.schema.XmlSchemaLengthFacet) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType)

Example 12 with NamespaceMap

use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.

the class ImportRepairTest method newSchema.

private XmlSchema newSchema(String uri) {
    XmlSchema schema = collection.newXmlSchemaInCollection(uri);
    NamespaceMap map = new NamespaceMap();
    map.add("", XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schema.setNamespaceContext(map);
    return schema;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap)

Example 13 with NamespaceMap

use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.

the class AegisDatabinding method createSchemas.

private void createSchemas(Service s, Set<AegisType> deps) {
    Map<String, Set<AegisType>> tns2Type = new HashMap<>();
    for (AegisType t : deps) {
        String ns = t.getSchemaType().getNamespaceURI();
        Set<AegisType> types = tns2Type.get(ns);
        if (types == null) {
            types = new HashSet<>();
            tns2Type.put(ns, types);
        }
        types.add(t);
    }
    for (ServiceInfo si : s.getServiceInfos()) {
        SchemaCollection col = si.getXmlSchemaCollection();
        if (col.getXmlSchemas().length > 1) {
            // someone has already filled in the types
            continue;
        }
    }
    Map<String, String> namespaceMap = getDeclaredNamespaceMappings();
    for (ServiceInfo si : s.getServiceInfos()) {
        // these two must be recalculated per-service-info!
        boolean needXmimeSchema = false;
        boolean needTypesSchema = false;
        for (Map.Entry<String, Set<AegisType>> entry : tns2Type.entrySet()) {
            String schemaNamespaceUri = entry.getKey();
            if (Constants.URI_2001_SCHEMA_XSD.equals(schemaNamespaceUri)) {
                continue;
            }
            if (AegisContext.UTILITY_TYPES_SCHEMA_NS.equals(schemaNamespaceUri)) {
                // we handle this separately.
                continue;
            }
            if (AbstractXOPType.XML_MIME_NS.equals(schemaNamespaceUri)) {
                // similiarly.
                continue;
            }
            SchemaInfo schemaInfo = si.addNewSchema(entry.getKey());
            XmlSchema schema = schemaInfo.getSchema();
            NamespaceMap xmlsNamespaceMap = new NamespaceMap();
            // user-requested prefix mappings.
            if (namespaceMap != null) {
                for (Map.Entry<String, String> e : namespaceMap.entrySet()) {
                    xmlsNamespaceMap.add(e.getValue(), e.getKey());
                }
            }
            // tns: is conventional, and besides we have unit tests that are hardcoded to it.
            if (!xmlsNamespaceMap.containsKey(WSDLConstants.CONVENTIONAL_TNS_PREFIX) && // if some wants something other than TNS, they get it.
            !xmlsNamespaceMap.containsValue(entry.getKey())) {
                xmlsNamespaceMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, entry.getKey());
            }
            // ditto for xsd: instead of just namespace= for the schema schema.
            if (!xmlsNamespaceMap.containsKey("xsd") && !xmlsNamespaceMap.containsValue(Constants.URI_2001_SCHEMA_XSD)) {
                xmlsNamespaceMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
            }
            schema.setNamespaceContext(xmlsNamespaceMap);
            schema.setTargetNamespace(entry.getKey());
            schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
            schema.setAttributeFormDefault(XmlSchemaForm.QUALIFIED);
            for (AegisType t : entry.getValue()) {
                try {
                    t.writeSchema(schema);
                } catch (XmlSchemaException ex) {
                    QName name = t.getSchemaType();
                    String expected = " Schema for namespace '" + name.getNamespaceURI() + "' already contains type '" + name.getLocalPart() + "'";
                    String message = ex.getMessage();
                    if (expected.equals(message)) {
                        continue;
                    }
                    throw ex;
                }
            }
            if (schemaImportsXmime(schema)) {
                needXmimeSchema = true;
            }
            if (AegisContext.schemaImportsUtilityTypes(schema)) {
                needTypesSchema = true;
            }
        }
        if (needXmimeSchema) {
            XmlSchema schema = aegisContext.addXmimeSchemaDocument(si.getXmlSchemaCollection().getXmlSchemaCollection());
            SchemaInfo schemaInfo = new SchemaInfo(schema.getTargetNamespace());
            schemaInfo.setSchema(schema);
            si.addSchema(schemaInfo);
        }
        if (needTypesSchema) {
            XmlSchema schema = aegisContext.addTypesSchemaDocument(si.getXmlSchemaCollection().getXmlSchemaCollection());
            SchemaInfo schemaInfo = new SchemaInfo(schema.getTargetNamespace());
            schemaInfo.setSchema(schema);
            si.addSchema(schemaInfo);
        }
        // it's quite likely that the code in Aegis missed at least one ...
        si.getXmlSchemaCollection().addCrossImports();
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) AegisType(org.apache.cxf.aegis.type.AegisType) XmlSchemaException(org.apache.ws.commons.schema.XmlSchemaException) QName(javax.xml.namespace.QName) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) HashMap(java.util.HashMap) Map(java.util.Map) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 14 with NamespaceMap

use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.

the class ReflectionServiceFactoryBean method getOrCreateSchema.

private SchemaInfo getOrCreateSchema(ServiceInfo serviceInfo, String namespaceURI, boolean qualified) {
    for (SchemaInfo s : serviceInfo.getSchemas()) {
        if (s.getNamespaceURI().equals(namespaceURI)) {
            return s;
        }
    }
    SchemaInfo schemaInfo = new SchemaInfo(namespaceURI);
    SchemaCollection col = serviceInfo.getXmlSchemaCollection();
    XmlSchema schema = col.getSchemaByTargetNamespace(namespaceURI);
    if (schema != null) {
        schemaInfo.setSchema(schema);
        serviceInfo.addSchema(schemaInfo);
        return schemaInfo;
    }
    schema = col.newXmlSchemaInCollection(namespaceURI);
    if (qualified) {
        schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
    }
    schemaInfo.setSchema(schema);
    Map<String, String> explicitNamespaceMappings = this.getDataBinding().getDeclaredNamespaceMappings();
    if (explicitNamespaceMappings == null) {
        explicitNamespaceMappings = Collections.emptyMap();
    }
    NamespaceMap nsMap = new NamespaceMap();
    for (Map.Entry<String, String> mapping : explicitNamespaceMappings.entrySet()) {
        nsMap.add(mapping.getValue(), mapping.getKey());
    }
    if (!explicitNamespaceMappings.containsKey(WSDLConstants.NS_SCHEMA_XSD)) {
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
    }
    if (!explicitNamespaceMappings.containsKey(serviceInfo.getTargetNamespace())) {
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, serviceInfo.getTargetNamespace());
    }
    schema.setNamespaceContext(nsMap);
    serviceInfo.addSchema(schemaInfo);
    return schemaInfo;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 15 with NamespaceMap

use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.

the class ObjectReferenceVisitor method addWSAddressingImport.

private void addWSAddressingImport(XmlSchema s) {
    boolean alreadyImported = false;
    for (XmlSchemaExternal ext : s.getExternals()) {
        if (ext instanceof XmlSchemaImport) {
            XmlSchemaImport schemaImport = (XmlSchemaImport) ext;
            if (schemaImport.getNamespace().equals(ReferenceConstants.WSADDRESSING_NAMESPACE)) {
                alreadyImported = true;
                break;
            }
        }
    }
    if (!alreadyImported) {
        // We need to add an import statement to include the WS addressing types
        XmlSchemaImport wsaImport = new XmlSchemaImport(s);
        wsaImport.setNamespace(ReferenceConstants.WSADDRESSING_NAMESPACE);
        wsaImport.setSchemaLocation(ReferenceConstants.WSADDRESSING_LOCATION);
    }
    // Add the addressing namespace to the WSDLs list of namespaces.
    definition.addNamespace(ReferenceConstants.WSADDRESSING_PREFIX, ReferenceConstants.WSADDRESSING_NAMESPACE);
    try {
        // This is used to get the correct prefix in the schema section of
        // the wsdl.  If we don't have this, then this namespace gets an
        // arbitrary prefix (e.g. ns5 instead of wsa).
        NamespaceMap nsMap = (NamespaceMap) s.getNamespaceContext();
        if (nsMap == null) {
            nsMap = new NamespaceMap();
            nsMap.add(ReferenceConstants.WSADDRESSING_PREFIX, ReferenceConstants.WSADDRESSING_NAMESPACE);
            s.setNamespaceContext(nsMap);
        } else {
            nsMap.add(ReferenceConstants.WSADDRESSING_PREFIX, ReferenceConstants.WSADDRESSING_NAMESPACE);
        }
    } catch (ClassCastException ex) {
    // Consume the exception.  It is still OK with the default prefix,
    // just not as clear.
    }
}
Also used : XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport)

Aggregations

NamespaceMap (org.apache.ws.commons.schema.utils.NamespaceMap)21 XmlSchema (org.apache.ws.commons.schema.XmlSchema)18 QName (javax.xml.namespace.QName)7 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)7 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)6 XmlSchemaImport (org.apache.ws.commons.schema.XmlSchemaImport)5 XmlSchemaObject (org.apache.ws.commons.schema.XmlSchemaObject)5 HashMap (java.util.HashMap)4 XmlSchemaCollection (org.apache.ws.commons.schema.XmlSchemaCollection)4 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)4 XmlSchemaObjectCollection (org.apache.ws.commons.schema.XmlSchemaObjectCollection)4 XmlSchemaSequence (org.apache.ws.commons.schema.XmlSchemaSequence)4 Project (com.twinsoft.convertigo.beans.core.Project)3 File (java.io.File)3 XmlSchemaSimpleType (org.apache.ws.commons.schema.XmlSchemaSimpleType)3 XmlSchemaType (org.apache.ws.commons.schema.XmlSchemaType)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 XmlSchemaWalker (com.twinsoft.convertigo.engine.util.XmlSchemaWalker)2 Field (java.lang.reflect.Field)2