package org.sintef.umt.systemfamily;
import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringReader;
import java.io.StringWriter;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.sintef.umt.transformer.XMLUtility;
import org.sintef.umt.umtmain.UMTMain;
import org.w3c.dom.Element;
public class VariabilityResolver extends JPanel {
private JLabel label = new JLabel ("Not resolved");
private Element modelRoot = null;
VariabilityResolveEngine engine = null;
public VariabilityResolver () {
super (new BorderLayout());
setBackground (VariabilityManager.bgColor);
label.setBackground(VariabilityManager.bgColor);
add(label);
}
public void setModel (Element model) {
modelRoot = model;
}
public void startResolving (String exportType, boolean storeModel, boolean viewModel) {
// First - check for errors
engine = VariabilityFactory.createVariabilityResolveEngine();
Element result = engine.resolveModel(modelRoot);
// System.out.println ("Resulting model ... " + result);
if (exportType.startsWith("UMT")){
// We're ok. Don't need to perform any more transformations
if (storeModel) {
VariabilityManager.storeModel (result, exportType);
}
} else if (exportType.startsWith("XMI") || exportType.startsWith("UML2")) {
// Perform XMI Light 2 XMI Transformation
StringWriter writer = new StringWriter ();
XMLUtility.streamToXML(result, new BufferedWriter(writer));
StringBuffer buf = writer.getBuffer();
XMLUtility xmlUtil = new XMLUtility ();
BufferedReader sourceReader = new BufferedReader(new StringReader(buf.toString()));
String transformationUri = null;
if (exportType.startsWith("XMI"))
transformationUri = UMTMain.resource_dir + UMTMain.getResourceName("transformation.xmilight2xmi");
else if (exportType.startsWith("UML2"))
transformationUri = UMTMain.resource_dir + UMTMain.getResourceName("transformation.xmilight2uml2");
BufferedReader transformationReader = null;
try {
transformationReader = new BufferedReader (new FileReader(transformationUri));
} catch (FileNotFoundException fex) {
System.out.println ("VariabilityManager::startResolving:" + fex);
}
if (transformationReader != null) {
writer = new StringWriter ();
xmlUtil.transform(sourceReader, transformationReader, new BufferedWriter(writer));
buf = writer.getBuffer();
}
if (storeModel) {
VariabilityManager.storeTextModel(result, buf, exportType);
}
}
if (viewModel) {
// spawn a viewer to view the result
ResultViewer theViewer = new ResultViewer (result, this);
theViewer.open();
}
}
}