This piece of code will delete the unused object in the object map.
Just replace the following variables: workspacePath, scriptFolderName and scriptName with appropriate data.
This code is tested but cant assure 100%.
Please use at your own risk.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* @author
*
*/
public class DeleteUnusedObjectRFT {
private static String workspacePath="Z:\\AQA_Projects\\NEGOTIATED_Bonds";//Give your RFT workspace path
private static String scriptFolderName="eNegBonds_Trading.Submit_Termsheet.TestCases".replace(".", "\\");//Give your script package/folder name
private static String scriptName="Submit_Termsheet_As_Underwriter_kishan.java";//Give your rft script name
private static String rftdefFilePath = "";
private static String helperFilePath = "";
private static String scriptFilePath = "";
public static void main(String[] args) throws IOException
{
DeleteUnusedObjectRFT obj = new DeleteUnusedObjectRFT();
obj.initialize();
obj.updateUnusedObjects(obj.getUnusedObjects());
System.out.println(obj.refactorRFTDEF()+" and saved at "+rftdefFilePath);
}
public void initialize()
{
if(scriptFolderName==null){
rftdefFilePath = workspacePath+"\\resources\\"+scriptName.replace(".java", ".rftdef");
helperFilePath = workspacePath + "\\resources\\" + scriptName.replace(".", "Helper.");
scriptFilePath = workspacePath + "\\" + scriptName;
}else{
rftdefFilePath = workspacePath+"\\resources\\"+scriptFolderName+"\\"+scriptName.replace(".java", ".rftdef");
helperFilePath = workspacePath + "\\resources\\" + scriptFolderName + "\\"+ scriptName.replace(".", "Helper.");
scriptFilePath = workspacePath + "\\" + scriptFolderName + "\\" + scriptName;
}
}
@SuppressWarnings("resource")
public ArrayList<String> getUnusedObjects() throws IOException
{
Reader readHelper;
BufferedReader helperBufferReader;
Reader readMainScript;
BufferedReader mainScriptBufferReader;
// Reading helper class
readHelper = new FileReader(helperFilePath);
helperBufferReader = new BufferedReader(readHelper);
// Reading Script files
readMainScript = new FileReader(scriptFilePath);
mainScriptBufferReader = new BufferedReader(readMainScript);
ArrayList<String> mainScriptArray = new ArrayList<String>();
ArrayList<String> usedObjects = new ArrayList<String>();
ArrayList<String> UnusedObjects = new ArrayList<String>();
String tempLine = "";
while ((tempLine = mainScriptBufferReader.readLine()) != null) {
mainScriptArray.add(tempLine);
}
tempLine = "";
int found =0;
@SuppressWarnings("unused")
int allObjectAreUsed=0;
while ((tempLine = helperBufferReader.readLine()) != null) {
if ((tempLine.contains("protected") && tempLine.contains("()"))
&& (!tempLine.contains("Helper")) && (!tempLine.contains("IFtVerificationPoint"))) {
String object = tempLine.split(" ")[2];
found = 0;
for (int i = 0; i < mainScriptArray.size(); i++) {
if (mainScriptArray.get(i).contains(object.replace(")", ""))) {
found++;
}
}
if (found == 0) {
UnusedObjects.add(object);
allObjectAreUsed++;
}
if (found != 0)
{
usedObjects.add(object);
}
}
}
System.out.println("Script Name : "+scriptName);
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("Total Objects exists in the given script : "+(UnusedObjects.size()+usedObjects.size()));
System.out.println("Total Unused Objects in the given script : "+UnusedObjects.size());
System.out.println("Total Used Objects in the given script : " + usedObjects.size());
/*
//if you want to print object names uncomment following code
System.out.println("\n==>Following are the names of Unused Objects in the given script i.e. "+scriptName+"\n");
for ( int i = 0; i<UnusedObjects.size(); i++)
{
System.out.println(UnusedObjects.get(i));
}
System.out.println("\n==>Following are the names of Used Objects in the given script i.e. "+scriptName+"\n");
for ( int i = 0; i<usedObjects.size(); i++)
{
System.out.println(usedObjects.get(i));
}
if (allObjectAreUsed == 0 ) {
System.out.println("All objects are used in the given script i.e. "+scriptName+"\n");
}*/
return UnusedObjects;
}
public String updateUnusedObjects(ArrayList<String> unusedObjectsList) throws IOException
{
File xmlFile = new File(rftdefFilePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
dbFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
NodeList employees = doc.getElementsByTagName("TestObject");
Element emp = null;
int noOfObjectsUpdatedToTrue=0;
for(int i=0; i<employees.getLength();i++){
emp = (Element) employees.item(i);
String s=emp.getElementsByTagName("Name").item(0).getTextContent();
for(int j=0;j<unusedObjectsList.size();j++)
{
if(s.equals(unusedObjectsList.get(j).replace("()", "")))
{
Node name = emp.getElementsByTagName("Deleted").item(0).getFirstChild();
name.setTextContent("true");
noOfObjectsUpdatedToTrue++;
}
}
}
System.out.println("Total number of unused objects updated to TRUE are : "+noOfObjectsUpdatedToTrue);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(rftdefFilePath));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
}
catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {
e1.printStackTrace();
}
return "unused objects updated successfully";
}
/**
*25th July 2015<br>
*<B><U><FONT color="BLUE">Few notes about this method</FONT></U></B><br>
* Following method[refactorRFTDEF()] is optional. I just created to make sure that before and after updating rftdef file xml tags should be same.
<br>actually while creating[updateUnusedObjects() method updates .rftdef file] updated rftdef file it is just converting and creating in standard xml format which is expected and it will not break any functionality.
<br>So you can ignore calling of this method[refactorRFTDEF()]
<br>if required you may need to change this method according to your rftdef file content and replace how you want your final rftdef file.
<br>
* @return
* @throws IOException
*/
public String refactorRFTDEF() throws IOException
{
//Refactor updated rftdef file
File file = new File(rftdefFilePath);
BufferedReader reader = new BufferedReader(new FileReader(file));
String tempStr = "", completeFilecontent = "";
while((tempStr = reader.readLine()) != null)
{
completeFilecontent += tempStr + "\r\n";
}
reader.close();
// replace a word in a file
String temp1 = completeFilecontent.replaceAll("standalone=\"no\"", "");
String temp2 = temp1.replaceAll("L=\".PropSet\"/>", " L=\".PropSet\"></Properties>");
String temp3 = temp2.replaceAll("<KeywordName/>", "<KeywordName></KeywordName>");
String updatedFileContent = temp3.replaceAll("<VisualScriptName/>", "<VisualScriptName></VisualScriptName>");
FileWriter writer = new FileWriter(rftdefFilePath);
writer.write(updatedFileContent);
writer.close();
return "RFTDEF file refactored successfully ";
}
}