Search in sources :

Example 1 with Declaration

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

the class ExmlToModelParser method createDeclaration.

private Declaration createDeclaration(Element element, ExmlModel model) {
    final String name = element.getAttribute(Exmlc.EXML_DECLARATION_NAME_ATTRIBUTE);
    String type = element.getAttribute(Exmlc.EXML_DECLARATION_TYPE_ATTRIBUTE);
    if (type == null || type.isEmpty()) {
        type = AS3Type.ANY.toString();
    }
    final NodeList valueElements = element.getElementsByTagNameNS(Exmlc.EXML_NAMESPACE_URI, Exmlc.EXML_DECLARATION_VALUE_NODE_NAME);
    final Object valueObject;
    boolean addTypeCast = false;
    if (valueElements.getLength() > 0) {
        if (element.hasAttribute(Exmlc.EXML_DECLARATION_VALUE_ATTRIBUTE)) {
            throw new ExmlcException("The value of <exml:var> or <exml:const> '\" + name + \"' must be specified as either an attribute or a sub-element, not both.", getLineNumber(element));
        }
        Element valueElement = (Element) valueElements.item(0);
        final List<Element> valueChildElements = getChildElements(valueElement);
        if (valueChildElements.isEmpty()) {
            valueObject = getAttributeValue(valueElement.getTextContent(), type);
        } else {
            valueObject = parseValue(model, "Array".equals(type), valueChildElements);
            addTypeCast = !AS3Type.ANY.toString().equals(type) && !ApplyExpr.isCoerceFunction(type);
        }
    } else {
        valueObject = getAttributeValue(element.getAttribute(Exmlc.EXML_DECLARATION_VALUE_ATTRIBUTE), type);
    }
    String value = JsonObject.valueToString(valueObject, 2, 4);
    if (addTypeCast) {
        value = type + "(" + value + ")";
    }
    return new Declaration(name, value, type);
}
Also used : NodeList(org.w3c.dom.NodeList) ExmlcException(net.jangaroo.exml.api.ExmlcException) Element(org.w3c.dom.Element) JsonObject(net.jangaroo.exml.json.JsonObject) Declaration(net.jangaroo.exml.model.Declaration)

Example 2 with Declaration

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

the class ExmlToModelParser method parse.

/**
   * Parse the input stream content into a model.
   * Close the input stream after reading.
   *
   * @param inputStream the input stream
   * @param model the model
   * @throws IOException  if the input stream could not be read
   * @throws SAXException if the XML was not well-formed
   */
