|
2008年9月24日
学习就从官方的测试开始,但不知道怎么回事官方的测试有问题,这我就把我配置的简单junit环境共享给大家把! 1.下载 hibernate-annotations-3.4.0.GA 并解压 ,cd 到解压后目录下 2. $> ant junit (天知道,怎么官方出的东西测试不能通过!!) 不要急,我们来看错误 (错误提示有删减) 错误-1:$>build.xml:95: ./jdbc not found. 解决办法-1: $>mkdir ./jdbc 3. $> ant junit 错误-2: ./build.xml:124: The following error occurred while executing this line: ./build.xml:126: The following error occurred while executing this line: ./common-build.xml:349: .\src\filters\hsqldb.filter as it doesn't exist. 解决办法-2: $> mkdir ./src/filters ; $> echo "" > ./src/filters/hsqldb.filter; 4. $>ant junit 错误-3: ./build.xml:124: The following error occurred while executing this line: ./build.xml:126: The following error occurred while executing this line: ./common-build.xml:351: .\src\test-resources not found. $> mkdir ./src/test-resources ; 5. $> ant junit 错误-4:没有ant 错误了!,但测试都为错误,那我们就看看 第一个测试吧,找下原因! test-resources: [mkdir] Created dir: hibernate-anntations-3.4.0.GA\build\test-reports\hsqldb [echo] Running against db: hsqldb [junit] Running org.hibernate.test.annotations.ConfigurationTest [junit] Tests run: 7, Failures: 0, Errors: 6, Time elapsed: 1.218 sec ............. $> find . -name "ConfigurationTest.java" #pwd /hibernate-anntations-3.4.0.GA ./test/org/hibernate/test/annotations/configuration/ConfigurationTest.java ./test/org/hibernate/test/annotations/ConfigurationTest.java 根据路径 org.hibernate.test.annotations.ConfigurationTest gvim ./test/org/hibernate/test/annotations/ConfigurationTest.java 发现第20行: cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" ); 感觉应该没有配置 jdbc gvim ./test/org/hibernate/test/annotations/hibernate.cfg.xml #果然 没配置 我这就放个我的简单配置上来(使用 hbm hsqldb 内存数据库测 试 ): <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property> <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:mem:hbn_ann</property> <property name="connection.username">sa</property> <property name="connection.password"></property> <mapping resource="org/hibernate/test/annotations/Boat.hbm.xml"/> <mapping package="org.hibernate.test.annotations"/> <mapping class="org.hibernate.test.annotations.Plane"/> </session-factory> </hibernate-configuration> 我这就是要hsqldb吧,既然上面错误1有 jdbc 文件夹,那就是放 jdbc jar 的地方 $> cp ../hsqldb.jar ./jdbc 6. $> ant junit 呵呵 测试 第一个正确了 ! (目前配置后,测试环境并不是全部都正确,比如第2个测试) 其他测试错误日志在 .\build\test-reports\hsqldb ... 比如第2个错误 异常部分 org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect. at org.hibernate.dialect.Dialect.instantiateDialect(Dialect.java:256) at org.hibernate.dialect.Dialect.getDialect(Dialect.java:234) ........ 大家一边学习,一边配置测试 环境吧。 错误日记 都会在 build\test-reports\hsqldb下
参考: http://tech.ddvip.com/2008-12/122835390597191.html 页面可以直接运行: <html> <head>
<style type="text/css">
.box1 { float:left; width:21px; height:21px; position:relative; background: #F3F2E2 url(http://mp3.youdao.com/images/mbox_bg.gif) no-repeat; } .box1 { background-position: 0px -60px; }
</style>
</head>
<body>
<div class="box1" onmouseover="this.style.backgroundPosition='0px -81px' " onmouseout="this.style.backgroundPosition='0px -102px' " > </div>
</body>
</html>
javascript 学习
javascript 大体上可分为3个不同部分组成: 核心(ECMAscript),文本对象(DOM),浏览器对象(BOM)
-
核心(ECMAscript): 关键字,语句,运算符,对象
-
文本对象(DOM):DOM将把整个页面规划成由节点层级构成的文档.
-
解析遵循 W3C html dom 标准:
-
W3C dom 参考特别关注 DOM Node 说明
-
BOM 浏览器对象. cookie,弹出新浏览器,浏览器设置大小
核心(ECMAscript)Global 内置对象;
方法: parseInt(),isNan(),encodeURI()...等都为此对象方法 特别注意 eval();动态语言的象征 比如:eval("alert('hi')"); 但这个方法很邪恶(安全方面)
文本对象(DOM)说明:
<bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> </bookstore> | | |
1. ECMAscript基础 $ 变量弱类型 ; 匈牙利类型标示 : var iOuouValue=100;
$ 结束行分号有无都可以; 但再 onsubmit="javascript:function();return false;" $ 关键字 ; 提别注意
"constructor" bean.constructor
//print bean function(){ ....
}
"typeof" var test=1; alert(typeof testX); //output "undefined" "NaN" - not a number -> isNan("blue"); //output "true" ->isNan("123"); //output "false" $ 对象; var o = new Object(); var a = {}
这里特别说明下 我们普通写的 一个 function 就是一个 object 这 var a = {name:"刘凯毅"} 等同与 var a = function(){this.name="刘凯毅"}; 来个 {name:"test",pass:"123456",addr:"bj"} //这是什么 ?! json
当 var str = '{name:"test",pass:"123456",addr:"bj"}' var objectBean = eval(str); //这里就是 对象 objectBea.name 使用了
域概念: <SCRIPT type=text/javascript>
var sMessage = 'Hello';
function setSomething() {
sColor = 'red';
sMessage = 'Hello World!';
}
setSomething();
alert(sMessage); //Hello World!
alert(sColor); //red
</SCRIPT> | <SCRIPT type=text/javascript>
var sMessage = 'Hello';
function setSomething() {
var sColor = 'red';
sMessage = 'Hello World!';
}
setSomething();
alert(sMessage); //Hello World!
alert(sColor); // 什么都没有
</SCRIPT>
| <SCRIPT type=text/javascript>
var sMessage = 'Hello';
function setSomething() {
var sColor = 'red';
var sMessage = 'Hello World!';
}
setSomething();
alert(sMessage); //Hello
alert(sColor); // 什么都没有
</SCRIPT>
|
为面向对象做基础:object prototype 类型的对象应用。参考 // 最简单的 继承
Object.prototype.inObj = 1;
function A()
{
this.inA = 2;
}
A.prototype.inAProto = 3;
B.prototype = new A; // Hook up A into B's prototype chain
B.prototype.constructor = B;
function B()
{
this.inB = 4;
}
B.prototype.inBProto = 5;
x = new B;
document.write(x.inObj + ', ' + x.inA + ', ' + x.inAProto + ', ' + x.inB + ', ' + x.inBProto);
//1, 2, 3, 4, 5
//增加点信心 http://www.json.org/json.js
Object.prototype.toJSONString = function (filter) { return JSON.stringify(this, filter); }; 后我们就可以使用 bean.toJSONString()不是吗?
|
$ arguments ; function getFun(){alert(arguments.length);} ;
getFun("xx") //output 1 getFun("xx",23) //output 2
$ 语句 ;特殊说明下 for for(var i=0i<iCount;i++) 或 for( attr in object ) ;
如果无聊 你可以 for( sProp in window ){alert(sProp+"你丫点啊!");} //看看 javascript 的反射
面向对象: var bean = new Bean();
1.工厂方法 function getAttr(){ alert(this.attr)
} function Bean(tattr){ var bean = new Object; bean.attr = tattr; bean.getAttr = getAttr; return bean ;
} 根本就是山寨版 面向对象
2.构造 function Bean(tattr){ this.attr = tattr ; bean.getAttr = function(){ alert(this.attr); } } 其上 2 总 再Bean 对象创建时,方法会 “重复生成函数”!
3.原型模式 function Bean(){} Bean.prototype.attr = ""; Bean.prototype.getAttr=function(){alert(this.attr);}
解决 “重复生成函数” 问题,但新的问题 Bean.prototype.getArray = new Array();
其 new 对象 bean1 和 bean2 都会共享 new Array 空间(是我们不想看到的)
4.混合 模型 :) 哈哈 function Bean(){ this.attr= "";
this.getArray=new Array;
} Bean.prototype.getAttr=function(){alert(this.attr);}
5.动态原型 (注意下面开始,就是真正的面向对象!!!) function Bean(){ this.attr= "";
this.getArray=new Array; //classload 加载 时
if(typeof Bean._initialized == "undefined" ){ Bean.prototype.getAttr=function(){alert(this.attr);};
Bean._initialized= true ; }
}
/****************************************************************/
对象继承
1.对象冒充!!(可支持多继承,山寨很强大)
function classA(sstr){ this.color = sstr ; this.sayColor = function(){ alert(this.color);
};
} function classC(){}
function classB(){ this.newMethod =ClassA ; this.newMethod(); delete this.newMethod ;
this.newMethod =ClassC ; this.newMethod(); delete this.newMethod ; this.arrt = "google";
}
2.call() apply() 也山寨, function classA(sstr){ this.color = sstr ; this.sayColor = function(str){ alert(str+this.color);
};
}
function classB(){ // this.newMethod =ClassA ; // this.newMethod(); // delete this.newMethod ; classA.call(this,"red"); //classA.apply(this,new Array("red"))
this.arrt = "baidu";
} 3.正统的继承 原型链 (但不支持多继承) function classA(){this.oo="test";} classA.prototype.color = "red"; function classB(){} classB.prototype = new classA ; classB.prototype.sayName = function(){ alert( this.color ); } var bb = new classB ; bb.sayName(); // output red alert(bb.oo); // output test
alert( bb instanceof classA); //output true alert( bb instanceof classB); //output true4.如果你要多继承!!并且还支持 instanceof 混合方式: function classA(){} function classB(){} function classC(){ classA.call(this); classC.call(this); } classC.prototype = new classA ;//注意 这 instanceof 只能对 A有用
下载:flash-plugin-10.0.15.3-release.i386.rpm
rpm -ivh flash-plugin-10.0.15.3-release.i386.rpm
tomcat > server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="D:/workspace/meganotes/mn_mod_admin/target/mn_mod_admin/" path="/" />
。。。。
关键:在Context上不要加reloadable和debug属性 然后 使用eclipse tomcat 调试 ok
<?
xml version="1.0" encoding="UTF-8"
?>
<
project
name
="ssh"
>
<
property
file
="ssh.properties"
/>
<
tstamp
>
<
format
property
="TODAY"
pattern
="yyyyMMddHHmm"
/>
</
tstamp
>
<
property
name
="ssh.path"
value
="/data/meganotes/shell/"
/>
<
target
name
="shell/ 文件 上载/覆盖"
>
<
input
addproperty
="scp_coverage_shell_file"
message
="输入(ssh/ 下文件名 \n\r 上载或覆盖 到服务器 218 ${ssh.path} ):"
>
</
input
>
<
input
message
="请确定是否使用 ssh/${scp_coverage_shell_file} 覆盖服务器中 ${ssh.host}:${ssh.path}${scp_coverage_shell_file} (y,n)"
validargs
="y,n"
addproperty
="do.coverage"
defaultvalue
="n"
/>
<
condition
property
="do.abort"
>
<
equals
arg1
="n"
arg2
="${do.coverage}"
/>
</
condition
>
<
fail
if
="do.abort"
>
退出
</
fail
>
<
sshexec
host
="${ssh.host}"
username
="${ssh.myName}"
password
="${ssh.myPass}"
trust
="true"
command
=" mkdir -p ${ssh.path}backup/${TODAY} ; if [ -f ${ssh.path}${scp_coverage_shell_file} ] ; then cp ${ssh.path}${scp_coverage_shell_file} ${ssh.path}backup/${TODAY}/ ; fi ; "
>
</
sshexec
>
<
scp
file
="ssh/${scp_coverage_shell_file}"
todir
="${ssh.myName}:${ssh.myPass}@${ssh.host}:${ssh.path}"
trust
="true"
verbose
="true"
/>
<
sshexec
host
="${ssh.host}"
username
="${ssh.myName}"
password
="${ssh.myPass}"
trust
="true"
command
="chmod 777 ${ssh.path}${scp_coverage_shell_file}"
>
</
sshexec
>
</
target
>
<
target
name
="shell/ 文件查看"
>
<
input
addproperty
="show_shell_file"
message
="输入查看文件名(*sh):"
defaultvalue
="*"
>
</
input
>
<
sshexec
host
="${ssh.host}"
username
="${ssh.myName}"
password
="${ssh.myPass}"
trust
="true"
command
="ls /data/meganotes/shell/${show_shell_file}"
>
</
sshexec
>
</
target
>
<
target
name
="shell/ 文件拷贝"
>
<
input
addproperty
="scp_shell_file"
message
="输入 /data/meganotes/shell/ 下需要文件名(cp 到 ssh 下):"
>
</
input
>
<
scp
file
="${ssh.myName}:${ssh.myPass}@${ssh.host}:/data/meganotes/shell/${scp_shell_file}"
todir
="ssh"
trust
="true"
verbose
="true"
/>
</
target
>
<
target
name
="admin-mvn组合操作"
>
<
exec
executable
="cmd"
>
<
arg
value
="/c"
/>
<
arg
value
="${basedir}/sh/mvn/meganotes_mvn_clear.bat"
/>
</
exec
>
<
exec
executable
="cmd"
>
<
arg
value
="/c"
/>
<
arg
value
="${basedir}/sh/mvn/meganotes_mvn_install.bat"
/>
</
exec
>
<
exec
executable
="cmd"
>
<
arg
value
="/c"
/>
<
arg
value
="${basedir}/sh/mvn/mn_mod_admin_mvn_intall.bat"
/>
</
exec
>
</
target
>
<
target
name
="admin-tomcat日志"
>
<
sshexec
host
="${ssh.host}"
username
="${ssh.myName}"
password
="${ssh.myPass}"
trust
="true"
command
="${ssh.adminRunLog}"
>
</
sshexec
>
</
target
>
<
target
name
="schedule-tomcat日志"
>
<
sshexec
host
="${ssh.host}"
username
="${ssh.myName}"
password
="${ssh.myPass}"
trust
="true"
command
="${ssh.scheduleRunLog}"
>
</
sshexec
>
</
target
>
<
target
name
="查找2008/下Mp3没转128-64文件夹"
>
<
sshexec
host
="${ssh.host}"
username
="${ssh.myName}"
password
="${ssh.myPass}"
trust
="true"
command
="${ssh.64k}"
>
</
sshexec
>
<
sshexec
host
="${ssh.host}"
username
="${ssh.myName}"
password
="${ssh.myPass}"
trust
="true"
command
="${ssh.128k}"
>
</
sshexec
>
<
sshexec
host
="${ssh.host}"
username
="${ssh.myName}"
password
="${ssh.myPass}"
trust
="true"
command
="${ssh.128kto64k.err}"
>
</
sshexec
>
</
target
>
<
target
name
="admin模块测试"
>
<
input
addproperty
="admin_test_input"
message
="测试类输入(Video*Test):"
defaultvalue
="*Test"
>
</
input
>
<
echo
message
="${admin_test_input}"
></
echo
>
<
exec
executable
="cmd"
>
<
arg
value
="/c"
/>
<
arg
value
="${basedir}/sh/admin_test.bat ${admin_test_input} "
/>
</
exec
>
</
target
>
</
project
>
public
static
void
main(String[] args)
{
try
{ String charset
=
"
UTF-8
"
; String outfile
=
"
d:\\category.20081218.gz
"
; BufferedReader in
=
new
BufferedReader(
new
InputStreamReader(
new
FileInputStream(
"
d:\\category.20081218
"
), charset)); BufferedOutputStream out
=
new
BufferedOutputStream(
new
GZIPOutputStream(
new
FileOutputStream(outfile))); System.out.println(
"
Writing file
"
); String c;
while
((c
=
in.readLine())
!=
null
) out.write( (c
+
"
\n
"
).getBytes(charset)); in.close(); out.close(); System.out.println(
"
Reading file
"
); BufferedReader in2
=
new
BufferedReader(
new
InputStreamReader(
new
GZIPInputStream(
new
FileInputStream(outfile) ),charset ) ); String s;
while
((s
=
in2.readLine())
!=
null
) System.out.println(s); }
catch
(Exception e)
{ e.printStackTrace(); }
}
官方上下 http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ $.补全中文 修改 jquery.autocomplete.js 中 $input.bind(($.browser.opera ? "keypress" : "keydown") 改为 $input.bind(($.browser.opera ? "keypress" : " keyup") $. 在 localdata.js 》》 var cities = [.. 中放些 中文 测试吧 测试页面 ,最简单的使用页面 。(应该算是最简单补全测试了) <script type="text/javascript" src="../lib/jquery.js"></script> <script type='text/javascript' src='../jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="../jquery.autocomplete.css" /> <script type="text/javascript"> $().ready(function() { $("#suggest1").autocomplete([ "test1", "test2", "测试1", "测试2" ]); }); </script>
<input type="text" id="suggest1" />
使用json <script type="text/javascript" src="../lib/jquery.js"></script> <script type='text/javascript' src='../jquery.autocomplete.js'></script>
<script type='text/javascript' src='localdata.js'></script> <link rel="stylesheet" type="text/css" href="../jquery.autocomplete.css" /> <script type="text/javascript"> var emails = [ { name: "Peter Pan", to: "peter@pan.de" }, { name: "Molly", to: "molly@yahoo.com" }, { name: "Forneria Marconi", to: "live@japan.jp" }, { name: "Master <em>Sync</em>", to: "205bw@samsung.com" }, { name: "Dr. <strong>Tech</strong> de Log", to: "g15@logitech.com" }, { name: "Don Corleone", to: "don@vegas.com" }, { name: "Mc Chick", to: "info@donalds.org" }, { name: "Donnie Darko", to: "dd@timeshift.info" }, { name: "Quake The Net", to: "webmaster@quakenet.org" }, { name: "Dr. Write", to: "write@writable.com" } ]
$().ready(function() { $("#suggest13").autocomplete(emails, { //补全中选择显示 formatItem: function(row, i, max) { return i + "/" + max + ": \"" + row.name + "\" [" + row.to + "]"; }, //补全匹配格式 formatMatch: function(row, i, max) { return row.name ; }, //补全选定后显示 formatResult: function(row) { return row.to; } }) }); </script>
不要为 拼写 ajax url 而郁闷了。希望能对大家有帮助。 /* 1.form ajax 使用: eG: form就是普通的 html form <input type="button" value="xx" onclick="formAjax('data.jsp',this.form,ajaxReturn)"/> function ajaxReturn(data){ document.getElementById("div1").innerHTML = data ; } 2.ajax 使用 : AjaxFunction AjaxFunction('url?param=tt¶m2=te',function(text){ alert(text); });
3.迭代器: recursiveNodes(array,dom,pushFun,layer) array 迭代寄存器 dom 被迭代对象 pushFun 自定义收集方法 pushFun(array,node,layer) node 迭代中对象 layer 深度 EG: var arr = [] ; recursiveNodes(arr ,dom,function(array,node,layer){ arr.push(node); },2);
*/ var _request; try { this._request = new XMLHttpRequest(); } catch (e) { try { this._request = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { try { this._request = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) { request = false; } } }
var AjaxFunction = function(urls, getAjaxText) { AjaxFunction._getAjaxText = null;
_url = ''; _params = '';
AjaxFunction._getAjaxText = getAjaxText; var strs = urls.split(/\?/); _url = strs[0]; if (strs.length > 1) _params = strs[1];
_request.open("POST", this._url, true); _request.setRequestHeader("Cache-Control", "no-cache"); _request.setRequestHeader("content-type", "application/x-www-form-urlencoded"); _request.send(_params); _request.onreadystatechange = _onComplete; }; function _onComplete() { if (_request.readyState == 4) { if (_request.status == 200 || _request.status == 0) { if (_request.responseText != null && _request.responseText != '' && _request.responseText != ' ') AjaxFunction._getAjaxText(_request.responseText); } } }
/* * 迭代 1. array,dom 2. array,dom,layer */ function recursiveNodes(array, dom, pushFun, layer) { if (layer == 0) return; if (layer > 0) { --layer; if (dom.hasChildNodes && dom.hasChildNodes()) { for ( var i = 0; i < dom.childNodes.length; i++) { var nodeT = dom.childNodes.item(i); if (typeof pushFun == "function") pushFun(array,nodeT, layer); else array.push(nodeT); recursiveNodes(array, dom.childNodes.item(i), pushFun, layer); } } else return; } else if (layer < 0) { ++layer; var nodeT = dom.parentNode; if (nodeT) { if (typeof pushFun == "function") { pushFun(array,nodeT, layer); } else { array.push(nodeT); } recursiveNodes(array, dom.parentNode, pushFun, layer) } else return; } }
function formAjax(url,ttform,returnFun){ var datas = []; var arr = ['input','select']; recursiveNodes(datas,ttform,function(array,node,layer){ for(var i=0;i<arr.length;i++){ if( node.getAttribute ){ var paramName = node.getAttribute('name') ; if( /select/i.test( node.nodeName ) ){ array.push(paramName+'='+node.options[node.selectedIndex].value); }else if( paramName !=null && /^\s*(\w+)\s*$/g.test(paramName) && new RegExp(arr[i],'i').test(node.nodeName) ){ array.push(paramName+'='+node.value); } } } },999); //? ? //& & var urls = url+'?'+datas.join("&"); AjaxFunction(urls,returnFun); }
function trim(str){ return str.replace(/(^\s*)|(\s*$)/g,""); }
function recursiveNodes(array,dom,layer){ if( layer==0 ) return ; if(layer>0){ --layer; if( dom.hasChildNodes && dom.hasChildNodes() ){ for(var i=0;i<dom.childNodes.length;i++ ){ array.push(dom.childNodes.item(i)); recursiveNodes(array, dom.childNodes.item(i),layer ) ; } }else return ; }else if(layer<0){ ++layer; if( dom.parentNode ){ array.push( dom.parentNode ); recursiveNodes( array,dom.parentNode,layer) }else return ; } }
/* [@name || && > < >= <= != @location='UK'] */ function singleStep(dom,stepAction,stepStr){ var allArrData = [] ; var arrData = [] ; var num = stepAction.split(/\//).length ; recursiveNodes(allArrData,dom,num==2?1:999); stepStr = trim(stepStr) ; var nodeName ; var nodeNum ; var conds = true ; //判断单匹配 是否有 属性对比 [@..=..] if( /\[(.*?)\]/.test(stepStr) ){ nodeName = trim( /.*?(?=\[{1})/g.exec(stepStr)[0] ).replace(/\*/g,".*") ; conds = /\[(.*?)\]/.exec(stepStr)[1].replace(/\*/g,".*") ; if(! isNaN(conds) ){ nodeNum = parseInt(conds)-1;
}else{ //属性 替换为 conds = conds.replace(/(@)(.*?)(?=\W)/g," allArrData[i].getAttribute('$2') ") ; conds = conds.replace( /([\W]+)(.*?)::\'(.*?)\'/g," $1 /$3/g.test($2) " ) ; conds = conds.replace( /(text\(\))/g ," allArrData[i].innerHTML ") ; conds = conds.replace( /([\w\s])(=)/g ,"$2=") ;
} }else{ nodeName = stepStr ; }
for(var j=i=0;i<allArrData.length;i++){ if( eval('/'+nodeName+'/i.test( allArrData[i].nodeName ) ') ){ if(typeof nodeNum=="number"){ if(j==nodeNum){ arrData.push( allArrData[i] ); return arrData; } else ++j; }else if( eval(' allArrData[i].getAttribute && '+conds) ){ arrData.push( allArrData[i] ); } } } return arrData ; }
function getXpath(dom,xpathStr){ var xpaths = xpathStr.split(/(\/{1,2})/g); var arrDom = [dom] ; for(var i=1;i<xpaths.length;i=i+2){ var arrData = []; for(var j=0;j<arrDom.length;j++) arrData = arrData.concat( singleStep(arrDom[j],xpaths[i],xpaths[i+1]) ); arrDom = arrData ; } return arrDom ; }
****************************************************************************************************** function trim(str){ return str.replace(/(^\s*)|(\s*$)/g,""); }
function recursiveNodes(array,dom,layer){ if( layer==0 ) return ; if(layer>0){ --layer; if( dom.hasChildNodes && dom.hasChildNodes() ){ for(var i=0;i<dom.childNodes.length;i++ ){ array.push(dom.childNodes.item(i)); recursiveNodes(array, dom.childNodes.item(i),layer ) ; } }else return ; }else if(layer<0){ ++layer; if( dom.parentNode ){ array.push( dom.parentNode ); recursiveNodes( array,dom.parentNode,layer) }else return ; }
}
/* [@name || && > < >= <= != @location='UK'] */ function singleStep(dom,stepAction,stepStr){ var allArrData = [] ; var arrData = [] ; var num = stepAction.split(/\//).length ; recursiveNodes(allArrData,dom,num==2?1:999); stepStr = trim(stepStr) ; var nodeName ; var conds = true ; //判断单匹配 是否有 属性对比 [@..=..] if( /\[(.*?)\]/.test(stepStr) ){ nodeName = trim( /^(.*?)\[{1}/.exec(stepStr)[1] ) ; //属性 替换为 conds = /\[(.*?)\]/.exec(stepStr)[1].replace(/(@)(.*?)(?=\W)/g," allArrData[i].getAttribute('$2').toString() ") ; conds = conds.replace( /([\W]+)(.*?)::\'(.*?)\'/g," $1 /$3/g.test($2) " ) ; conds = conds.replace( /(text\(\))/g ," allArrData[i].innerHTML ") ; conds = conds.replace( /([\w\s])(=)/g ,"$2=") ; }else{ nodeName = stepStr ; }
for(var i=0;i<allArrData.length;i++){ if( new RegExp(nodeName, 'i').test( allArrData[i].nodeName ) && eval(conds) ){ arrData.push( allArrData[i] ); } } return arrData ; }
function getXpath(dom,xpathStr){ var xpaths = xpathStr.split(/(\/{1,2})/g); var arrDom = [dom] ; for(var i=1;i<xpaths.length;i=i+2){ var arrData = []; for(var j=0;j<arrDom.length;j++) arrData = arrData.concat( singleStep(arrDom[j],xpaths[i],xpaths[i+1]) ); arrDom = arrData ; } return arrDom ; }
|