Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Blog Archive
Stats
SwiXml Xml schema
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <frame xmlns="http://www.swixml.org/2007/SwixmlTags" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.     xsi:schemaLocation="http://www.swixml.org/2007/SwixmlTags http://www.swixml.org/2007/swixml.xsd"
  4.     title="Config Editor" DefaultCloseOperation="JFrame.EXIT_ON_CLOSE"
  5.     size="800,600">
  6.     <menubar>
  7.         ....
  8.     </menubar>
  9.     <scrollpane id="spEditPane" constraints="BorderLayout.CENTER">
  10.         ....
  11.     </scrollpane>
  12. </frame>
XML Schema for Swixml
CKEditor compact implement guid
implement way 1 : use css
  1. <textarea id="editor" class="ckeditor"></textarea>
implement way 2 : use javascript
  1. <textarea id="editor"></textarea>
  2. <script type="text/javascript">
  3. // default
  4. CKEDITOR.replace("editor");
  5. // with settings
  6. CKEDITOR.replace("editor", {skin:"office2003", toolbar:"Basic"});
  7. </script>
implement way 3 : use jQuery
  1. <textarea id="editor"></textarea>
  2. <script type="text/javascript">
  3. // default
  4. $("#editor").ckeditor();
  5. // with settings
  6. $("#editor").ckeditor(
  7.     function(){
  8.         // callback code
  9.     },
  10.     {
  11.         uiColor: "pink",
  12.         skin: "kama",
  13.         toolbar: [
  14.             ["Source", "Preview"],
  15.             "/",  // line break
  16.             ["Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "Print", "SpellChecker", "Scayt"]
  17.         ]
  18.     }
  19. );
  20. </script>
toolbar properties
"Source"
"Save" "NewPage" "Preview" "Templates"
"Cut" "Copy" "Paste" "PasteText" "PasteFromWord" "Print" "SpellChecker" "Scayt"
"Undo" "Redo" "Find" "Replace" "SelectAll" "RemoveFormat"
"Form" "Checkbox" "Radio" "TextField" "Textarea" "Select" "Button" "ImageButton" "HiddenField"
"Bold" "Italic" "Underline" "Strike" "Subscript" "Superscript"
"NumberedList" "BulletedList" "Outdent" "Indent" "Blockquote"
"JustifyLeft" "JustifyCenter" "JustifyRight" "JustifyBlock"
"Link" "Unlink" "Anchor"
"Image" "Flash" "Table" "HorizontalRule" "Smiley" "SpecialChar" "PageBreak"
"Styles" "Format" "Font" "FontSize"
"TextColor" "BGColor"
"Maximize" "ShowBlocks" "About"

CKEditor 3 JavaScript API Documentation

jQuery UI, adjust font size
  1. .ui-widget { font-family: Verdana, Arial, sans-serif; font-size: 10pt; }
File properties
  1. FOR %%? IN (file_to_be_queried) DO (
  2. ECHO File Name Only : %%~n?
  3. ECHO File Extension : %%~x?
  4. ECHO Name in 8.3 notation : %%~sn?
  5. ECHO File Attributes : %%~a?
  6. ECHO Located on Drive : %%~d?
  7. ECHO File Size : %%~z?
  8. ECHO Last-Modified Date : %%~t?
  9. ECHO Parent Folder : %%~dp?
  10. ECHO Fully Qualified Path : %%~f?
  11. ECHO FQP in 8.3 notation : %%~sf?
  12. ECHO Location in the PATH : %%~dp$PATH:?
  13. )
Get class name in static method
  1. public class Parent {
  2.  
  3.     public static String getClassName() {
  4.         return new Object() {
  5.         }.getClass().getEnclosingClass().getName();
  6.     }
  7.  
  8.     public static void main(String[] args) {
  9.         System.out.println(getClassName());
  10.     }
  11. }
Dynamic Proxy
  1. import java.lang.reflect.InvocationHandler;
  2. import java.lang.reflect.Method;
  3. import java.lang.reflect.Proxy;
  4. import java.util.Date;
  5.  
  6. public class MathObj implements IMath {
  7.  
  8.     @Override
  9.     public double sum(Number... value) {
  10.         double sum = 0;
  11.         for (int i = 0; i < value.length; i++) {
  12.             System.out.print(value[i]);
  13.             if (< value.length - 1)
  14.                 System.out.print(" + ");
  15.             sum += value[i].doubleValue();
  16.         }
  17.         System.out.printf(" = %f%n", sum);
  18.         return sum;
  19.     }
  20.  
  21.     public static void main(String[] args) throws ClassNotFoundException {
  22.         // 建立代理物件
  23.         Object deleagate = Proxy.newProxyInstance(
  24.                 ObjectHandler.class.getClassLoader(),
  25.                 new Class[] { IMath.class },
  26.                 new ObjectHandler(new MathObj())
  27.         );
  28.  
  29.         // 利用代理執行
  30.         double sum = ((IMath) deleagate).sum(1, 2, 3, 4, 5, 6, 7);
  31.  
  32.         System.out.printf("Invoke result: %f%n", sum);
  33.     }
  34.  
  35. }
  36.  
  37. // 定義代理程式
  38. class ObjectHandler implements InvocationHandler {
  39.  
  40.     // 原始物件
  41.     Object o;
  42.  
  43.     public ObjectHandler(Object o) {
  44.         this.= o;
  45.     }
  46.  
  47.     @Override
  48.     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  49.         // 委派代理呼叫方法時, 要處理的動作
  50.         System.out.println("Start at " + new Date());
  51.         Object result = method.invoke(o, args);
  52.         System.out.println("End at " + new Date());
  53.         return result;
  54.     }
  55.  
  56. }
  57.  
  58. // 定義介面
  59. interface IMath {
  60.     double sum(Number... value);
  61. }
