Revision: 6634
http://squirrel-sql.svn.sourceforge.net/squirrel-sql/?rev=6634&view=rev
Author: gerdwagner
Date: 2012-06-18 19:17:57 +0000 (Mon, 18 Jun 2012)
Log Message:
-----------
Made SQL formating configurable
Modified Paths:
--------------
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/action/ActionCollection.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/mainframe/MainFrameMenuBar.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/session/SQLInternalFrame.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/session/SessionPanel.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/GlobalPreferencesSheet.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/SQLPanelAPI.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/mainpanel/objecttree/tabs/FormattedSourceTab.java
trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/resources/squirrel.properties
trunk/sql12/plugins/dbcopy/src/main/java/net/sourceforge/squirrel_sql/plugins/dbcopy/CopyProgressMonitor.java
trunk/sql12/plugins/editextras/src/main/java/net/sourceforge/squirrel_sql/plugins/editextras/EditExtrasPlugin.java
trunk/sql12/plugins/editextras/src/main/resources/net/sourceforge/squirrel_sql/plugins/editextras/Resources.properties
trunk/sql12/plugins/graph/src/main/java/net/sourceforge/squirrel_sql/plugins/graph/querybuilder/sqlgen/QueryBuilderSQLGenerator.java
trunk/sql12/plugins/hibernate/src/main/java/net/sourceforge/squirrel_sql/plugins/hibernate/util/HibernateSQLUtil.java
trunk/sql12/plugins/oracle/src/main/java/net/sourceforge/squirrel_sql/plugins/oracle/tab/OracleSourceTab.java
trunk/sql12/plugins/postgres/src/main/java/net/sourceforge/squirrel_sql/plugins/postgres/PostgresPlugin.java
trunk/sql12/plugins/postgres/src/main/java/net/sourceforge/squirrel_sql/plugins/postgres/tab/PostgresSourceTab.java
trunk/sql12/plugins/sqlscript/src/main/java/net/sourceforge/squirrel_sql/plugins/sqlscript/table_script/CreateFileOfCurrentSQLCommand.java
Added Paths:
-----------
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlConfigPrefsTab.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlController.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPanel.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPref.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPrefReader.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPref.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPrefCtrl.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/action/FormatSQLAction.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/action/FormatSQLCommand.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CodeReformator.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CodeReformatorConfig.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CodeReformatorConfigFactory.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CodeReformatorKernel.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CommentSpec.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/ICodeReformator.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/Piece.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/PieceMarkerSpec.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/StateOfPosition.java
trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/preferences/codereformat/
trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/preferences/codereformat/I18NStrings.properties
trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/util/
trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/util/codereformat/
trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/util/codereformat/I18NStrings.properties
trunk/sql12/plugins/editextras/src/main/java/net/sourceforge/squirrel_sql/plugins/editextras/config/
trunk/sql12/plugins/editextras/src/main/resources/net/sourceforge/squirrel_sql/plugins/editextras/config/
trunk/sql12/plugins/editextras/src/main/resources/net/sourceforge/squirrel_sql/plugins/editextras/config/I18NStrings.properties
trunk/sql12/plugins/postgres/src/main/java/net/sourceforge/squirrel_sql/plugins/postgres/tab/ViewSourceTab.java
Removed Paths:
-------------
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/mainpanel/objecttree/tabs/ViewSourceTab.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/codereformat/CodeReformator.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/codereformat/CodeReformatorKernel.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/codereformat/CommentSpec.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/codereformat/ICodeReformator.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/codereformat/Piece.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/codereformat/PieceMarkerSpec.java
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/codereformat/StateOfPosition.java
trunk/sql12/fw/src/main/resources/net/sourceforge/squirrel_sql/fw/codereformat/I18NStrings.properties
trunk/sql12/plugins/editextras/src/main/java/net/sourceforge/squirrel_sql/plugins/editextras/FormatSQLAction.java
trunk/sql12/plugins/editextras/src/main/java/net/sourceforge/squirrel_sql/plugins/editextras/FormatSQLCommand.java
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/action/ActionCollection.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/action/ActionCollection.java 2012-06-09 14:34:58 UTC (rev 6633)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/action/ActionCollection.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -484,8 +484,9 @@
add(new NextSqlAction(_app));
add(new SelectSqlAction(_app));
add(new OpenSqlHistoryAction(_app));
-
- add(new RenameSessionAction(_app));
+ add(new FormatSQLAction(_app));
+
+ add(new RenameSessionAction(_app));
}
}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/mainframe/MainFrameMenuBar.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/mainframe/MainFrameMenuBar.java 2012-06-09 14:34:58 UTC (rev 6633)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/mainframe/MainFrameMenuBar.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -321,6 +321,8 @@
addToMenu(rsrc, NextSqlAction.class, menu);
addToMenu(rsrc, SelectSqlAction.class, menu);
menu.addSeparator();
+ addToMenu(rsrc, FormatSQLAction.class, menu);
+ menu.addSeparator();
addToMenu(rsrc, EditWhereColsAction.class, menu);
menu.addSeparator();
addToMenu(rsrc, NewSQLWorksheetAction.class, menu);
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/session/SQLInternalFrame.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/session/SQLInternalFrame.java 2012-06-09 14:34:58 UTC (rev 6633)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/session/SQLInternalFrame.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -190,6 +190,8 @@
add(actions.get(NextSqlAction.class));
add(actions.get(SelectSqlAction.class));
addSeparator();
+ add(actions.get(FormatSQLAction.class));
+ addSeparator();
add(actions.get(SQLFilterAction.class));
actions.get(SQLFilterAction.class).setEnabled(true);
}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/session/SessionPanel.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/session/SessionPanel.java 2012-06-09 14:34:58 UTC (rev 6633)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/session/SessionPanel.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -576,6 +576,8 @@
add(actions.get(PreviousSqlAction.class));
add(actions.get(NextSqlAction.class));
add(actions.get(SelectSqlAction.class));
+ addSeparator();
+ add(actions.get(FormatSQLAction.class));
}
}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/GlobalPreferencesSheet.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/GlobalPreferencesSheet.java 2012-06-09 14:34:58 UTC (rev 6633)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/GlobalPreferencesSheet.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -23,6 +23,9 @@
import net.sourceforge.squirrel_sql.client.gui.desktopcontainer.DialogWidget;
import net.sourceforge.squirrel_sql.client.plugin.PluginInfo;
import static net.sourceforge.squirrel_sql.client.preferences.PreferenceType.DATATYPE_PREFERENCES;
+
+import net.sourceforge.squirrel_sql.client.preferences.codereformat.FormatSqlConfigPrefsTab;
+import net.sourceforge.squirrel_sql.client.preferences.codereformat.FormatSqlController;
import net.sourceforge.squirrel_sql.fw.gui.CursorChanger;
import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
import net.sourceforge.squirrel_sql.fw.util.StringManager;
@@ -309,6 +312,7 @@
_panels.add(new DataTypePreferencesPanel());
_panels.add(new UpdatePreferencesTab());
_panels.add(new WikiTablePreferencesTab());
+ _panels.add(new FormatSqlConfigPrefsTab(_app));
// Go thru all loaded plugins asking for panels.
PluginInfo[] plugins = _app.getPluginManager().getPluginInformation();
Added: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlConfigPrefsTab.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlConfigPrefsTab.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlConfigPrefsTab.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -0,0 +1,56 @@
+package net.sourceforge.squirrel_sql.client.preferences.codereformat;
+
+import net.sourceforge.squirrel_sql.client.IApplication;
+import net.sourceforge.squirrel_sql.client.preferences.IGlobalPreferencesPanel;
+import net.sourceforge.squirrel_sql.fw.util.StringManager;
+import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
+
+import java.awt.*;
+
+public class FormatSqlConfigPrefsTab implements IGlobalPreferencesPanel
+{
+ private static final StringManager s_stringMgr =
+ StringManagerFactory.getStringManager(FormatSqlConfigPrefsTab.class);
+
+
+ private FormatSqlController _formatSqlController;
+
+ public FormatSqlConfigPrefsTab(IApplication app)
+ {
+ _formatSqlController = new FormatSqlController(app);
+ }
+
+ @Override
+ public void initialize(IApplication app)
+ {
+ }
+
+ @Override
+ public void uninitialize(IApplication app)
+ {
+ }
+
+ @Override
+ public void applyChanges()
+ {
+ _formatSqlController.applyChanges();
+ }
+
+ @Override
+ public String getTitle()
+ {
+ return s_stringMgr.getString("editextras.FormatSqlConfigPrefsTab.title");
+ }
+
+ @Override
+ public String getHint()
+ {
+ return s_stringMgr.getString("editextras.FormatSqlConfigPrefsTab.hint");
+ }
+
+ @Override
+ public Component getPanelComponent()
+ {
+ return _formatSqlController.getPanel();
+ }
+}
Added: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlController.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlController.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlController.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -0,0 +1,88 @@
+package net.sourceforge.squirrel_sql.client.preferences.codereformat;
+
+import net.sourceforge.squirrel_sql.client.IApplication;
+import net.sourceforge.squirrel_sql.fw.util.StringManager;
+import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
+import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
+
+import java.util.ArrayList;
+
+public class FormatSqlController
+{
+ private static final StringManager s_stringMgr =
+ StringManagerFactory.getStringManager(FormatSqlController.class);
+
+
+ private FormatSqlPanel _formatSqlPanel;
+ private FormatSqlPref _formatSqlPref;
+ private IApplication _app;
+
+ public FormatSqlController(IApplication app)
+ {
+ _app = app;
+ _formatSqlPref = FormatSqlPrefReader.loadPref();
+
+ _formatSqlPanel = new FormatSqlPanel(_formatSqlPref.getKeywordBehaviourPrefs());
+ _formatSqlPanel.txtIndentCount.setValue(_formatSqlPref.getIndent());
+ _formatSqlPanel.txtPreferedLineLength.setValue(_formatSqlPref.getPreferedLineLength());
+ }
+
+
+ public void applyChanges()
+ {
+ try
+ {
+ if (null != _formatSqlPanel.txtIndentCount.getText())
+ {
+ try
+ {
+ Integer indent = Integer.valueOf(_formatSqlPanel.txtIndentCount.getText());
+ if (indent >= 0)
+ {
+ _formatSqlPref.setIndent(indent);
+ }
+ }
+ catch (NumberFormatException e)
+ {
+ // ignore
+ }
+ }
+
+ if (null != _formatSqlPanel.txtPreferedLineLength.getText())
+ {
+ try
+ {
+ Integer preferedLineLength = Integer.valueOf(_formatSqlPanel.txtPreferedLineLength.getText());
+ if (preferedLineLength >= 0)
+ {
+ _formatSqlPref.setPreferedLineLength(preferedLineLength);
+ }
+ }
+ catch (NumberFormatException e)
+ {
+ // ignore
+ }
+ }
+
+ ArrayList<KeywordBehaviourPref> buf = new ArrayList<KeywordBehaviourPref>();
+ for (KeywordBehaviourPrefCtrl keywordBehaviourPrefCtrl : _formatSqlPanel._keywordBehaviourPrefCtrls)
+ {
+ keywordBehaviourPrefCtrl.applyChanges();
+ buf.add(keywordBehaviourPrefCtrl.getKeywordBehaviourPref());
+ }
+ _formatSqlPref.setKeywordBehaviourPrefs(buf.toArray(new KeywordBehaviourPref[buf.size()]));
+
+ XMLBeanWriter bw = new XMLBeanWriter(_formatSqlPref);
+ bw.save(FormatSqlPrefReader.getPrefsFile());
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public FormatSqlPanel getPanel()
+ {
+ return _formatSqlPanel;
+ }
+}
Added: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPanel.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPanel.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPanel.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -0,0 +1,118 @@
+package net.sourceforge.squirrel_sql.client.preferences.codereformat;
+
+import net.sourceforge.squirrel_sql.client.util.codereformat.PieceMarkerSpec;
+import net.sourceforge.squirrel_sql.fw.util.StringManager;
+import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
+
+import javax.swing.*;
+import java.awt.*;
+import java.text.NumberFormat;
+import java.util.ArrayList;
+
+public class FormatSqlPanel extends JPanel
+{
+ private static final StringManager s_stringMgr =
+ StringManagerFactory.getStringManager(FormatSqlPanel.class);
+
+
+ public static enum KeywordBehaviour
+ {
+ ALONE_IN_LINE(1, s_stringMgr.getString("editextras.FormatSqlPanel.aloneInLine"), PieceMarkerSpec.TYPE_PIECE_MARKER_IN_OWN_PIECE),
+ START_NEW_LINE(2, s_stringMgr.getString("editextras.FormatSqlPanel.startNewLine"), PieceMarkerSpec.TYPE_PIECE_MARKER_AT_BEGIN),
+ NO_INFLUENCE_ON_NEW_LINE(3, s_stringMgr.getString("editextras.FormatSqlPanel.noInfluenceOnNewLine"), null);
+
+ private String _title;
+ private Integer _pieceMarkerSpecType;
+ private int _id;
+
+ KeywordBehaviour(int id, String title, Integer pieceMarkerSpecType)
+ {
+ _id = id;
+ _title = title;
+ _pieceMarkerSpecType = pieceMarkerSpecType;
+ }
+
+ @Override
+ public String toString()
+ {
+ return _title;
+ }
+
+
+ public int getID()
+ {
+ return _id;
+ }
+
+ public static KeywordBehaviour forId(int id)
+ {
+ for (KeywordBehaviour keywordBehaviour : values())
+ {
+ if(id == keywordBehaviour.getID())
+ {
+ return keywordBehaviour;
+ }
+ }
+ throw new IllegalArgumentException("Invalid ID: " + id);
+ }
+
+ public Integer getPieceMarkerSpecType()
+ {
+ return _pieceMarkerSpecType;
+ }
+ }
+
+
+ JFormattedTextField txtIndentCount;
+ JFormattedTextField txtPreferedLineLength;
+ ArrayList<KeywordBehaviourPrefCtrl> _keywordBehaviourPrefCtrls = new ArrayList<KeywordBehaviourPrefCtrl>();
+
+
+ public FormatSqlPanel(KeywordBehaviourPref[] keywordBehaviourPrefs)
+ {
+ setLayout(new GridBagLayout());
+
+ GridBagConstraints gbc;
+
+ gbc = new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
+ add(new JLabel(s_stringMgr.getString("editextras.FormatSqlPanel.indent")), gbc);
+
+ gbc = new GridBagConstraints(1,0,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
+ txtIndentCount = new JFormattedTextField(NumberFormat.getInstance());
+ txtIndentCount.setColumns(7);
+ add(txtIndentCount, gbc);
+
+
+ gbc = new GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
+ add(new JLabel(s_stringMgr.getString("editextras.FormatSqlPanel.preferedLineLen")), gbc);
+
+ gbc = new GridBagConstraints(1,1,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
+ txtPreferedLineLength = new JFormattedTextField(NumberFormat.getInstance());
+ txtPreferedLineLength.setColumns(7);
+ add(txtPreferedLineLength, gbc);
+
+ gbc = new GridBagConstraints(0,2,2,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(30,5,5,5),0,0);
+ add(new JLabel(s_stringMgr.getString("editextras.FormatSqlPanel.keywordBehavior")), gbc);
+
+
+ int gridy = 2;
+
+ for (KeywordBehaviourPref keywordBehaviourPref : keywordBehaviourPrefs)
+ {
+ _keywordBehaviourPrefCtrls.add(createKeywordBehaviourPrefCtrl(keywordBehaviourPref, ++gridy));
+ }
+ }
+
+ private KeywordBehaviourPrefCtrl createKeywordBehaviourPrefCtrl(KeywordBehaviourPref keywordBehaviourPref, int gridy)
+ {
+ GridBagConstraints gbc;
+ gbc = new GridBagConstraints(0, gridy,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
+ add(new JLabel(keywordBehaviourPref.getKeyWord()), gbc);
+
+ gbc = new GridBagConstraints(1, gridy,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
+ JComboBox cbo = new JComboBox();
+ add(cbo, gbc);
+ return new KeywordBehaviourPrefCtrl(cbo, keywordBehaviourPref);
+ }
+
+}
Added: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPref.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPref.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPref.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -0,0 +1,62 @@
+package net.sourceforge.squirrel_sql.client.preferences.codereformat;
+
+public class FormatSqlPref
+{
+ public static final String JOIN_DISPLAY_STRING = "INNER/LEFT/RIGHT JOIN";
+
+
+ private KeywordBehaviourPref[] _keywordBehaviourPrefs = new KeywordBehaviourPref[]
+ {
+ new KeywordBehaviourPref("SELECT", FormatSqlPanel.KeywordBehaviour.ALONE_IN_LINE.getID()),
+ new KeywordBehaviourPref("UNION", FormatSqlPanel.KeywordBehaviour.ALONE_IN_LINE.getID()),
+ new KeywordBehaviourPref("FROM", FormatSqlPanel.KeywordBehaviour.START_NEW_LINE.getID()),
+
+ new KeywordBehaviourPref(JOIN_DISPLAY_STRING, FormatSqlPanel.KeywordBehaviour.START_NEW_LINE.getID()),
+
+ new KeywordBehaviourPref("WHERE", FormatSqlPanel.KeywordBehaviour.START_NEW_LINE.getID()),
+ new KeywordBehaviourPref("AND", FormatSqlPanel.KeywordBehaviour.START_NEW_LINE.getID()),
+ new KeywordBehaviourPref("OR", FormatSqlPanel.KeywordBehaviour.NO_INFLUENCE_ON_NEW_LINE.getID()),
+ new KeywordBehaviourPref("GROUP", FormatSqlPanel.KeywordBehaviour.START_NEW_LINE.getID()),
+ new KeywordBehaviourPref("ORDER", FormatSqlPanel.KeywordBehaviour.START_NEW_LINE.getID()),
+
+ new KeywordBehaviourPref("INSERT", FormatSqlPanel.KeywordBehaviour.START_NEW_LINE.getID()),
+ new KeywordBehaviourPref("VALUES", FormatSqlPanel.KeywordBehaviour.START_NEW_LINE.getID()),
+ new KeywordBehaviourPref("UPDATE", FormatSqlPanel.KeywordBehaviour.START_NEW_LINE.getID()),
+ new KeywordBehaviourPref("DELETE", FormatSqlPanel.KeywordBehaviour.START_NEW_LINE.getID()),
+ };
+
+
+ private int _indent = 3;
+ private int _preferedLineLength = 80;
+
+ public KeywordBehaviourPref[] getKeywordBehaviourPrefs()
+ {
+ return _keywordBehaviourPrefs;
+ }
+
+ public void setIndent(int indent)
+ {
+ _indent = indent;
+ }
+
+
+ public void setPreferedLineLength(int preferedLineLength)
+ {
+ _preferedLineLength = preferedLineLength;
+ }
+
+ public void setKeywordBehaviourPrefs(KeywordBehaviourPref[] keywordBehaviourPrefs)
+ {
+ _keywordBehaviourPrefs = keywordBehaviourPrefs;
+ }
+
+ public int getIndent()
+ {
+ return _indent;
+ }
+
+ public int getPreferedLineLength()
+ {
+ return _preferedLineLength;
+ }
+}
Added: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPrefReader.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPrefReader.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPrefReader.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -0,0 +1,37 @@
+package net.sourceforge.squirrel_sql.client.preferences.codereformat;
+
+import net.sourceforge.squirrel_sql.client.IApplication;
+import net.sourceforge.squirrel_sql.client.util.ApplicationFiles;
+import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
+
+import java.io.File;
+
+public class FormatSqlPrefReader
+{
+ public static FormatSqlPref loadPref()
+ {
+ try
+ {
+ File xmlFile = getPrefsFile();
+
+ if (false == xmlFile.exists())
+ {
+ return new FormatSqlPref();
+ }
+
+ XMLBeanReader reader = new XMLBeanReader();
+ reader.load(xmlFile, IApplication.class.getClassLoader());
+ return (FormatSqlPref) reader.iterator().next();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static File getPrefsFile()
+ {
+ File userSettingsDirectory = new ApplicationFiles().getUserSettingsDirectory();
+ return new File(userSettingsDirectory, "FormatSqlPrefs.xml");
+ }
+}
Added: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPref.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPref.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPref.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -0,0 +1,37 @@
+package net.sourceforge.squirrel_sql.client.preferences.codereformat;
+
+public class KeywordBehaviourPref
+{
+ private String _keyWord;
+ private int _keywordBehaviourId;
+
+ public KeywordBehaviourPref()
+ {
+ }
+
+ public KeywordBehaviourPref(String keyWord, int keywordBehaviourId)
+ {
+ _keyWord = keyWord;
+ _keywordBehaviourId = keywordBehaviourId;
+ }
+
+ public String getKeyWord()
+ {
+ return _keyWord;
+ }
+
+ public void setKeywordBehaviourId(int keywordBehaviourId)
+ {
+ _keywordBehaviourId = keywordBehaviourId;
+ }
+
+ public int getKeywordBehaviourId()
+ {
+ return _keywordBehaviourId;
+ }
+
+ public void setKeyWord(String keyWord)
+ {
+ _keyWord = keyWord;
+ }
+}
Added: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPrefCtrl.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPrefCtrl.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPrefCtrl.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -0,0 +1,33 @@
+package net.sourceforge.squirrel_sql.client.preferences.codereformat;
+
+import javax.swing.*;
+
+public class KeywordBehaviourPrefCtrl
+{
+ private final JComboBox _cbo;
+ private final KeywordBehaviourPref _keywordBehaviourPref;
+
+ public KeywordBehaviourPrefCtrl(JComboBox cbo, KeywordBehaviourPref keywordBehaviourPref)
+ {
+ _cbo = cbo;
+
+ for (FormatSqlPanel.KeywordBehaviour keywordBehaviour : FormatSqlPanel.KeywordBehaviour.values())
+ {
+ cbo.addItem(keywordBehaviour);
+ }
+ cbo.setSelectedItem(FormatSqlPanel.KeywordBehaviour.forId(keywordBehaviourPref.getKeywordBehaviourId()));
+
+ _keywordBehaviourPref = keywordBehaviourPref;
+ }
+
+ public void applyChanges()
+ {
+ FormatSqlPanel.KeywordBehaviour keywordBehaviour = (FormatSqlPanel.KeywordBehaviour) _cbo.getSelectedItem();
+ _keywordBehaviourPref.setKeywordBehaviourId(keywordBehaviour.getID());
+ }
+
+ public KeywordBehaviourPref getKeywordBehaviourPref()
+ {
+ return _keywordBehaviourPref;
+ }
+}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/SQLPanelAPI.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/SQLPanelAPI.java 2012-06-09 14:34:58 UTC (rev 6633)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/SQLPanelAPI.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -118,6 +118,8 @@
_toolsPopupController.addAction("sqlnext", ac.get(NextSqlAction.class));
_toolsPopupController.addAction("sqlselect", ac.get(SelectSqlAction.class));
+ _toolsPopupController.addAction("format", ac.get(FormatSQLAction.class));
+
_toolsPopupController.addAction("sqlhist", ac.get(OpenSqlHistoryAction.class));
if (_panel.isInMainSessionWindow())
@@ -145,6 +147,9 @@
resources.configureMenuItem(vioAction, item);
}
+ Action formatSqlAction = _panel.getSession().getApplication().getActionCollection().get(FormatSQLAction.class);
+ item = getSQLEntryPanel().addToSQLEntryAreaMenu(formatSqlAction);
+ resources.configureMenuItem(formatSqlAction, item);
}
Copied: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/action/FormatSQLAction.java (from rev 6628, trunk/sql12/plugins/editextras/src/main/java/net/sourceforge/squirrel_sql/plugins/editextras/FormatSQLAction.java)
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/action/FormatSQLAction.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/action/FormatSQLAction.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -0,0 +1,88 @@
+package net.sourceforge.squirrel_sql.client.session.action;
+/*
+ * Copyright (C) 2003 Gerd Wagner
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+import java.awt.event.ActionEvent;
+
+import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
+import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
+import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
+import net.sourceforge.squirrel_sql.fw.util.StringManager;
+
+import net.sourceforge.squirrel_sql.client.IApplication;
+import net.sourceforge.squirrel_sql.client.action.SquirrelAction;
+import net.sourceforge.squirrel_sql.client.session.ISession;
+import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
+
+/**
+ * This action will format the SQL.
+ *
+ * @author Gerd Wagner
+ */
+public class FormatSQLAction extends SquirrelAction implements ISQLPanelAction
+{
+ private static final StringManager s_stringMgr =
+ StringManagerFactory.getStringManager(FormatSQLAction.class);
+
+ /** Logger for this class. */
+ private static final ILogger s_log =
+ LoggerController.createLogger(FormatSQLAction.class);
+
+ /** Current session. */
+ private ISession _session;
+ private ISQLPanelAPI _panel;
+
+
+ public FormatSQLAction(IApplication app)
+ {
+ super(app);
+ }
+
+ public void setSQLPanel(ISQLPanelAPI panel)
+ {
+ _panel = panel;
+ if(null != panel)
+ {
+ _session = panel.getSession();
+ }
+ else
+ {
+ _session = null;
+ }
+ setEnabled(null != _session);
+ }
+
+
+ public void actionPerformed(ActionEvent evt)
+ {
+ if (_session != null)
+ {
+ try
+ {
+ new FormatSQLCommand(_session, _panel).execute();
+ }
+ catch (Throwable ex)
+ {
+ // i18n[editextras.errorProcessingFormat=Error processing Format SQL command: {0}]
+ final String msg = s_stringMgr.getString("editextras.errorProcessingFormat", ex);
+ _session.showErrorMessage(msg);
+ s_log.error(msg, ex);
+ }
+ }
+ }
+
+}
Copied: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/action/FormatSQLCommand.java (from rev 6628, trunk/sql12/plugins/editextras/src/main/java/net/sourceforge/squirrel_sql/plugins/editextras/FormatSQLCommand.java)
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/action/FormatSQLCommand.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/action/FormatSQLCommand.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -0,0 +1,79 @@
+package net.sourceforge.squirrel_sql.client.session.action;
+/*
+ * Copyright (C) 2003 Gerd Wagner
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
+import net.sourceforge.squirrel_sql.client.session.ISession;
+import net.sourceforge.squirrel_sql.client.util.codereformat.CodeReformator;
+import net.sourceforge.squirrel_sql.client.util.codereformat.CodeReformatorConfigFactory;
+import net.sourceforge.squirrel_sql.client.util.codereformat.CommentSpec;
+import net.sourceforge.squirrel_sql.fw.util.BaseException;
+import net.sourceforge.squirrel_sql.fw.util.ICommand;
+import net.sourceforge.squirrel_sql.fw.util.StringUtilities;
+/**
+ * This command will "quote" an SQL string.
+ *
+ * @author Gerd Wagner
+ */
+class FormatSQLCommand implements ICommand
+{
+ private final ISession _session;
+ private ISQLPanelAPI _panel;
+
+ FormatSQLCommand(ISession session, ISQLPanelAPI panel)
+ {
+ super();
+ _session = session;
+ _panel = panel;
+ }
+
+ public void execute() throws BaseException
+ {
+
+ int[] bounds = _panel.getSQLEntryPanel().getBoundsOfSQLToBeExecuted();
+
+ if(bounds[0] == bounds[1])
+ {
+ return;
+ }
+
+ String textToReformat = _panel.getSQLEntryPanel().getSQLToBeExecuted();
+
+ if (null == textToReformat)
+ {
+ return;
+ }
+
+ CommentSpec[] commentSpecs =
+ new CommentSpec[]
+ {
+ new CommentSpec("/*", "*/"),
+ new CommentSpec("--", StringUtilities.getEolStr())
+ };
+
+ String statementSep = _session.getQueryTokenizer().getSQLStatementSeparator();
+
+ CodeReformator cr = new CodeReformator(CodeReformatorConfigFactory.createConfig(_session));
+
+ String reformatedText = cr.reformat(textToReformat);
+
+ _panel.getSQLEntryPanel().setSelectionStart(bounds[0]);
+ _panel.getSQLEntryPanel().setSelectionEnd(bounds[1]);
+ _panel.getSQLEntryPanel().replaceSelection(reformatedText);
+
+ }
+}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/mainpanel/objecttree/tabs/FormattedSourceTab.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/mainpanel/objecttree/tabs/FormattedSourceTab.java 2012-06-09 14:34:58 UTC (rev 6633)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/mainpanel/objecttree/tabs/FormattedSourceTab.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -23,9 +23,7 @@
import java.sql.SQLException;
import net.sourceforge.squirrel_sql.client.session.ISession;
-import net.sourceforge.squirrel_sql.fw.codereformat.CodeReformator;
-import net.sourceforge.squirrel_sql.fw.codereformat.CommentSpec;
-import net.sourceforge.squirrel_sql.fw.codereformat.ICodeReformator;
+import net.sourceforge.squirrel_sql.client.util.codereformat.*;
import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
import net.sourceforge.squirrel_sql.fw.sql.SQLUtilities;
@@ -95,7 +93,7 @@
this.commentSpecs = commentSpecs;
}
statementSeparator = stmtSep;
- formatter = new CodeReformator(stmtSep, this.commentSpecs);
+ formatter = new CodeReformator(CodeReformatorConfigFactory.createConfig(stmtSep, this.commentSpecs));
}
/**
Deleted: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/mainpanel/objecttree/tabs/ViewSourceTab.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/mainpanel/objecttree/tabs/ViewSourceTab.java 2012-06-09 14:34:58 UTC (rev 6633)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/mainpanel/objecttree/tabs/ViewSourceTab.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -1,55 +0,0 @@
-package net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs;
-
-/*
- * Copyright (C) 2007 Rob Manning
- * man...@us...
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/**
- * This class will display the source for a view. This will work for databases that support the SQL standard
- * infomation_schema.views table.
- */
-public class ViewSourceTab extends FormattedSourceTab
-{
- /**
- * Constructor
- *
- * @param hint
- * what the user sees on mouse-over tool-tip
- * @param stmtSep
- * the string to use to separate SQL statements
- */
- public ViewSourceTab(String hint, String stmtSep)
- {
- super(hint);
- super.setCompressWhitespace(true);
- super.setupFormatter(stmtSep, null);
- }
-
- /**
- * @see net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.table.PSFormattedSourceTab#getSqlStatement()
- */
- @Override
- protected String getSqlStatement()
- {
- return
- "select view_definition " +
- "from information_schema.views " +
- "where table_schema = ? " +
- "and table_name = ? ";
- }
-}
Copied: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CodeReformator.java (from rev 6628, trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/codereformat/CodeReformator.java)
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CodeReformator.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CodeReformator.java 2012-06-18 19:17:57 UTC (rev 6634)
@@ -0,0 +1,574 @@
+package net.sourceforge.squirrel_sql.client.util.codereformat;
+
+/*
+ * Copyright (C) 2003 Gerd Wagner
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import net.sourceforge.squirrel_sql.fw.util.StringManager;
+import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
+import net.sourceforge.squirrel_sql.fw.util.StringUtilities;
+import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
+import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
+
+public class CodeReformator implements ICodeReformator
+{
+ private static final StringManager s_stringMgr = StringManagerFactory
+ .getStringManager(CodeReformator.class);
+
+
+
+ /** Platform-specific line separator string */
+ private String _lineSep = StringUtilities.getEolStr();
+
+ private static ILogger s_log = LoggerController.createLogger(CodeReformator.class);
+ private CodeReformatorConfig _codeReformatorConfig;
+
+ public CodeReformator(CodeReformatorConfig codeReformatorConfig)
+ {
+ _codeReformatorConfig = codeReformatorConfig;
+ }
+
+ /**
+ * @see ICodeReformator#reformat(java.lang.String)
+ */
+ public String reformat(String in) {
+ in = flatenWhiteSpaces(in, false);
+
+ PieceMarkerSpec[] markerExcludeComma = createPieceMarkerSpecExcludeColon();
+ String[] pieces =
+ getReformatedPieces(in, markerExcludeComma).toArray(new String[0]);
+
+ pieces = doInsertSpecial(pieces);
+
+ StringBuffer ret = new StringBuffer();
+ int braketCount = 0;
+ for (int i = 0; i < pieces.length; ++i) {
+ if (")".equals(pieces[i])) {
+ --braketCount;
+ }
+ ret.append(indent(pieces[i], braketCount));
+ ret.append(_lineSep);
+ if ("(".equals(pieces[i])) {
+ ++braketCount;
+ }
+ }
+
+ validate(in, ret.toString());
+
+ return ret.toString();
+ }
+
+ private void validate(String beforeReformat, String afterReformat) {
+ String normalizedBefore = getNormalized(beforeReformat);
+ String normalizedAfter = getNormalized(afterReformat);
+
+ if (!normalizedBefore.equalsIgnoreCase(normalizedAfter)) {
+ int minLen = Math.min(normalizedAfter.length(), normalizedBefore
+ .length());
+ StringBuffer diffPos = new StringBuffer();
+ for (int i = 0; i < minLen; ++i) {
+ if (Character.toUpperCase(normalizedBefore.charAt(i)) != Character
+ .toUpperCase(normalizedAfter.charAt(i))) {
+ break;
+ }
+ diffPos.append('-');
+ }
+ diffPos.append('^');
+
+ // i18n[editextras.reformatFailed=Reformat failed, normalized
+ // Strings differ]
+ StringBuilder msg = new StringBuilder(s_stringMgr.getString("editextras.reformatFailed"));
+ msg.append(_lineSep);
+ msg.append(normalizedBefore);
+ msg.append(_lineSep);
+ msg.append(normalizedAfter);
+ msg.append(_lineSep);
+ msg.append(diffPos.toString());
+ msg.append(_lineSep);
+
+ if (s_log.isInfoEnabled()) {
+ s_log.info(msg.toString());
+ }
+
+ throw new IllegalStateException(msg.toString());
+ }
+ }
+
+ /**
+ * Returns a normalized version of a SQL string. Normalized strings before
+ * and after reformatting should be the same. So normalized Strings may be
+ * used for validation of the reformating process.
+ */
+ private String getNormalized(String s) {
+ String ret = s.replaceAll("\\(", " ( ");
+ ret = ret.replaceAll("\\)", " ) ");
+ ret = ret.replaceAll(",", " , ");
+ String sep = _codeReformatorConfig.getStatementSeparator();
+
+ // If our separator is the regular expression special char '|', then
+ // quote it before formatting.
+ if (sep.equals("|")) {
+ sep = "\\|";
+ }
+ ret = ret.replaceAll(sep, concat(" ",sep," "));
+ return flatenWhiteSpaces(ret, true).trim();
+ }
+
+ /**
+ * Concatenates the specified strings and returns the result.
+ *
+ * @param strings the strings in array form to concatenate.
+ *
+ * @return the concatenated result.
+ */
+ private String concat(String... strings) {
+ StringBuilder result = new StringBuilder();
+ for (String string : strings) {
+ result.append(string);
+ }
+ return result.toString();
+ }
+
+ private List<String> getReformatedPieces(String in, PieceMarkerSpec[] markers) {
+ CodeReformatorKernel kernel = new CodeReformatorKernel(
+ _codeReformatorConfig.getStatementSeparator(), markers, _codeReformatorConfig.getCommentSpecs());
+ String[] pieces = kernel.toPieces(in);
+ ArrayList<String> piecesBuf = new ArrayList<String>();
+
+ for (int i = 0; i < pieces.length; ++i) {
+ if (_codeReformatorConfig.getTrySplitLineLen() < pieces[i].length()) {
+ String[] splitPieces = trySplit(pieces[i], 0,
+ _codeReformatorConfig.getTrySplitLineLen());
+ piecesBuf.addAll(Arrays.asList(splitPieces));
+ } else {
+ piecesBuf.add(pieces[i]);
+ }
+ }
+ return piecesBuf;
+ }
+
+ private String[] doInsertSpecial(String[] pieces) {
+ int insertBegin = -1;
+ boolean hasValues = false;
+
+ ArrayList<String> ret = new ArrayList<String>();
+ ArrayList<String> insertPieces = new ArrayList<String>();
+
+ for (int i = 0; i < pieces.length; ++i) {
+ if ("INSERT ".length() <= pieces[i].length()
+ && pieces[i].substring(0, "INSERT ".length())
+ .equalsIgnoreCase("INSERT ")) {
+ if (-1 != insertBegin) {
+ // Inserts are not properly separated. We give up.
+ return pieces;
+ }
+ insertBegin = i;
+ }
+
+ if (-1 == insertBegin) {
+ ret.add(pieces[i]);
+ } else {
+ insertPieces.add(pieces[i]);
+ }
+
+ if (-1 < insertBegin
+ && -1 != pieces[i].toUpperCase().indexOf("VALUES")) {
+ hasValues = true;
+ }
+
+ if (-1 < insertBegin
+ && _codeReformatorConfig.getStatementSeparator().equalsIgnoreCase(pieces[i])) {
+ if (hasValues) {
+ ret.addAll(reformatInsert(insertPieces));
+ } else {
+ // No special treatment
+ ret.addAll(insertPieces);
+ }
+
+ insertBegin = -1;
+ hasValues = false;
+ insertPieces = new ArrayList<String>();
+ }
+ }
+
+ if (-1 < insertBegin) {
+ if (hasValues) {
+ ret.addAll(reformatInsert(insertPieces));
+ } else {
+ // No special treatment
+ ret.addAll(insertPieces);
+ }
+ }
+
+ return ret.toArray(new String[0]);
+ }
+
+ private ArrayList<String> reformatInsert(ArrayList<String> piecesIn) {
+ String[] pieces =
+ splitAsFarAsPossible(piecesIn.toArray(new String[piecesIn.size()]));
+
+ ArrayList<String> insertList = new ArrayList<String>();
+ ArrayList<String> valuesList = new ArrayList<String>();
+ ArrayList<String> behindInsert = new ArrayList<String>();
+
+ StringBuffer statementBegin = new StringBuffer();
+ int braketCountAbsolute = 0;
+ for (int i = 0; i < pieces.length; ++i) {
+ if (3 < braketCountAbsolute) {
+ behindInsert.add(pieces[i]);
+ }
+ if ("(".equals(pieces[i]) || ")".equals(pieces[i])) {
+ ++braketCountAbsolute;
+ }
+
+ if (0 == braketCountAbsolute) {
+ statementBegin.append(pieces[i]).append(' ');
+ }
+ if (1 == braketCountAbsolute && !"(".equals(pieces[i])
+ && !")".equals(pieces[i])) {
+ String buf = pieces[i].trim();
+ if (buf.endsWith(",")) {
+ buf = buf.substring(0, buf.length() - 1);
+ }
+ insertList.add(buf);
+ }
+ if (3 == braketCountAbsolute && !"(".equals(pieces[i])
+ && !")".equals(pieces[i])) {
+ String buf = pieces[i].trim();
+ if (buf.endsWith(",")) {
+ buf = buf.substring(0, buf.length() - 1);
+ }
+ valuesList.add(buf);
+ }
+ }
+
+ ArrayList<String> ret = new ArrayList<String>();
+
+ if (0 == insertList.size()) {
+ // Not successful
+ ret.addAll(piecesIn);
+ return ret;
+ }
+
+ if (insertList.size() == valuesList.size()) {
+ ret.add(statementBegin.toString());
+ StringBuffer insert = new StringBuffer();
+ StringBuffer values = new StringBuffer();
+
+ String insBuf = insertList.get(0);
+ String valsBuf = valuesList.get(0);
+
+ insert.append('(').append(adjustLength(insBuf, valsBuf));
+ values.append('(').append(adjustLength(valsBuf, insBuf));
+
+ for (int i = 1; i < insertList.size(); ++i) {
+ insBuf = insertList.get(i);
+ valsBuf = valuesList.get(i);
+
+ insert.append(',').append(adjustLength(insBuf, valsBuf));
+ values.append(',').append(adjustLength(valsBuf, insBuf));
+ }
+ insert.append(") VALUES");
+ values.append(')');
+ ret.add(insert.toString());
+ ret.add(values.toString());
+ ret.addAll(behindInsert);
+ return ret;
+ } else {
+ // Not successful
+ ret.addAll(piecesIn);
+ return ret;
+ }
+ }
+
+ private String[] splitAsFarAsPossible(String[] pieces) {
+ ArrayList<String> ret = new ArrayList<String>();
+ for (int i = 0; i < pieces.length; i++) {
+ ret.addAll(Arrays.asList(trySplit(pieces[i], 0, 1)));
+ }
+ return ret.toArray(new String[ret.size()]);
+ }
+
+ private String adjustLength(String s1, String s2) {
+ int max = Math.max(s1.length(), s2.length());
+
+ if (s1.length() == max) {
+ return s1;
+ } else {
+ StringBuffer sb = new StringBuffer();
+ sb.append(s1);
+ while (sb.length() < max) {
+ sb.append(' ');
+ }
+ return sb.toString();
+ }
+ }
+
+ private String[] trySplit(String piece, int braketDepth, int trySplitLineLen) {
+ String trimmedPiece = piece.trim();
+ CodeReformatorKernel dum = new CodeReformatorKernel(
+ _codeReformatorConfig.getStatementSeparator(), new PieceMarkerSpec[0], _codeReformatorConfig.getCommentSpecs());
+
+ if (hasTopLevelColon(trimmedPiece, dum)) {
+ PieceMarkerSpec[] pms = createPieceMarkerSpecIncludeColon();
+ CodeReformatorKernel crk = new CodeReformatorKernel(
+ _codeReformatorConfig.getStatementSeparator(), pms, _codeReformatorConfig.getCommentSpecs());
+ String[] splitPieces1 = crk.toPieces(trimmedPiece);
+ if (1 == splitPieces1.length) {
+ return splitPieces1;
+ }
+
+ ArrayList<String> ret = new ArrayList<String>();
+
+ for (int i = 0; i < splitPieces1.length; ++i) {
+ if (trySplitLineLen < splitPieces1[i].length() + braketDepth
+ * _codeReformatorConfig.getIndent().length()) {
+ String[] splitPieces2 = trySplit(splitPieces1[i],
+ braketDepth, trySplitLineLen);
+ for (int j = 0; j < splitPieces2.length; ++j) {
+ ret.add(splitPieces2[j].trim());
+ }
+ } else {
+ ret.add(splitPieces1[i].trim());
+ }
+ }
+ return (purgeEmptyStrings(ret)).toArray(new String[0]);
+ } else {
+ int[] tlbi = getTopLevelBraketIndexes(trimmedPiece, dum);
+ if (-1 != tlbi[0] && tlbi[0] < tlbi[1]) {
+ // ////////////////////////////////////////////////////////////////////////
+ // Split the first two matching toplevel brakets here
+ PieceMarkerSpec[] pms = createPieceMarkerSpecExcludeColon();
+ CodeReformatorKernel crk = new CodeReformatorKernel(
+ _codeReformatorConfig.getStatementSeparator(), pms, _codeReformatorConfig.getCommentSpecs());
+ String[] splitPieces1 = crk.toPieces(trimmedPiece.substring(
+ tlbi[0] + 1, tlbi[1]));
+
+ ArrayList<String> buf = new ArrayList<String>();
+ buf.add(trimmedPiece.substring(0, tlbi[0]).trim());
+ buf.add("(");
+ for (int i = 0; i < splitPieces1.length; ++i) {
+ buf.add(splitPieces1[i]);
+ }
+ buf.add(")");
+ if (tlbi[1] + 1 < trimmedPiece.length()) {
+ buf.add(trimmedPiece.substring(tlbi[1] + 1,
+ trimmedPiece.length()).trim());
+ }
+ splitPieces1 = buf.toArray(new String[0]);
+ //
+ // ////////////////////////////////////////////////////////////////////
+
+ // ///////////////////////////////////////////////////////////////////
+ // Now check length of Strings in splitPieces1 again
+ ArrayList<String> ret = new ArrayList<String>();
+ for (int i = 0; i < splitPieces1.length; ++i) {
+ if (trySplitLineLen < splitPieces1[i].length()
+ + braketDepth * _codeReformatorConfig.getIndent().length()) {
+ String[] splitPieces2 = trySplit(splitPieces1[i],
+ braketDepth + 1, trySplitLineLen);
+ for (int j = 0; j < splitPieces2.length; ++j) {
+ ret.add(splitPieces2[j]);
+ }
+ } else {
+ ret.add(splitPieces1[i]);
+ }
+ }
+ //
+ // ///////////////////////////////////////////////////////////////////
+
+ return (purgeEmptyStrings(ret)).toArray(new String[0]);
+ } else {
+ return new String[] { piece };
+ }
+ }
+ }
+
+ /**
+ * Takes the given list of string and removes elements that are either
+ * null or empty strings
+ *
+ * @param items
+ * @return
+ */
+ private List<String> purgeEmptyStrings(List<String> items) {
+ for (Iterator<String> iter = items.iterator(); iter.hasNext();) {
+ String item = iter.next();
+ if (item == null || "".equals(item)) {
+ iter.remove();
+ }
+ }
+ return items;
+ }
+
+ private boolean hasTopLevelColon(String piece, CodeReformatorKernel crk) {
+ int ix = piece.indexOf(",");
+ StateOfPosition[] stateOfPositions = crk.getStatesOfPosition(piece);
+
+ while (-1 != ix) {
+ if (stateOfPositions[ix].isTopLevel) {
+ return true;
+ }
+ if (ix < piece.length() - 1) {
+ ix = piece.indexOf(",", ix + 1);
+ } else {
+ break;
+ }
+ }
+
+ return false;
+
+ }
+
+ private int[] getTopLevelBraketIndexes(String piece,
+ CodeReformatorKernel crk) {
+ int[] ret = new int[2];
+ ret[0] = -1;
+ ret[1] = -1;
+
+ StateOfPosition[] stateOfPositions = crk.getStatesOfPosition(piece);
+
+ int bra = piece.indexOf("(");
+ while (-1 != bra) {
+ crk.getStatesOfPosition(piece);
+
+ if (0 == bra || stateOfPositions[bra - 1].isTopLevel) {
+ ret[0] = bra;
+ break; // break when first braket found
+ }
+ if (bra < piece.length() - 1) {
+ bra = piece.indexOf("(", bra + 1);
+ } else {
+ break;
+ }
+ }
+
+ if (-1 == ret[0]) {
+ return ret;
+ }
+
+ int ket = piece.indexOf(")", bra);
+ while (-1 != ket) {
+ if (ket == piece.length() - 1 || stateOfPositions[ket].isTopLevel) {
+ // the next top level ket is the counterpart to bra
+ ret[1] = ket;
+ break;
+ }
+ if (ket < piece.length() - 1) {
+ ket = piece.indexOf(")", ket + 1);
+ } else {
+ break;
+ }
+ }
+ return ret;
+ }
+
+ private String indent(String piece, int callDepth) {
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < callDepth; ++i) {
+ sb.append(_codeReformatorConfig.getIndent());
+ }
+ sb.append(piece);
+
+ return sb.toString();
+ }
+
+ private String flatenWhiteSpaces(String in, boolean force) {
+
+ if (hasCommentEndingWithLineFeed(in) && !force) {
+ // No flaten. We would turn statement parts to comment
+ return in;
+ }
+
+ StringBuffer ret = new StringBuffer();
+ int aposCount = 0;
+ for (int i = 0; i < in.length(); ++i) {
+
+ if ('\'' == in.charAt(i)) {
+ ++aposCount;
+ }
+
+ boolean dontAppend = false;
+
+ if (0 != aposCount % 2) {
+
+ } else {
+ if (Character.isWhitespace(in.charAt(i)) && i + 1 < in.length()
+ && Character.isWhitespace(in.charAt(i + 1))) {
+ dontAppend = true;
+ }
+ }
+
+ if (false == dontAppend) {
+ char toAppend;
+ if (Character.isWhitespace(in.charAt(i)) && 0 == aposCount % 2) {
+ toAppend = ' ';
+ } else {
+ toAppend = in.charAt(i);
+ }
+ ret.append(toAppend);
+ }
+ }
+
+ return ret.toString();
+
+ }
+
+ boolean hasCommentEndingWithLineFeed(String in) {
+ CodeReformatorKernel dum = new CodeReformatorKernel(
+ _codeReformatorConfig.getStatementSeparator(), new PieceMarkerSpec[0], _codeReformatorConfig.getCommentSpecs());
+ StateOfPosition[] sops = dum.getStatesOfPosition(in);
+
+ boolean inComment = false;
+ for (int i = 0; i < sops.length; ++i) {
+ if (!inComment && -1 < sops[i].commentIndex) {
+ if (-1 < _codeReformatorConfig.getCommentSpecs()[sops[i].commentIndex].commentEnd
+ .indexOf('\n')) {
+ ...
[truncated message content] |