Tuesday, November 17, 2009

FW: How to copy arrays and objects in Javascript

from: http://my.opera.com/GreyWyvern/blog/show.dml/1725165

How to copy arrays and objects in Javascript

Friday, 8. February 2008, 01:29:48

object, tip, clone, copy, array, javascript
There are a few things that trip people up with regards to Javascript. One is the fact that assigning a boolean or string to a variable makes a copy of that value, while assigning an array or an object to a variable makes a reference to the value. The trouble this causes can range from befuddlement - when two variables you assume are separate are in fact references to the same value - to frustration when you realize there is no native way to tell the Javascript engine to pass a value rather than a reference.

In PHP, for example, all assignments make copies unless you explicitly tell the engine to pass a reference using the =& operator. If only it were so simple in Javascript.

I'm pretty much reinventing the wheel with this post since this issue has been solved before, but the reason I'm writing it is because up until recently I had always been designing custom functions to copy objects of types I designed myself. Very efficient on a per-case basis, but not very reusable. There had to be a way to make such a thing work for any and all arrays and objects. So I searched the interwebs and was enlightened.

Firstly, arrays. Surprisingly, arrays are easy to copy because a couple of native Array object methods actually return a copy of the array. The easiest to use is the slice() method:

var foo = [1, 2, 3];
var bar = foo;
bar[1] = 5;
alert(foo[1]);
// alerts 5

var foo = [1, 2, 3];
var bar = foo.slice(0);
bar[1] = 5;
alert(foo[1]);
// alerts 2


The slice(0) method means, return a slice of the array from element 0 to the end. In other words, the entire array. Voila, a copy of the array. The only caveat to remember here is that this method works if the array contains only simple data types, like numbers, strings and booleans. If the array contains objects or other arrays (a multi-dimensional array), then those contained "objects" will be copied by reference, retaining a connection with the source array. In such a case you will need to copy the array as a full-fledged object.

Objects are trickier because there is no native method which returns a copy of the object. So instead we add one ourselves using a prototype method:

Object.prototype.clone = function() {
var newObj = (this instanceof Array) ? [] : {};
for (i in this) {
if (i == 'clone') continue;
if (this[i] && typeof this[i] == "object") {
newObj[i] = this[i].clone();
} else newObj[i] = this[i]
} return newObj;
};


Notice the recursion going on; isn't it divine? :smile: The clone() method steps through the properties of any object one by one. If the property is itself an object or array, it calls the clone() method on that object too. If the property is anything else, it just takes the value of the property. Then the result received is assigned to a property of the same name in a new object.

Finally, after we're done stepping through all properties, return the new object which is a copy of - not a reference to - the old object. A call to the clone method is as simple as this:

var foo = {a: 1, b: 2, c: 3};
var bar = foo;
bar.b = 5;
alert(foo.b);
// alerts 5

var foo = {a: 1, b: 2, c: 3};
var bar = foo.clone();
bar.b = 5;
alert(foo.b);
// alerts 2

Monday, November 2, 2009

Forms' element samples (checkbox,radio,select,option)

