Friday, December 16, 2005

 

String Escaper 0.1

download now
使用方法:
解壓縮後在有java的windows環境下執行run.bat
使用ctrl+x (剪下)、ctrl+c (複製)、ctrl+v (貼上)與上面的按鈕進行操作

之前在玩Jakarta Commons
看到了一個還蠻有意思的class
就是org.apache.commons.lang.StringEscapeUtils
它可以幫你把一些特殊字元在各種不同的應用中進行跳脫
我也為這個有趣的class弄了一個對我而言還算方便(其實是簡陋)的GUI介面來使用
最近卻發現我使用它的機會還蠻大的
像是在blog上po程式碼的時候...
所以雖然程式很簡單我還是跟大家分享一下
歡迎大家為它弄出一個更好的介面或功能再分享回來 :)
code:


package org.swanky.tools;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

import org.apache.commons.lang.StringEscapeUtils;

/**
 * StringEscaper
 * 
 * @author swanky
 * @version 0.1, 2005-11-27
 */
@SuppressWarnings("serial")
public class StringEscaper extends JFrame {
   /** frame width */
   public static final int WIDTH = 1024;

   /** frame height */
   public static final int HEIGHT = 768;

   /** text area */
   private JTextArea ta = new JTextArea();

   /** tool bar */
   private JToolBar toolBar = new JToolBar();

   /**
    * constractor
    * 
    * @param arg0
    *            frame title
    * @throws HeadlessException
    */
   public StringEscaper(String arg0) throws HeadlessException {
      super(arg0);
      ta.setFont(new Font("Courier New", Font.PLAIN, 12));
   }

   public static void main(String[] args) {
      StringEscaper f = new StringEscaper("String Escaper");

      // add actions
      f.toolBar.add(f.new EscapeHTML());
      f.toolBar.add(f.new EscapeJava());
      f.toolBar.add(f.new EscapeJavaScript());
      f.toolBar.add(f.new EscapeSQL());
      f.toolBar.add(f.new EscapeXML());

      // set components on content pane
      f.add(f.toolBar, BorderLayout.NORTH);
      JScrollPane sp = new JScrollPane(f.ta);
      f.add(sp, BorderLayout.CENTER);

      // show frame
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setSize(WIDTH, HEIGHT);
      f.setVisible(true);
   }

   class EscapeHTML extends AbstractAction {
      EscapeHTML() {
         putValue(Action.NAME, "EscapeHTML");
         putValue(Action.SHORT_DESCRIPTION, "Escape HTML");
      }

      public void actionPerformed(ActionEvent arg0) {
         ta.setText(StringEscapeUtils.escapeHtml(ta.getText()));
      }
   }

   class EscapeJava extends AbstractAction {
      EscapeJava() {
         putValue(Action.NAME, "EscapeJava");
         putValue(Action.SHORT_DESCRIPTION, "Escape Java");
      }

      public void actionPerformed(ActionEvent arg0) {
         ta.setText(StringEscapeUtils.escapeJava(ta.getText()));
      }
   }

   class EscapeJavaScript extends AbstractAction {
      EscapeJavaScript() {
         putValue(Action.NAME, "EscapeJavaScript");
         putValue(Action.SHORT_DESCRIPTION, "Escape JavaScript");
      }

      public void actionPerformed(ActionEvent arg0) {
         ta.setText(StringEscapeUtils.escapeJavaScript(ta.getText()));
      }
   }

   class EscapeSQL extends AbstractAction {
      EscapeSQL() {
         putValue(Action.NAME, "EscapeSQL");
         putValue(Action.SHORT_DESCRIPTION, "Escape SQL");
      }

      public void actionPerformed(ActionEvent arg0) {
         ta.setText(StringEscapeUtils.escapeSql(ta.getText()));
      }
   }

   class EscapeXML extends AbstractAction {
      EscapeXML() {
         putValue(Action.NAME, "EscapeXML");
         putValue(Action.SHORT_DESCRIPTION, "Escape XML");
      }

      public void actionPerformed(ActionEvent arg0) {
         ta.setText(StringEscapeUtils.escapeXml(ta.getText()));
      }
   }
}


Comments: Post a Comment



<< Home