Java to Sql - Sql to Java TransformThe Java to Sql and vice versa transform help take Java code which has SQL embedded in it and extracts into sql and vice versa. Below is the code which shows how these plugins work but more importantly it shows a real example of a plugin. SqlTransformPlugin.javapackage superquail.plugins.rudster.sqltransform; import javax.swing.JPanel; import superquail.model.application.ApplicationContext; import superquail.plugin.IPluginAction; import superquail.plugin.IPluginMultipleActions; import superquail.plugin.PluginInfoPanel; /** * Sql Transform plugin. Takes java code * and transforms it into sql and vice versa * @author Rudster * */ public class SqlTransformPlugin implements IPluginMultipleActions { private IPluginAction[] pluginActions; public IPluginAction[] getIPluginActions() { if (pluginActions == null){ pluginActions = new IPluginAction[]{ new SqlToJavaTransformAction(), new JavaToSqlTransformAction()}; } return pluginActions; } public String getName() { return "Sql Transform"; } public JPanel[] getSettingsPanel(ApplicationContext context) { JPanel[] panels = new JPanel[1]; panels[0] = new PluginInfoPanel("Rudy Yeung", "March 2007", "1", getName()); return panels; } public void initialize(ApplicationContext context) { // do nothing } public void unInitialize(ApplicationContext context) { // do nothing } } JavaToSqlTransformAction.javapackage superquail.plugins.rudster.sqltransform; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.KeyStroke; import superquail.model.application.ApplicationContext; import superquail.plugin.IPluginAction; import superquail.view.SqlTabPanel; public class JavaToSqlTransformAction implements IPluginAction { public void run(ApplicationContext context) { SqlTabPanel currentSqlTabPanel = context.getViews().getCurrentSqlTabPanel(); String sql = currentSqlTabPanel.getEditor().getText(); if (sql.indexOf("\"") == -1) { //simple check JOptionPane.showMessageDialog(context.getViews().getMainFrame(), "Sql is not transformable."); return; } String transformSql = convertJavaToSql(sql); currentSqlTabPanel.getEditor().setText(transformSql); } public String convertJavaToSql(String sql) { StringBuffer buffer = new StringBuffer(); boolean startIndexFound = true; int quoteIndex = sql.indexOf("\""); //starting string int startIndex = quoteIndex; int endIndex = 0; while (quoteIndex != -1) { int fakeQuoteIndex = sql.indexOf("\\\"", quoteIndex + 1) + 1; //trying to find quote within string quoteIndex = sql.indexOf("\"", quoteIndex + 1); //trying to find end of start of string if (fakeQuoteIndex == quoteIndex) { quoteIndex += 1; continue; } if (quoteIndex == -1) { break; } if (startIndexFound) { //found end index endIndex = quoteIndex; String subSqlString = sql.substring(startIndex + 1, endIndex) + " "; buffer.append(subSqlString.replaceAll("\\\\\"", "\"")); startIndexFound = false; startIndex = quoteIndex; } else if (quoteIndex != -1){ //found start index startIndex = quoteIndex; startIndexFound = true; String gap = sql.substring(endIndex + 1, startIndex); //look at gap and remove + sign buffer.append(gap.replaceAll("\\+", "")); } } return buffer.toString(); } public String getName() { return "Java to Sql"; } public JPanel[] getSettingsPanel(ApplicationContext context) { return null; } public void initialize(ApplicationContext context) { //do nothing } public String getMenuPath() { return "/Plugins/Sql Transform/Java To Sql"; } public void unInitialize(ApplicationContext context) { //do nothing } public KeyStroke getShortCut() { return null; } } SqlToJavaTransformAction.javapackage superquail.plugins.rudster.sqltransform; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.KeyStroke; import superquail.constants.ApplicationConstants; import superquail.model.application.ApplicationContext; import superquail.plugin.IPluginAction; import superquail.view.SqlTabPanel; import superquail.view.SyntaxHighlightTextArea; /** * Takes a sql statement and creates a Java like string out of it. * * @author Rudster * */ public class SqlToJavaTransformAction implements IPluginAction, ApplicationConstants { public void run(ApplicationContext context) { SqlTabPanel panel = context.getViews().getCurrentSqlTabPanel(); if (panel != null) { String sql = panel.getSqlTabData().getSql(); String transformedSql = convertSqlToJava(sql); JScrollPane sp = new JScrollPane(); SyntaxHighlightTextArea textPane = new SyntaxHighlightTextArea(context, SyntaxHighlightTextArea.SYNTAX_JAVA, sp); textPane.setText(transformedSql); sp.setViewportView(textPane); JOptionPane.showMessageDialog(context.getViews().getMainFrame(), sp); } else { String message = "Must select a sql tab"; JOptionPane.showMessageDialog(context.getViews().getMainFrame(), message); } } public String convertSqlToJava(String sql) { String firstWord = sql.trim().split(SPACE)[0].toLowerCase().trim(); StringBuffer transformedSql = new StringBuffer(); transformedSql.append("String " + firstWord + "Sql = " + NEW_LINE); String[] lines = sql.split("\\n|\\r"); for (int i = 0; i < lines.length; i++) { String line = lines[i]; //empty line if (line.trim().length() == 0) { transformedSql.append(NEW_LINE); } else { //regular line //indent transformedSql.append(SPACE+SPACE+SPACE+SPACE); //do spaces char[] val = line.toCharArray(); int len = val.length; int st = 0; while ((st < len) && (val[st] <= ' ')) { transformedSql.append(SPACE); st++; } StringBuffer wordsBuffer = new StringBuffer(); while (st < len) { wordsBuffer.append(val[st]); st++; } transformedSql.append("\" " + wordsBuffer.toString().trim() + " \""); if (i == lines.length - 1) { transformedSql.append(SEMICOLON); } else { transformedSql.append(" + " + NEW_LINE); } } } return transformedSql.toString(); } public String getName() { return "Sql to Java"; } public JPanel[] getSettingsPanel(ApplicationContext context) { return null; } public void initialize(ApplicationContext context) { //do nothing } public String getMenuPath() { return "/Plugins/Sql Transform/Sql To Java"; } public void unInitialize(ApplicationContext context) { //do nothing } public KeyStroke getShortCut() { return null; } } |