All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.michaelhradek.aurkitu.core.output.Schema Maven / Gradle / Ivy

There is a newer version: 0.0.7.6
Show newest version
package com.michaelhradek.aurkitu.core.output;

import com.michaelhradek.aurkitu.Config;
import com.michaelhradek.aurkitu.core.Validator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;

/**
 * @author m.hradek
 *
 */
@Getter
@Setter
public class Schema {

    // IDL values
    String name;
    String fileIdentifier;
    String fileExtension;
    String namespace;
    String rootType;
    List enums;
    List types;
    List includes;
    List attributes;
    List> integerConstants;
    List> floatConstants;

    // Aurkitu values
    boolean generateVersion;
    Boolean isValidSchema;
    Validator validator;

    public Schema() {
        enums = new ArrayList();
        types = new ArrayList();
        includes = new ArrayList();
        attributes = new ArrayList();
        integerConstants = new ArrayList>();
        floatConstants = new ArrayList>();
    }

    /**
     * @param input Add an enum declaration to the schema
     */
    public void addEnumDeclaration(EnumDeclaration input) {
        if (!enums.contains(input)) {
            enums.add(input);
        }
    }

    /**
     * @param input Add a type (i.e. class) declaration to the schema
     */
    public void addTypeDeclaration(TypeDeclaration input) {
        if (!types.contains(input)) {
            types.add(input);
        }
    }

    /**
     * @param input Add another schema to include within this schema
     */
    public void addInclude(String input) {
        includes.add(input);
    }

    /**
     * @param input Add an attribute to the schema
     */
    public void addAttribute(String input) {
        attributes.add(input);
    }

    /**
     * @param input Add an integer constant to the schema
     */
    public void addIntegerConstant(Constant input) {
        integerConstants.add(input);
    }

    /**
     * @param input Add a float constant to the schema
     */
    public void addFloatConstant(Constant input) {
        floatConstants.add(input);
    }

    /**
     * @param  A class which contains the name, value, and options used to define Numbers at the schema level
     *
     */
    public static class Constant {
        public String name;
        public T value;
        public Map options = new HashMap();
    }

    /**
     * @param input Set the 4 character file identifier.
     */
    public void setFileIdentifier(String input) {
        if(input == null) {
            fileIdentifier = null;
            return;
        }

        if (input.length() != 4) {
            return;
        }

        fileIdentifier = input.toUpperCase();
    }

    /**
     * @param input Set the file extension. Default is {@link com.michaelhradek.aurkitu.Application#fileExtension}
     */
    public void setFileExtension(String input) {
        if(input == null) {
            fileExtension = null;
            return;
        }

        if (input.length() < 1) {
            return;
        }

        fileExtension = input.toLowerCase();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder(Config.SCHEMA_INTRO_COMMENT);
        builder.append(System.lineSeparator());

        if (generateVersion) {
            builder.append(Config.SCHEMA_VERSION_COMMENT);
            builder.append(System.lineSeparator());
        }
        builder.append(System.lineSeparator());

        if (includes != null && includes.size() > 0) {
            for (String include : includes) {
                builder.append("include ");
                builder.append(include);
                if(!include.endsWith(";"))
                    builder.append(";");
                builder.append(System.lineSeparator());
            }

            builder.append(System.lineSeparator());
        }

        if (attributes != null && attributes.size() > 0) {
            for (String attribute : attributes) {
                builder.append("attribute \"");
                builder.append(attribute);
                builder.append("\"");
                builder.append(";");
                builder.append(System.lineSeparator());
            }

            builder.append(System.lineSeparator());
        }

        if (integerConstants.size() > 0) {
            for (Constant constant : integerConstants) {
                builder.append("int ");
                builder.append(constant.name);
                builder.append(" ");
                builder.append(constant.value);
                builder.append(";");
                builder.append(System.lineSeparator());
            }

            builder.append(System.lineSeparator());
        }

        if (floatConstants.size() > 0) {
            for (Constant constant : floatConstants) {
                builder.append("float ");
                builder.append(constant.name);
                builder.append(" ");
                builder.append(constant.value);
                builder.append(";");
                builder.append(System.lineSeparator());
            }

            builder.append(System.lineSeparator());
        }

        if (namespace != null) {
            builder.append("namespace ");
            builder.append(namespace);
            if(!namespace.endsWith(";"))
                builder.append(";");
            builder.append(System.lineSeparator());
            builder.append(System.lineSeparator());
        }

        for (EnumDeclaration enumD : enums) {
            builder.append(enumD.toString());
        }

        for (TypeDeclaration typeD : types) {
            builder.append(typeD.toString());
        }

        if (rootType != null) {
            builder.append("root_type ");
            builder.append(rootType);
            builder.append(";");
            builder.append(System.lineSeparator());
            builder.append(System.lineSeparator());
        }

        if (fileIdentifier != null) {
            builder.append("file_identifier ");
            builder.append("\"");
            builder.append(fileIdentifier);
            builder.append("\"");
            builder.append(";");
            builder.append(System.lineSeparator());
            builder.append(System.lineSeparator());
        }

        if (fileExtension != null) {
            builder.append("file_extension ");
            builder.append("\"");
            builder.append(fileExtension);
            builder.append("\"");
            builder.append(";");
            builder.append(System.lineSeparator());
            builder.append(System.lineSeparator());
        }

        if (isValidSchema != null) {
            builder.append(validator.getErrorComments());
        }

        String result = builder.toString();

        if (generateVersion) {
            return result.replace(Config.SCHEMA_VERSION_PLACEHOLDER,
                    Integer.toHexString(result.hashCode()));
        }

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy