Element serializeAttribute()

in src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java [362:480]


    Element serializeAttribute(Document doc, XmlSchemaAttribute attributeObj, XmlSchema schema)
        throws XmlSchemaSerializerException {

        boolean refPresent = attributeObj.getRef().getTargetQName() != null;

        Element attribute = createNewElement(doc, "attribute", schema.getSchemaNamespacePrefix(),
                                             XmlSchema.SCHEMA_NS);
        if (refPresent) {
            String refName = resolveQName(attributeObj.getRef().getTargetQName(), schema);
            attribute.setAttribute("ref", refName);
        } else if (!attributeObj.isAnonymous()) {
            attribute.setAttribute("name", attributeObj.getName());
        }

        /*
         * TODO: should this be caught by refusing to allow both to be true at the same time?
         */
        if (attributeObj.getSchemaTypeName() != null && !refPresent) {
            String typeName = resolveQName(attributeObj.getSchemaTypeName(), schema);
            attribute.setAttribute("type", typeName);
        }

        if (attributeObj.getDefaultValue() != null) {
            attribute.setAttribute("default", attributeObj.getDefaultValue());
        }
        if (attributeObj.getFixedValue() != null) {
            attribute.setAttribute("fixed", attributeObj.getFixedValue());
        }

        /*
         * TODO: should this be caught by refusing to allow both to be true at the same time?
         */
        if (attributeObj.isFormSpecified() && !refPresent) {
            attribute.setAttribute("form", attributeObj.getForm().toString());
        }

        if (attributeObj.getId() != null) {
            attribute.setAttribute("id", attributeObj.getId());
        }

        if (attributeObj.getUse() != null && attributeObj.getUse() != XmlSchemaUse.NONE) {
            attribute.setAttribute("use", attributeObj.getUse().toString());
        }

        if (attributeObj.getAnnotation() != null) {
            Element annotation = serializeAnnotation(doc, attributeObj.getAnnotation(), schema);
            attribute.appendChild(annotation);
        }

        /*
         * TODO: should this be caught by refusing to allow both to be true at the same time?
         */
        if (attributeObj.getSchemaType() != null && !refPresent) {
            try {
                XmlSchemaSimpleType simpleType = attributeObj.getSchemaType();
                Element simpleTypeEl = serializeSimpleType(doc, simpleType, schema);
                attribute.appendChild(simpleTypeEl);
            } catch (ClassCastException e) {
                throw new XmlSchemaSerializerException(
                    "Only an inline simple type is allowed as an attribute's inline type");
            }
        }

        Attr[] unhandled = attributeObj.getUnhandledAttributes();

        Map<String, String> namespaces = Collections.synchronizedMap(new HashMap<String, String>());

        if (unhandled != null) {

            // this is to make the wsdl:arrayType work
            // since unhandles attributes are not handled this is a special case
            // but the basic idea is to see if there is any attibute whose value has ":"
            // if it is present then it is likely that it is a namespace prefix
            // do what is neccesary to get the real namespace for it and make
            // required changes to the prefix

            for (Attr element : unhandled) {
                String name = element.getNodeName();
                String value = element.getNodeValue();
                if ("xmlns".equals(name)) {
                    namespaces.put("", value);
                } else if (name.startsWith("xmlns")) {
                    namespaces.put(name.substring(name.indexOf(":") + 1), value);
                }
            }

            for (Attr element : unhandled) {
                String value = element.getNodeValue();
                String nodeName = element.getNodeName();
                if (value.indexOf(":") > -1 && !nodeName.startsWith("xmlns")) {
                    String prefix = value.substring(0, value.indexOf(":"));
                    String oldNamespace;
                    oldNamespace = namespaces.get(prefix);
                    if (oldNamespace != null) {
                        value = value.substring(value.indexOf(":") + 1);
                        NamespacePrefixList ctx = schema.getNamespaceContext();
                        String[] prefixes = ctx.getDeclaredPrefixes();
                        for (String pref : prefixes) {
                            String uri = ctx.getNamespaceURI(pref);
                            if (uri.equals(oldNamespace)) {
                                value = prefix + ":" + value;
                            }
                        }
                    }

                }
                if (element.getNamespaceURI() != null) {
                    attribute.setAttributeNS(element.getNamespaceURI(), nodeName, value);
                } else {
                    attribute.setAttribute(nodeName, value);
                }
            }
        }

        // process extension
        processExtensibilityComponents(attributeObj, attribute);

        return attribute;
    }