📄 xml-parser.js
字号:
//parser element
/*
function parseElement(node,context){
var next = node.attributes;
context.append('<'+node.tagName);
for (var i=0; i<next.length; i++) {
context.parseNode(next.item(i))
}
var next = node.firstChild;
if(next){
context.append('>')
var postfix = '</'+node.tagName+'>';
do{
context.parseNode(next)
}while(next = next.nextSibling)
context.append(postfix)
}else{
context.append('/>')
}
return true;
}
*/
//parser attribute
function parseAttribute(node,context){
var name = node.name;
var value = node.value;
var buf = parseText(value);
var isStatic;
var isDynamic;
//hack parseText is void
var i = buf.length;
while(i--){
//hack reuse value param
var value = buf[i];
if(value.constructor == String){
if(value){
isStatic = true;
}else{
buf.splice(i,1);
}
}else{
isDynamic = true;
}
}
if(isDynamic && !isStatic){
//remove attribute;
//context.append(" "+name+'=""');
if(buf.length > 1){
//TODO:....
throw Error();
}else{//只考虑单一EL表达式的情况
buf = buf[0];
if(buf[0] != 0){
throw Error("属性内只能有单一EL表达式!!");
}
buf = buf[1];
}
context.append( [1,name,buf]);
return true;
}
context.append(" "+name+'="');
if(/^xmlns$/i.test(name)){
if(buf[0] == 'http://www.xidea.org/taglib/xhtml'){
buf[0] = 'http://www.w3.org/1999/xhtml'
}
}
context.append.apply(context,buf)
context.append('"')
return true;
}
function parseTextNode(node,context){
var data = node.data;
context.append.apply(context,parseText(data.replace(/^\s+|\s+$/g,' ')))
return true;
}
function parseCDATA(node,context){
context.append("<![CDATA[");
context.append.apply(context,parseText(node.data,true));
context.append("]]>");
return true;
}
function parseEntityReference(){
return true;//not support
}
function parseEntity(){
return true;//not support
}
function parseProcessingInstruction(node,context){
context.append("<?"+node.nodeName+" "+node.data+"?>");
return true;
}
function parseComment(){
return true;//not support
}
function parseDocument(node,context){
for(var n = node.firstChild;n!=null;n = n.nextSibling){
context.parseNode(n);
}
}
/**
* @protected
*/
function parseDocumentType(node,context){
if(node.xml){
context.append(node.xml);
}else{
if(node.publicId){
context.append('<!DOCTYPE ');
context.append(node.nodeName);
context.append(' PUBLIC "');
context.append(node.publicId );
context.append( '" "');
context.append(node.systemId);
context.append('">');
}else{
context.append("<!DOCTYPE ");
context.append(node.nodeName);
context.append("[");
context.append(node.internalSubset);
context.append("]>");
}
}
return true;
}
// /**
// * @protected
// */
// function parseDocumentFragment(node,context){
// var nl = node.childNodes;
// for (var i=0; i<nl.length; i++) {
// context.parseNode(nl.item(i));
// }
// return true;
// }
/**
*/
function parseNotation(node,context){
return true;//not support
}
//1 2
/**
* @internal
*/
var stringRegexp = /["\\\x00-\x1f\x7f-\x9f]/g;
/**
* 转义替换字符
* @internal
*/
var charMap = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
};
/**
* 转义替换函数
* @internal
*/
function charReplacer(item) {
var c = charMap[item];
if (c) {
return c;
}
c = item.charCodeAt().toString(16);
return '\\u00' + (c.length>1?c:'0'+c);
}
/**
* 1 必要
* 2 EL属性
* 从XML属性集中载入需要的属性集合,同时报告缺失和冗余
*/
function loadAttribute(node,setting){
var attributes =node.attributes
var tagName = node.tagName;
var i = attributes.length;
var data = {};
while(i--){
var item = attributes[i];//item 不行 htmlunit
var key = item.name;
if(key in setting){
data[key] = item.value.replace(/^\s+|\s+$/g,'');
}else{
if(!/^xmlns(?:\:.+)/.test(key)){
$log.error("未知属性:", key, tagName);
}
}
}
for(var key in setting){
var type = setting[key];
if(type & 1){
var value = data[key];
if(value == null){
$log.error("缺少必要属性:", key, tagName);
}
}
if(type & 2){
if(value){
var value2 = value.replace(/^\s*\$\{\s*(\S[\S\s]*)\}\s*$/,'$1')
if(value2 != value){
data[key] = parseEL('',value2);
}else{
$log.error("属性需要为表达式(${...}):", key,value,type,tagName);
}
}
}
}
return data;
}
function toEL(value,type){
var value2 = value.replace(/^\s*\$\{\s*(\S[\S\s]*)\}\s*$/,'$1')
if(value2 != value){
return parseEL('',value2);
}else{
if(type == Number){//int
}else{//String
value = '"' + (stringRegexp.test(value) ?
value.replace(stringRegexp,charReplacer) :
value)
+ '"';
}
return value;
}
}
/**
* @public
*/
function toDoc(text){
if(window.DOMParser){
var doc = new DOMParser().parseFromString(text,"text/xml");
}else{
//["Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"];
var doc = new ActiveXObject("Microsoft.XMLDOM");
doc.loadXML(text);
}
return doc;
}
/**
* TODO:貌似需要importNode
*/
function selectNodes(doc,xpath){
var docFragment = doc.createDocumentFragment();
try{
var nodes = doc.selectNodes(xpath);
var buf = [];
for (var i=0; i<nodes.length; i++) {
buf.push(nodes.item(i))
}
}catch(e){
}
if(!buf){
var xpe = doc.evaluate? doc: new XPathEvaluator();
var nsResolver = xpe.createNSResolver(doc.documentElement);
var result = xpe.evaluate(xpath, doc.documentElement, nsResolver, 5, null);
var node;
var buf = [];
while (node = result.iterateNext()){
buf.push(node);
}
}
while (node = buf.shift()){
node.parentNode.removeChild(node);
docFragment.appendChild(node);
}
return docFragment;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -