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
/
fil26DA2E9042FFB9D78F04746B1F57B984
< prev
next >
Wrap
Text File
|
2010-07-12
|
6KB
|
166 lines
XB.Preset = function(content, address) {
this._widgetsInfo = [];
if (content instanceof Ci.nsIFile) {
this._loadFromFile(content);
} else if (content instanceof Ci.nsIDOMDocument) {
this._loadFromDocument(content);
} else
throw new CustomErrors.EArgType("content", "nsIFile|nsIDOMDocument", typeof content);
if (!sysutils.isString(address))
throw new CustomErrors.EArgType("address", "String", typeof address);
this._address = address;
};
XB.Preset.EPresetSyntax = CustomErrors.ECustom.extend({
$name: "EPresetSyntax",
constructor: function EPresetSyntax_constructor(elementName) {
this.base("XB preset parse error");
this._elementName = elementName.toString();
},
get _details() {
return [this._elementName];
},
_elementName: undefined
});
XB.Preset.prototype = {
constructor: XB.Preset,
get address() {
return this._address;
},
get name() {
return this._name;
},
get version() {
return this._version;
},
get author() {
return this._author;
},
get icon() {
return this._icon;
},
get widgetsInfo() {
return sysutils.copyObj(this._widgetsInfo, true);
},
get content () {
return this._content;
},
_consts: {
STR_PRESET_ELEMENT_NAME: "preset",
STR_SETTING_ELEMENT_NAME: "setting",
WARN_WIDGET_IGNORED: "Widget was ignored because of syntax errors",
WARN_SETTING_IGNORED: "Widget setting was ignored because of syntax errors",
ERR_NO_NAME: "No preset name",
ERR_NO_AUTHOR: "No preset author",
ERR_PKG_BINDING: "Invalid package manifest binding",
ERR_INVALID_ADDRESS: "Invalid preset address"
},
_address: undefined,
_author: undefined,
_name: undefined,
_version: "1.0",
_icon: undefined,
_widgetsInfo: undefined,
_loadFromFile: function XBPreset_loadFromFile(presetFile) {
this._loadFromDocument( sysutils.xmlDocFromFile(presetFile) );
},
_loadFromDocument: function XBPreset_loadFromDocument(XMLDocument) {
let rootName = XMLDocument.documentElement.localName;
if (rootName != this._consts.STR_PRESET_ELEMENT_NAME)
throw new XB.Preset.EPresetSyntax(rootName);
this._parsePreset(XMLDocument.documentElement);
XB._base.logger.debug("Preset loaded");
var serializer = Components.classes["@mozilla.org/xmlextras/xmlserializer;1"].createInstance(Components.interfaces.nsIDOMSerializer);
this._content = serializer.serializeToString(XMLDocument);
},
_parsePreset: function XBPreset_parsePreset(presetElement) {
function findBestMatch(elementsArray, appLang) {
for each (let element in elementsArray) {
if (element.getAttribute("lang") == appLang) {
return element.textContent;
}
}
if (elementsArray.length > 0) {
return elementsArray[0].textContent;
}
}
this._version = presetElement.getAttribute("version") || "1.0";
let appLang = misc.parseLocale(XB._base.application.localeString).language;
let authorNodes = XB._base.runtime.queryXMLDoc("./author", presetElement);
this._author = findBestMatch(authorNodes, appLang);
if (!this._author)
throw new XB.Preset.EPresetSyntax("author");
let nameNodes = XB._base.runtime.queryXMLDoc("./name", presetElement);
this._name = findBestMatch(nameNodes, appLang);
if (!this._name)
throw new XB.Preset.EPresetSyntax("name");
let iconNodes = XB._base.runtime.queryXMLDoc("./icon", presetElement);
this._icon = findBestMatch(iconNodes, appLang);
let widgetNodes = XB._base.runtime.queryXMLDoc("./widget", presetElement);
for each (let widgetNode in widgetNodes) {
let widgetInfo = this._parseWidgetElement(widgetNode);
if (widgetInfo)
this._widgetsInfo.push(widgetInfo);
}
},
_parseWidgetElement: function XBPreset_parseWidgetElement(widgetElement) {
let protoID = widgetElement.getAttribute("id");
if (!protoID) {
XB._base.logger.warn(this._consts.WARN_WIDGET_IGNORED);
return null;
}
let separatorPos = protoID.indexOf("#");
let packageURI = protoID.substring(0, separatorPos);
let widgetInfo = {
protoID: protoID,
packageID: packageURI,
packageURI: packageURI,
name: protoID.substring(separatorPos + 1),
settings: {}
};
for (let settingIndex = widgetElement.childNodes.length; settingIndex--; ) {
let settingElement = widgetElement.childNodes[settingIndex];
if (settingElement.nodeType != settingElement.ELEMENT_NODE ||
settingElement.localName != this._consts.STR_SETTING_ELEMENT_NAME)
continue;
let settingName = settingElement.getAttribute("name");
if (!settingName) {
XB._base.logger.warn(this._consts.WARN_SETTING_IGNORED);
continue;
}
widgetInfo.settings[settingName] = settingElement.textContent;
}
return widgetInfo;
}
};