感想: 不好用xd
Test file exist in batch
  1. @echo off
  2. SET file=mes.log
  3. SET filename=mes
  4.  
  5. for /f %%a in ('dir /b /a-d %file%') do (
  6. if %%~na==%filename% GOTO :FileFound
  7. )
  8. GOTO :FileNotFound
  9.  
  10. :FileFound
  11. ECHO FILE FOUND
  12. GOTO :BatchEnd
  13.  
  14. :FileNotFound
  15. ECHO FILE NOT FOUND
  16.  
  17. :BatchEnd
  18. ECHO END
SQL Server T-SQL Cheat Sheet
DECLARE and SET Varibales
  1. DECLARE @Mojo int
  2. SET @Mojo = 1
  3. SELECT @Mojo = Column FROM Table WHERE id=1
IF / ELSE IF / ELSE Statement
  1. IF @Mojo < 1
  2. BEGIN
  3. PRINT 'Mojo Is less than 1'
  4. END
  5. ELSE IF @Mojo = 1
  6. BEGIN
  7. PRINT 'Mojo Is 1'
  8. END
  9. ELSE
  10. BEGIN
  11. PRINT 'Mojo greater than 1'
  12. END
CASE Statement
  1. SELECT Day = CASE
  2. WHEN (DateAdded IS NULL) THEN 'Unknown'
  3. WHEN (DateDiff(day, DateAdded, getdate()) = 0) THEN 'Today'
  4. WHEN (DateDiff(day, DateAdded, getdate()) = 1) THEN 'Yesterday'
  5. WHEN (DateDiff(day, DateAdded, getdate()) = -1) THEN 'Tomorrow'
  6. ELSE DATENAME(dw , DateAdded)
  7. END
  8. FROM Table
Add A Column
  1. ALTER TABLE YourTableName ADD [ColumnName] [int] NULL;
Rename a Column
  1. EXEC sp_rename 'TableName.OldColName', 'NewColName','COLUMN';
Rename a Table
  1. EXEC sp_rename 'OldTableName', 'NewTableName';
Add a Foreign Key
  1. ALTER TABLE Products WITH CHECK
  2. ADD CONSTRAINT [FK_Prod_Man] FOREIGN KEY(ManufacturerID)
  3. REFERENCES Manufacturers (ID);
Add a NULL Constraint
  1. ALTER TABLE TableName ALTER COLUMN ColumnName int NOT NULL;
Set Default Value for Column
  1. ALTER TABLE TableName ADD CONSTRAINT
  2. DF_TableName_ColumnName DEFAULT 0 FOR ColumnName;
Create an Index
  1. CREATE INDEX IX_Index_Name ON Table(Columns)
Check Constraint
  1. ALTER TABLE TableName
  2. ADD CONSTRAINT CK_CheckName CHECK (ColumnValue > 1)
DROP a Column
  1. ALTER TABLE TableName DROP COLUMN ColumnName;
Single Line Comments
  1. SET @mojo = 1 --THIS IS A COMMENT
Multi-Line Comments
  1. /* This is a comment
  2. that can span
  3. multiple lines
  4. */
Try / Catch Statements
  1. BEGIN TRY
  2. -- try / catch requires SQLServer 2005
  3. -- run your code here
  4. END TRY
  5. BEGIN CATCH
  6. PRINT 'Error Number: ' + str(error_number())
  7. PRINT 'Line Number: ' + str(error_line())
  8. PRINT error_message()
  9. -- handle error condition
  10. END CATCH
