| /******************************************************************************* |
| * Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Andras Schmidt, Andras Balogh, Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.framework; |
| |
| import java.io.FileWriter; |
| import java.io.IOException; |
| import java.util.ArrayList; |
| import java.util.Collection; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.Set; |
| |
| import org.eclipse.viatra2.buffers.BufferStore; |
| import org.eclipse.viatra2.codegen.CodeOutputPlugin; |
| import org.eclipse.viatra2.errors.VPMRuntimeException; |
| import org.eclipse.viatra2.framework.properties.VPMProperties; |
| import org.eclipse.viatra2.logger.Logger; |
| |
| /** |
| * A code formatter implementation, that accumulates many code formatters. Code |
| * formatters can be plugged into this code formatter and all output will be |
| * forwarded to all plugged code formatters. This module is used by the VPM |
| * modeling framework to make it available to use more code output plugins the |
| * same time. |
| * |
| * @author Andras Schmidt |
| * |
| * Modified on 2006.09.14 by Istvan Rath: - added logger, props |
| * references - added a call to a listener's init() function upon |
| * registration (add()) Modified on 2008.07.30 by Istvan Rath - add |
| * IFramework reference - update API |
| * |
| */ |
| public class MultiCodeFormatter implements CodeOutputPlugin { |
| |
| protected VPMProperties props; |
| |
| protected Logger logger; |
| |
| protected IFramework frame; |
| |
| public void beginWork() { |
| for (int i = 0; i < listeners.size(); ++i) { |
| CodeOutputPlugin cp = listeners.get(i); |
| cp.beginWork(); |
| } |
| } |
| |
| public void codeOut(String s) throws VPMRuntimeException { |
| for (int i = 0; i < listeners.size(); ++i) { |
| CodeOutputPlugin cp = listeners.get(i); |
| cp.codeOut(s); |
| } |
| } |
| |
| public void endWork() { |
| for (int i = 0; i < listeners.size(); ++i) { |
| CodeOutputPlugin cp = listeners.get(i); |
| cp.endWork(); |
| } |
| // close file buffers in the buffer store
|
| Set<Map.Entry<String, FileWriter>> fileBuffers = BufferStore |
| .getAllFileBuffers(frame.getTopmodel()); |
| if (fileBuffers != null) { |
| try { |
| for (Map.Entry<String, FileWriter> buf : fileBuffers) { |
| buf.getValue().flush(); |
| buf.getValue().close(); |
| } |
| } catch (IOException e) { |
| logger.fatal(e.getMessage()); |
| } |
| // remove stale file buffers
|
| BufferStore.removeFileBuffers(frame.getTopmodel()); |
| } |
| } |
| |
| public void init(IFramework fw) { |
| frame = fw; |
| props = fw.getProperties(); |
| logger = fw.getLogger(); |
| } |
| |
| List<CodeOutputPlugin> listeners = new ArrayList<CodeOutputPlugin>(); |
| |
| /** |
| * Add a listening code output plugin. |
| * |
| * @param listener |
| * this plugin will get all code out events from now |
| */ |
| public void addListener(CodeOutputPlugin listener) { |
| listeners.add(listener); |
| listener.init(frame); |
| } |
| |
| /** |
| * Remove a listening code output plugin. |
| * |
| * @param listener |
| * this plugin will not get all code out events from now |
| */ |
| public void removeListener(CodeOutputPlugin listener) { |
| listeners.remove(listener); |
| } |
| |
| /** |
| * Get all listeners of this code out pipeline |
| * |
| * @returns all listeners |
| */ |
| public Collection<CodeOutputPlugin> getListeners() { |
| return listeners; |
| } |
| |
| } |