Search in sources :

Example 1 with ExmlcException

use of net.jangaroo.exml.api.ExmlcException in project jangaroo-tools by CoreMedia.

the class Exmlc method generateXsd.

@Override
public File generateXsd() {
    // Maybe even the directory does not exist.
    File targetPackageFolder = getConfig().getResourceOutputDirectory();
    if (!targetPackageFolder.exists()) {
        // noinspection ResultOfMethodCallIgnored
        // NOSONAR
        targetPackageFolder.mkdirs();
    }
    File result = new File(targetPackageFolder, getConfig().getConfigClassPackage() + ".xsd");
    Writer writer = null;
    try {
        writer = new OutputStreamWriter(new FileOutputStream(result), net.jangaroo.exml.api.Exmlc.OUTPUT_CHARSET);
        configClassRegistry.generateXsd(writer);
    } catch (Exception e) {
        throw new ExmlcException("unable to generate xsd file: " + e.getMessage(), e);
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
        // never happen
        }
    }
    return result;
}
Also used : FileOutputStream(java.io.FileOutputStream) ExmlcException(net.jangaroo.exml.api.ExmlcException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) ExmlcException(net.jangaroo.exml.api.ExmlcException) IOException(java.io.IOException) CommandLineParseException(net.jangaroo.jooc.cli.CommandLineParseException)

Example 2 with ExmlcException

use of net.jangaroo.exml.api.ExmlcException 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 3 with ExmlcException

use of net.jangaroo.exml.api.ExmlcException 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 4 with ExmlcException

use of net.jangaroo.exml.api.ExmlcException in project jangaroo-tools by CoreMedia.

the class ExmlToMxml method convert.

public File[] convert() {
    InputStream migrationMapStream = resourceClassLoader.getResourceAsStream("ext-as-3.4-migration-map.properties");
    if (migrationMapStream == null) {
        throw new ExmlcException("Could not find migration map 'ext-as-3.4-migration-map.properties' in classpath.");
    }
    try {
        this.migrationMap.load(migrationMapStream);
    } catch (IOException e) {
        throw new ExmlcException("Unable to load migration map", e);
    }
    new CatalogComponentsParser(mxmlComponentRegistry).parse(resourceClassLoader.getResourceAsStream("catalog.xml"));
    try {
        new MxmlLibraryManifestGenerator(configClassRegistry).createManifestFile();
    } catch (IOException e) {
        throw new ExmlcException("Unable to create manifest.xml file: " + e.getMessage(), e);
    }
    Map<String, ExmlSourceFile> exmlSourceFilesByConfigClassName = configClassRegistry.getExmlSourceFilesByConfigClassName();
    List<File> mxmlFiles = new ArrayList<File>();
    Collection<ExmlSourceFile> exmlSourceFiles;
    List<File> sourceFiles = configClassRegistry.getConfig().getSourceFiles();
    if (sourceFiles.isEmpty()) {
        exmlSourceFiles = exmlSourceFilesByConfigClassName.values();
    } else {
        exmlSourceFiles = new ArrayList<ExmlSourceFile>();
        for (File exmlFile : sourceFiles) {
            exmlSourceFiles.add(configClassRegistry.getExmlSourceFile(exmlFile));
        }
    }
    for (ExmlSourceFile exmlSourceFile : exmlSourceFiles) {
        System.out.printf("Converting EXML file %s...%n", exmlSourceFile.getSourceFile());
        if (exmlSourceFile.hasSourceTargetClass()) {
            System.out.printf("  Target class %s is implemented in ActionScript: no need to generate MXML target class.", exmlSourceFile.getTargetClassName());
        } else {
            try {
                File mxmlFile = exmlToMxml(exmlSourceFile);
                mxmlFiles.add(mxmlFile);
                System.out.printf("  Generated MXML target class %s into file %s.%n", exmlSourceFile.getTargetClassName(), mxmlFile.getPath());
            } catch (Exception e) {
                throw new ExmlcException("Unable to convert to MXML: " + e.getMessage(), exmlSourceFile.getSourceFile(), e);
            }
        }
    }
    if (!configClassRegistry.getConfig().isKeepExmlFiles()) {
        // clean up EXML files:
        for (ExmlSourceFile exmlSourceFile : exmlSourceFiles) {
            if (!exmlSourceFile.getSourceFile().delete()) {
                System.err.println("Failed to delete EXML source file " + exmlSourceFile.getSourceFile().getPath());
            }
        }
    }
    return mxmlFiles.toArray(new File[mxmlFiles.size()]);
}
Also used : InputStream(java.io.InputStream) ExmlcException(net.jangaroo.exml.api.ExmlcException) CatalogComponentsParser(net.jangaroo.jooc.mxml.CatalogComponentsParser) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExmlcException(net.jangaroo.exml.api.ExmlcException) IOException(java.io.IOException) MxmlLibraryManifestGenerator(net.jangaroo.exml.generator.MxmlLibraryManifestGenerator) ExmlSourceFile(net.jangaroo.exml.model.ExmlSourceFile) ExmlSourceFile(net.jangaroo.exml.model.ExmlSourceFile) File(java.io.File)

Example 5 with ExmlcException

use of net.jangaroo.exml.api.ExmlcException in project jangaroo-tools by CoreMedia.

the class ExmlConfigClassGenerator method generateClass.

private static void generateClass(final ConfigClass configClass, final Writer output) throws IOException, TemplateException {
    if (configClass.getSuperClassName() == null) {
        throw new ExmlcException("Config class " + configClass.getFullName() + "'s super class name is null!");
    }
    Configuration cfg = new Configuration();
    cfg.setClassForTemplateLoading(ConfigClass.class, "/");
    cfg.setObjectWrapper(new DefaultObjectWrapper());
    Template template = cfg.getTemplate("/net/jangaroo/exml/templates/exml_config_class.ftl");
    Environment env = template.createProcessingEnvironment(configClass, output);
    env.setOutputEncoding(Exmlc.OUTPUT_CHARSET);
    env.process();
}
Also used : Configuration(freemarker.template.Configuration) ExmlcException(net.jangaroo.exml.api.ExmlcException) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) Environment(freemarker.core.Environment) Template(freemarker.template.Template)

Aggregations

ExmlcException (net.jangaroo.exml.api.ExmlcException)12 IOException (java.io.IOException)6 File (java.io.File)5 Element (org.w3c.dom.Element)3 InputStream (java.io.InputStream)2 JsonObject (net.jangaroo.exml.json.JsonObject)2 ConfigClass (net.jangaroo.exml.model.ConfigClass)2 Declaration (net.jangaroo.exml.model.Declaration)2 NodeList (org.w3c.dom.NodeList)2 Environment (freemarker.core.Environment)1 Configuration (freemarker.template.Configuration)1 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)1 Template (freemarker.template.Template)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Exmlc (net.jangaroo.exml.compiler.Exmlc)1