Search in sources :

Example 1 with ConfigAttribute

use of net.jangaroo.exml.model.ConfigAttribute in project jangaroo-tools by CoreMedia.

the class ExmlToModelParser method fillModelAttributes.

private void fillModelAttributes(ExmlModel model, JsonObject jsonObject, Element componentNode, ConfigClass configClass) {
    NamedNodeMap attributes = componentNode.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attribute = (Attr) attributes.item(i);
        String attributeName = attribute.getLocalName();
        String attributeValue = attribute.getValue();
        ConfigAttribute configAttribute = getCfgByName(configClass, attributeName);
        jsonObject.set(attributeName, getAttributeValue(attributeValue, configAttribute == null ? null : configAttribute.getType()));
    }
    fillModelAttributesFromSubElements(model, jsonObject, componentNode, configClass);
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) ConfigAttribute(net.jangaroo.exml.model.ConfigAttribute) Attr(org.w3c.dom.Attr)

Example 2 with ConfigAttribute

use of net.jangaroo.exml.model.ConfigAttribute in project jangaroo-tools by CoreMedia.

the class ExmlToModelParserTest method testConfigDefaultValues.

@Test
public void testConfigDefaultValues() throws Exception {
    setUp("testNamespace.config");
    ExmlToModelParser exmlToModelParser = new ExmlToModelParser(getConfigClassRegistry());
    ExmlModel model = exmlToModelParser.parse(getFile("/testPackage/TestComponentWithCfgDefaults.exml"));
    List<ConfigAttribute> cfgs = model.getConfigClass().getDirectCfgs();
    Assert.assertEquals(9, cfgs.size());
    JsonObject expectedJsonObject = new JsonObject("propertyWithLiteralDefault", "foobar", "propertyWithExpressionDefault", JsonObject.code("'foo' + 'bar'"), "propertyWithDefaultElement", new JsonObject("xtype", "button", "text", "click me!"), "propertyWithDefaultElementUsingConfig", new JsonObject("xtype", "button", "text", JsonObject.code("config.title + '!'")), "arrayPropertyWithDefaultElement", new JsonArray(new JsonObject("xtype", "button", "text", "button1"), new JsonObject("xtype", "button", "text", "button2")), "propertyWithInterfaceAndDefault", JsonObject.code(" new TestImpl() "), "propertyFromOtherPackage", JsonObject.code(" new SomeOtherClass('lala') "));
    System.out.println(model.getJsonObject().toString(2));
    Assert.assertEquals(expectedJsonObject.toString(2), model.getCfgDefaults().toString(2));
}
Also used : JsonArray(net.jangaroo.exml.json.JsonArray) ExmlModel(net.jangaroo.exml.model.ExmlModel) ConfigAttribute(net.jangaroo.exml.model.ConfigAttribute) JsonObject(net.jangaroo.exml.json.JsonObject) ExmlToModelParser(net.jangaroo.exml.parser.ExmlToModelParser) Test(org.junit.Test)

Example 3 with ConfigAttribute

use of net.jangaroo.exml.model.ConfigAttribute in project jangaroo-tools by CoreMedia.

the class ConfigClassBuilderTest method testBuildConfigClass.

@Test
public void testBuildConfigClass() throws Exception {
    ConfigClass configClass = buildConfigClass("/testNamespace/config/testComponent.as", "expected");
    Assert.assertEquals("testNamespace.config", configClass.getPackageName());
    Assert.assertEquals("testComponent", configClass.getName());
    Assert.assertEquals("testPackage.TestComponent", configClass.getComponentClassName());
    Assert.assertEquals("This is a TestComponent with panel as baseclass. <p>This class serves as a typed config object for the constructor of the class <code>testPackage.TestComponent</code>. It defines the EXML element <code>&lt;tc:testComponent></code> with <code>xmlns:tc=\"exml:testNamespace.config\"</code>.</p> <p>Using this config class also takes care of registering the target class under the xtype <code>\"testNamespace.config.testComponent\"</code> with Ext JS.</p> @see testPackage.TestComponent @see ext.Panel", configClass.getDescription());
    Set<String> attributeNames = new HashSet<String>();
    for (ConfigAttribute configAttribute : configClass.getCfgs()) {
        attributeNames.add(configAttribute.getName());
        if ("propertyOne".equals(configAttribute.getName())) {
            Assert.assertEquals("Boolean", configAttribute.getType());
            Assert.assertEquals("Some Boolean property @see Boolean", configAttribute.getDescription());
        }
        if ("propertyTwo".equals(configAttribute.getName())) {
            Assert.assertEquals("Number", configAttribute.getType());
            Assert.assertEquals("Some Number property", configAttribute.getDescription());
        }
    }
    Assert.assertEquals(new HashSet<String>(Arrays.asList("propertyOne", "propertyTwo", "propertyThree", "propertyFour", "propertyFive", "propertySix", "propertySeven", "propertyEight", "propertyNine", "propertyTen", "propertyEleven", "propertyTwelve", "property13", "property14")), attributeNames);
}
Also used : ConfigClass(net.jangaroo.exml.model.ConfigClass) ConfigAttribute(net.jangaroo.exml.model.ConfigAttribute) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with ConfigAttribute

use of net.jangaroo.exml.model.ConfigAttribute 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)

Example 5 with ConfigAttribute

use of net.jangaroo.exml.model.ConfigAttribute in project jangaroo-tools by CoreMedia.

the class ExmlToModelParser method getCfgByName.

private ConfigAttribute getCfgByName(ConfigClass configClass, String attributeName) {
    ConfigClass current = configClass;
    while (current != null) {
        ConfigAttribute configAttribute = current.getCfgByName(attributeName);
        if (configAttribute != null) {
            return configAttribute;
        }
        String superClassName = current.getSuperClassName();
        if (superClassName == null || superClassName.equals("Object")) {
            break;
        }
        current = registry.getConfigClassByName(superClassName);
    }
    return null;
}
Also used : ConfigClass(net.jangaroo.exml.model.ConfigClass) ConfigAttribute(net.jangaroo.exml.model.ConfigAttribute)

Aggregations

ConfigAttribute (net.jangaroo.exml.model.ConfigAttribute)5 ConfigClass (net.jangaroo.exml.model.ConfigClass)3 JsonObject (net.jangaroo.exml.json.JsonObject)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 ExmlcException (net.jangaroo.exml.api.ExmlcException)1 Code (net.jangaroo.exml.json.Code)1 JsonArray (net.jangaroo.exml.json.JsonArray)1 ExmlModel (net.jangaroo.exml.model.ExmlModel)1 ExmlToModelParser (net.jangaroo.exml.parser.ExmlToModelParser)1 Attr (org.w3c.dom.Attr)1 Element (org.w3c.dom.Element)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1