ole.OLEModel.java Source code

Java tutorial

Introduction

Here is the source code for ole.OLEModel.java

Source

/*******************************************************************************
 * Copyright (C) 2013 Prisoners of War - All Rights Reserved
 *
 * This file is part of POW.
 *
 * POW is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * POW is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with POW.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package ole;

import java.util.ArrayList;

import ole.parser.OLELexer;
import ole.parser.OLEParser;
import ole.parser.OLEWalkerListener;
import ole.parser.OLErrorListener;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import pow.views.POWConsole;

/**
 * A Model containing all OLEntities
 * @author POW
 *
 */
public class OLEModel {

    private ArrayList<OLEntity> entities = new ArrayList<OLEntity>();

    public void addOLEntity(OLEntity entity) {
        if (this.findEntity(entity.getEntityName()) != null)
            return;
        entities.add(entity);
    }

    public ArrayList<OLEntity> getEntities() {
        return entities;
    }

    public void setEntities(ArrayList<OLEntity> entities) {
        this.entities = entities;
    }

    /**
     * Makes an OLEModel from OLE text
     * @param text The OLE text
     * @return The resulting OLEModel
     */
    public static OLEModel fromText(String text) {
        OLEModel model = new OLEModel();

        CharStream cs = new ANTLRInputStream(text);
        OLELexer lexer = new OLELexer(cs);

        CommonTokenStream cts = new CommonTokenStream(lexer);
        OLEParser parser = new OLEParser(cts);
        parser.addErrorListener(new OLErrorListener());
        ParseTreeWalker ptw = new ParseTreeWalker();
        ParserRuleContext ctx = parser.start();
        ptw.walk(new OLEWalkerListener(model), ctx);

        POWConsole.printInfo("Parsed entities: " + model.getEntities().toString());

        return model;
    }

    /**
     * Returns an array of all OLEntity names
     * @return A array of all OLEntity names
     */
    public String[] entitiesToString() {
        String[] array = new String[entities.size()];
        for (int i = 0; i < entities.size(); i++) {
            array[i] = entities.get(i).getEntityName();
        }
        return array;
    }

    /**
     * Finds the OLEntity with given name
     * @param name The OLEntity name
     * @return The OLEntity
     */
    public OLEntity findEntity(String name) {
        for (OLEntity e : entities) {
            if (e.getEntityName().equals(name))
                return e;
        }
        return null;
    }

}