home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2011 November
/
CHIP_2011_11.iso
/
Programy
/
Narzedzia
/
AIMP2
/
aimp_2.61.583.exe
/
$TEMP
/
YandexPackSetup.msi
/
fil322579FA43F870D48B3FE187A5D7B988
< prev
next >
Wrap
Text File
|
2010-07-12
|
16KB
|
455 lines
var XB = {};
XB.init = function XB_init(application) {
XB._base.application = application;
XB._base.loggersRoot = application.name + ".XB";
XB._base.logger = application.core.Log4Moz.repository.getLogger(XB._base.loggersRoot + ".Base");
XB.cache.init();
XB.network.init();
};
XB.finalize = function XB_finalize() {
XB.network.finalize();
XB.cache.finalize();
XB._base.runtime.finalize();
XB._base.logger = null;
XB._base.domParser = null;
};
XB._Cc = Components.classes;
XB._Ci = Components.interfaces;
XB._Cr = Components.results;
XB._Cu = Components.utils;
XB._base = {
application: null,
consts: {
STR_LOCALE_DECIMAL_SEPARATOR: (1.1).toLocaleString()[1],
STR_FUNCTIONAL_NS: "http://bar.yandex.ru/dev/functional",
STR_UI_NS: "http://bar.yandex.ru/dev/gui",
STR_VAL_REF_ELEM_NAME: "__value__",
STR_VAL_REF_ID_KEY_NAME: "uid",
STR_WIDGET_ELEMENT_NAME: "widget",
ENUM_SCOPE_PACKAGE: 0,
ENUM_SCOPE_WIDGET: 1,
ENUM_SCOPE_INSTANCE: 2,
ERR_XML_PARSE_ERROR: "XML Parse error",
ERR_INVALID_DOC_STRUC: "Invalid document structure",
ERR_UID_NOT_FOUND: "Node uid attribute not found",
ERR_DOM_DOC_EXPECTED: "DOM document expected",
ERR_DOM_NODES_EXPECTED: "DOM node or node list expected",
ERR_NUMBER_EXPECTED: "Number expected",
ERR_STRING_EXPECTED: "String expected",
ERR_NO_RUNTIME_FUNCTION: "Function is not defined",
ERR_UID_REQUIRED: "UID required",
ERR_RUNTIME_ERROR: "Runtime error occured",
ERR_UNIT_REQUIRED: "XB unit required",
ERR_PACKAGE_REQUIRED: "Widget package required",
ERR_WINST_REQUIRED: "Widget instance required",
ERR_DATA_UNDEFINED: "No data, variable or setting with this name was defined",
ERR_PROC_NODE_EXPECTED: "ProcNode instance expected",
ERR_FUNC_NODE_EXPECTED: "FuncNode instance expected",
ERR_DISPOSED_XML: "Disposed XML object"
},
logger: null,
domParser: XB._Cc["@mozilla.org/xmlextras/domparser;1"].getService(XB._Ci.nsIDOMParser),
genWindowEngineID: function XB_genWindowEngineID() {
return this._newWEID++;
},
genXULBuilderID: function XB_genXULBuilderID() {
return this._newXBID++;
},
_newWEID: 0,
_newXBID: 0
};
XB._base.runtime = {
finalize: function XBRuntime_finalize() {
this._tempDoc = null;
},
xToString: function XBRuntime_xToString(fromXVal) {
if (sysutils.isString(fromXVal))
return fromXVal;
if (fromXVal == undefined || fromXVal == XB.types.empty)
return "";
if (this.isXML(fromXVal))
return fromXVal.textContent;
if (this.isRequestData(fromXVal))
return fromXVal.url;
if (this.isException(fromXVal))
throw new Error(this._consts.ERR_EXCEPTION_CONV);
return fromXVal.toString();
},
xToNumber: function XBRuntime_xToNumber(fromXVal) {
if (sysutils.isNumber(fromXVal)) {
if (isNaN(fromXVal))
throw new Error(this._consts.ERR_CANT_CONVERT);
return fromXVal;
}
if (this.isException(fromXVal))
throw new Error(this._consts.ERR_EXCEPTION_CONV);
if (sysutils.isBool(fromXVal))
return Number(fromXVal);
var trimmedStrVal = sysutils.trimAllSpaces( this.xToString(fromXVal) );
if (!trimmedStrVal)
throw new Error(this._consts.ERR_CANT_CONVERT);
if (!this._numPattern.test(trimmedStrVal))
throw new Error(this._consts.ERR_CANT_CONVERT + " '" + trimmedStrVal + "'");
var num = Number(trimmedStrVal);
if (isNaN(num))
throw new Error(this._consts.ERR_CANT_CONVERT);
return num;
},
xToBool: function XBRuntime_xToBool(fromXVal) {
if (sysutils.isBool(fromXVal))
return fromXVal;
if (sysutils.isNumber(fromXVal))
return fromXVal != 0;
if (this.isException(fromXVal))
throw new Error(this._consts.ERR_EXCEPTION_CONV);
var strVal = sysutils.trimSpaces( this.xToString(fromXVal) );
if (strVal == "" || strVal == "false")
return false;
if (this._numPattern.test(strVal)) {
let numVal = Number(strVal);
if (!isNaN(numVal))
return numVal != 0;
}
return true;
},
xToXML: function XBRuntime_xToXML(fromXVal) {
if (this.isXML(fromXVal))
return fromXVal;
var node = this._tempDoc.createTextNode(this.xToString(fromXVal));
var xmlObj = new XB.types.XML(node);
return xmlObj;
},
xToRequestData: function XBRuntime_xToRequestData(fromXVal) {
if (this.isRequestData(fromXVal))
return fromXVal;
const monthTimeRange = 60 * 60 * 24 * 30;
return new XB.types.RequestData(this.xToString(fromXVal), "GET", monthTimeRange, monthTimeRange * 2,
XB.types.RequestData.Format.FMT_XML, {start:100, end:399});
},
isXML: function XBRuntime_IsXML(xVal) {
return (xVal instanceof XB.types.XML);
},
isException: function XBRuntime_IsException(xVal) {
return xVal instanceof XB.types.Exception;
},
isRequestData: function XBRuntime_IsRequestData(xVal) {
return xVal instanceof XB.types.RequestData;
},
compareValues: function XBRuntime_CompareValues(value1, value2, mode) {
if (value1 === value2)
return 0;
var [value1, value2, rank] = this._checkTypes(value1, value2, mode);
switch (rank) {
case this._ranksMap.Empty:
return 0;
case this._ranksMap.Bool:
return Number(value1) - Number(value2);
case this._ranksMap.Number:
return value1 - value2;
case this._ranksMap.String:
return sysutils.strcmp(value1, value2);
case this._ranksMap.XML:
case this._ranksMap.RequestData:
case this._ranksMap.Exception:
return value1.equalsTo(value2) ? 0 : NaN;
default:
return NaN;
}
},
serializeXML: function XBRuntime_serializeXML(node) {
return this._XMLSerializer.serializeToString(node);
},
get tempXMLDoc() {
return this._tempDoc;
},
queryXMLDoc: function XBRuntime_QueryXMLDoc(expr, contextNode) {
try {
XB._base.logger.trace("XPATH expr: " + expr);
if ( !(contextNode instanceof XB._Ci.nsIDOMNode) )
throw new TypeError(XB._base.consts.ERR_DOM_NODES_EXPECTED);
var doc = (contextNode instanceof XB._Ci.nsIDOMDocument)? contextNode: contextNode.ownerDocument;
var NSResolver = this._xpathEvaluator.createNSResolver(doc.documentElement);
var rawResult = this._xpathEvaluator.evaluate(expr, contextNode, NSResolver, 0, null);
XB._base.logger.trace("Got raw XPATH result of type " + rawResult.resultType);
var result;
switch (rawResult.resultType) {
case rawResult.NUMBER_TYPE:
result = rawResult.numberValue;
if (isNaN(result))
throw new this.EXPATHNaN(expr);
break;
case rawResult.STRING_TYPE:
result = rawResult.stringValue;
break;
case rawResult.BOOLEAN_TYPE:
result = rawResult.booleanValue;
break;
case rawResult.UNORDERED_NODE_ITERATOR_TYPE:
var nodesArray = [];
var node;
while (node = rawResult.iterateNext())
nodesArray.push(node);
XB._base.logger.trace("XPATH result nodes length = " + nodesArray.length);
result = nodesArray;
break;
default:
throw new this.EXPATHUnexpectedResult(expr, rawResult.resultType);
}
XB._base.logger.trace("XPATH (" + expr + ") result is \"" + result + "\"");
return result;
}
catch (e) {
if (!(e instanceof this.EXPATHNaN)) // this happens pretty often
XB._base.logger.warn(this._consts.ERR_XPATH_QUERY_FAILED + ". " + misc.formatError(e));
throw e;
}
},
transformXML: function XBRuntime_transformXML(node, stylesheet) {
try {
this._xsltProcessor.importStylesheet(stylesheet);
return this._xsltProcessor.transformToFragment(node, this._tempDoc);
}
finally {
this._xsltProcessor.reset();
}
},
createXBExceptionFromRTError: function XBRuntime_XBEFromRTE(nid, error) {
var XBE = new XB.types.Exception(nid, XB.types.Exception.types.E_RUNTIME, error.message);
XBE.name = error.name;
XBE.fileName = error.fileName;
XBE.lineNumber = error.lineNumber;
return XBE;
},
describeValue: function XBRuntime_describeValue(xValue) {
var valueRank = this._rankValue(xValue);
var descr = this._typeNames[valueRank];
if (valueRank > this._ranksMap.Empty && valueRank < this._ranksMap.XML)
descr += "(" + this.xToString(xValue) + ")";
if (valueRank == this._ranksMap.Exception)
descr += "(" + xValue.toString() + ")";
return descr;
},
ensureValueTypeIs: function XBRuntime_ensureValueTypeIs(xValue, xType) {
var valueRank = this._rankValue(xValue);
var expectedRank = this._ranksMap[xType];
if (expectedRank === undefined)
throw new Error("Unknown type name");
if (expectedRank != valueRank)
throw new TypeError(xType + " expected, got " + this._typeNames[valueRank]);
},
_consts: {
ERR_CANT_CONVERT: "This value conversion is impossible",
ERR_EXCEPTION_CONV: "Exception objects cannot be converted",
ERR_XPATH_QUERY_FAILED: "XPATH query failed",
ERR_UNKNOWN_TYPE: "Unknown type ",
ERR_NO_CAST_FUNC: "No casting function for this rank"
},
_tempDoc: XB._Cc["@mozilla.org/xml/xml-document;1"].createInstance(XB._Ci.nsIDOMDocument).implementation.createDocument("", "xbruntime", null),
_XMLSerializer: XB._Cc["@mozilla.org/xmlextras/xmlserializer;1"].getService(XB._Ci.nsIDOMSerializer),
_xpathEvaluator: XB._Cc["@mozilla.org/dom/xpath-evaluator;1"].getService(XB._Ci.nsIDOMXPathEvaluator),
_xsltProcessor: XB._Cc["@mozilla.org/document-transformer;1?type=xslt"].createInstance(XB._Ci.nsIXSLTProcessor),
_numPattern: /^[+\-]?(\d+\.?|\d*\.\d+)$/,
_typeNames: {
0: "Empty",
1: "Bool",
2: "Number",
3: "String",
4: "XML",
5: "RequestData",
6: "Exception"
},
_ranksMap: {
Empty: 0,
Bool: 1,
Number: 2,
String: 3,
XML: 4,
RequestData: 5,
Exception: 6
},
_rankValue: function XBRuntime_rankValue(value) {
if (value === XB.types.empty)
return this._ranksMap.Empty;
if (sysutils.isBool(value))
return this._ranksMap.Bool;
if (sysutils.isNumber(value))
return this._ranksMap.Number;
if (sysutils.isString(value))
return this._ranksMap.String;
if (this.isXML(value))
return this._ranksMap.XML;
if (this.isRequestData(value))
return this._ranksMap.RequestData;
if (this.isException(value))
return this._ranksMap.Exception;
throw new TypeError(this._consts.ERR_UNKNOWN_TYPE + typeof value);
},
_castToRank: function XBRuntime_CastToRank(value, rank) {
var destType = this._typeNames[rank];
var castFunc = this["xTo" + destType];
if (castFunc == undefined)
throw new Error(this._consts.ERR_NO_CAST_FUNC);
return castFunc.call(this, value);
},
_checkTypes: function XBRuntime_CheckTypes(value1, value2, mode) {
var r1 = this._rankValue(value1);
var r2 = this._rankValue(value2);
if (r1 == r2)
return [value1, value2, r1];
if (mode == XB._base.runtime.cmpModes.CMP_STRICT ||
r1 > this._ranksMap.RequestData || r2 > this._ranksMap.RequestData)
return [value1, value2, NaN];
var maxRank = Math.max(r1, r2);
var commonRank = (mode == this.cmpModes.CMP_SMART ? Math.min(this._ranksMap.String, maxRank) : maxRank);
try {
return [this._castToRank(value1, commonRank), this._castToRank(value2, commonRank), commonRank]
}
catch (e) {
XB._base.logger.warn("Could not cast either '" + this.describeValue(value1) + "' or '" +
this.describeValue(value2) + "' to type '" + this._typeNames[commonRank] + "'");
return [value1, value2, NaN];
}
}
};
XB._base.runtime.cmpModes = {
CMP_STRICT: 0,
CMP_FREE: 1,
CMP_SMART: 2
};
XB._base.runtime.EXPATH = CustomErrors.ECustom.extend({
$name: "EXPATH",
constructor: function EXPATH_constructor(expression) {
this.base();
this._expr = expression.toString();
},
_message: "XPATH query error",
_expr: undefined,
get _details() {
return [this._expr];
}
});
XB._base.runtime.EXPATHNaN = XB._base.runtime.EXPATH.extend({
$name: "EXPATHNaN",
_message: "XPATH 'number' expression returned NaN"
});
XB._base.runtime.EXPATHUnexpectedResult = XB._base.runtime.EXPATH.extend({
$name: "EXPATHUnexpectedResult",
constructor: function EXPATHUnexpReslt_constructor(expression, resultType) {
this.base(expression);
this._resType = resultType.toString();
},
_message: "XPATH query error",
_resType: undefined,
get _details() {
return [this._expr, this._resType];
}
});
XB._base.WidgetBase = Base.extend({
constructor: function XBWidgetBase_constructor(id) {
if (!id)
throw new Error(this._consts.ERR_ID_REQUIRED);
this._id = id;
this._varsMap = {};
this._dataMap = {};
this._settingsMap = {};
this._refsMap = {};
},
get id() {
return this._id;
},
get settings() {
return sysutils.copyObj(this._settingsMap, false);
},
findReference: function XBWidgetBase_findReference(refID) {
var refNode = this._refsMap[refID];
return refNode? refNode: null;
},
findVariable: function XBWidgetBase_findVariable(varName) {
var varNode = this._varsMap[varName];
return varNode? varNode: null;
},
findData: function XBWidgetBase_findData(dataName) {
var dataNode = this._dataMap[dataName];
return dataNode? dataNode: null;
},
findSetting: function XBWidgetBase_findSetting(settingName) {
var setting = this._settingsMap[settingName];
return setting? setting.node: null;
},
eraseSettings: function XBWidgetBase_eraseSettings() {
for each (let setting in this._settingsMap) {
setting.node.erase();
}
},
_consts : {
WARN_VAR_REDEFINED: "Redefined variable",
WARN_DATA_REDEFINED: "Redefined data",
ERR_ID_REQUIRED: "ID required",
ERR_INVALID_SCOPE: "Widget or instance scope expected"
},
_varsMap: null,
_dataMap: null,
_settingsMap: null,
_refsMap: null
});