Skip to content

Commit

Permalink
Merge pull request #3129 from katysaintin/master
Browse files Browse the repository at this point in the history
Add HMI for AdvancedConverter in an App
  • Loading branch information
shroffk authored Sep 25, 2024
2 parents d391b61 + 1e5450c commit 38a94da
Show file tree
Hide file tree
Showing 16 changed files with 1,089 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,94 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;


import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.csstudio.display.builder.model.ModelPlugin.logger;

/**
* Extension of Converter class to manage opi folder and several features
* assumed in opi2bob_recursive_converter.python script
*
*
* @author Katy SAINTIN
*/

public class AdvancedConverter extends Converter {

// Singleton design pattern
private static AdvancedConverter instance = null;

// different state of the conversion process
protected enum ConvertType {
FILELIST, NEWFILE, SUCCESS, ERROR
};

private IConverterListener listener = null;

public static AdvancedConverter getInstance() {
if (instance == null) {
instance = new AdvancedConverter();
}

return instance;
}

protected class ConverterEvent {

private String[] fileList = null;
private ConvertType type = null;
private File file = null;
private String message = null;

protected String getMessage() {
return message;
}

private void setMessage(String errorMessage) {
this.message = errorMessage;
}

protected File getFile() {
return file;
}

private void setFile(File file) {
this.file = file;
}

protected ConvertType getType() {
return type;
}

private void setType(ConvertType type) {
this.type = type;
}

protected String[] getFileList() {
return fileList;
}

private void setFileList(String[] fileList) {
this.fileList = fileList;
}

}

protected interface IConverterListener {
public void convertEvent(ConverterEvent event); // tracking events
}

protected void setConverterListener(IConverterListener listener) {
this.listener = listener;
}

/**
* Generate argument for the original Converter
*
*
* @param args
* @return
*/
public static String[] generateRecursiveArguments(String[] args) {
public String[] generateRecursiveArguments(String[] args) {
List<String> converterArguments = new ArrayList<>();
if (args != null && args.length > 0) {
String firstArg = args[0];
Expand All @@ -58,9 +128,10 @@ public static String[] generateRecursiveArguments(String[] args) {
}

// Assume a folder for a recursive management
File file = null;
for (String fileName : args) {
if (!fileName.startsWith("-o")) {
File file = new File(fileName);
file = new File(fileName);
if (file.exists()) {
if (file.isDirectory()) {
List<String> listOpiFiles = listOpiFiles(file.getAbsolutePath());
Expand All @@ -82,7 +153,7 @@ public static String[] generateRecursiveArguments(String[] args) {
/**
* This method generate the tree structure hosts bob script and pictures ...
*/
private static void populateTreeStructure(File rootOutputFolder, File outputFolder, File opiRootFolder) {
private void populateTreeStructure(File rootOutputFolder, File outputFolder, File opiRootFolder) {

if (outputFolder != null && opiRootFolder != null) {
// Create the tree structure in root folder then copy all files except opi file
Expand Down Expand Up @@ -117,8 +188,6 @@ private static void populateTreeStructure(File rootOutputFolder, File outputFold
Path copyPath = copyFile.toPath();
Files.copy(tmpPath, copyPath);



} else if (!rootOutputFolder.getAbsolutePath().equals(outputFolder.getAbsolutePath())) {
// Move the corresponding generated bob file in the sub folder
// Find the corresponding bob file
Expand All @@ -139,38 +208,151 @@ private static void populateTreeStructure(File rootOutputFolder, File outputFold
} catch (IOException e) {
String errMessage = "Error to move or copy " + tmpFile.getAbsolutePath();
errMessage = errMessage + " " + e.getMessage();
System.err.println(errMessage);
// e.printStackTrace();
logger.log(Level.WARNING, errMessage, e);
notifyConvertEvent(ConvertType.ERROR, errMessage + " may already exist in the output folder");
e.printStackTrace();
}
}
}
}
}

/**
* Tracking information about the currant converting file and get access to the
* case newFile
*/
protected void traceProgression(File infile, File outfile) {
super.traceProgression(infile, outfile);
notifyConvertEvent(ConvertType.NEWFILE, infile);
}

/**
* This method manage the different state of the conversion process and update
* the JDialog display
*
* @param type
* @param value
*/
private void notifyConvertEvent(ConvertType type, Object value) {
if (listener != null) {
ConverterEvent event = new ConverterEvent();
event.setType(type);
boolean setValue = false;
switch (type) {
case FILELIST: // before conversion
if (value instanceof String[]) {
// get an array with every opi file to convert
setValue = true;
event.setFileList((String[]) value);
}
break;
case NEWFILE: // every new file to convert
if (value instanceof File) {
setValue = true;
event.setFile((File) value);
}
break;
case SUCCESS:
case ERROR:
default:
break;
}

if (!setValue) {
event.setMessage(String.valueOf(value));
}
listener.convertEvent(event);
}
}

/**
* Fix error conversion that Converter not manage
*/
private static void updateScriptsAndBobFiles(File outputFolderFile) {
private void updateScriptsAndBobFiles(File outputFolderFile) {
// Changing import in script
System.out.println("Update import in scripts");
List<String> scriptList = listScriptFiles(outputFolderFile.getAbsolutePath());
List<String> bobList = listBobFiles(outputFolderFile.getAbsolutePath());
scriptList.addAll(bobList);
File tmpFile = null;
scriptList.addAll(bobList);
Path tmpPath;
for (String file : scriptList) {

try {
if (!isScriptFile(file) || file.contains(PHOEBUS))
tmpFile = new File(file);
tmpPath = tmpFile.toPath();
tmpPath = tmpFile.toPath();

// Change import css to phoebus in embedded script
String contains = Files.readString(tmpPath, Charset.defaultCharset());
String newContains = contains.replaceAll(IMPORT_CSS, IMPORT_PHOEBUS);

// add name of the file in phoebus
String name = "<name>" + tmpFile.getName().substring(0, tmpFile.getName().length() - 4) + "</name>\n";

if (isBobFile(tmpFile.getName())) {
// regex used to find the place where the name should be (between the display
// tag and background_color)
String research = "<display(.+?)<background_color>";
Pattern formula = Pattern.compile(research, Pattern.DOTALL);
Matcher match = formula.matcher(newContains);
match.find();
String oldName = match.group(1);
// if the place found before does not contain the name tag we create a name
// based on it file name
if (!oldName.contains("<name>")) {
Pattern bckgrnd = Pattern.compile("<background_color>");
Matcher index = bckgrnd.matcher(newContains);
List<Integer> result = new ArrayList<>();
while (index.find()) {
result.add(index.start());
}
for (int i = 0; i < newContains.length(); i++) {
if (i == result.get(0) - 1) {
newContains = newContains.substring(0, i - 1) + name + newContains.substring(i);
}
}
}

// convert & to &&
// regex used to find the place where the boolean expression should be (between
// the <exp tag and >)
String search = "<exp[^>]*>";
Pattern pattern = Pattern.compile(search);
Matcher matcher = pattern.matcher(newContains);
StringBuffer next = new StringBuffer();
while (matcher.find()) {
String hit = matcher.group();
if (!hit.contains("&amp;&amp;")) {
String modifAnd = hit.replace("&amp;", "&amp;&amp;");
matcher.appendReplacement(next, Matcher.quoteReplacement(modifAnd));
}

}
matcher.appendTail(next);
newContains = next.toString();

// convert | to ||
Pattern pattern2 = Pattern.compile(search);
Matcher matcher2 = pattern2.matcher(newContains);
StringBuffer next2 = new StringBuffer();
while (matcher2.find()) {
String hit = matcher2.group();
if (!hit.contains("||")) {
String modifOr = hit.replace("|", "||");
matcher2.appendReplacement(next2, Matcher.quoteReplacement(modifOr));
}

}
matcher2.appendTail(next2);
newContains = next2.toString();
}

// Replace embedded opi by bob
newContains = newContains.replaceAll(OPI_EXTENSION, BOB_EXTENSION);
newContains = newContains.replaceAll(OPI_EXTENSION.toUpperCase(), BOB_EXTENSION.toUpperCase());
newContains = newContains.replaceAll(OPI_EXTENSION, BOB_EXTENSION); // prevent replaceAll problem for opi becoming .bob in
// the bob file
newContains = newContains.replaceAll("." + OPI_EXTENSION, "." + BOB_EXTENSION);
newContains = newContains.replaceAll("." + OPI_EXTENSION.toUpperCase(), "." + BOB_EXTENSION.toUpperCase());

// Replace name of script for phoebus => phoebus_scritfile.py
for (String scriptFile : scriptList) {
Expand All @@ -183,21 +365,19 @@ private static void updateScriptsAndBobFiles(File outputFolderFile) {
// Write new contains
Files.writeString(tmpPath, newContains, Charset.defaultCharset());
} catch (Exception e) {
e.printStackTrace();
String errMessage = "Error update " + file;
errMessage = errMessage + " " + e.getMessage();
System.err.println(errMessage);
//System.err.println(errMessage);
logger.log(Level.WARNING, errMessage, e);
notifyConvertEvent(ConvertType.ERROR, errMessage);
}
}

}

/**
* Call Converter main class with full arguments list all the opi file
*
* @param i
*/
public static void main(String[] args) {

@Override
protected void launchConversion(String[] args) {
if (args.length == 0 || args[0].startsWith("-h")) {
System.out.println(
"Usage: -main org.csstudio.display.builder.model.AdvancedConverter [-help] [-output /path/to/folder] </path/to/opi/folder>");
Expand All @@ -213,7 +393,6 @@ public static void main(String[] args) {

// Parse arguments in order to generate a full arguments with all opi file
String[] generateArguments = generateRecursiveArguments(args);

// Find opi root folder the shorter path parent folder
File opiRootFolder = null;
File outputFolderFile = null;
Expand All @@ -226,7 +405,8 @@ public static void main(String[] args) {
} catch (Exception e) {
String errMessage = "Error on Output folder " + outputFolderFile.getAbsolutePath() + " creation";
errMessage = errMessage + " " + e.getMessage();
System.err.println(errMessage);
logger.log(Level.WARNING, errMessage, e);
notifyConvertEvent(ConvertType.ERROR, errMessage);
// e.printStackTrace();
}
}
Expand All @@ -248,19 +428,30 @@ public static void main(String[] args) {
}
}
// Call main super class
Converter.main(generateArguments);
System.out.println("Conversion done");
notifyConvertEvent(ConvertType.FILELIST, generateArguments);

super.launchConversion(generateArguments);
System.out.println("Conversion done");
// All the bob are generated in the output folder
// Generate the same tree structure from opi project
// Copy and move scripts and pictures in the generated output folder
System.out.println("populate output tree structure");
populateTreeStructure(outputFolderFile, outputFolderFile, opiRootFolder);

// Pre processing modifications
// Replace import in embedded scripts and scripts file
System.out.println("pre processing modifications");
updateScriptsAndBobFiles(outputFolderFile);
notifyConvertEvent(ConvertType.SUCCESS, "Process is finished");

}

/**
* Call Converter main class with full arguments list all the opi file
*
* @param i
*/
public static void main(String[] args) {
AdvancedConverter.getInstance().launchConversion(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

package org.csstudio.display.builder.model;

import java.util.logging.Logger;

import org.phoebus.framework.spi.AppDescriptor;
import org.phoebus.framework.spi.AppInstance;

@SuppressWarnings("nls")
public class AdvancedConverterApp implements AppDescriptor
{
public static final Logger logger = Logger.getLogger(AdvancedConverterApp.class.getPackageName());
public static final String NAME = "advancedConverter";
public static final String DisplayName = "OPI Converter";


@Override
public String getName() {
return NAME;
}

@Override
public AppInstance create() {
BrowserConverter.displayBrowserConverter();
return null;
}
}

Loading

0 comments on commit 38a94da

Please sign in to comment.