001: /*
002: * CleanJavaCodeAction.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.actions;
013:
014: import java.awt.EventQueue;
015: import java.awt.event.ActionEvent;
016: import java.awt.event.InputEvent;
017: import java.awt.event.KeyEvent;
018:
019: import java.util.regex.Matcher;
020: import java.util.regex.Pattern;
021: import javax.swing.KeyStroke;
022:
023: import workbench.interfaces.TextContainer;
024: import workbench.resource.ResourceMgr;
025: import workbench.util.StringUtil;
026:
027: /**
028: * Action to convert Java code into a SQL statement
029: *
030: * @see workbench.util.StringUtil#cleanJavaString(String)
031: * @author support@sql-workbench.net
032: */
033: public class CleanJavaCodeAction extends WbAction {
034: protected TextContainer client;
035:
036: public CleanJavaCodeAction(TextContainer aClient) {
037: super ();
038: this .client = aClient;
039: this .initMenuDefinition("MnuTxtCleanJavaCode", KeyStroke
040: .getKeyStroke(KeyEvent.VK_L, InputEvent.ALT_MASK));
041: this .setMenuItemName(ResourceMgr.MNU_TXT_SQL);
042: }
043:
044: public void executeAction(ActionEvent e) {
045: boolean selected = true;
046: String code = this .client.getSelectedText();
047: if (code == null || code.length() == 0) {
048: code = this .client.getText();
049: selected = false;
050: }
051: final String sql = cleanJavaString(code);
052: if (sql != null && sql.length() > 0) {
053: final boolean sel = selected;
054: EventQueue.invokeLater(new Runnable() {
055: public void run() {
056: if (sel)
057: client.setSelectedText(sql);
058: else
059: client.setText(sql);
060: }
061: });
062: }
063: }
064:
065: public String cleanJavaString(String aString) {
066: if (StringUtil.isEmptyString(aString))
067: return "";
068: // a regex to find escaped newlines in the literal
069: Pattern newline = Pattern.compile("\\\\n|\\\\r");
070: String lines[] = StringUtil.PATTERN_CRLF.split(aString);
071: StringBuilder result = new StringBuilder(aString.length());
072: int count = lines.length;
073: for (int i = 0; i < count; i++) {
074: String l = lines[i];
075: if (l == null)
076: continue;
077: if (l.trim().startsWith("//")) {
078: l = l.replaceFirst("//", "--");
079: } else {
080: l = l.trim();
081: //if (l.startsWith("\"")) start = 1;
082: int start = l.indexOf("\"");
083: int end = l.lastIndexOf("\"");
084: if (end == start)
085: start = 1;
086: if (end == 0)
087: end = l.length() - 1;
088: if (start > -1)
089: start++;
090: if (start > -1 && end > -1) {
091: l = l.substring(start, end);
092: }
093: }
094: Matcher m = newline.matcher(l);
095: l = m.replaceAll("");
096: l = StringUtil.replace(l, "\\\"", "\"");
097: result.append(l);
098: if (i < count - 1)
099: result.append('\n');
100: }
101: return result.toString();
102: }
103:
104: }
|