While Loop
  1. DECLARE @i int
  2. SET @i = 0
  3. WHILE (@i < 10)
  4. BEGIN
  5. SET @i = @i + 1
  6. PRINT @i
  7. IF (@i >= 10)
  8. BREAK
  9. ELSE
  10. CONTINUE
  11. END
CREATE a Table
  1. CREATE TABLE TheNameOfYourTable (
  2. ID INT NOT NULL IDENTITY(1,1),
  3. DateAdded DATETIME DEFAULT(getdate()) NOT NULL,
  4. Description VARCHAR(100) NULL,
  5. IsGood BIT DEFAULT(0) NOT NULL,
  6. TotalPrice MONEY NOT NULL,
  7. CategoryID int NOT NULL REFERENCES Categories(ID),
  8. PRIMARY KEY (ID)
  9. );
User Defined Function
  1. CREATE FUNCTION dbo.DoStuff(@ID int)
  2. RETURNS int
  3. AS
  4. BEGIN
  5. DECLARE @result int
  6. IF @ID = 0
  7. BEGIN
  8. RETURN 0
  9. END
  10. SELECT @result = COUNT(*)
  11. FROM table WHERE ID = @ID
  12. RETURN @result
  13. END
  14. GO
  15. SELECT dbo.DoStuff(0)
Reference : SQL Server T-SQL Cheat Sheet
Log4j : Conversion Pattern
ConversionPattern參數的格式含義 
格式名 含義 
%c     輸出日誌資訊所屬的類的全名 
%d     輸出日誌時間點的日期或時間,默認格式為ISO8601,也可以在其後指定格式,比如:%d{yyy-MM-dd HH:mm:ss },輸出類似:2002-10-18- 22:10:28 
%f     輸出日誌資訊所屬的類的類名 
%l     輸出日誌事件的發生位置,即輸出日誌資訊的語句處於它所在的類的第幾行 
%m     輸出代碼中指定的資訊,如log(message)中的message 
%n     輸出一個回車換行符,Windows平臺為“rn”,Unix平臺為“n” 
%p     輸出優先順序,即DEBUG,INFO,WARN,ERROR,FATAL。如果是調用debug()輸出的,則為DEBUG,依此類推 
%r     輸出自應用啟動到輸出該日誌資訊所耗費的毫秒數 
%t     輸出產生該日誌事件的線程名
Level : OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL
JDBC: oracle date type missing "time"
  1. Class.forName(jdbc_driver);
  2. Properties prop = new Properties();
  3. prop.setProperty("user", user);
  4. prop.setProperty("password", password);
  5. // 解決使用 ojdbc14.jar 連某些版本的資料庫,若使用 getObject() 來取得 DATE TYPE 欄位資料時,會有取不到時間的問題
  6. prop.setProperty("oracle.jdbc.V8Compatible", "true");
  7. Connection conn = DriverManager.getConnection(url, prop);