[HTML>
[HEAD>
[TITLE>Checkbox Inspector[/TITLE>
[SCRIPT LANGUAGE="JavaScript">
function inspectBox() {
if (document.forms[0].checkThis.checked) {
alert("The box is checked.")
} else {
alert("The box is not checked at the moment.")
}
}


function inspectRadio() {
for (var i = 0; i [ document.forms[0].sex.length; i++) {
if (document.forms[0].sex[i].checked) {
break
}
}
alert("You chose " + document.forms[0].sex[i].value + ".")
}


function verifySong(entry) {
var song = entry.value
alert("Checking whether " + song + " is a Beatles tune...")
}


function showCarSelected() {
var list = document.forms[0].carSelect
alert("SelectIndex="+list.selectedIndex+" Value="+list.options[list.selectedIndex].value+" "+list.options[list.selectedIndex].text+" Car is selected.");
}


function checkAll(field)
{
for (i = 0; i [ field.length; i++)
field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i [ field.length; i++)
field[i].checked = false ;
}

[/SCRIPT>
[/HEAD>
[BODY>
[FORM>
[INPUT TYPE='checkbox' NAME='checkThis' onClick='javascript:inspectBox()'>Check here[BR>

[input type="radio" name="sex" value="male" onClick='javascript:inspectRadio()' > Male
[br />
[input type="radio" name="sex" value="female" onClick='javascript:inspectRadio()'> Female
[br>

[INPUT TYPE="text" NAME="song" VALUE = "Eleanor Rigby" onChange="verifySong(this)">[P>


[select name="carSelect" onChange="showCarSelected()" >
[option value="0" >Volvo[/option>
[option value="1" >Saab[/option>
[option >Mercedes[/option>
[option selected >Audi[/option>
[/select>


[/FORM>

[form name="myform" action="checkboxes.asp" method="post">

[b>Your Favorite Scripts & Languages[/b>[br>
[input type="checkbox" name="list" value="1">Java[br>
[input type="checkbox" name="list" value="2">Javascript[br>
[input type="checkbox" name="list" value="3">Active Server Pages[br>
[input type="checkbox" name="list" value="4">HTML[br>
[input type="checkbox" name="list" value="5">SQL[br>

[input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.myform.list)">
[input type="button" name="UnCheckAll" value="Uncheck All"
onClick="uncheckAll(document.myform.list)">
[br>
[/FORM>
[/BODY>
[/HTML>

Thursday, October 22, 2009

转:PHP网站开发方案(开发新人必读)

PHP网站开发方案(开发新人必读)
发布时间:2009-9-18 11:30  作者: php.cn  信息来源: PHPchina  发表评论
一、开发成员

a)项目主管
b)页面美工
c)页面开发
d)服务端程序开发
e)系统与数据管理
f)测试与版本控制

二、 网站组开发简明流程


三、 开发工具与环境

a)服务器配置
i. WEB服务器: FreeBSD6.1+Apache2.0+PHP5.0,SVN版本控制服务(仅测试机)。
ii.数据库服务器: WIN2003 server+SQL server 2000+MySQL5.0,CLUSTERED SERVER 集群服务,邮件服务器。
iii. 100M/1000M交换机
b) 开发工具
i. 前台: Macromedia flash 8.0、Macromedia Dreamweaver 8.0、Adobe photoshop CS
ii. 后台: Zend Studio 5.2、SQL Server Enterprise Manager、PhpMyAdmin

四、 技术规则

a) 浏览器兼容策略: 兼容IE5.0以上版本,同时兼容FireFOX2.0以上浏览器。
b) 搜索引擎优化: 着重针对baidu、Google、Yahoo搜索优化,制作清晰明确的网站地图。
c) 字符编码规则:中文网站一率采用GB2312字符标准。[目前我统一使用UTF-8编码包括了数据库的文件内容的编码统一]
d) 文件与文件夹命名: 为兼容win32与unix系统,一率采用小写字母命名。
(统一全站的文件夹与文件的命名都使用小写的形式处理。这样不会出现问题了!)
e) 公共文件命名规则:
i. 默认首页: index.htm、index.html、index.php
ii. 主CSS文件: style.css
iii. 主JS文件: main.js
iv. 主程序配置文件:config.php
f) 公共文件目录统一命名
i. 图片目录: /images 或者 /pic
ii. CSS样式目录: /css
iii. JS脚本程序目录:/js
iv.模板文件目录:/tpl
v. 类或者组件目录:/class 或者 /lib
g) 页面脚本规范: 统一采用JavaScript
h)代码中的类、函数、变量名:统一使用近似英文单词命名,如:DefaultClass 或者 default_class
i) 代码注释规则:所有程序中的函数或者过程必须加确切的注释。
j) 数据库相关规则
i. 数据表与字段命名规则: 全部小写字母命名,并归类命名前缀,如:用户表组,user_passport、user_info、user_service….
ii. 日期类型字段: 统一使用unix时间戳,char(12)
iii. 主关键字命名: 所有表必须建立以id命名的主键。
k) 模板组件规则:统一使用兼容版本的Smarty,统一缓存目录,便于Unix下权限控制。
(设置全部的可写文件统一放入到一个目录下面去做成缓存目录用)
l) 数据库虚拟层: 统一使用Adodb 或 Pdo,SQL语句要兼容现有主流数据库规则。
m)工厂模式开发规则: 以comm.php为中心开发或继承组件类,统一控制文件调用IO与类的实例化。
n) 面向对象开发规则: 所有函数必须以类 ---> 过程的方式存在。
(学会封装代码成类的形式出现)
o) SQL封装规则: 所有SQL语句及数据库查询必须存在于过程中。

p) URL转向规则: 为优化搜索引擎,尽量使用Apache的mod_rewrite模块来美化URL,如:http://www.yourname.com/action.php?id=123转化为:http://www.yourname.com/action/id/123或者http://www.yourname.com/action/id_123.html


五、 网站安全与维护策略

a) 服务器与数据库安全:
i. 建立完善的病毒防御机制,安装防火墙,关闭服务器上任何不必要的端口以及服务。
ii. 统一管理用户权限,定期跟踪用户及系统事件,定期查看系统日志。
b) 容灾与备份机制:
i. 建立数据库集群,至少保持一台服务器同步数据,确保意外发生时数据库系统可自动转移到正常的服务器稳定运行。
ii. 定期备份文件及数据,通过各种方式保存数据与文件。
c) 程序安全策略:
i. SQL注入防范:坚决过滤不可预见的非法字符,严格做好数据库查询、更新的SQL语句检验。
ii. 不使用来路不明的第三方源码,不轻易将未知代码拷贝到服务器。

TAG: 新人 网站开发 PHP php 方案

Tuesday, October 20, 2009

Convert binary file to ASCII file

/************************************************/
/* Convert binary file to ASCII file */
/************************************************/


#include < D:/VC6Green/VC98/Include/math.h>
#include < D:/VC6Green/VC98/Include/stdio.h>
#include < D:/VC6Green/VC98/Include/string.h>



void __ConvertBinFile_To_ASCIIFile(void *ptrInRd, void *ptrOutWr)
{
unsigned char lcByteIn,i;
FILE *ptrRd,*ptrWr;

ptrRd=(FILE *)(ptrInRd);
ptrWr=(FILE *)(ptrOutWr);


i=0;

fprintf(ptrWr, " ");

while (1)
{
lcByteIn = fgetc(ptrRd);
if(feof(ptrRd)!=0)
break;
printf("%02X ",lcByteIn); /* O/P the character to the screen */
fprintf(ptrWr, "%02X ",lcByteIn);
i++;
if (i==16)
{
printf("\n");
fprintf(ptrWr, "\n ");
i=0;
}
}

fclose(ptrRd); /* Close the file. */
fclose(ptrWr); /* Close the file. */

printf("\n");
printf("////////////Done Bin To ASCII conversion //////////// \n");

}




main()
{
FILE *fileRdPtr, *fileWrtPtr;

/* Open Binary file for output. */
fileWrtPtr = fopen("Result.txt","w");
fileRdPtr = fopen("BinaryFile2Convert.bin", "rb");

if(fileRdPtr==NULL)
{
printf("Could not find input binary file: BinaryFile2Convert.bin \n");
return 0;
}

printf("Pls make sure binary input file is using file name: 'BinaryFile2Convert.bin'. Output file is: 'Result.txt'\n");


__ConvertBinFile_To_ASCIIFile(fileRdPtr,fileWrtPtr);

return 0;


}

Tuesday, October 6, 2009

java socket comms

/**
* SimpleSocketClient.java
*
* Created on October 2, 2002, 5:34 PM
*/

import java.awt.*;
import java.net.*; //contains socket class
import java.io.*; //The java.io package provides input and output classes that we will use to extract the data from the response

import java.applet.Applet;


/**
*
* @author Stephen Potts
*/
public class AppletSocketClient // extends Applet

{
Socket socket1;
int portNumber = 20000;
String str = "";


ObjectInputStream ois;

InputStream istream;
OutputStream ostream;

byte[] theByteArray;

/** Creates a new instance of AppletSocketClient */
public AppletSocketClient()
{



try
{


// socket1 = new Socket(InetAddress.getLocalHost(), portNumber);

socket1 = new Socket("192.168.0.201", portNumber);

// socket1 = new Socket("192.168.0.201", portNumber,"192.168.0.1", 8000);

System.out.println("--Debug: build socket ");

istream=socket1.getInputStream();

// ois = new ObjectInputStream(istream);

System.out.println("--Debug: build input stream ");

ostream=socket1.getOutputStream();

// ObjectOutputStream oos = new ObjectOutputStream(socket1.getOutputStream());

System.out.println("--Debug: build output stream ");

str = "initialize";

System.out.println("--Debug: about to send string "+str);

theByteArray=str.getBytes();

ostream.write(theByteArray);

// oos.writeObject(str);

// System.out.println("--Debug: out string"+str);;
//
// str = (String) ois.readObject();
//
// System.out.println("--Debug: input string"+str);
//
// oos.writeObject("bye");



// while ((str = (String) ois.readObject()) == null);

// while ((str = (String) ois.readObject()) != null)
// {
// System.out.println(str);
// oos.writeObject("bye");
//
// if (str.equals("bye bye"))
// break;
// }

System.out.println("--Debug:ois close, oos close, socket close");

// ois.close();
// oos.close();
socket1.close();

} catch (Exception e)
{
System.out.println("Exception " + e);
}
}

public void paint(Graphics g)
{
g.drawString("Hello, Applet", 10, 10);
}


public static void main(String args[])
{
System.out.println("--Debug: main start ");

AppletSocketClient lsp = new AppletSocketClient();
}

}



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/*
* LoopingSocketServer.java
*
* Created on October 2, 2002, 5:24 PM
*/

import java.net.*;
import java.io.*;

/**
*
* @author Stephen Potts
*/
public class AppSocketServer
{
ServerSocket servSocket;
Socket fromClientSocket;
int cTosPortNumber = 20000;
String str;


/** Creates a new instance of LoopingSocketServer */
public AppSocketServer()
{
// Create ServerSocket to listen for connections
try
{
servSocket = new ServerSocket(cTosPortNumber);

// Wait for client to connnect, then get Socket
System.out.println("ServerSocket created");
System.out.println("Waiting for a connection on " + cTosPortNumber);

fromClientSocket = servSocket.accept();
System.out.println("fromClientSocket accepted");

// Use ObjectOutputStream to send String to the client
ObjectOutputStream oos =
new ObjectOutputStream(fromClientSocket.getOutputStream());

//Use ObjectInputStream to get String from client
ObjectInputStream ois =
new ObjectInputStream(fromClientSocket.getInputStream());


while ((str = (String) ois.readObject()) != null)
{
System.out.println("The message from client is *** " + str);

if (str.equals("bye"))
{
oos.writeObject("bye bye");
break;
}
else
{
str = "Server returns " + str;
oos.writeObject(str);
}

}
oos.close();

// Close Sockets
fromClientSocket.close();
} catch (Exception e)
{
System.out.println("Exception " + e);
}
}

public static void main(String args[])
{
AppSocketServer lss = new AppSocketServer();
}


}

Tuesday, September 1, 2009

MiMe header from HTTP Server

// Sample MiMe Header:
// HTTP/1.1·200·OK(CR)(LF)
// Date:·Wed,·02·Sep·2009·04:51:49·GMT(CR)(LF)
// Server:·Apache/2.0.52·(Red·Hat)(CR)(LF)
// Last-Modified:·Mon,·15·Nov·2004·18:23:08·GMT(CR)(LF)
// ETag:·"179c2-1ca39-21dc1b00"(CR)(LF)
// Accept-Ranges:·bytes(CR)(LF)
// Content-Length:·117305(CR)(LF)
// Connection:·close(CR)(LF)
// Content-Type:·application/x-rpm(CR)(LF)
// (CR)(LF)
//
// Faked MiMe Header:
// HTTP/1.1·200·OK0D0A
// Server:·CableTrackerIChip0D0A
// Accept-Ranges:·bytes0D0A
// Content-Length:·xxxx0D0A
// Connection:·close0D0A
// Content-Type:·application/octet-stream0D0A
// 0D0A

//

// HTTP/1.1·200·OK
// Server:·CableTrackerIChip
// Accept-Ranges:·bytes
// Content-Length:·xxxx
// Connection:·close
// Content-Type:·application/octet-stream

Sunday, August 16, 2009

String processing functions in C library

[1] http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/

/* String functions in C library debugged in VC6 IDE

strncmp, strtol, strtok,strncpy,sprintf,atoi,

*/

#include "stdafx.h"
#include
#include //strncmp,strtok
#include //strtol

typedef unsigned char GSNTYPES_BYTE; /* 0 to 255 */
typedef char GSNTYPES_CHAR; /* -128 to 127 */


#define MAX_NUM_BYTES_HTML_TAG_VALUE (256) //limited by ConnectOne WiFi module.



int main()
{
//unsigned int len;

//len = strspn("this is a test","siht ");
//len = strspn("this is a test","se");
//printf("%d\n",len);


const char DASHSTRING[]="-";

const char CMD2TARGETREAD[]="Cmd2Target_Rd_\x31";
GSNTYPES_CHAR lcTagsValue[MAX_NUM_BYTES_HTML_TAG_VALUE];
GSNTYPES_BYTE lcTemp,lcWeb2TargetCmdCode;
char stringTemp[11];
char FakedMiMe_01[]="HTTP/1.1\xb7 200\xb7 OK\r\nServer:\xb7 CableTrackerIChip\r\nContent-Type:\xb7 application/octet-stream\r\n\r\n";
char word[]="\x6d=\x64=";



int i;


sprintf(lcTagsValue,"I/(-1)\r\n");
sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("decimal value of %s is %d \n", stringTemp,atoi(stringTemp));

sprintf(lcTagsValue,"I/(001)\r\n");
sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("decimal value of %s is %d \n", stringTemp,atoi(stringTemp));

sprintf(lcTagsValue,"I/(000)\r\n");
sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("decimal value of %s is %d \n", stringTemp,atoi(stringTemp));


printf (word);
printf(FakedMiMe_01);


// char string []="abc def ghi";
// char * word;


/* char num[15];

// Test a valid number
strcpy(num,"09012345");

printf("%s(Oct) is %i(Dec)\n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)\n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)\n", num, strtol(num, NULL, 16));

puts("----------------------------------");

*/

printf("string is %s \n", CMD2TARGETREAD);


for (i=0;i {

printf("[%i]=%2X \n",i, CMD2TARGETREAD[i]);
}


sprintf(lcTagsValue,"I/(u got me)\r\nW");

sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
printf("before bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("in bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("After CR/LR %s \n",stringTemp);
printf("After CR/LR 0x%02X \n",stringTemp[0]);

printf("len in bracket %u \n",strlen(stringTemp));


sprintf(lcTagsValue,"I/(u got me)\r\n");

sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
printf("before bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("in bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("After CR/LR %s \n",stringTemp);
printf("After CR/LR 0x%02X \n",stringTemp[0]);

printf("len in bracket %u \n",strlen(stringTemp));



//sprintf(lcTagsValue,"Cmd2Target_Rd_0A");
sprintf(lcTagsValue,"11-22-33-AA-BB-CF");

printf("strlen(CMD2TARGETREAD) %d\n", strlen(CMD2TARGETREAD));


sprintf(stringTemp,"%s",strtok(lcTagsValue, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 1 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 2 %02x\n",lcTemp);


sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 3 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 4 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 5 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 6 %02X\n",lcTemp);


/* if (strncmp(CMD2TARGETREAD,lcTagsValue,strlen(CMD2TARGETREAD))==0)
{
//Read command -- 'Cmd2Target_Rd_4F'
//Change ASCII ('0' ~ 'F' )to HEX and refill the gcU2_RxBuffer[]

strncpy(stringTemp,&lcTagsValue[strlen(CMD2TARGETREAD)],2);

lcWeb2TargetCmdCode=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);



}
*/

printf("%d\n",lcWeb2TargetCmdCode);



/*
word=strtok(string, " ");

printf("%s\n",word);

//word=strtok(NULL, " ");
word=strtok(string, " ");

printf("%s\n",word);

*/

}

Wednesday, August 12, 2009

String operation in C/C++/Java -- strtok strspn

/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/* a sample program that prints the index of the first
character in the string pointed to by str1 that does not
match any of the characters in str2.*/

#include "stdafx.h"
#include
#include

int main()
{
//unsigned int len;

//len = strspn("this is a test","siht ");
//len = strspn("this is a test","se");
//printf("%d\n",len);


char string []="abc def ghi";
char * word;

word=strtok(string, " ");

printf("%s\n",word);

//word=strtok(NULL, " ");
word=strtok(string, " ");

printf("%s\n",word);



}






/* STRSTR.C */

#include
#include

char str[] = "lazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] = " 1 2 3 4 5";
char fmt2[] = "12345678901234567890123456789012345678901234567890";

void main( void )
{
char *pdest;
int result;
printf( "String to be searched:\n\t%s\n", string );
printf( "\t%s\n\t%s\n\n", fmt1, fmt2 );
pdest = strstr( string, str );
result = pdest - string + 1;
if( pdest != NULL )
printf( "%s found at position %d\n\n", str, result );
else
printf( "%s not found\n", str );
}


Output

String to be searched:
The quick brown dog jumps over the lazy fox
1 2 3 4 5
12345678901234567890123456789012345678901234567890

lazy found at position 36


String Manipulation Routines

See Also strcspn, strcmp, strpbrk, strrchr, strspn

Tuesday, August 11, 2009

DecimalHexBinary data convertion

{HTML>
{HEAD>
{TITLE>Number Conversion Table{/TITLE>
{/HEAD>
{BODY>
{B>Using toString() to convert to other number bases:{/B>
{HR>
{TABLE BORDER=1>
{TR>
{TH>Decimal{/TH>{TH>Hexadecimal{/TH>{TH>Binary{/TH>{/TR>
{SCRIPT LANGUAGE="JavaScript">
var content = ""
for (var i = 0; i {= 20; i++) {
content += "{TR>"
content += "{TD>" + i.toString(10) + "{/TD>"
content += "{TD>" + i.toString(16) + "{/TD>"
//content += "{TD>" + i.toString(16).toUpperCase() + "{/TD>"
content += "{TD>" + i.toString(2) + "{/TD>{/TR>"
}
document.write(content)
{/SCRIPT>
{/TABLE>
{/BODY>
{/HTML>

Thursday, August 6, 2009

Multiple lines of string in JavaScript. Using 'back slash'

function createWarningPage()
{


content="";

content+="\
{CENTER>\
{a> {b>{SPAN CLASS='preppy'> Device Communication Time out after "+parent.left.timeoutduration+" Seconds . {/SPAN>{/b> {/a>\
{br>\
{a> Please try again. {/a>\
{BR>\
{BR>\
{BR>\
{BR>\
{/CENTER>";

document.write(content);

}

Check PC Info with 'dxdiag' in command line

Escape sequence in string

From: http://www.python.org/doc/2.5.2/ref/strings.html


Friday, July 31, 2009

Timeout in JavaScript -- Sample

{head>

{script language="JavaScript1.2">

//configure flash (1000=1 second)
var speed=500

function flashit(){
var crosstable=document.getElementById? document.getElementById("spaexample") : document.all? document.all.spaexample : ""
if (crosstable){
if (crosstable.style.borderColor.indexOf("green")!=-1)//Start configure border colors//
crosstable.style.borderColor="red"
else
crosstable.style.borderColor="green" //End configure border colors//
}


//document.all.textcolumn.value=speed
mainform.textcolumn.value=speed



if (speed { 510)
{
speed++
setTimeout("flashit()",1000);
}

}


//setInterval("flashit()", speed)


{/script>
{/head>
{body>

{form name=mainform>

{table border="0" width="280" id="spaexample" style="border:5px solid green">
{tr>
{td>
Insert anything you want into this table.
{br>Insert anything you want into this table.
{br>Insert anything you want into this table.{br>{/td>
{/tr>
{/table>

{input type="text" name=textcolumn id="textcolumn" value=initvalue >

{script language="JavaScript1.2"> flashit() {/script>

{/form>
{/body>

Tuesday, June 23, 2009

JavaScripts-Examples

{TITLE>onStop Event Handler{/TITLE>
{SCRIPT LANGUAGE="JavaScript">
var counter = 0
var timerID
function startCounter() {
document.forms[0].display.value = ++counter;

if(document.forms[0].display.value >= 100){
haltCounter();
}
else{

timerID = setTimeout("startCounter()", 10);
}

}
function haltCounter() {

document.forms[0].display.value = "Time due! ";
clearTimeout(timerID);
counter = 0;

}
{!-- document.onstop = haltCounter; -->
{/SCRIPT>
{/HEAD>
{BODY>
{H1>onStop Event Handler{/H1>
{HR>
{P>Click the browser’s Stop button (in IE) to stop the script counter.{/P>
{FORM>
{P>{INPUT TYPE="text", NAME="display">{/P>
{INPUT TYPE="button", VALUE="Start Counter", onClick="startCounter()">
{INPUT TYPE="button", VALUE="Halt Counter", onClick="haltCounter()">
{/FORM>
{/BODY>
{/HTML>

Saturday, May 2, 2009

Alias in Linux

QUENTIN'S ALIAS SUMMARY
---------------------------------




QuentinFedoraHome updated @ 20-NOV-2010 SAT 07:09Am
-------------------------------------------------------
[q.yang@QuentinFedoraHome ~]$ alias
alias cp='cp -pi'
alias egrepin='egrep -i -n'
alias fd='find . -name'
alias l.='ls -d .* --color=tty'
alias la='ls -all --color=tty'
alias lax='ls -all -X'
alias lct='ls -all -ct'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias lt='ll -t'
alias lx='ls -X'
alias mda7='mount /dev/hda7 /mnt/hda7 && ls /mnt/hda7 '
alias mda8='mount /dev/hda8 /mnt/hda8 && ls /mnt/hda8 '
alias mda9='mount /dev/hda9 /mnt/hda9 && ls /mnt/hda9 '
alias mv='mv -i'
alias rm='rm -i'
alias sshuni='ssh quentin@thiazi.snrc.uow.edu.au'
alias svnstart='svnserve -d'
alias tardel='tar --delete --file='
alias tartvf='tar -tvf '
alias uda7='umount /mnt/hda7 && echo "successfully unmounted hda7" '
alias uda8='umount /mnt/hda8 && echo "successfully unmounted hda8" '
alias uda9='umount /mnt/hda9 && echo "successfully unmounted hda9" '
alias vi='vim'
alias webstart='/opt/apache/bin/apachectl start'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'





CREATE YOUR OWN COMMAND WITH ALIAS
---------------------------------------------------
edit file /home/user/.bashrc

Added the short command name for your easy word typing.



[q.yang@QuentinFedoraHome ~]$ vi ./.bashrc# .bashrc
# Source global definitionsif [ -f /etc/bashrc ]; then . /etc/bashrcfi
# User specific aliases and functions
alias mda8='mount /dev/hda8 /mnt/hda8 && ls /mnt/hda8 'alias uda8='umount /mnt/hda8 && echo "successfully unmounted hda8" '
alias mda7='mount /dev/hda7 /mnt/hda7 && ls /mnt/hda7 'alias uda7='umount /mnt/hda7 && echo "successfully unmounted hda7" '
alias mda9='mount /dev/hda9 /mnt/hda9 && ls /mnt/hda9 'alias uda9='umount /mnt/hda9 && echo "successfully unmounted hda9" '
alias sshuni='ssh quentin@thiazi.snrc.uow.edu.au'
alias l.='ls -d .* --color=tty'alias la='ls -all --color=tty'alias lct='ls -all -ct'alias lt='ll -t'alias lax='ls -all -X'alias lx='ls -X'
alias fd='find . -name'

alias mv='mv -i'alias cp='cp -pi'alias rm='rm -i'
alias vi='vim'
alias tardel='tar --delete --file='alias tartvf='tar -tvf '#egrep, ignore case, print line numberalias egrepin='egrep -i -n'
alias stsvn='svnserve -d'

Check Out Repository using Tortoise SVN

right click a newly created folder, and choose checkout on tortoise menus. Filling the URL of the repository as attached figure. svn:\\10.1.1.7\var\svn\repos_ebook\

Monday, April 6, 2009

Install and Uninstall Programs in Fedora

[1] http://www.rootninja.com/easy-install-flash-player-in-firefox-on-fedora-12/

su - root

yum install gcc gcc-c++



rpm -ivh xxxxxx.rpm

1. 以.gz结尾的为压缩文件,用命令:gzip -d filename来解压,得到的文件在当前目录中,但已没有了.gz。

2、以.tar结尾的为归档文件,用命令:tar -xvf filename来展开,生成的文件与源文件在同一目录中,只是少了.tar。


3、以.tar.gz结尾的文件最常见,可直接用命令:gzip -cd filename | tar xfv -来安装

[q.yang@QuentinFedora12 Download]$ gunzip install_flash_player_10_linux.tar.gz
[q.yang@QuentinFedora12 Download]$ ll
total 11700
-rw-rw-r--. 1 q.yang q.yang 11960320 2010-09-25 06:06 install_flash_player_10_linux.tar


[q.yang@QuentinFedora12 Download]$ tar -xvf install_flash_player_10_linux.tar
libflashplayer.so
[q.yang@QuentinFedora12 Download]$ ll
total 23372
-rw-rw-r--. 1 q.yang q.yang 11960320 2010-09-25 06:06 install_flash_player_10_linux.tar
-rwxrwxr-x. 1 q.yang q.yang 11951040 2010-09-15 10:41 libflashplayer.so


# yum install gnash-plugin

# mozilla-plugin-config

Monday, February 16, 2009

Files shared among SVN repositories or projects.

1. Create an folder 'IncludeShare' in project COM as below

+---COM
| +---Include
| +---IncludeShare
| +---Sources
|
\---DAQ
+---Include
+---IncludeShare (appear after update project from repository)
+---Sources

2. Choose properties for DAQ project in SVN right click menu
3. Click 'ADD' Button and select 'svn:externals' in the drop down list of 'property name'.
4. type text in the edit box
'IncludeShare https://JUPITER:8443/svn/CableTracker/trunk/Firmware/COM/IncludeShare'
5. click 'OK' to close all pop up windows.
6. Update project 'DAQ' using SVN menu, then you will see '\IncludeShare' appeared under DAQ project.
7. Check the SVN revision of \DAQ\IncludeShare, you will see that it's pointing to COM project.


Details, you can refer to tortoise help:

5.2.4. Referenced Projects

Sometimes it is useful to construct a working copy that is made out of a number of different checkouts. For example, you may want different subdirectories to come from different locations in a repository, or perhaps from different repositories altogether. If you want every user to have the same layout, you can define the svn:externals properties.

Let's say you check out a working copy of /project1 to D:\dev\project1. Select the folder D:\dev\project1, right click and choose Windows Menu ¡ú Properties from the context menu. The Properties Dialog comes up. Then go to the Subversion tab. There, you can set properties. Select the svn:externals property from the combobox and write in the edit box the repository url in the format name url or if you want to specify a particular revision, name -rREV url You can add multiple external projects, 1 per line. Note that URLs must be properly escaped or they will not work properly. For example you must replace each space with %20. Note that it is not possible to use foldernames with spaces in them. Suppose that you have set these properties on D:\dev\project1:

sounds http://sounds.red-bean.com/repos
quick_graphs http://graphics.red-bean.com/repos/fast%20graphics
skins/toolkit -r21 http://svn.red-bean.com/repos/skin-maker

Now click Set and commit your changes. When you (or any other user) update your working copy, Subversion will create a subfolder D:\dev\project1\sounds and checkout the sounds project, another subfolder D:\dev\project1\quick graphs containing the graphics project, and finally a nested subfolder D:\dev\project1\skins\toolkit containing revision 21 of the skin-maker project.

If the external project is in the same repository, any changes you make there there will be included in the commit list when you commit your main project.

If the external project is in a different repository, any changes you make to the external project will be notified when you commit the main project, but you have to commit those external changes separately.

Note that if you change the URL in an svn:externals property, then next time you update your working copy Subversion will delete the old external folder and make a fresh checkout, so you will see files being Added, rather than being Updated as you might have expected. This situation might occur if you reference a tag from another project. When a new version of that project is released, you change your external reference to point to the new tag.

Thursday, January 22, 2009

教你新建别人进不了删不掉的文件夹

From: http://www.anzstockphoto.com/space.php?uid=3&do=blog&id=18

教你新建别人进不了删不掉的文件夹


建立一个别人既无法进入又无法删除的文件夹。相信大家都遇到过自己的一些隐私文件不愿意让别人看到的情况 吧,怎么解决呢?隐藏起来?换个名字?或者加密?这些办法都可以办到,其实还有一种方法,就是建立一个别人既不能进入又不能删除的文件夹,把自己的隐私文 件放进去,别人就看不到啦,下面讲讲如何实现,很简单的。
第一步:在运行中输入cmd,回车,打开命令行窗口
第二步:在命令行窗口中切换到想要建立文件夹的硬盘分区,如D盘,输入:d: 程序开发,操作系统,服务器,源码下载,Linux,Unix,BSD,PHP,Apach,asp,下载,源码,黑客,安全,技术社区,技术论坛:K,i5m1d4v#D,s!U
第三步:输入md 123..\ 回车,注意文件夹名后有2个小数点
OK,搞定,看看你的D盘下面是不是多了一个名为123.的文件夹了?它是既不能进入又不能被删除的!不信你就试试看吧。 3M5X6h5V0Y2M7n/v/T [
那么,如果自己想删除或者进入这个文件夹,又应该如何操作呢?同样也很简单。 ]4o9K2L(B7n a$L7N
如果想删除,在命令行窗口中输入 rd 123..\ 回车,即可删除,当然删除前请确认里面的文件都是不需要的,不要删错了。 程序开发,操作系统,服务器,源码下载,Linux,Unix,BSD,PHP,Apach,asp,下载,源码,黑客,安全,技术社区,技术论坛:a*\8A/K*o9X6F
如果想进入,在命令行窗口中输入 start d:\123..\ 注意这里一定要是文件夹的绝对路径,否则无法打开即可打开此文件夹,你就可以随心所欲的把不想让别人看到的资料放进去啦!

Thursday, January 15, 2009

提高Java代码可重用性的三个措施

From: http://www.java114.com/278/content4210.html


提高Java代码可重用性的三个措施
 本文介绍了三种修改现有代码提高其可重用性的方法,它们分别是:改写类的实例方法,把参数类型改成接口,选择最简单的参数接口类型。  

措施一:改写类的实例方法  通过类继承实现代码重用不是精确的代码重用技术,因此它并不是最理想的代码重用机制。换句话说,如果不继承整个类的所有方法和数据成员,我们无法重用该类里面的单个方法。继承总是带来一些多余的方法和数据成员,它们总是使得重用类里面某个方法的代码复杂化。另外,派生类对父类的依赖关系也使得代码进一步复杂化:对父类的改动可能影响子类;修改父类或者子类中的任意一个类时,我们很难记得哪一个方法被子类覆盖、哪一个方法没有被子类覆盖;最后,子类中的覆盖方法是否要调用父类中的对应方法有时并不显而易见。  任何方法,只要它执行的是某个单一概念的任务,就其本身而言,它就应该是首选的可重用代码。为了重用这种代码,我们必须回归到面向过程的编程模式,把类的实例方法移出成为全局性的过程。为了提高这种过程的可重用性,过程代码应该象静态工具方法一样编写:它只能使用自己的输入参数,只能调用其他全局性的过程,不能使用任何非局部的变量。这种对外部依赖关系的限制简化了过程的应用,使得过程能够方便地用于任何地方。当然,由于这种组织方式总是使得代码具有更清晰的结构,即使是不考虑重用性的代码也同样能够从中获益。  在Java中,方法不能脱离类而单独存在。为此,我们可以把相关的过程组织成为独立的类,并把这些过程定义为公用静态方法。  例如,对于下面这个类:  class Polygon {  .   .   public int getPerimeter() {...}   public boolean isConvex() {...}  public boolean containsPoint(Point p) {...}   .   .   }   我们可以把它改写成:  class Polygon {  .   .   public int getPerimeter() {return pPolygon.computePerimeter(this);}  public boolean isConvex() {return pPolygon.isConvex(this);}   public boolean containsPoint(Point p) {return pPolygon.containsPoint(this, p);}   .  }   其中,pPolygon是:  class pPolygon {   static public int computePerimeter(Polygon polygon) {...}   static public boolean isConvex(Polygon polygon) {...}   static public boolean   containsPoint(Polygon polygon, Point p) {...}   }   从类的名字pPolygon可以看出,该类所封装的过程主要与Polygon类型的对象有关。名字前面的p表示该类的唯一目的是组织公用静态过程。在Java中,类的名字以小写字母开头是一种非标准的做法,但象pPloygon这样的类事实上并不提供普通Java类的功能。也就是说,它并不代表着一类对象,它只是Java语言组织代码的一种机制。  在上面这个例子中,改动代码的最终效果是使得应用Polygon功能的客户代码不必再从Polygon继承。Polygon类的功能现在已经由pPolygon类以过程为单位提供。客户代码只使用自己需要的代码,无需关心Polygon类中自己不需要的功能。但它并不意味着在这种新式过程化编程中类的作用有所削弱。恰恰相反,在组织和封装对象数据成员的过程中,类起到了不可或缺的作用,而且正如本文接下来所介绍的,类通过多重接口实现多态性的能力本身也带来了卓越的代码重用支持。然而,由于用实例方法封装代码功能并不是首选的代码重用手段,所以通过类继承达到代码重用和多态性支持也不是最理想的。  

措施二:把参数类型改成接口  正如Allen Holub在《Build User Interfaces for Object-Oriented Systems》中所指出的,在面向对象编程中,代码重用真正的要点在于通过接口参数类型利用多态性,而不是通过类继承:  “……我们通过对接口而不是对类编程达到代码重用的目的。如果某个方法的所有参数都是对一些已知接口的引用,那么这个方法就能够操作这样一些对象:当我们编写方法的代码时,这些对象的类甚至还不存在。从技术上说,可重用的是方法,而不是传递给方法的对象。”  在“措施一”得到的结果上应用Holub的看法,当某块代码能够编写为独立的全局过程时,只要把它所有类形式的参数改为接口形式,我们就可以进一步提高它的可重用能力。经过这个改动之后,过程的参数可以是实现了该接口的所有类的对象,而不仅仅是原来的类所创建的对象。由此,过程将能够对可能存在的大量的对象类型进行操作。  例如,假设有这样一个全局静态方法:  static public boolean contains(Rectangle rect, int x, int y) {...}   这个方法用于检查指定的点是否包含在矩形里面。在这个例子中,rect参数的类型可以从Rectangle类改变为接口类型,如下所示:  static public boolean contains(Rectangular rect, int x, int y) {...}   而Rectangular接口的定义是:  public interface Rectangular {Rectangle getBounds();}   现在,所有可以描述为矩形的类(即,实现了Rectangular接口的类)所创建的对象都可以作为提供给pRectangular.contains()的rect参数。通过放宽参数类型的限制,我们使方法具有更好的可重用性。  不过,对于上面这个例子,Rectangular接口的getBounds方法返回Rectangle,你可能会怀疑这么做是否真正值得。换言之,如果我们知道传入过程的对象会在被调用时返回一个Rectangle,为什么不直接传入Rectangle取代接口类型呢?之所以不这么做,最重要的原因与集合有关。让我们假设有这样一个方法:  static public boolean areAnyOverlapping(Collection rects) {...}   该方法用于检查给定集合中的任意矩形对象是否重叠。在这个方法的内部,当我们用循环依次访问集合中的各个对象时,如果我们不能把对象cast成为Rectangular之类的接口类型,又如何能够访问对象的矩形区域呢?唯一的选择是把对象cast成为它特有的类形式(我们知道它有一个方法可以返回矩形),它意味着方法必须事先知道它所操作的对象类型,从而使得方法的重用只限于那几种对象类型。而这正是前面这个措施力图先行避免的问题!  


措施三:选择最简单的参数接口类型  在实施第二个措施时,应该选用哪一种接口类型来取代给定的类形式?答案是哪一个接口完全满足过程对参数的需求,同时又具有最少的多余代码和数据。描述参数对象要求的接口越简单,其他类实现该接口的机会就越大——由此,其对象能够作为参数使用的类也越多。从下面这个例子可以很容易地看出这一点:  static public boolean areOverlapping(Window window1, Window window2) {...}   这个方法用于检查两个窗口(假定是矩形窗口)是否重叠。如果这个方法只要求从参数获得两个窗口的矩形坐标,此时相应地简化这两个参数是一种更好的选择:  static public boolean areOverlapping(Rectangular rect1, Rectangular rect2) {...}   上面的代码假定Window类型实现了Rectangular接口。经过改动之后,对于任何矩形对象我们都可以重用该方法的功能。  有些时候可能会出现描述参数需求的接口拥有太多方法的情况。此时,我们应该在全局名称空间中定义一个新的公共接口供其他面临同一问题的代码重用。  当我们需要象使用C语言中的函数指针一样使用参数时,创建唯一的接口描述参数需求是最好的选择。例如,假设有下面这个过程:  static public void sort(List list, SortComparison comp) {...}   该方法运用参数中提供的比较对象comp,通过比较给定列表list中的对象排序list列表。sort对comp对象的唯一要求是要调用一个方法进行比较。因此,SortComparison应该是只带有一个方法的接口:  public interface SortComparison {  boolean comesBefore(Object a, Object b);  }   SortComparison接口的唯一目的在于为sort提供一个它所需功能的钩子,因此SortComparison接口不能在其他地方重用。  总而言之,本文三个措施适合于改造现有的、按照面向对象惯例编写的代码。这三个措施与面向对象编程技术结合就得到了一种可在以后编写代码时使用的新式代码编写技术,它能够简化方法的复杂性和依赖关系,同时提高方法的可重用能力和内部凝聚力。  当然,这里的三个措施不能用于那些天生就不适合重用的代码。不适合重用的代码通常出现在应用的表现层。例如,创建程序用户界面的代码,以及联结到输入事件的控制代码,都属于那种在程序和程序之间千差万别的代码,这种代码几乎不可能重用。

Thursday, January 8, 2009

Java - GUI development using Eclipse IDE




under eclipse Java is using SWT for GUI development.

http://www.eclipse.org/swt/

Following is a small sample code to demostrate how to use SWT under eclipse to develop GUI using java. Also, it tells how to run your java class under ms-dos command line.

==== CablePowerView.java =============

import org.eclipse.swt.widgets.*;

public class CablePowerView {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Cable Power Viewer Started!");
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Swt working!");
shell. setSize(500, 375);
shell.open();
while(!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep();
display.dispose();
}
}
====== antbuild.xml ===========

Screen shot for antbuild.xml is attached as *.xml can not be displayed properly in the blog.

====== Run it in command line ============

1. From http://www.eclipse.org/swt/
We download SWT Stable Version for Windows (3.4 Final Release - 19 June 2008) swt-3.4-win32-win32-x86.zip
unzip we get swt.jar, then unzip swt.jar we get swt-win32-3448.dll.
2. creat run.bat as follows:
chdir .\CablePowerView\build
java -classpath .\swt.jar; -Djava.library.path=.\ CablePowerView
(Note: swt.jar, swt-win32-3448.dll are stored together with CalbePowerView.class under .\CablePowerView\build
run.bat is stored at root directory
.\ -Djava.library.path=.\ use to tell where swt-win32-3448.dll stored.)
Ms-Dos> run.bat
you will see the result that one window popped up. : - )

Wednesday, January 7, 2009

Java - Serial port communication

1. Java Development Environment set up under winxp.

a. download Eclipse IDE (Eclipse IDE for Java Developers (85 MB)) from
http://www.eclipse.org/downloads/
b. download JDK (Java SE Development Kit (JDK) 6 Update 11 72.9MB) from
http://java.sun.com/javase/downloads/?intcmp=1281

Installing JavaVM (JDK,JRE), and IDE.
The JDK files are placed at C:\Program Files\Java\jdk1.6.0_11\ and the runtime files are placed at C:\Program Files\Java\jdk1.6.0_11\. Ensure the System Variable "path" (held in My Computer->properties->Advanced->Environment Varables->System Variables) includes ";C:\Program Files\Java\jdk1.6.0_11\bin" at the end.

"C:\Program Files\Java\jre6" have JRE installed.

2. Download 'RXTX-2.1-7r2(stable) Binary rxtx-2.1-7-bins-r2.zip ' zip file from
http://www.rxtx.org/ wiki

a. Install RXTX
This involves placing rxtxSerial.dll and rxtxParallell.dll in both C:\Program Files\Java\jdk1.6.0_11\jre\bin and C:\Program Files\Java\jre6\bin. It also involves placing RXTXcomm.jar in both C:\Program Files\Java\jdk1.6.0_11\jre\lib\ext and C:\Program Files\Java\jre6\lib\ext.

b. Example is in link path: usage(Using RXTX)->Code Examples->Writing "Hello World" to a USB to serial converter

This example shows how write a Java program to write out "Hello World" to a serial port or to a USB to serial converter. Before you start you will need to ensure that a suitable Java VM is installed, along with the RXTX Libraries (see platform specifc instructions below).
These instructions all make use of the SimpleWrite.java example file, which you will need to download.


This Java program can be compiled and run by typing the following from the command line:
D:\MyDocXp\MyJavaEclipse\HelloWorld\src> javac SimpleWrite.java
D:\MyDocXp\MyJavaEclipse\HelloWorld\src>java SimpleWrite

"Hello World" should then appear on the device connected to the serial port, assuming that it has been set up to receive a 19200 baud rate, 8 data bits, 1 stop bit, no parity and no handshaking.


c. SimpleWrite.java

import java.io.*;
import java.util.*;
//import javax.comm.*;
import gnu.io.*;
/** * Class declaration * * * @author * @version 1.10, 08/04/00 */public class SimpleWrite { static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!";
static SerialPort serialPort;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;
/** * Method declaration * * * @param args * * @see */ public static void main(String[] args) { boolean portFound = false;
//String defaultPort = "/dev/term/a";
String defaultPort = "COM4";
if (args.length > 0) { defaultPort = args[0];
}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) { System.out.println("Found port " + defaultPort);
portFound = true;
try { serialPort = (SerialPort) portId.open("SimpleWrite", 2000);
} catch (PortInUseException e) { System.out.println("Port in use.");
continue;
}
try { outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try { serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try { serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) { System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit(-1);
}
System.out.println( "Writing \""+messageString+"\" to " +serialPort.getName());
try { outputStream.write(messageString.getBytes());
} catch (IOException e) {}
try { Thread.sleep(2000); // Be sure data is xferred before closing } catch (Exception e) {} serialPort.close();
System.exit(1);
} } }
if (!portFound) { System.out.println("port " + defaultPort + " not found.");
} }
}

struts开发实践—曲线图实例

From: http://www.7880.com/Info/Article-456d7000.html

本案主要功能是完成曲线图的绘制,并将曲线图生成jpg图形并显示。
1。画图的主要函数说明
Graphics g
g.drawRect(x_top,x_bottom,width,height):画矩形
g.setColor(color):指定颜色;
g.fillRect(x_top,x_bottom,width,height):画填充的矩形//
g.drawRoundRect(x_top,x_bottom,width,height):画圆边矩形
g.fillRoundRect(x_top,x_bottom,width,height):画一个填充的圆边矩形
g.drawArc(beg_x,beg_y,width,height,beg_tangle,end_tangle):画弧线
g.fillArc(beg_x,beg_y,width,height,beg_tangle,end_tangle):填充弧
g.drawLine(beg_x,beg_y,end_x,end_y):画一条直线
g.drawString(theString,stringTopLeft_x,StringTopLeft_y):画字
g.setFont(font):设置画笔的字体
g.setPaint(color):设置画笔的颜色
2。曲线图绘制文件
/*****************program CurveFrame begin************************/
package test;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.JFrame;
import java.lang.Math;
/** *曲线图绘制 */
public class CurveFrame
extends JFrame {
/**数据*/
private double[] result;
/**数据项名称*/
private String[] title;
/**参数:结果集,名称集*/
public CurveFrame(double[] result, String[] title) {
this.result = result;
this.title = title;
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Dimension dim = this.getSize();
g2.setColor(Color.white);
g2.fillRect(0, 0, dim.width, dim.height);
//写title;
Font font = g2.getFont().deriveFont(12.0f);
g2.setFont(font);
FontMetrics metrics = g2.getFontMetrics();
g2.setPaint(Color.black);
g2.drawString(title[0], (dim.width - title[0].length() * 5) / 2, 10);
// draw the x and y axis
g2.setStroke(new BasicStroke(2.0f));
g2.draw(new Line2D.Double(40, 30, 40, dim.height - 30));
g2.draw(new Line2D.Double(40, dim.height - 30,
dim.width - 20, dim.height - 30));
long unit=(Math.round((result[0]+750)/1500))*50;
long yMax = result[0] == 0 ? unit : Math.round((result[0]+unit/2)/unit) * unit;
int widthPer = (dim.width - 40) / result.length;
long heightPer = (dim.height - 60) / (yMax / unit);
//draw the y axis scale;
g2.setPaint(Color.lightGray);
g2.setStroke(new BasicStroke(1.0f));
for (int i = 0; i<=yMax / unit; i++) {
g2.draw(new Line2D.Double(40, dim.height - 30 - i * heightPer,
dim.width - 40, dim.height - 30 - i * heightPer));
String ylabel = String.valueOf(i * unit);
g2.drawString(ylabel, 35 - metrics.stringWidth(ylabel),
dim.height - 30 - (i * heightPer));
}
//draw x title;
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
result.length - 2);
for (int i = 1; i < result.length; i++) {
//draw x scale;
g2.setPaint(Color.lightGray);
g2.setStroke(new BasicStroke(1.0f));
g2.draw(new Line2D.Double(40 + (i - 1) * widthPer, dim.height - 30,
40 + (i - 1) * widthPer,30));
font = g2.getFont().deriveFont(10.0f);
g2.setFont(font);
g2.setPaint(Color.black);
g2.drawString(title[i], 40 + widthPer * (i - 1)-metrics.stringWidth(title[i])/2, dim.height - 10);
g2.setPaint(Color.red);
g2.setStroke(new BasicStroke(2.0f));
if (i == 1) {
path.moveTo(40,
Math.round(dim.height - 30 -
(result[i] / unit) * heightPer));
}
else {
path.lineTo(Math.round(widthPer * (i - 1)) + 40,
Math.round(dim.height - 30 -
(result[i] / unit) * heightPer));
}
}
g2.draw(path);
}

}
/*****************program end************************/
3。生成jpg图形并显示
/*****************program begin************************/
package test;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.*;

import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;


/**
* 变化曲线
*/
public class WageChangeCurveAction
extends Action {
private static final String CONTENT_TYPE = "image/gif";
private static final int WIDTH = 700;
private static final int HEIGHT = 500;
public ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException {
response.setContentType(CONTENT_TYPE);
ServletOutputStream out = response.getOutputStream();
double[] result=。。。String[] title = 。。。//获得图形数据
try {
CurveFrame dummy = new CurveFrame(result,title);
dummy.setSize(new Dimension(WIDTH, HEIGHT));
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
dummy.paint(g2);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.flush();
}
catch (Throwable e) {
e.printStackTrace();
}
return mapping.findForward("success");
}

}
/*****************program end************************/

Tuesday, January 6, 2009

Java IDE选择

From: http://www.knowsky.com/362256.html

随着Java应用程序越做越大、越做越复杂。Java IDE在其中所起的作用也日益显著。有了Java IDE,使软件的生产率倍增。但目前Java IDE的种类繁多,功能也各不相同。这就给我们的选择带来了诸多不便。因此,本文就目前比较流行的几种Java IDE做了一个深入的对比。主要比较4种Java IDE,它们是Eclipse、Netbeans、Jbuilder和Jcreator。本文将从以下8个方面进行探讨。   一、 Java IDE介绍   1. Eclipse   Eclipse是一个非常成功的开源项目。在世纪之交的时候,IBM为了对抗微软越来越强的垄断地位,投入了10亿美元进行Linux、pc、笔记本电脑以及服务器等产品的研发。在这一系列举措中,影响最深远的就是Eclipse。   Eclipse是IBM“日独计划”的产物。在2001年6月,IBM将价值4000万美元的Eclipse捐给了开源组织。Eclipse由四个计划组成:Eclipse Project、Eclipse Tools Project、Eclipse Technology Project和Eclipse Web Tools Platform Project。每一个计划都遵照CPL1.0协议发布。经过几年的发展,Eclipse已经成为目前最流行的Java IDE。并且拥有了很多的Eclipse社区和新闻组。目前,Eclipse已经成为开发Java程序的首选IDE。   2. Netbeans   Netbeans是Sun自己的开源Java IDE。随着Eclipse逐渐兴起,Sun也在试探性地向Eclipse靠拢。但同时又在不移余力地开发自己的Java IDE:Netbeans。Netbeans在功能上和Eclipse类似。但和Eclipse也有一此区别。如Netbeans集成了Web开发以及最近流行的ajax,而Eclipse要想达到这一点,必须得安装其它的第三方插件。还有Eclipse鼓励使用SWT做为Java的GUI库,而Netbeans使用的是更纯正的Java GUI库:Swing/AWT。   3.Jbuilder   Jbuilder是Borland开发的Java IDE。它在Eclipse和Netbeans出现之前是非常流行了。原因很简单,因为那时Jbuilder是唯一能够真正称得上IDE的产品。但在 Eclipse和Netbeans出现之后Jbuilder就每况愈下。发生这种情况的原因很多。可能是因为Jbuilder是收费的,而Eclipse 和Netbeans是免费的;也可能是因为Borland的产品有一个最大的缺点。就是不管功能多强,而它的代码编辑器和其它辅助书写代码的工具差不多未发生什么变化。从Jbuilder2和Jbuilder9似乎都差不多。   4. Jcreator   以上介绍的都是大家伙。而Jcreator则是一个轻量型的Java IDE。它的功能非常单一。最核心的功能就是可能编写Java源程序,并且支持代码变色和code complete。但Jcreator有一个最显著的优点。就是运行速度快,并且占用资源少。这主要是因为Eclipse、Netbeans和 Jbuilder本身都是使用Java编写的。在启动时必须得先启动Java虚拟机。而Jcreator使用的是本地代码。因而速度更快。假如你的机器配置不高(如只有64M或128M内存),还是使用Jcreator为好。
  二、 Java IDE是否能跨平台 由于Java是跨平台的,因此,它的IDE最好也能跨平台。由于Eclipse、Netbeans和Jbuilder都是使用Java编写的,因此它们也是跨平台的。但由于Eclipse采用的GUI接口是 SWT。而这个库并未加入Sun JCP。因此,Eclipse的跨平台要受到SWT的限制,即SWT所能跨的平台数也就是Eclipse所跨的平台数。而对于Jcreator来说,目前只有Windows版本。这也不能不说是一个遗憾。不过由于大多数的Java开发都是在Windows上,因此。支持Windows平台是几乎是所有的 Java IDE最先考虑的事情。   三、 Java IDE是否收费   这个问题是决定一个IDE是否能够流行的重要原因之一。众所周知,假如一个软件是收费的,那么获得这个软件的方法一般有两种。一种是购买正版的软件。另一种就是使用盗版的软件。而在一些对盗版打击很严厉的国家可能使用正版软件就成为最佳的选择。然而,使用正版软件将会带来很大的成本。所以一但有一种或几种免费的软件,并且在功能上足可以取代收费软件时,人们就会马上转向这些免费的软件。在上述的4种Java IDE中Eclipse和Netbeans不仅是完全免费的,而且是开源的。因此,它们一出现,就对收费Jbuilder产生了很大的威胁。目前 Eclipse和Netbeans的使用率已经超过了Jbuilder。而Jcreator虽然也是收费的。但是费用也并不高。因此,以Jcreator 为首的一些小型的Java IDE以其体积小,占用系统资源少等优点也会占有一定的比例(尽管这个比例并不大)。   四、 Java IDE的界面友好程度   界面是否友好是决定一个IDE是否成功的另一个重要原因。对于界面来说,Eclipse和Netbeans差不多。只是Eclipse是使用SWT开发的。因此,它的界面看起来更像是本地的程序。而Netbeans的界面风格是Java的标准风格。