Package com.nexirius.framework.datamodel.parser

Source Code of com.nexirius.framework.datamodel.parser.DataModelClasses

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.datamodel.parser;

import com.nexirius.framework.datamodel.ComboBoxModel;
import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.datamodel.StructModel;

import java.util.Enumeration;
import java.util.Hashtable;

/**
* A class which holds all DataModelTypeClass instances
*
* @author Marcel Baumann
*/
public class DataModelClasses {



    Hashtable table;

    DataModelClasses() {
        table = new Hashtable();
    }

    public void add(DataModelTypeClass c)
            throws Exception {
        String name = c.getName();

        if (null != getClass(name)) {
            throw new Exception("Class '" + name + "' is already defined");
        }

        table.put(name, c);
    }

    public void add(DataModelTypeEnum e)
            throws Exception {
        String name = e.getName();

        if (null != getEnum(name)) {
            throw new Exception("Enum '" + name + "' is already defined");
        }

        table.put(name, e);
    }

    public DataModelTypeClass getClass(String name) {
        return (DataModelTypeClass) table.get(name);
    }

    public DataModelTypeEnum getEnum(String name) {
        return (DataModelTypeEnum) table.get(name);
    }

    public boolean isClass(String type) {
        try {
            return null != getClass(type);
        } catch (Throwable ex) {
            return false;
        }
    }

    public boolean isEnum(String type) {
        try {
            return null != getEnum(type);
        } catch (Throwable ex) {
            return false;
        }
    }

    public StructModel createClassInstance(DataModelClasses classes, String name, String init)
            throws ParserException {
        DataModelTypeClass c = getClass(name);

        if (c == null) {
            throw new ParserException("Class " + name + " is not defined");
        }

        StructModel ret = (StructModel) c.newInstance(classes, init);

        ret.setFieldName(name);

        return ret;
    }

    public ComboBoxModel createEnumInstance(String name, String init)
            throws ParserException {
        DataModelTypeEnum c = getEnum(name);

        if (c == null) {
            throw new ParserException("Enum " + name + " is not defined");
        }

        ComboBoxModel ret = (ComboBoxModel) c.newInstance(this);

        ret.setFieldName(name);

        if (init != null) {
            try {
                ret.setText(init);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return ret;
    }

    public DataModel newInstance(String type, String init)
            throws ParserException {
        if (isClass(type)) {

            return createClassInstance(this, type, init);
        } else if (isEnum(type)) {

            return createEnumInstance(type, init);
        }

        return DataModelType.createPrimitivInstance(type, init);
    }

    public synchronized String[] getClassNames() {
        Enumeration e = table.keys();
        int num = 0;
        int i = 0;

        while (e.hasMoreElements()) {
            String name = (String) e.nextElement();

            if (isClass(name)) {
                ++num;
            }
        }

        String ret[] = new String[num];

        e = table.keys();

        while (e.hasMoreElements()) {
            String name = (String) e.nextElement();

            if (isClass(name)) {
                ret[i++] = name;
            }
        }

        return ret;
    }

    /**
     * This method returns an XML DTD describing the data structure of the classes
     * defined within this collection of classes
     */
    public String getDTD() {
        StringBuffer ret = new StringBuffer();

        ret.append("<?xml version='1.0' encoding='us-ascii'?>\n");
        ret.append("<!-- automatically generated DTD for DataModel classes -->\n");

        String cl[] = getClassNames();

        ret.append("<!ELEMENT DataModel (");

        for (int i = 0; i < cl.length; ++i) {
            ret.append(cl[i]);

            if (i + 1 < cl.length) {
                ret.append(" | ");
            }
        }
        ret.append(")*>\n");

        for (int i = 0; i < cl.length; ++i) {
            getClass(cl[i]).appendDTD(ret);
        }

        return ret.toString();
    }
}
TOP

Related Classes of com.nexirius.framework.datamodel.parser.DataModelClasses

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.