JavaMail sample
  1. import java.io.File;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.Date;
  4. import java.util.Properties;
  5.  
  6. import javax.activation.DataHandler;
  7. import javax.activation.FileDataSource;
  8. import javax.mail.Message;
  9. import javax.mail.MessagingException;
  10. import javax.mail.PasswordAuthentication;
  11. import javax.mail.Session;
  12. import javax.mail.Transport;
  13. import javax.mail.internet.InternetAddress;
  14. import javax.mail.internet.MimeBodyPart;
  15. import javax.mail.internet.MimeMessage;
  16. import javax.mail.internet.MimeMultipart;
  17. import javax.mail.internet.MimeUtility;
  18.  
  19. public class JavaMailSample {
  20.  
  21.     static String smtpHost = "smtp.bruce.com.tw";
  22.     static int smtpPort = 25;
  23.  
  24.     static String[] from = new String[] { "bruce@bruce.com.tw", "Bruce Tasi" };
  25.     static String to = "nanashi@bruce.com.tw";
  26.     static String account = "account";
  27.     static String password = "password";
  28.  
  29.     static String subject = "信件主旨";
  30.     static String contentType = "text/html;charset=utf-8";
  31.     static String body = "<html><body><h1 style=\"color:blue;\">Mail test</h1><img src=\"cid:image01\" /></body></html>";
  32.  
  33.     static Message getMessage(Session session) throws MessagingException, UnsupportedEncodingException {
  34.         Message msg = new MimeMessage(session);
  35.         // 寄件者
  36.         msg.setFrom(new InternetAddress(from[0], from[1]));
  37.         // msg.setFrom(new InternetAddress(String.format("\"%s\"<%s>", from[1], from[0])));
  38.         // 收件者
  39.         InternetAddress[] address = InternetAddress.parse(to, false);
  40.         msg.setRecipients(Message.RecipientType.TO, address);
  41.         // 主旨 (需編碼)
  42.         msg.setSubject(MimeUtility.encodeText(subject, "utf-8", "B"));
  43.         // 寄件時間
  44.         msg.setSentDate(new Date());
  45.  
  46.         // 附件
  47.         File file = new File("write.png");
  48.         if (file.exists()) {
  49.             // 內文
  50.             MimeBodyPart contentBody = new MimeBodyPart();
  51.             // 內容與格式
  52.             contentBody.setContent(body, contentType);
  53.  
  54.             // 附件
  55.             MimeBodyPart attachment = new MimeBodyPart();
  56.             FileDataSource fds = new FileDataSource(file.getName());
  57.             attachment.setDataHandler(new DataHandler(fds));
  58.             // 附件名稱 (需編碼)
  59.             attachment.setFileName(MimeUtility.encodeText(fds.getName(), "utf-8", "B"));
  60.             // 附件代號, 用於顯示郵件內文
  61.             attachment.setHeader("Content-ID", "<image01>");
  62.  
  63.             // 組成郵件
  64.             MimeMultipart mp = new MimeMultipart();
  65.             // 內容顯示附件圖片時, 必須設定為related
  66.             mp.setSubType("related");
  67.             mp.addBodyPart(contentBody);
  68.             mp.addBodyPart(attachment);
  69.             msg.setContent(mp);
  70.         } else {
  71.             // 若沒有檔案時,就直接存郵件內容
  72.             msg.setContent(body, contentType);
  73.         }
  74.         return msg;
  75.     }
  76.  
  77.     static void sendMail1() throws MessagingException, UnsupportedEncodingException {
  78.         // 建立工作階段
  79.         Session session = Session.getDefaultInstance(new Properties(), null);
  80.         // 開啟除錯模式
  81.         session.setDebug(true);
  82.  
  83.         Message msg = getMessage(session);
  84.  
  85.         Transport transport = session.getTransport("smtp");
  86.         transport.connect(smtpHost, smtpPort, account, password);
  87.         transport.sendMessage(msg, msg.getAllRecipients());
  88.         transport.close();
  89.     }
  90.  
  91.     static void sendMail2() throws UnsupportedEncodingException, MessagingException {
  92.         Properties props = new Properties();
  93.  
  94.         props.put("mail.smtp.host", smtpHost);
  95.         props.put("mail.smtp.port", smtpPort);
  96.         props.put("mail.smtp.auth", "true");
  97.         // props.put("mail.smtp.socketFactory.port", smtpPort);
  98.         // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  99.  
  100.         Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
  101.             protected PasswordAuthentication getPasswordAuthentication() {
  102.                 return new PasswordAuthentication(account, password);
  103.             }
  104.         });
  105.         session.setDebug(true);
  106.  
  107.         Message msg = getMessage(session);
  108.  
  109.         Transport.send(msg);
  110.     }
  111.  
  112. }
JavaMail
Dynamic load class & invoke method
  1. import java.io.FileInputStream;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. import java.net.URL;
  5. import java.net.URLClassLoader;
  6. import java.util.jar.JarEntry;
  7. import java.util.jar.JarInputStream;
  8.  
  9. public class ClassLoaderSample {
  10.  
  11.     public static void main(String[] args) throws Exception {
  12.         String jarFile = "C:/workspace/bruce.lib/Blib.jar";
  13.  
  14.         URLClassLoader dynamicLoader = URLClassLoader.newInstance(new URL[] { new URL("file", null, jarFile) }, ClassLoaderTest.class.getClassLoader());
  15.  
  16.         // List content
  17.         JarInputStream jis = new JarInputStream(new FileInputStream(jarFile));
  18.         JarEntry entry = null;
  19.  
  20.         while ((entry = jis.getNextJarEntry()) != null) {
  21.             String name = entry.getName();
  22.             System.out.println(name);
  23.         }
  24.  
  25.         System.out.println("=====");
  26.  
  27.         // Inovke method
  28.         Class objClass = Class.forName("aaa.bbb.PrintTable", true, dynamicLoader);
  29.         Object obj = objClass.newInstance();
  30.         Method objMethod = objClass.getDeclaredMethod("print");
  31.         objMethod.invoke(obj);
  32.  
  33.         // Field value
  34.         Field objField = objClass.getDeclaredField("tableName");
  35.         System.out.println(objField.get(obj));
  36.         // Change field value
  37.         objField.set(obj, "myTable");
  38.     }
  39.  
  40. }