private void parse(InputStream inputStream, ExmlModel model) throws IOException, SAXException {
    Document document = buildDom(inputStream);
    Node root = document.getFirstChild();
    validateRootNode(root);
    NamedNodeMap attributes = root.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attribute = (Attr) attributes.item(i);
        //baseClass attribute has been specified, so the super class of the component is actually that
        if (Exmlc.EXML_BASE_CLASS_ATTRIBUTE.equals(attribute.getLocalName())) {
            model.setSuperClassName(attribute.getValue());
        } else if (Exmlc.EXML_PUBLIC_API_ATTRIBUTE.equals(attribute.getLocalName())) {
            PublicApiMode publicApiMode = Exmlc.parsePublicApiMode(attribute.getValue());
            switch(publicApiMode) {
                case TRUE:
                    model.addAnnotation(Jooc.PUBLIC_API_INCLUSION_ANNOTATION_NAME);
                // fall through!
                case CONFIG:
                    model.getConfigClass().addAnnotation(Jooc.PUBLIC_API_INCLUSION_ANNOTATION_NAME);
            }
        }
    }
    NodeList childNodes = root.getChildNodes();
    Element componentNode = null;
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (ExmlUtils.isExmlNamespace(node.getNamespaceURI())) {
                Element element = (Element) node;
                if (Exmlc.EXML_IMPORT_NODE_NAME.equals(node.getLocalName())) {
                    String importedClassName = element.getAttribute(Exmlc.EXML_IMPORT_CLASS_ATTRIBUTE);
                    if (importedClassName == null || importedClassName.equals("")) {
                        int lineNumber = getLineNumber(componentNode);
                        throw new ExmlcException("<exml:import> element must contain a non-empty class attribute", lineNumber);
                    }
                    model.addImport(importedClassName);
                } else if (Exmlc.EXML_ANNOTATION_NODE_NAME.equals(node.getLocalName())) {
                    AnnotationAt annotationAt = Exmlc.parseAnnotationAtValue(element.getAttribute(Exmlc.EXML_ANNOTATION_AT_ATTRIBUTE));
                    if (annotationAt != AnnotationAt.CONFIG) {
                        model.addAnnotation(element.getTextContent());
                    }
                } else if (Exmlc.EXML_CONSTANT_NODE_NAME.equals(node.getLocalName())) {
                    String constantTypeName = element.getAttribute(Exmlc.EXML_DECLARATION_TYPE_ATTRIBUTE);
                    model.addImport(constantTypeName);
                } else if (Exmlc.EXML_DESCRIPTION_NODE_NAME.equals(node.getLocalName())) {
                    model.setDescription(node.getTextContent());
                } else if (Exmlc.EXML_VAR_NODE_NAME.equals(node.getLocalName())) {
                    Declaration var = createDeclaration(element, model);
                    if (!model.getVars().contains(var)) {
                        model.addVar(var);
                    }
                } else if (Exmlc.EXML_CFG_NODE_NAME.equals(node.getLocalName())) {
                    String cfgName = element.getAttribute(Exmlc.EXML_CFG_NAME_ATTRIBUTE);
                    String cfgType = element.getAttribute(Exmlc.EXML_CFG_TYPE_ATTRIBUTE);
                    String cfgDefault = element.getAttribute(Exmlc.EXML_CFG_DEFAULT_ATTRIBUTE);
                    Element defaultValueElement = findChildElement(element, Exmlc.EXML_NAMESPACE_URI, Exmlc.EXML_CFG_DEFAULT_NODE_NAME);
                    if (cfgDefault.length() != 0 && defaultValueElement != null) {
                        throw new ExmlcException("<exml:cfg> default value must be specified as either an attribute or a sub-element, not both for config '" + cfgName + "'.", getLineNumber(element));
                    }
                    if (cfgDefault.length() > 0) {
                        model.getCfgDefaults().set(cfgName, getAttributeValue(cfgDefault, cfgType));
                        model.addImport(cfgType);
                    } else if (defaultValueElement != null) {
                        model.getCfgDefaults().set(cfgName, parseValue(model, "Array".equals(cfgType), getChildElements(defaultValueElement)));
                    }
                }
            } else {
                if (componentNode != null) {
                    int lineNumber = getLineNumber(componentNode);
                    throw new ExmlcException("root node of EXML contained more than one component definition", lineNumber);
                }
                componentNode = (Element) node;
            }
        }
    }
    if (componentNode == null) {
        return;
    }
    String superFullClassName = createFullConfigClassNameFromNode(componentNode);
    if (superFullClassName.equals(model.getConfigClass().getFullName())) {
        int lineNumber = getLineNumber(componentNode);
        throw new ExmlcException("Cyclic inheritance error: super class and this component are the same.", lineNumber);
    }
    ConfigClass superConfigClass = getConfigClassByName(superFullClassName, componentNode);
    String superComponentClassName = superConfigClass.getComponentClassName();
    if (model.getSuperClassName() == null) {
        model.setSuperClassName(superComponentClassName);
    }
    //but we still need the import
    model.addImport(superComponentClassName);
    fillModelAttributes(model, model.getJsonObject(), componentNode, superConfigClass);
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) PublicApiMode(net.jangaroo.exml.model.PublicApiMode) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ExmlcException(net.jangaroo.exml.api.ExmlcException) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) AnnotationAt(net.jangaroo.exml.model.AnnotationAt) ConfigClass(net.jangaroo.exml.model.ConfigClass) Declaration(net.jangaroo.exml.model.Declaration)

Example 3 with Declaration

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

the class ExmlToModelParserTest method testParseAllElements.

@Test
public void testParseAllElements() throws Exception {
    setUp("exmlparser.config");
    ExmlToModelParser exmlToModelParser = new ExmlToModelParser(getConfigClassRegistry());
    ExmlModel model = exmlToModelParser.parse(getFile("/exmlparser/AllElements.exml"));
    Assert.assertEquals(new HashSet<String>(Arrays.asList("exmlparser.config.allElements", "ext.config.component", "ext.MessageBox", "ext.Panel")), model.getImports());
    Assert.assertEquals("ext.Panel", model.getSuperClassName());
    final List<Declaration> varDeclarations = model.getVars();
    Assert.assertEquals(3, varDeclarations.size());
    final Declaration myVar = varDeclarations.get(0);
    Assert.assertEquals("myVar", myVar.getName());
    Assert.assertEquals("String", myVar.getType());
    Assert.assertEquals("config.myProperty + '_suffix'", myVar.getValue());
    final Declaration myVar2 = varDeclarations.get(1);
    Assert.assertEquals("myVar2", myVar2.getName());
    Assert.assertEquals("Object", myVar2.getType());
    Assert.assertEquals("{\n" + "      prop: config.myProperty\n" + "    }", myVar2.getValue().replaceAll(System.getProperty("line.separator"), "\n"));
    final Declaration myVar3 = varDeclarations.get(2);
    Assert.assertEquals("myVar3", myVar3.getName());
    Assert.assertEquals("ext.config.component", myVar3.getType());
    Assert.assertEquals("ext.config.component({\n" + "      xtype: \"button\",\n" + "      text: \"Foo\"\n" + "    })", myVar3.getValue().replaceAll(System.getProperty("line.separator"), "\n"));
    final List<Declaration> constantDeclarations = model.getConfigClass().getConstants();
    Assert.assertEquals(3, constantDeclarations.size());
    final Declaration myConst1 = constantDeclarations.get(0);
    Assert.assertEquals("SOME_CONSTANT", myConst1.getName());
    Assert.assertEquals("1234", myConst1.getValue());
    Assert.assertEquals("uint", myConst1.getType());
    Assert.assertEquals("This is my <b>constant</b>", myConst1.getDescription().trim());
    final Declaration myConst2 = constantDeclarations.get(1);
    Assert.assertEquals("ANOTHER_CONSTANT", myConst2.getName());
    Assert.assertEquals("\"\\n      Lorem ipsum & Co.\\n      Another line.\\n    \"", myConst2.getValue());
    Assert.assertEquals("String", myConst2.getType());
    Assert.assertEquals("This is another <b>constant</b>", myConst2.getDescription().trim());
    final Declaration myConst3 = constantDeclarations.get(2);
    Assert.assertEquals("CODE_CONSTANT", myConst3.getName());
    Assert.assertEquals("1 + 1", myConst3.getValue());
    Assert.assertEquals("int", myConst3.getType());
    JsonObject expectedJsonObject = new JsonObject("layout", JsonObject.code("config.myLayout"), "title", "I am a panel", "someList", new JsonArray(new JsonObject("xtype", "button", "text", "click me!")), "defaults", new JsonObject("layout", "border"), "layoutConfig", new JsonObject("bla", "blub", "anchor", new JsonObject("style", "test"), "border", new JsonObject("type", "solid")), "items", new JsonArray(new JsonObject("xtype", "button", "text", "Save", "handler", JsonObject.code("function():void {\n" + "          window.alert('gotcha!');\n" + "        }"))), "menu", new JsonArray(new JsonObject("xtype", "menuitem", "text", "juhu1"), new JsonObject("xtype", "menuitem", "text", "juhu2"), new JsonObject("xtype", "menuitem", "text", "juhu3")), "tools", new JsonArray(new JsonObject("handler", JsonObject.code("function(x){return ''+x;}"), "id", "gear")), "plugins", new JsonArray(new JsonObject("ptype", "aplugin"), new JsonObject("ptype", "aplugin")), "layout2", new JsonObject("type", "a"));
    System.out.println(model.getJsonObject().toString(2));
    Assert.assertEquals(expectedJsonObject.toString(2), model.getJsonObject().toString(2));
}
Also used : JsonArray(net.jangaroo.exml.json.JsonArray) ExmlModel(net.jangaroo.exml.model.ExmlModel) JsonObject(net.jangaroo.exml.json.JsonObject) ExmlToModelParser(net.jangaroo.exml.parser.ExmlToModelParser) Declaration(net.jangaroo.exml.model.Declaration) Test(org.junit.Test)

