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

eu.mihosoft.vmf.vmftext.vmtemplates.unparser-base-formatter.vm Maven / Gradle / Ivy

The newest version!
#*
 * Copyright 2017-2018 Michael Hoffer . All rights reserved.
 * Copyright 2017-2018 Goethe Center for Scientific Computing, University Frankfurt. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * If you use this software for scientific research then please cite the following publication(s):
 *
 * M. Hoffer, C. Poliwoda, & G. Wittum. (2013). Visual reflection library:
 * a framework for declarative GUI programming on the Java platform.
 * Computing and Visualization in Science, 2013, 16(4),
 * 181–192. http://doi.org/10.1007/s00791-014-0230-y
 *#
package ${modelPackageName}.unparser;

// Java Core API
import java.io.PrintWriter;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.Map;
import java.util.HashMap;


public class BaseFormatter implements Formatter {
    private Deque> state = new ArrayDeque<>();


    public BaseFormatter() {
        state.push(new HashMap<>());
        setPrevRuleInfo(RuleInfo.emptyRule());

        setIndent("");
    }

    private Map getCurrentState() {
        return state.peek();
    }

    @Override
    public void pushState() {
        state.push(copy(getCurrentState()));
    }

    @Override
    public void acceptState() {
        Map s = state.pop();
        copyFromTo(s,getCurrentState());
    }

    @Override
    public void rejectState() {
        state.pop();
    }

    private void copyFromTo(Map src, Map target) {
        target.putAll(src);
    }

    private Map copy(Map src) {
        Map result = new HashMap<>();

        result.putAll(src);

        return result;
    }

    public RuleInfo getPrevRuleInfo() {
        return (RuleInfo) getCurrentState().get("rule-info");
    }

    private void setPrevRuleInfo(RuleInfo info) {
        getCurrentState().put("rule-info", info);
    }

    public String getIndent() {
        return getStringState("indent");
    }

    private void setIndent(String s) {
        setStringState("indent", s);
    }

    protected void incIndent() {
        setIndent(getIndent()+"  ");
    }

    protected void decIndent() {
        if(getIndent().length() > 1) {
            setIndent(getIndent().substring(2));
        }
    }

    public Boolean getBoolState(String key) {
        if(!getCurrentState().containsKey(key)) {
            return false;
        }

        return (Boolean) getCurrentState().get(key);
    }

    public void setBoolState(String key, boolean value) {
        getCurrentState().put(key, value);
    }

    public String getStringState(String key) {
        if(!getCurrentState().containsKey(key)) {
            return null;
        }

        return (String) getCurrentState().get(key);
    }

    public void setStringState(String key, String value) {
        getCurrentState().put(key, value);
    }

    @Override
    public void pre(${model.grammarName}ModelUnparser unparser, RuleInfo ruleInfo, PrintWriter w) {
    
    }

    @Override
    public void post(${model.grammarName}ModelUnparser unparser, RuleInfo ruleInfo, PrintWriter w) {
        setPrevRuleInfo(ruleInfo);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy