Introduction to JScript quick development framework EDK: overview

Source: Internet
Author: User
Tags servervariables
Document directory
  • Client Information
  • Serialization request object
  • Getrawpost ()
  • XML Processing

Since today, I have been writing articles about EDK. Start to straighten out your ideas ~ Thoroughly complete EDK!

Client Information

Encapsulate the client information:

/*** Return the Request Path (that is, the path of the current page ). * @ Return {string} URL */function getpath () {return request. servervariables ('url') () ;}/ *** gets the request method. * @ Return {string} */function gethttp_method () {return request. servervariables ('HTTP _ method') ();}/*** @ return {string/null} */function getreferer () {var arr = request. servervariables ('all _ raw ')(). match (/Referer: \ s *(. *)/I); Return arr = NULL? Null: arr [0];}

Response:

/*** Unified encoding for UTF-8. */Function setutf8 () {response. charset = "UTF-8"; Session. codePage = 65001; Session. timeout = 200; server. scripttimeout = 100;}/*** 301 permanent redirection. * @ Param {string} the URL to jump. */Function urlforward (URL) {response. status = "301 moved permanently"; response. addheader ("location", URL); response. end ();}/*** force not to cache. */Function nocache () {with (response) {buffer = false; expires =-1; addheader ("Pragma", "No-Cache "); addheader ("cache-ctrol", "No-Cache ");}}

Serialization request object

Obtain data from fields in the form. The data submitted by HTTP form is the key-Value Pair structure. This function converts these key/value sets into JavaScript objects. This is the core function of the post package. The decodeuri (), Unescape (), and $. getperm () fields are automatically converted. @ Param {Boolean/string} isallrequest true indicates the collection of request objects returned by querystring and form. If form is specified
Hash value. If querystring is specified, the hash parameter above the URL is returned.

/*** @ Return {object} data submitted by the client or query parameters. */Function every (Collection) {var COUNT = collection. count; var OBJ = {count: Count // total number of fields}; var EMU = new enumerator (Collection); var key, V; while (count> 0 &&! EMU. atend () {key = EMU. item (). tostring (); V = Collection (key) (); // The ms asp here is so strange that no () can return the string OBJ [Key] =$ $. getprimitives (V); // performs automatic type conversion. EMU. movenext ();} return OBJ;}; this. formpost = Every. Delegate (request. form); this. querystring = Every. Delegate (request. querystring );
Getrawpost ()

The Parser of raw post. Raw post is also a standard http post method, but unlike the text submitted in the form of key1 = value2 & key2 = value2, It is a bunch of binary data. The post data submitted by the browser is treated as the original data and can be used only after the binary Decoding of ADODB. Stream. Note: once this method is used. Request. Form ('key') cannot be used, but querystring passed through the URL is still available.

/*** @ Return {object} */function getrawpost () {with (New activexobject ('ADODB. stream ') {open (); type = 2; // 2 = adtypetextwritetext (request. binaryread (request. totalbytes); position = 0; charset = "US-ASCII"; // gb2312? Position = 2; EVAL ('var requestobj = '+ readtext (); // when using the eval () function, a variable is required to carry it! Close (); Return requestobj;}/*** indicates whether to submit data by post. * @ Return {Boolean} */function ispost () {return request. servervariables ('HTTP _ method') () = "Post";}/*** get the contenttype of the HTTP message. The contenttype of the get operation is supported. * @ Return {string} */function getcontenttype () {return request. servervariables ('content _ type') () ;}/ *** return raw post. This method must be used in some scenarios, such as Ext. Direct. * Convention: Any application/JSON submission is considered as raw post. * @ Return {Boolean} */function israwpost () {return ispost () & (getcontenttype (). indexof ('application/json '))! =-1}
XML Processing

Cross browser XML Loader:

/*** Compatible with XML loaders of browsers and servers. Note that asynchronous communication is disabled in current mode. * Create a Document Object * @ Param {string} xmlxml document path or XML document fragment. * @ Param {Boolean} isnodetrue indicates the XML file segment sent. * @ Return {object} The document */function loadxml (XML, isnode) {var Doc; If (typeof activexobject! = 'Undefined') {Doc = registry.xml.doc ();} else if (typeof document! = 'Undefined '&&! Isnode) {If (document. implementation & document. implementation. createdocument) {Doc = document. implementation. createdocument ("", "", null) ;}} else if (typeof domparser! = 'Undefined' & isnode) {Doc = new domparser (). parsefromstring (XML, "text/XML"); // load the XML snippet (moliza Firefox) return Doc;} If (! Doc) {throw': An error occurred while creating the XML document object! ';} Doc. async = false; // disable the asynchronous feature if (xml &&! Isnode & (Doc. load (XML) = false) {// load a complete XML document (both in moliza Firefox and IE) throw' loading XML Document Resources failed! ';} Else if (xml & isnode & (Doc. loadxml (XML) = false) {// loading XML fragment (IE) throw' failed to load XML fragment! ';} Return Doc;}/*** locates an XML node. If not, an empty array is returned. * Usage: var Doc = loadxml ('edk. xml'); var xml = getnode. call (Doc, '// head', false); * @ Param {string} xpathquery query locator. * @ Param {Boolean} isserialize (optional) indicates whether the serial number query result is a string. The default value is true. */Loadxml. gettpl = TPL. gettpl = function (xpathquery, isserialize) {var nodes = []; var MSG = 'no exist node! No matched node exists. Check the input conditions before querying. '; If (typeof isserialize = 'undefined') {isserialize = true;} If (typeof this. selectnodes! = 'Undefined') {nodes = This. selectsinglenode (xpathquery); If (nodes = NULL) {Throw MSG;} return isserialize? Nodes. XML: nodes;} else if (document. implementation. hasfeature ('xpath ', '3. 0 ') {// FF needs to be further converted var resolver = this.creatensresolver(this.doc umentelement); var items = This. evaluate (xpathquery, this, resolver, xpathresult. ordered_node_snapshot_type, null); For (VAR I = 0, j = items. snapshotlength; I <j; I ++) {nodes [I] = items. snapshotitem (I);} If (! Nodes. Length) {Throw MSG;} return isserialize? New xmlserializer (). serializetostring (nodes [0]): nodes;} return nodes ;}

Save the log in XML format:

Function log (log) {var JSON, TMP; If (log. SQL) {TMP = log. SQL; Delete log. SQL;} JSON =$ $. XML. json2xml (log); var logitem = paiyun.xml.doc (); If (! Logitem. loadxml ('<item>' + JSON + '</item>') {Throw "error in generating the log XML document";} VaR value = logitem. createelement ("value"); var Cd = logitem. createcdatasection (TMP); value. appendchild (CD); logitem. firstchild. appendchild (value); // Insert a new XML node var xml = new activexobject ('msxml2. domdocument.6.0 '); If (! XML. load (server. mappath ('/APP/public/log. XML ') {Throw "error when saving the log XML document";} XML. lastchild. appendchild (logitem. firstchild); $. XML. savexml (XML); logitem = xml = value = Cd = NULL; return true ;}

All XML helper:

/*** @ Class * @ Singleton * is compatible with XML loaders of browsers and servers. */$. Xml = $. xml = new (function () {/*** converts the XML format to JSON output. MS only temporarily. * @ Param {ixmldomnode} n * @ return {object} JSON */This. xml2json = function (node) {var OBJ ={}; var element = node. firstchild; while (element) {If (element. nodetype = 1) {var name = element. nodename; var sub; sub = arguments. callee (element) sub. nodevalue = ""; sub. xml = element. XML; sub. tostring = function () {return this. nodevalue;}; sub. toxmlstring = function () {return this. XML;} // get attributesif (eleme NT. attributes) {for (VAR I = 0; I <element. attributes. length; I ++) {var attribute = element. attributes [I]; sub [attribute. nodename] = attribute. nodevalue;} // get nodevalueif (element. firstchild) {var nodetype = element. firstchild. nodetype; If (nodetype = 3 | nodetype = 4) {sub. nodevalue = element. firstchild. nodevalue ;}// node already exists? If (OBJ [name]) {// need to create array? If (! OBJ [name]. length) {var temp = OBJ [name]; OBJ [name] = []; OBJ [name]. push (temp);} // append object to arrayobj [name]. push (sub);} else {// create objectobj [name] = sub ;}} element = element. nextsibling;} return OBJ;} // @ todo? VaR xml = {text: function (text) {return text. replace (// G ,"&"). replace (/</g, "<"). replace (/>/g, ">")}, ATTR: function (name, value) {return (Name & value! = NULL )? ("" + Name + "= \" "+ this. Text (value). Replace (/"/g,/* "--> \ x22? */"") + "\" "):" ";}, CDATA: function (text) {return (text )? "<! [CDATA ["+ text. tostring (). replace ("]>", "]>") + "]>": "" ;}};/*** json2xml */This. json2xml = function (OBJ) {var STR = ''; vaR TPL = '<{0} type = "{1}"> {2} </{0}> \ n'; function serialize (OBJ) {var fn = arguments. callee; var xml = []; var typ = typeof OBJ; Switch (typ) {Case "object": {If (OBJ = NULL) {XML. push ("<" + I + "/> \ n"); // empty closed tag} else if (typeof obj. gettime = "function") {XML. push (TPL. format (I, 'date', obj. toutcstring ();} else if (typeof obj. join = "function") {for (var j = 0; j <obj. length; j ++) {XML. push (FN (OBJ [J]);} else {XML. push (TPL. format (I, typ, FN (OBJ);} break;} case "string": OBJ = obj. replace (// G ,"&"). replace (/</g, "<"). replace (/>/g, ">"); Case "date": Case "Boolean": Case "Number": XML. push (TPL. format (I, typ, OBJ);} return XML. join ("") ;}for (var I in OBJ) {STR + = Serialize (OBJ [I]);} return STR;}/*** compatible with XML loaders of browsers and servers. Note: Disable asynchronous communication in current mode. * Create a Document Object * @ Param {string} xmlxml document path or XML document fragment. * @ Param {Boolean} isnodetrue indicates the XML file segment sent. * @ Return {object} The document */This. loadxml = function (XML, isnode) {var Doc; If (typeof activexobject! = 'Undefined') {Doc = registry.xml.doc ();} else if (typeof document! = 'Undefined '&&! Isnode) {If (document. implementation & document. implementation. createdocument) {Doc = document. implementation. createdocument ("", "", null) ;}} else if (typeof domparser! = 'Undefined' & isnode) {Doc = new domparser (). parsefromstring (XML, "text/XML"); // load the XML snippet (moliza Firefox) return Doc;} If (! Doc) {throw': An error occurred while creating the XML document object! ';} Doc. async = false; // disable the asynchronous feature if (xml &&! Isnode & (Doc. load (XML) = false) {// load a complete XML document (both in moliza Firefox and IE) throw' loading XML Document Resources failed! ';} Else if (xml & isnode & (Doc. loadxml (XML) = false) {// loading XML fragment (IE) throw' failed to load XML fragment! ';} Return Doc;}/*** locates an XML node. If not, an empty array is returned. * Usage: var Doc = loadxml ('edk. xml'); var xml = getnode. call (Doc, '// head', false); * @ Param {string} xpathquery query locator. * @ Param {Boolean} isserialize (optional) indicates whether the serial number query result is a string. The default value is true. */This. getnode = function (xpathquery, isserialize) {var nodes = []; var MSG = 'no exist node! No matched node exists. Check the input conditions before querying. '; If (typeof isserialize = 'undefined') {isserialize = true;} If (typeof this. selectnodes! = 'Undefined') {nodes = This. selectsinglenode (xpathquery); If (nodes = NULL) {Throw MSG;} return isserialize? Nodes. XML: nodes;} else if (document. implementation. hasfeature ('xpath ', '3. 0 ') {// FF needs to be further converted var resolver = this.creatensresolver(this.doc umentelement); var items = This. evaluate (xpathquery, this, resolver, xpathresult. ordered_node_snapshot_type, null); For (VAR I = 0, j = items. snapshotlength; I <j; I ++) {nodes [I] = items. snapshotitem (I);} If (! Nodes. Length) {Throw MSG;} return isserialize? New xmlserializer (). serializetostring (nodes [0]): nodes;} return nodes ;} /*** perform an xls transformation on an XML document and store the results in an element * @ Param {element} target the element to populate with the results of the transformation */This. transform = function (effectdoc, target) {If (effectprocessor) {var processor = new effectprocessor (); processor. importstylesheet (xsltdoc); var Doc = pro Cessor. transformtofragment (this, document); target. appendchild (DOC);} else {target. innerhtml = This. transformnode (effectdoc);}/*** determines which option control to select based on the data value (search for matching in data. * @ Param {ixmldomnode} nxml Node object. * @ Param {object} data object. * @ Return {ixmldomnode} XML Node object. */This. setselectbynode = function (n, data) {var I, K; var selectel; for (I in data) {k = ". // select [@ name = '{0}'] ". format (I); selectel = n. selectsinglenode (k); If (selectel) {// select the node with the same value as for (VAR z = 0; Z <2; Z ++) {If (selectel. childnodes (z ). attributes (0 ). value = data [I]) {selectel. childnodes (z ). setattribute ('selected', 'true'); // set it to selected! }}} Return N;}/*** determines which radio control to select based on the data value (search for matching in data. * @ Param {ixmldomnode} nxml Node object. * @ Param {object} data object. * @ Return {ixmldomnode} XML Node object. */This. setradiobynode = function (n, data) {var K, selectel; For (var I in data) {k = ". // input [@ name = '{0}'] ". format (I); selectel = n. selectnodes (k); If (selectel & selectel. length> 0) {// select the node with the same value for (VAR z = 0; Z <selectel. length; Z ++) {// The default attributes (3) is the name attribute if (selectel (z ). attributes (1 ). value = data [I]) {selectel (z ). setattribute ('checked', 'true'); // set it to selected! }}} Return N;}/*** select the specified template file from the XML file and bind the data to the data output. * You can cancel the conversion of CDATA. * @ Param {string} xmlfilexml fragment or XML file requires a complete file path address. Generally, you need server. mappath () to obtain the actual address before entering it here. * @ Param {string} XPath path. * @ Param {object} data (optional) data entity, usually a JSON object or configuration file $. cfg. * @ Return {string} refers to the HTML containing the data. */This. from_xml_tpl = function (xmlfile, XPath, data) {var xml = new activexobject ('msxml2. domdocument.6.0 '), node, TPL, HTML; // automatically determines whether the server-side method can be used if (typeof server! = 'Undefiend') {If (XML. Load (server. mappath (xmlfile ))! = True) {throw' load '+ xmlfile + 'template file failed' ;};} else {If (XML. loadxml (xmlfile )! = True) {Throw 'loaders '+ xmlfile + 'fragment failed' ;};} node = xml. selectsinglenode (XPath); If (! Node) {throw' no template node ';} // The selected Attribute of option does not use the complete XML statement form !! If (data) {$. XML. setselectbynode (node, data); $. XML. setradiobynode (node, data);} TPL = node. XML, TPL = TPL. replace ('&', '&') // avoids XML standard escaping. xml = NULL, html = data? New EDK. template (TPL). applytemplate (data. If (html. indexof ('<! [CDATA [') {html = html. Replace (' <! [CDATA [',''). replace (']>', '');} return HTML;} This. savecdata = function () {var post =$ $. form. post (); var filepath = server. mappath ($. cfg. edk_root + '/APP/form/staticpage. XML '); var xml = new activexobject ('msxml2. domdocument.6.0 '); If (! XML. load (filepath) {Throw "Opening template file error";} var parentnode = xml. selectsinglenode ('//' + Page. split ('. '). pop (); var cdatanode = xml. createcdatasection (post ['content']); parentnode. replaceChild (cdatanode, parentnode. childnodes (0); $. XML. savexml (XML, filepath); response. write ('write CDATA successful! '); Return true;} function setxml_cdata (data, wraptag, iscdata) {This. contenttype = 'text/xml'; If (typeof DATA = 'string') {If (wraptag &&! Iscdata) {DATA = '<{0}> {1} </{0}> '. format (wraptag, data);} else if (wraptag & iscdata) {DATA = '<{0}>' + ("<! [Cd "+" ata [") +" {1} "+ ("]> "+"> ") + '</{0}> '. format (wraptag, Data. replace ("]>" + ">"), "]>");} return data;} else {data =$ $. XML. json2xml (data);} return datal }});

XML class

$. Xml = {DOC: function () {var Doc;/*** ActiveX objects for importing XML (ie only) * @ type array */var MSXML = ["msxml2.domdocument. 6.0 "," msxml2.domdocument. 5.0 "," msxml2.domdocument. 4.0 "," msxml2.domdocument. 3.0 "," msxml2.domdocument "," Microsoft. xmldom "]; for (VAR I = 0, j = MSXML. length; I <j; I ++) {try {Doc = new activexobject (MSXML [I]); break;} catch (e) {}} return Doc ;} /*** Save the XML object as an XML text file. * Note: server side only * @ Param {xmldocument} xmldocxml document object itself. * @ Param {string} xmlfilepath is optional. The actual disk path of the XML document. * @ Return {Boolean} indicates whether the operation is successful. */, Save: function (xmldoc, xmlfilepath) {xmlfilepath = xmlfilepath | xmldoc. URL. replace (/file: \/\ //, ''); // make a clonevarsaver = this.doc (); saver. loadxml (xmldoc. XML); If (saver. readystate = 4 & amp; saver. parsed) {saver. save (xmlfilepath);} return true;}/*** @ Param {string} node * @ Param {string} Str */, savecdata: function (node, STR) {var parentnode = This. selectsinglenode (node); var cdatanode = thi S. createcdatasection (STR); parentnode. replaceChild (cdatanode, parentnode. childnodes (0); Return true; // data written to CDATA is successful !}};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.