⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xml-parser.js

📁 JS编译工具
💻 JS
📖 第 1 页 / 共 2 页
字号:
/*
 * JavaScript Integration Framework
 * License LGPL(您可以在任何地方免费使用,但请不要吝啬您对框架本身的改进)
 * http://www.xidea.org/project/jsi/
 * @author jindw
 * @version $Id: template.js,v 1.4 2008/02/28 14:39:06 jindw Exp $
 */
//parse

//add as default
function XMLParser(){
    var list = this.parserList.concat([]);
    this.parserList = list;
    this.result = [];
}
XMLParser.prototype = new TextParser()
XMLParser.prototype.parse = function(url){
    var data = this.load(url);
    this.parseNode(data);
    return this.reuslt;
}
XMLParser.prototype.load = function(url){
	if(/^[\s\ufeff]*</.test(url)){
        var doc =toDoc(url)
		//alert([data,doc.documentElement.tagName])
    }else{
	    var pos = url.indexOf('#');
	    var xhr = new XMLHttpRequest();
	    xhr.open("GET",pos+1?url.substr(0,pos):url,false)
	    xhr.send('');
	    if(/\bxml\b/.test(xhr.getResponseHeader("Content-Type"))){//text/xml,application/xml...
	        var doc = xhr.responseXML;
	    }else{
	        var doc = toDoc(xhr.responseText)
	    }
	    if(pos>0){
	        doc = selectNodes(doc,url.substr(pos+1));
	    }
	    this.url = url;
	}
	return doc;
}
/**
 * 解析函数集
 * @private
 */
XMLParser.prototype.addParser(function(node,context){
    switch(node.nodeType){
        //case 1: //NODE_ELEMENT 
        //    return parseElement(node,context)
        case 2: //NODE_ATTRIBUTE                             
            return parseAttribute(node,context)
        case 3: //NODE_TEXT                                        
            return parseTextNode(node,context)
        case 4: //NODE_CDATA_SECTION                     
            return parseCDATA(node,context)
        case 5: //NODE_ENTITY_REFERENCE                
            return parseEntityReference(node,context)
        case 6: //NODE_ENTITY            
            return parseEntity(node,context)
        case 7: //NODE_PROCESSING_INSTRUCTION    
            return parseProcessingInstruction(node,context)
        case 8: //NODE_COMMENT                                 
            return parseComment(node,context)
        case 9: //NODE_DOCUMENT                                
        case 11://NODE_DOCUMENT_FRAGMENT             
            return parseDocument(node,context)
        case 10://NODE_DOCUMENT_TYPE                     
            return parseDocumentType(node,context)
        //case 11://NODE_DOCUMENT_FRAGMENT             
        //    return parseDocumentFragment(node,context)
        case 12://NODE_NOTATION 
            return parseNotation(node,context)
        default://文本节点
            //this.println("<!-- ERROR: UNKNOW nodeType:"+node.nodeType+"-->")
    }
});


var htmlLeaf = /^(?:meta|link|img|br|hr)$/i;
var scriptTag = /^script$/i

XMLParser.prototype.addParser(function(node,context){
    if(node.nodeType ==1){
        var next = node.attributes;
        context.append('<'+node.tagName);
        for (var i=0; i<next.length; i++) {
            context.parseNode(next[i],context)
        }
        if(htmlLeaf.test(node.tagName)){
            context.append('/>')
            return true;
        }
        context.append('>')
        next = node.firstChild
        if(next){
            do{
                context.parseNode(next,context)
            }while(next = next.nextSibling)
        }
        context.append('</'+node.tagName+'>')
        return true;
    }
});

