Search in sources :

Example 1 with Code

use of net.jangaroo.exml.json.Code in project jangaroo-tools by CoreMedia.

the class ExmlToModelParser method fillModelAttributesFromSubElements.

private void fillModelAttributesFromSubElements(ExmlModel model, JsonObject jsonObject, Element componentNode, ConfigClass configClass) {
    List<Element> childNodes = getChildElements(componentNode);
    for (Element element : childNodes) {
        String elementName = element.getLocalName();
        if (ExmlUtils.isExmlNamespace(element.getNamespaceURI()) && "mixins".equals(elementName)) {
            for (Element mixinElement : getChildElements(element)) {
                String mixinClassName = createFullConfigClassNameFromNode(mixinElement);
                ConfigClass mixinConfigClass = getConfigClassByName(mixinClassName, mixinElement);
                fillModelAttributes(model, jsonObject, mixinElement, mixinConfigClass);
            }
            continue;
        }
        boolean isConfigTypeArray = isConfigTypeArray(configClass, elementName);
        String configMode = isConfigTypeArray ? element.getAttribute(Exmlc.EXML_MODE_ATTRIBUTE) : "";
        // Special case: if an EXML element representing a config property has attributes, it is treated as
        // having an untyped object value. Exception: it is an Array-typed property and the sole attribute is "mode".
        int attributeCount = element.getAttributes().getLength();
        if (attributeCount > 1 || attributeCount == 1 && configMode.length() == 0) {
            // it's an untyped complex object with attributes and sub-properties
            parseJavaScriptObjectProperty(jsonObject, element);
        } else {
            // derive the corresponding "at" value from the specified config mode (if any):
            Code atValue = CONFIG_MODE_TO_AT_VALUE.get(configMode);
            if (atValue != null) {
                isConfigTypeArray = true;
                if (!configClass.isExmlGenerated()) {
                    throw new ExmlcException("Non-EXML class " + configClass.getComponentClassName() + " does not support config modes.", getLineNumber(element));
                }
            }
            String directTextContent = getDirectTextContent(element);
            if (directTextContent != null) {
                ConfigAttribute configAttribute = getCfgByName(configClass, elementName);
                final Object attributeValue = getAttributeValue(directTextContent, configAttribute == null ? null : configAttribute.getType());
                jsonObject.set(elementName, attributeValue);
            } else {
                // it seems to be an array or an object
                fillJsonObjectProperty(model, jsonObject, elementName, isConfigTypeArray, getChildElements(element));
                ifContainerDefaultsThenExtractXtype(jsonObject, configClass, elementName);
            }
            // if any "at" value is specified, set the extra mode attribute (...$at):
            if (atValue != null) {
                jsonObject.set(elementName + CONFIG_MODE_AT_SUFFIX, atValue);
            }
        // empty properties are omitted if the type is not fixed to Array
        }
    }
}
Also used : ConfigClass(net.jangaroo.exml.model.ConfigClass) ConfigAttribute(net.jangaroo.exml.model.ConfigAttribute) Element(org.w3c.dom.Element) ExmlcException(net.jangaroo.exml.api.ExmlcException) JsonObject(net.jangaroo.exml.json.JsonObject) Code(net.jangaroo.exml.json.Code)

Aggregations

ExmlcException (net.jangaroo.exml.api.ExmlcException)1 Code (net.jangaroo.exml.json.Code)1 JsonObject (net.jangaroo.exml.json.JsonObject)1 ConfigAttribute (net.jangaroo.exml.model.ConfigAttribute)1 ConfigClass (net.jangaroo.exml.model.ConfigClass)1 Element (org.w3c.dom.Element)1