Example 4 with Declaration

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

the class ExmlToModelParserTest method testConstants.

@Test
public void testConstants() throws Exception {
    setUp("exmlparser.config");
    ExmlToModelParser exmlToModelParser = new ExmlToModelParser(getConfigClassRegistry());
    ExmlModel model = exmlToModelParser.parse(getFile("/exmlparser/TestConstants.exml"));
    Declaration aConstant = model.getConfigClass().getConstants().get(0);
    Assert.assertEquals("A_CONSTANT", aConstant.getName());
    Assert.assertEquals("\"One two three\"", aConstant.getValue());
    Assert.assertEquals("String", aConstant.getType());
    Assert.assertEquals("This is some constant", aConstant.getDescription().trim());
    Declaration bConstant = model.getConfigClass().getConstants().get(1);
    Assert.assertEquals("B_CONSTANT", bConstant.getName());
    Assert.assertEquals("456", bConstant.getValue());
    Assert.assertEquals("uint", bConstant.getType());
    Assert.assertNull(bConstant.getDescription());
    Declaration cConstant = model.getConfigClass().getConstants().get(2);
    Assert.assertEquals("C_CONSTANT", cConstant.getName());
    Assert.assertEquals("new Object()", cConstant.getValue());
    Assert.assertEquals("Object", cConstant.getType());
    Assert.assertNull(cConstant.getDescription());
    Declaration dConstant = model.getConfigClass().getConstants().get(3);
    Assert.assertEquals("D_CONSTANT", dConstant.getName());
    Assert.assertEquals("new button()", dConstant.getValue());
    Assert.assertEquals("ext.config.button", dConstant.getType());
    Assert.assertTrue(model.getImports().contains("ext.config.button"));
    Assert.assertNull(dConstant.getDescription());
    Declaration eConstant = model.getConfigClass().getConstants().get(4);
    Assert.assertEquals("E_CONSTANT", eConstant.getName());
    Assert.assertEquals("new Container()", eConstant.getValue());
    Assert.assertEquals("ext.Component", eConstant.getType());
    Assert.assertTrue(model.getImports().contains("ext.Component"));
    Assert.assertTrue(model.getImports().contains("ext.Container"));
    Assert.assertNull(eConstant.getDescription());
}
Also used : ExmlModel(net.jangaroo.exml.model.ExmlModel) ExmlToModelParser(net.jangaroo.exml.parser.ExmlToModelParser) Declaration(net.jangaroo.exml.model.Declaration) Test(org.junit.Test)

Aggregations

Declaration (net.jangaroo.exml.model.Declaration)4 ExmlcException (net.jangaroo.exml.api.ExmlcException)2 JsonObject (net.jangaroo.exml.json.JsonObject)2 ExmlModel (net.jangaroo.exml.model.ExmlModel)2 ExmlToModelParser (net.jangaroo.exml.parser.ExmlToModelParser)2 Test (org.junit.Test)2 Element (org.w3c.dom.Element)2 NodeList (org.w3c.dom.NodeList)2 JsonArray (net.jangaroo.exml.json.JsonArray)1 AnnotationAt (net.jangaroo.exml.model.AnnotationAt)1 ConfigClass (net.jangaroo.exml.model.ConfigClass)1 PublicApiMode (net.jangaroo.exml.model.PublicApiMode)1 Attr (org.w3c.dom.Attr)1 Document (org.w3c.dom.Document)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1