//:core
XMLParser.prototype.addParser(function(node,context){//for
    if(node.nodeType ==1){
        var tagName = node.tagName.toLowerCase();
        if(/^c\:/.test(tagName)){
            switch(tagName.substr(2)){
            case 'if':
                parseIfTag(node,context);
                break;
            case 'elseif':
            case 'else-if':
                parseElseIfTag(node,context);
                break;
            case 'else':
                parseElseTag(node,context);
                break;
            case 'for':
            case 'foreach':
                parseForTag(node,context);
                break;
            case 'set':
            case 'var':
                parseVarTag(node,context);
                break;
            case 'out':
                parseOutTag(node,context);
                break;
            case 'choose':
                parseChooseTag(node,context);
                break;
            case 'when':
            case 'otherwise':
                break;
            
            
            //for other
            case 'include':
                processIncludeTag(node,context);
                break;
            default:
                $log.error("未知标签:",tagName,node.ownerDocument.documentURI)
            }
            return true;
        }
    }
});
/**
 * 
 */
function processIncludeTag(node,context){
    var attributes = loadAttribute(node,{'var':0,path:0,xpath:0});
    var doc = node.ownerDocument;
    var parentURL = context.url;
	try{
	    if(attributes['var']){
            var next = node.firstChild;
            context.append([6,attributes['var']]);
            if(next){
                do{
                    context.parseNode(next,context)
                }while(next = next.nextSibling)
            }
            context.append([]);
	    }
	    if(attributes.path!=null){
	        var url = parentURL.replace(/[^\/]*(?:[#\?].*)?$/,attributes.path);
	        var doc = context.load(url)
	    }
	    if(attributes.xpath!=null){
	        doc = selectNodes(doc,attributes.xpath);
	    }
	    context.parseNode(doc,context)
    }finally{
        context.url = parentURL;
    }
}
function parseIfTag(node,context){
    var next = node.firstChild;
    var attributes = loadAttribute(node,{test:3});
    context.append([2,attributes.test]);
    if(next){
        do{
            context.parseNode(next,context)
        }while(next = next.nextSibling)
    }
    context.append([]);
}

function parseElseIfTag(node,context){
    context.removeLastEnd();
    var next = node.firstChild;
    var attributes = loadAttribute(node,{test:3});
    context.append([3,attributes.test]);
    if(next){
        do{
            context.parseNode(next,context)
        }while(next = next.nextSibling)
    }
    context.append([]);
}


function parseElseTag(node,context){
    context.removeLastEnd();
    var next = node.firstChild;
    var attributes = loadAttribute(node,{});
    context.append([4]);
    if(next){
        do{
            context.parseNode(next,context)
        }while(next = next.nextSibling)
    }
    context.append([]);
}


function parseChooseTag(node,context){
	var next = node.firstChild;
	var first = true;
	var whenTag = node.tagName.split(':')[0];
	var elseTag = whenTag + ':otherwise';
	whenTag += ':when';
    if(next){
        do{
        	if(next.tagName == whenTag){
        		var n = next.parentNode.firstChild;
        		if(first){
        			first = false;
        			parseIfTag(next,context);
        		}else{
		            parseElseIfTag(next,context);
        		}
        	}else if(next.tagName == elseTag){
        		parseElseTag(next,context);
        	}else if(next.tagName){
        		$log.error("choose 只接受 when,otherwise 节点");
        	}
        	context.parseNode(next,context)
		}while(next = next.nextSibling)
    }
}

function parseForTag(node,context){
    var next = node.firstChild;
    var attributes = loadAttribute(node,{items:3,'var':1,begin:0,end:0,status:0});
    context.append([5,attributes['var'],attributes.items,attributes.status]);
    if(next){
        do{
            context.parseNode(next,context)
        }while(next = next.nextSibling)
    }
    context.append([]);
}
function parseVarTag(node,context){
    var attributes = loadAttribute(node,{name:1,value:0});
    var valueEl = attributes.value
    if(valueEl){
        context.append([6,attributes.name,toEL(valueEl)]);
    }else{
        var next = node.firstChild;
        context.append([6,attributes.name]);
        if(next){
            do{
                context.parseNode(next,context)
            }while(next = next.nextSibling)
        }
        context.append([]);
    }
}

function parseOutTag(node,context){
    var attributes = loadAttribute(node,{value:3});
    context.append([0,attributes.value,true]);
}








⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -