зеркало из https://github.com/mozilla/pjs.git
Adding in datepicker xbl component, and jslib files which are now needed by the calendar.
This commit is contained in:
Родитель
868e9bb44c
Коммит
55e48c0d45
|
@ -0,0 +1,113 @@
|
|||
|
||||
calendar {
|
||||
-moz-binding: url("calendar.xml#calendar");
|
||||
}
|
||||
/*
|
||||
.cal-menu {
|
||||
font-family: Arial;
|
||||
font-size: 14px;
|
||||
color: #ffffff;
|
||||
border: 1px outset #ffffff;
|
||||
}
|
||||
|
||||
.cal-menu:hover {
|
||||
font-family: Arial;
|
||||
font-size: 14px;
|
||||
color: #ffffff;
|
||||
border: 2px outset #ffffff;
|
||||
}
|
||||
|
||||
.cal-popup {
|
||||
min-width: 130px;
|
||||
background-color: #ffffff;
|
||||
border: 1px outset #ffffff;
|
||||
padding: 0px;
|
||||
}
|
||||
*/
|
||||
|
||||
.cal-mainbox {
|
||||
min-width: 130px;
|
||||
background-color: #ffffff;
|
||||
border: 1px outset #ffffff;
|
||||
padding: 0px;
|
||||
}
|
||||
|
||||
.cal-month-box {
|
||||
background-color: #D2B48C;
|
||||
border: 1px outset #D2B48C;
|
||||
}
|
||||
|
||||
.cal-month-name {
|
||||
font-family: Arial;
|
||||
font-size: 16px;
|
||||
font-weight: bold;
|
||||
color: #000000;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.cal-navbtn {
|
||||
min-width: 10px;
|
||||
-moz-binding: url("calendar.xml#cal-navbtn");
|
||||
}
|
||||
|
||||
.cal-navbtn[dir="-1"] {
|
||||
background: url("image/left-arrow.gif") no-repeat 50% 50%;
|
||||
}
|
||||
|
||||
.cal-navbtn[dir="1"] {
|
||||
background: url("image/right-arrow.gif") no-repeat 50% 50%;
|
||||
}
|
||||
|
||||
.cal-navbtn[dir="-1"]:hover {
|
||||
background: url("image/left-arrow-hover.gif") no-repeat 50% 50%;
|
||||
}
|
||||
|
||||
.cal-navbtn[dir="1"]:hover {
|
||||
background: url("image/right-arrow-hover.gif") no-repeat 50% 50%;
|
||||
}
|
||||
|
||||
.cal-navbtn:active {
|
||||
left: 2px;
|
||||
top: 2px;
|
||||
}
|
||||
|
||||
.cal-cal-box {
|
||||
}
|
||||
|
||||
.cal-header {
|
||||
width: 25px;
|
||||
height: 20px;
|
||||
font-family: Arial;
|
||||
color: blue;
|
||||
}
|
||||
|
||||
.cal-row-body {
|
||||
}
|
||||
|
||||
.cal-day {
|
||||
width: 25px;
|
||||
height: 20px;
|
||||
font-family: Arial;
|
||||
color: #000000;
|
||||
border: 1px solid transparent;
|
||||
-moz-binding: url("calendar.xml#cal-day");
|
||||
}
|
||||
|
||||
.cal-day[othermonth="true"] {
|
||||
color: #d2d2d2;
|
||||
}
|
||||
|
||||
.cal-day[selected="true"] {
|
||||
background-color: #778899;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.cal-day:hover {
|
||||
color: #ff0000;
|
||||
border: 1px solid #d2d2d2;
|
||||
}
|
||||
|
||||
.cal-day:active {
|
||||
background-color: #d2d2d2;
|
||||
}
|
||||
|
|
@ -0,0 +1,265 @@
|
|||
<?xml version="1.0"?>
|
||||
|
||||
<bindings id="xulCalendar"
|
||||
xmlns="http://www.mozilla.org/xbl"
|
||||
xmlns:html="http://www.w3.org/TR/REC-html40"
|
||||
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
|
||||
|
||||
<binding id="calendar" extends="xul:box">
|
||||
|
||||
<!-- ::::::::::::::::: CONTENT ::::::::::::::::::::::::: -->
|
||||
<content>
|
||||
|
||||
<xul:vbox class="cal-mainbox" >
|
||||
<xul:hbox class="cal-month-box" flex="1">
|
||||
<xul:image class="cal-navbtn" dir="-1"/>
|
||||
<xul:spacer flex="1"/>
|
||||
<xul:text id="month-name"
|
||||
class="cal-month-name" value="Month"/>
|
||||
<xul:spacer flex="1"/>
|
||||
<xul:image class="cal-navbtn" dir="1"/>
|
||||
</xul:hbox>
|
||||
<xul:vbox class="cal-cal-box" flex="1">
|
||||
<xul:hbox class="cal-row-head" flex="1">
|
||||
<xul:text class="cal-header" value="S"/>
|
||||
<xul:text class="cal-header" value="M"/>
|
||||
<xul:text class="cal-header" value="T"/>
|
||||
<xul:text class="cal-header" value="W"/>
|
||||
<xul:text class="cal-header" value="T"/>
|
||||
<xul:text class="cal-header" value="F"/>
|
||||
<xul:text class="cal-header" value="S"/>
|
||||
</xul:hbox>
|
||||
<xul:hbox class="cal-row-body" flex="1">
|
||||
<xul:text class="cal-day" />
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
</xul:hbox>
|
||||
<xul:hbox class="cal-row-body" flex="1">
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
</xul:hbox>
|
||||
<xul:hbox class="cal-row-body" flex="1">
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
</xul:hbox>
|
||||
<xul:hbox class="cal-row-body" flex="1">
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
</xul:hbox>
|
||||
<xul:hbox class="cal-row-body" flex="1">
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
</xul:hbox>
|
||||
<xul:hbox class="cal-row-body" flex="1">
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
<xul:text class="cal-day"/>
|
||||
</xul:hbox>
|
||||
</xul:vbox>
|
||||
</xul:vbox>
|
||||
</content>
|
||||
|
||||
<!-- ::::::::::::::::: INTERFACE ::::::::::::::::::::::::: -->
|
||||
<implementation>
|
||||
|
||||
<property name="value"
|
||||
onget="return this.mValue"
|
||||
onset="this.update(val)"/>
|
||||
|
||||
<property name="onchange"/>
|
||||
|
||||
<constructor>
|
||||
<![CDATA[
|
||||
if (!this.mInitialized) {
|
||||
this.mInitialized = true;
|
||||
|
||||
// save references for convenience
|
||||
var monthbox = document.getAnonymousNodes(this)[0].childNodes[0];
|
||||
this.mLeftBtn = monthbox.childNodes[0];
|
||||
this.mLeftBtn.mCalendar = this;
|
||||
this.mRightBtn = monthbox.childNodes[4];
|
||||
this.mRightBtn.mCalendar = this;
|
||||
this.mMonthCell = monthbox.childNodes[2];
|
||||
|
||||
var val = this.parentNode.parentNode.parentNode.parentNode.value;
|
||||
if (val) this.mValue = new Date(val);
|
||||
else this.mValue = new Date();
|
||||
var change = this.getAttribute("onchange");
|
||||
if (change) this.onchange = new Function(change);
|
||||
|
||||
this.showMonth(this.mValue);
|
||||
}
|
||||
]]>
|
||||
</constructor>
|
||||
|
||||
<method name="showMonth">
|
||||
<parameter name="aDate"/>
|
||||
<body><![CDATA[
|
||||
if (!aDate) {
|
||||
aDate = new Date();
|
||||
} else {
|
||||
aDate = new Date(aDate.toString());
|
||||
aDate.setDate(1);
|
||||
}
|
||||
this.mEditorDate = aDate;
|
||||
|
||||
if (this.mSelected) {
|
||||
this.mSelected.setAttribute("selected", "");
|
||||
this.mSelected = null;
|
||||
}
|
||||
|
||||
//-------- Update the month title
|
||||
var mon = this.getMonthName(aDate) + " " + aDate.getFullYear();
|
||||
|
||||
this.mMonthCell.setAttribute("value", mon);
|
||||
|
||||
//-------- Update the calendar
|
||||
var calbox = document.getAnonymousNodes(this)[0].childNodes[1];
|
||||
|
||||
var daytime = 86400000; // milliseconds in a day
|
||||
var firstWeekday = aDate.getDay();
|
||||
var date = new Date((aDate.getTime() - (firstWeekday*daytime)));
|
||||
|
||||
for (var k = 1; k < calbox.childNodes.length; k++) {
|
||||
var row = calbox.childNodes[k];
|
||||
|
||||
|
||||
for (var i = 0; i < 7; i++) {
|
||||
var day = row.childNodes[i];
|
||||
|
||||
if (aDate.getMonth() != date.getMonth()) {
|
||||
day.setAttribute("othermonth", "true");
|
||||
} else {
|
||||
day.setAttribute("othermonth", "");
|
||||
}
|
||||
|
||||
// highlight the current date
|
||||
var val = this.value;
|
||||
if (val) {
|
||||
if ((val.getYear() == date.getYear()) &&
|
||||
(val.getMonth() == date.getMonth()) &&
|
||||
(val.getDate() == date.getDate()))
|
||||
{
|
||||
this.mSelected = day;
|
||||
day.setAttribute("selected", "true");
|
||||
}
|
||||
}
|
||||
day.date = date;
|
||||
day.calendar = this;
|
||||
day.setAttribute("value", date.getDate());
|
||||
date = new Date(date.getTime()+daytime);
|
||||
}
|
||||
}
|
||||
]]></body>
|
||||
</method>
|
||||
|
||||
<method name="advanceMonth">
|
||||
<parameter name="aDir"/>
|
||||
<body><![CDATA[
|
||||
|
||||
var ad = this.mEditorDate.getMonth()+aDir;
|
||||
this.mEditorDate.setMonth(ad);
|
||||
|
||||
this.showMonth(this.mEditorDate);
|
||||
|
||||
]]></body>
|
||||
</method>
|
||||
|
||||
<method name="dayClicked">
|
||||
<parameter name="aDay"/>
|
||||
<body><![CDATA[
|
||||
|
||||
if (this.mSelected) {
|
||||
this.mSelected.setAttribute("selected", "");
|
||||
}
|
||||
|
||||
this.mSelected = aDay;
|
||||
aDay.setAttribute("selected", "true");
|
||||
|
||||
this.value = aDay.date;
|
||||
|
||||
if (this.onchange) this.onchange();
|
||||
|
||||
]]></body>
|
||||
</method>
|
||||
|
||||
<method name="update">
|
||||
<parameter name="aValue"/>
|
||||
<body><![CDATA[
|
||||
|
||||
this.mValue = aValue;
|
||||
this.showMonth(aValue);
|
||||
|
||||
]]></body>
|
||||
</method>
|
||||
|
||||
<method name="getMonthName">
|
||||
<parameter name="aDate"/>
|
||||
<body><![CDATA[
|
||||
|
||||
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
||||
|
||||
var midx = aDate.getMonth();
|
||||
|
||||
return monthNames[midx];
|
||||
|
||||
]]></body>
|
||||
</method>
|
||||
|
||||
</implementation>
|
||||
|
||||
<!-- ::::::::::::::::: HANDLERS ::::::::::::::::::::::::: -->
|
||||
|
||||
<handlers>
|
||||
<handler event="bindingattached" action="this.initialize();"/>
|
||||
</handlers>
|
||||
|
||||
</binding>
|
||||
|
||||
<binding id="cal-navbtn" inherits="dir">
|
||||
<handlers>
|
||||
<handler event="click"><![CDATA[
|
||||
this.mCalendar.advanceMonth(parseInt(this.getAttribute('dir')));
|
||||
]]></handler>
|
||||
</handlers>
|
||||
|
||||
<implementation/>
|
||||
</binding>
|
||||
|
||||
<binding id="cal-day" extends="xul:text">
|
||||
<handlers>
|
||||
<handler event="click" action="this.calendar.dayClicked(this)"/>
|
||||
</handlers>
|
||||
|
||||
<implementation/>
|
||||
</binding>
|
||||
|
||||
</bindings>
|
|
@ -0,0 +1,25 @@
|
|||
|
||||
datepicker {
|
||||
-moz-binding: url("datepicker.xml#datepicker");
|
||||
max-height: 200px;
|
||||
}
|
||||
|
||||
menu.datepicker-button {
|
||||
background-image : url( "chrome://calendar/skin/ca-event-dialog/calendar_up.gif" );
|
||||
background-repeat : no-repeat;
|
||||
}
|
||||
|
||||
.datepicker-button:hover {
|
||||
//background-image : url( "chrome://calendar/skin/ca-event-dialog/calendar_hover.gif" );
|
||||
//background-repeat : no-repeat;
|
||||
}
|
||||
|
||||
.datepicker-button[open="true"] {
|
||||
//background-image : url( "chrome://calendar/skin/ca-event-dialog/calendar_down.gif" );
|
||||
//background-repeat : no-repeat;
|
||||
}
|
||||
|
||||
.menu-right
|
||||
{
|
||||
visibility : hidden;
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
<?xml version="1.0"?>
|
||||
|
||||
<bindings id="xulDatePicker"
|
||||
xmlns="http://www.mozilla.org/xbl"
|
||||
xmlns:html="http://www.w3.org/TR/REC-html40"
|
||||
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
|
||||
|
||||
<binding id="datepicker" extends="xul:box" inherits="value,disabled">
|
||||
<!-- ::::::::::::::::: CONTENT ::::::::::::::::::::::::: -->
|
||||
<content id="content">
|
||||
<xul:hbox flex="1" id="hbox">
|
||||
<xul:textbox id="textbox" xbl:inherits="disabled"
|
||||
onkeypress="if (event.keyCode == 13) this.parentNode.parentNode.update(new Date(this.value));"
|
||||
onblur="this.parentNode.parentNode.parentNode.parentNode.update(new Date(this.value));"/>
|
||||
|
||||
<xul:menu class="datepicker-button">
|
||||
<xul:menupopup popupanchor="bottomright" popupalign="topright" oncreate="this.parentNode.parentNode.parentNode.parentNode.openPopup()">
|
||||
<xul:calendar onchange="this.parentNode.hidePopup(); this.parentNode.parentNode.parentNode.parentNode.update(new Date(this.value)); "/>
|
||||
</xul:menupopup>
|
||||
</xul:menu>
|
||||
|
||||
</xul:hbox>
|
||||
</content>
|
||||
|
||||
<!-- ::::::::::::::::: INTERFACE ::::::::::::::::::::::::: -->
|
||||
<implementation>
|
||||
<property name="value"
|
||||
onset="this.update(val)"
|
||||
onget="return this.mValue"/>
|
||||
|
||||
<property name="callback"
|
||||
onset="this.callbackFunction = val;"
|
||||
/>
|
||||
|
||||
<constructor>
|
||||
<![CDATA[
|
||||
this.mTextBox = document.getAnonymousNodes(this)[0].childNodes[0];
|
||||
this.mPopup = document.getAnonymousNodes(this)[0].childNodes[1].childNodes[0];
|
||||
|
||||
var val = this.getAttribute("value");
|
||||
|
||||
if (val) this.value = val;
|
||||
else this.value = new Date();
|
||||
|
||||
]]>
|
||||
</constructor>
|
||||
|
||||
<method name="update">
|
||||
<parameter name="aValue"/>
|
||||
<body><![CDATA[
|
||||
if (!aValue || aValue.toString() == "Invalid Date") {
|
||||
this.mTextBox.setAttribute("value", this.getPrettyDate(this.mValue));
|
||||
} else {
|
||||
this.mValue = aValue;
|
||||
this.mTextBox.setAttribute("value", this.getPrettyDate(this.mValue));
|
||||
}
|
||||
if( this.callback )
|
||||
this.callbackFunction();
|
||||
]]></body>
|
||||
</method>
|
||||
|
||||
<method name="getPrettyDate">
|
||||
<parameter name="aValue"/>
|
||||
<body><![CDATA[
|
||||
return (aValue.getMonth()+1) + "/" + aValue.getDate() + "/" + aValue.getFullYear();
|
||||
]]></body>
|
||||
</method>
|
||||
|
||||
<method name="openPopup">
|
||||
<body><![CDATA[
|
||||
var cal = this.mPopup.childNodes[0];
|
||||
cal.update( this.mValue );
|
||||
]]></body>
|
||||
</method>
|
||||
|
||||
</implementation>
|
||||
|
||||
<!-- ::::::::::::::::: HANDLERS ::::::::::::::::::::::::: -->
|
||||
|
||||
<handlers>
|
||||
<handler event="bindingattached" action="this.initialize();"/>
|
||||
</handlers>
|
||||
|
||||
</binding>
|
||||
|
||||
</bindings>
|
||||
|
|
@ -0,0 +1,260 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2;
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is Collabnet code.
|
||||
The Initial Developer of the Original Code is Collabnet.
|
||||
|
||||
Portions created by Collabnet are Copyright (C) 2000 Collabnet.
|
||||
All Rights Reserved.
|
||||
|
||||
Contributor(s): Pete Collins, Doug Turner, Brendan Eich, Warren Harris,
|
||||
Eric Plaster, Martin Kutschker
|
||||
|
||||
|
||||
JS Directory Class API
|
||||
|
||||
dir.js
|
||||
|
||||
Function List
|
||||
|
||||
create(aPermissions); // permissions are optional
|
||||
|
||||
files(); // returns an array listing all files of a dirs contents
|
||||
dirs(); // returns an array listing all dirs of a dirs contents
|
||||
list(aDirPath); // returns an array listing of a dirs contents
|
||||
|
||||
// help!
|
||||
help(); // currently dumps a list of available functions
|
||||
|
||||
Instructions:
|
||||
|
||||
|
||||
*/
|
||||
|
||||
if(typeof(JS_LIB_LOADED)=='boolean')
|
||||
{
|
||||
|
||||
/************* INCLUDE FILESYSTEM *****************/
|
||||
if(typeof(JS_FILESYSTEM_LOADED)!='boolean')
|
||||
include(JS_LIB_PATH+'io/filesystem.js');
|
||||
/************* INCLUDE FILESYSTEM *****************/
|
||||
|
||||
|
||||
/****************** Globals **********************/
|
||||
|
||||
const JS_DIR_FILE = "dir.js";
|
||||
const JS_DIR_LOADED = true;
|
||||
|
||||
const JS_DIR_LOCAL_CID = "@mozilla.org/file/local;1";
|
||||
const JS_DIR_LOCATOR_PROGID = '@mozilla.org/filelocator;1';
|
||||
const JS_DIR_CID = "@mozilla.org/file/directory_service;1";
|
||||
|
||||
const JS_DIR_I_LOCAL_FILE = "nsILocalFile";
|
||||
const JS_DIR_INIT_W_PATH = "initWithPath";
|
||||
|
||||
const JS_DIR_PREFS_DIR = 65539;
|
||||
|
||||
const JS_DIR_DIRECTORY = 0x01; // 1
|
||||
const JS_DIR_OK = true;
|
||||
|
||||
const JS_DIR_DEFAULT_PERMS = 0755;
|
||||
|
||||
const JS_DIR_FilePath = new Components.Constructor( JS_DIR_LOCAL_CID, JS_DIR_I_LOCAL_FILE, JS_DIR_INIT_W_PATH);
|
||||
|
||||
|
||||
/****************** Dir Object Class *********************/
|
||||
function Dir(aPath) {
|
||||
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null,
|
||||
"Please enter a local file path to initialize",
|
||||
"NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_DIR_FILE);
|
||||
throw Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS;
|
||||
}
|
||||
|
||||
return this.initPath(arguments);
|
||||
|
||||
} // constructor
|
||||
|
||||
Dir.prototype = new FileSystem;
|
||||
|
||||
Dir.prototype.fileInst = null;
|
||||
|
||||
/********************* CREATE ****************************/
|
||||
Dir.prototype.create = function(aPermissions)
|
||||
{
|
||||
if(!this.mPath)
|
||||
{
|
||||
jslibError(null, "create (no file path defined)", "NS_ERROR_NOT_INITIALIZED");
|
||||
return Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
if(this.exists())
|
||||
{
|
||||
jslibError(null, "(Dir already exists", "NS_ERROR_FAILURE", JS_DIR_FILE+":create");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(aPermissions){
|
||||
var checkPerms = this.validatePermissions(aPermissions);
|
||||
|
||||
if(!checkPerms)
|
||||
{
|
||||
jslibError(null, "create (invalid permissions)", "NS_ERROR_INVALID_ARG", JS_DIR_FILE+":create");
|
||||
return Components.results.NS_ERROR_INVALID_ARG;
|
||||
}
|
||||
|
||||
//var baseTen = permissions.toString(10);
|
||||
|
||||
//if(baseTen.substring(0,1) != 0)
|
||||
//aPermissions = 0+baseTen;
|
||||
}
|
||||
else
|
||||
{
|
||||
checkPerms = JS_DIR_DEFAULT_PERMS;
|
||||
}
|
||||
|
||||
var rv = null;
|
||||
|
||||
try
|
||||
{
|
||||
rv=this.mFileInst.create(JS_DIR_DIRECTORY, parseInt(aPermissions) );
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
jslibError(e, "(unable to create)", "NS_ERROR_FAILURE", JS_DIR_FILE+":create");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
};
|
||||
|
||||
/********************* READDIR **************************/
|
||||
Dir.prototype.readDir = function ()
|
||||
{
|
||||
|
||||
if(!this.exists())
|
||||
{
|
||||
jslibError(null, "(Dir already exists", "NS_ERROR_FAILURE", JS_DIR_FILE+":readDir");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv=null;
|
||||
|
||||
try
|
||||
{
|
||||
if(!this.isDir())
|
||||
{
|
||||
jslibError(null, "(file is not a directory)", "NS_ERROR_FAILURE", JS_DIR_FILE+":readDir");
|
||||
return null;
|
||||
}
|
||||
|
||||
var files = this.mFileInst.directoryEntries;
|
||||
var listings = new Array();
|
||||
var file;
|
||||
|
||||
if(typeof(JS_FILE_LOADED)!='boolean')
|
||||
include(JS_LIB_PATH+'io/file.js');
|
||||
|
||||
while(files.hasMoreElements())
|
||||
{
|
||||
file = files.getNext().QueryInterface(Components.interfaces.nsILocalFile);
|
||||
if(file.isFile())
|
||||
listings.push(new File(file.path));
|
||||
|
||||
if(file.isDirectory())
|
||||
listings.push(new Dir(file.path));
|
||||
}
|
||||
|
||||
rv=listings;
|
||||
}
|
||||
|
||||
catch(e)
|
||||
{
|
||||
jslibError(e, "(unexpected error)", "NS_ERROR_UNEXPECTED", JS_FILE_FILE+":readDir");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
};
|
||||
|
||||
/********************* REMOVE *******************************/
|
||||
Dir.prototype.remove = function (aRecursive)
|
||||
{
|
||||
|
||||
if(typeof(aRecursive)!='boolean')
|
||||
aRecursive=false;
|
||||
|
||||
if(!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if(!this.mPath)
|
||||
{
|
||||
jslibError(null, "remove (no path defined)", "NS_ERROR_INVALID_ARG", JS_DIR_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv=null
|
||||
|
||||
try
|
||||
{
|
||||
if(!this.exists())
|
||||
{
|
||||
jslibError(null, "(directory doesn't exist)", "NS_ERROR_FAILURE", JS_DIR_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!this.isDir())
|
||||
{
|
||||
jslibError(null, "(file is not a directory)", "NS_ERROR_FAILURE", JS_DIR_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
|
||||
rv=this.mFileInst.remove(aRecursive);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
jslibError(e, "(dir not empty, use 'remove(true)' for recursion)", "NS_ERROR_UNEXPECTED",
|
||||
JS_DIR_FILE+":remove");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
};
|
||||
|
||||
/********************* HELP *****************************/
|
||||
Dir.prototype.super_help = FileSystem.prototype.help;
|
||||
|
||||
Dir.prototype.__defineGetter__('help',
|
||||
function()
|
||||
{
|
||||
var help = this.super_help() +
|
||||
|
||||
" create(aPermissions);\n" +
|
||||
" remove(aRecursive);\n" +
|
||||
" readDir(aDirPath);\n";
|
||||
|
||||
return help;
|
||||
});
|
||||
|
||||
jslib_debug('*** load: '+JS_DIR_FILE+' OK');
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
dump("JSLIB library not loaded:\n" +
|
||||
" \tTo load use: chrome://jslib/content/jslib.js\n" +
|
||||
" \tThen: include('chrome://jslib/content/io/dir.js');\n\n");
|
||||
}
|
||||
|
|
@ -0,0 +1,207 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2;
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is Collabnet code.
|
||||
The Initial Developer of the Original Code is Collabnet.
|
||||
|
||||
Portions created by Collabnet are Copyright (C) 2000 Collabnet.
|
||||
All Rights Reserved.
|
||||
|
||||
Contributor(s): Pete Collins, Doug Turner, Brendan Eich, Warren Harris,
|
||||
Eric Plaster, Martin Kutschker
|
||||
|
||||
|
||||
JS Directory Class API
|
||||
|
||||
dirUtils.js
|
||||
|
||||
Function List
|
||||
|
||||
|
||||
Instructions:
|
||||
|
||||
|
||||
*/
|
||||
|
||||
if(typeof(JS_LIB_LOADED)=='boolean')
|
||||
{
|
||||
|
||||
const JS_DIRUTILS_FILE = "dirUtils.js";
|
||||
const JS_DIRUTILS_LOADED = true;
|
||||
|
||||
const JS_DIRUTILS_FILE_LOCAL_CID = "@mozilla.org/file/local;1";
|
||||
const JS_DIRUTILS_FILE_DIR_CID = "@mozilla.org/file/directory_service;1";
|
||||
|
||||
const JS_DIRUTILS_FILE_I_LOCAL_FILE = "nsILocalFile";
|
||||
const JS_DIRUTILS_INIT_W_PATH = "initWithPath";
|
||||
const JS_DIRUTILS_I_PROPS = "nsIProperties";
|
||||
|
||||
const JS_DIRUTILS_NSIFILE = Components.interfaces.nsIFile;
|
||||
|
||||
const NS_APP_PREFS_50_DIR = "PrefD"; // /root/.mozilla/Default User/k1m30xaf.slt
|
||||
const NS_APP_CHROME_DIR = "AChrom"; // /usr/src/mozilla/dist/bin/chrome
|
||||
const NS_APP_USER_PROFILES_ROOT_DIR = "DefProfRt"; // /root/.mozilla
|
||||
const NS_APP_USER_PROFILE_50_DIR = "ProfD"; // /root/.mozilla/Default User/k1m30xaf.slt
|
||||
const NS_APP_APPLICATION_REGISTRY_DIR = "AppRegD"; // /root/.mozilla
|
||||
const NS_APP_APPLICATION_REGISTRY_FILE = "AppRegF"; // /root/.mozilla/appreg
|
||||
const NS_APP_DEFAULTS_50_DIR = "DefRt"; // /usr/src/mozilla/dist/bin/defaults
|
||||
const NS_APP_PREF_DEFAULTS_50_DIR = "PrfDef"; // /usr/src/mozilla/dist/bin/defaults/pref
|
||||
const NS_APP_PROFILE_DEFAULTS_50_DIR = "profDef"; // /usr/src/mozilla/dist/bin/defaults/profile/US
|
||||
const NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR = "ProfDefNoLoc";// /usr/src/mozilla/dist/bin/defaults/profile
|
||||
const NS_APP_RES_DIR = "ARes"; // /usr/src/mozilla/dist/bin/res
|
||||
const NS_APP_PLUGINS_DIR = "APlugns"; // /usr/src/mozilla/dist/bin/plugins
|
||||
const NS_APP_SEARCH_DIR = "SrchPlugns"; // /usr/src/mozilla/dist/bin/searchplugins
|
||||
const NS_APP_PREFS_50_FILE = "PrefF"; // /root/.mozilla/Default User/k1m30xaf.slt/prefs.js
|
||||
const NS_APP_USER_CHROME_DIR = "UChrm"; // /root/.mozilla/Default User/k1m30xaf.slt/chrome
|
||||
const NS_APP_LOCALSTORE_50_FILE = "LclSt"; // /root/.mozilla/Default User/k1m30xaf.slt/localstore.rdf
|
||||
const NS_APP_HISTORY_50_FILE = "UHist"; // /root/.mozilla/Default User/k1m30xaf.slt/history.dat
|
||||
const NS_APP_USER_PANELS_50_FILE = "UPnls"; // /root/.mozilla/Default User/k1m30xaf.slt/panels.rdf
|
||||
const NS_APP_USER_MIMETYPES_50_FILE = "UMimTyp"; // /root/.mozilla/Default User/k1m30xaf.slt/mimeTypes.rdf
|
||||
const NS_APP_BOOKMARKS_50_FILE = "BMarks"; // /root/.mozilla/Default User/k1m30xaf.slt/bookmarks.html
|
||||
const NS_APP_SEARCH_50_FILE = "SrchF"; // /root/.mozilla/Default User/k1m30xaf.slt/search.rdf
|
||||
const NS_APP_MAIL_50_DIR = "MailD"; // /root/.mozilla/Default User/k1m30xaf.slt/Mail
|
||||
const NS_APP_IMAP_MAIL_50_DIR = "IMapMD"; // /root/.mozilla/Default User/k1m30xaf.slt/ImapMail
|
||||
const NS_APP_NEWS_50_DIR = "NewsD"; // /root/.mozilla/Default User/k1m30xaf.slt/News
|
||||
const NS_APP_MESSENGER_FOLDER_CACHE_50_DIR = "MFCaD"; // /root/.mozilla/Default User/k1m30xaf.slt/panacea.dat
|
||||
|
||||
const JS_DIRUTILS_FilePath = new Components.Constructor(
|
||||
JS_DIRUTILS_FILE_LOCAL_CID,
|
||||
JS_DIRUTILS_FILE_I_LOCAL_FILE,
|
||||
JS_DIRUTILS_INIT_W_PATH);
|
||||
|
||||
const JS_DIRUTILS_DIR = new Components.Constructor(
|
||||
JS_DIRUTILS_FILE_DIR_CID,
|
||||
JS_DIRUTILS_I_PROPS);
|
||||
|
||||
// constructor
|
||||
function DirUtils(){}
|
||||
|
||||
DirUtils.prototype =
|
||||
{
|
||||
|
||||
getPath : function (aAppID) {
|
||||
|
||||
if(!aAppID)
|
||||
{
|
||||
jslibError(null, "(no arg defined)", "NS_ERROR_INVALID_ARG", JS_FILE_FILE+":getPath");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
|
||||
try { rv=(new JS_DIRUTILS_DIR()).get(aAppID, JS_DIRUTILS_NSIFILE).path; }
|
||||
|
||||
catch (e)
|
||||
{
|
||||
jslibError(e, "(unexpected error)", "NS_ERROR_FAILURE", JS_DIRUTILS_FILE+":getPath");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
|
||||
},
|
||||
|
||||
getPrefsDir : function () { return this.getPath(NS_APP_PREFS_50_DIR); },
|
||||
|
||||
getChromeDir : function () { return this.getPath(NS_APP_CHROME_DIR); },
|
||||
|
||||
getMozHomeDir : function () { return this.getPath(NS_APP_USER_PROFILES_ROOT_DIR); },
|
||||
|
||||
getMozUserHomeDir : function () { return this.getPath(NS_APP_USER_PROFILE_50_DIR); },
|
||||
|
||||
getAppRegDir : function () { return this.getPath(NS_APP_APPLICATION_REGISTRY_FILE); },
|
||||
|
||||
getAppDefaultDir : function () { return this.getPath(NS_APP_DEFAULTS_50_DIR); },
|
||||
|
||||
getAppDefaultPrefDir : function () { return this.getPath(NS_APP_PREF_DEFAULTS_50_DIR); },
|
||||
|
||||
getProfileDefaultsLocDir : function () { return this.getPath(NS_APP_PROFILE_DEFAULTS_50_DIR); },
|
||||
|
||||
getProfileDefaultsDir : function () { return this.getPath(NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR); },
|
||||
|
||||
getAppResDir : function () { return this.getPath(NS_APP_RES_DIR); },
|
||||
|
||||
getAppPluginsDir : function () { return this.getPath(NS_APP_PLUGINS_DIR); },
|
||||
|
||||
getSearchPluginsDir : function () { return this.getPath(NS_APP_SEARCH_DIR); },
|
||||
|
||||
getPrefsFile : function () { return this.getPath(NS_APP_PREFS_50_FILE); },
|
||||
|
||||
getUserChromeDir : function () { return this.getPath(NS_APP_USER_CHROME_DIR); },
|
||||
|
||||
getLocalStore : function () { return this.getPath(NS_APP_LOCALSTORE_50_FILE); },
|
||||
|
||||
getHistoryFile : function () { return this.getPath(NS_APP_HISTORY_50_FILE); },
|
||||
|
||||
getPanelsFile : function () { return this.getPath(NS_APP_USER_PANELS_50_FILE); },
|
||||
|
||||
getMimeTypes : function () { return this.getPath(NS_APP_USER_MIMETYPES_50_FILE); },
|
||||
|
||||
getBookmarks : function () { return this.getPath(NS_APP_BOOKMARKS_50_FILE); },
|
||||
|
||||
getSearchFile : function () { return this.getPath(NS_APP_SEARCH_50_FILE); },
|
||||
|
||||
getUserMailDir : function () { return this.getPath(NS_APP_MAIL_50_DIR); },
|
||||
|
||||
getUserImapDir : function () { return this.getPath(NS_APP_IMAP_MAIL_50_DIR); },
|
||||
|
||||
getUserNewsDir : function () { return this.getPath(NS_APP_NEWS_50_DIR); },
|
||||
|
||||
getMessengerFolderCache : function () { return this.getPath(NS_APP_MESSENGER_FOLDER_CACHE_50_DIR); },
|
||||
|
||||
get help(){
|
||||
|
||||
const help =
|
||||
|
||||
"\n\nFunction and Attribute List:\n" +
|
||||
"\n" +
|
||||
" getPrefsDir()\n" +
|
||||
" getChromeDir()\n" +
|
||||
" getMozHomeDir()\n" +
|
||||
" getMozUserHomeDir()\n" +
|
||||
" getAppRegDir()\n" +
|
||||
" getAppDefaultDir()\n" +
|
||||
" getAppDefaultPrefDir()\n" +
|
||||
" getProfileDefaultsLocDir()\n" +
|
||||
" getProfileDefaultsDir()\n" +
|
||||
" getAppResDir()\n" +
|
||||
" getAppPluginsDir()\n" +
|
||||
" getSearchPluginsDir()\n" +
|
||||
" getPrefsFile()\n" +
|
||||
" getUserChromeDir()\n" +
|
||||
" getLocalStore()\n" +
|
||||
" getHistoryFile()\n" +
|
||||
" getPanelsFile()\n" +
|
||||
" getMimeTypes()\n" +
|
||||
" getBookmarks()\n" +
|
||||
" getSearchFile()\n" +
|
||||
" getUserMailDir()\n" +
|
||||
" getUserImapDir()\n" +
|
||||
" getUserNewsDir()\n" +
|
||||
" getMessengerFolderCache()\n\n";
|
||||
|
||||
return help;
|
||||
|
||||
}
|
||||
}; //END CLASS
|
||||
|
||||
jslib_debug('*** load: '+JS_DIRUTILS_FILE+' OK');
|
||||
|
||||
} // END BLOCK JS_LIB_LOADED CHECK
|
||||
|
||||
else
|
||||
{
|
||||
dump("JSLIB library not loaded:\n" +
|
||||
" \tTo load use: chrome://jslib/content/jslib.js\n" +
|
||||
" \tThen: include('chrome://jslib/content/io/dirUtils.js');\n\n");
|
||||
}
|
||||
|
|
@ -0,0 +1,922 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2;
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is Collabnet code.
|
||||
The Initial Developer of the Original Code is Collabnet.
|
||||
|
||||
Portions created by Collabnet are
|
||||
Copyright (C) 2000 Collabnet. All
|
||||
Rights Reserved.
|
||||
|
||||
Contributor(s): Pete Collins,
|
||||
Doug Turner,
|
||||
Brendan Eich,
|
||||
Warren Harris,
|
||||
Eric Plaster,
|
||||
Martin Kutschker
|
||||
|
||||
The purpose of this file is to make it a little easier to use
|
||||
xpcom nsIFile file IO library from js
|
||||
|
||||
File API
|
||||
file.js
|
||||
|
||||
Base Class:
|
||||
FileSystem
|
||||
filesystem.js
|
||||
|
||||
Function List:
|
||||
// Constructor
|
||||
File(aPath) creates the File object and sets the file path
|
||||
|
||||
// file stream methods
|
||||
open(aMode, aPermissions); open a file handle for reading,
|
||||
writing or appending. permissions are optional.
|
||||
read(); returns the contents of a file
|
||||
readline(); returns the next line in the file.
|
||||
EOF; boolean check 'end of file' status
|
||||
write(aContents); writes the contents out to file.
|
||||
copy(aDest); copy the current file to a aDest
|
||||
close(); closes a file handle
|
||||
create(); creates a new file if one doesn't already exist
|
||||
exists(); check to see if a file exists
|
||||
|
||||
// file attributes
|
||||
size; read only attribute gets the file size
|
||||
ext; read only attribute gets a file extension if there is one
|
||||
permissions; attribute gets or sets the files permissions
|
||||
dateModified; read only attribute gets last modified date in locale string
|
||||
|
||||
// file path attributes
|
||||
leaf; read only attribute gets the file leaf
|
||||
path; read only attribute gets the path
|
||||
parent; read only attribute gets parent dir part of a path
|
||||
|
||||
// direct manipulation
|
||||
nsIFIle returns an nsIFile obj
|
||||
|
||||
// utils
|
||||
remove(); removes the current file
|
||||
append(aLeaf); appends a leaf name to the current file
|
||||
appendRelativePath(aRelPath); appends a relitave path the the current file
|
||||
|
||||
// help!
|
||||
help; currently dumps a list of available functions
|
||||
|
||||
Instructions:
|
||||
|
||||
First include this js file in your xul file.
|
||||
Next, create an File object:
|
||||
|
||||
var file = new File("/path/file.ext");
|
||||
|
||||
To see if the file exists, call the exists() member.
|
||||
This is a good check before going into some
|
||||
deep code to try and extract information from a non-existant file.
|
||||
|
||||
To open a file for reading<"r">, writing<"w"> or appending<"a">,
|
||||
just call:
|
||||
|
||||
file.open("w", 0644);
|
||||
|
||||
where in this case you will be creating a new file called '/path/file.ext',
|
||||
with a mode of "w" which means you want to write a new file.
|
||||
|
||||
If you want to read from a file, just call:
|
||||
|
||||
file.open(); or
|
||||
file.open("r");
|
||||
var theFilesContents = file.read();
|
||||
|
||||
---- or ----
|
||||
|
||||
while(!file.EOF) {
|
||||
var theFileContentsLine = file.readline();
|
||||
dump("line: "+theFileContentsLine+"\n");
|
||||
}
|
||||
|
||||
The file contents will be returned to the caller so you can do something usefull with it.
|
||||
|
||||
file.close();
|
||||
|
||||
Calling 'close()' destroys any created objects. If you forget to use file.close() no probs
|
||||
all objects are discarded anyway.
|
||||
|
||||
Warning: these API's are not for religious types
|
||||
|
||||
************/
|
||||
|
||||
|
||||
// insure jslib base is loaded
|
||||
if (typeof(JS_LIB_LOADED)=='boolean')
|
||||
{
|
||||
|
||||
// test to make sure filesystem base class is loaded
|
||||
if (typeof(JS_FILESYSTEM_LOADED)!='boolean')
|
||||
include(JS_LIB_PATH+'io/filesystem.js');
|
||||
|
||||
/****************** Globals **********************/
|
||||
const JS_FILE_LOADED = true;
|
||||
const JS_FILE_FILE = "file.js";
|
||||
|
||||
const JS_FILE_F_CHANNEL_CID = "@mozilla.org/network/local-file-channel;1";
|
||||
const JS_FILE_I_STREAM_CID = "@mozilla.org/scriptableinputstream;1";
|
||||
const JS_FILE_F_TRANSPORT_SERVICE_CID = "@mozilla.org/network/file-transport-service;1";
|
||||
|
||||
const JS_FILE_I_FILE_CHANNEL = "nsIFileChannel";
|
||||
const JS_FILE_I_SCRIPTABLE_IN_STREAM = "nsIScriptableInputStream";
|
||||
|
||||
const JS_FILE_READ = 0x01; // 1
|
||||
const JS_FILE_WRITE = 0x08; // 8
|
||||
const JS_FILE_APPEND = 0x10; // 16
|
||||
|
||||
const JS_FILE_READ_MODE = "r";
|
||||
const JS_FILE_WRITE_MODE = "w";
|
||||
const JS_FILE_APPEND_MODE = "a";
|
||||
|
||||
const JS_FILE_FILE_TYPE = 0x00; // 0
|
||||
|
||||
const JS_FILE_CHUNK = 1024; // buffer for readline => set to 1k
|
||||
|
||||
const JS_FILE_DEFAULT_PERMS = 0644;
|
||||
|
||||
const JS_FILE_OK = true;
|
||||
|
||||
const JS_FILE_FileChannel = new C.Constructor
|
||||
( JS_FILE_F_CHANNEL_CID, JS_FILE_I_FILE_CHANNEL );
|
||||
|
||||
const JS_FILE_InputStream = new C.Constructor
|
||||
( JS_FILE_I_STREAM_CID, JS_FILE_I_SCRIPTABLE_IN_STREAM );
|
||||
|
||||
// The File Transport Service provides nsITransport objects
|
||||
// which can supply nsIOutputStream objects for writing to files.
|
||||
const JS_FILE_FileTransportService =
|
||||
C.classes[JS_FILE_F_TRANSPORT_SERVICE_CID].
|
||||
getService(C.interfaces.nsIFileTransportService);
|
||||
|
||||
// Possible values for the ioFlags parameter to FileTransportService.createTransport.
|
||||
// From http://lxr.mozilla.org/seamonkey/source/netwerk/base/public/nsIFileChannel.idl
|
||||
|
||||
const JS_FILE_NS_RDONLY = 0x01;
|
||||
const JS_FILE_NS_WRONLY = 0x02;
|
||||
const JS_FILE_NS_RDWR = 0x04;
|
||||
const JS_FILE_NS_CREATE_FILE = 0x08;
|
||||
const JS_FILE_NS_APPEND = 0x10;
|
||||
const JS_FILE_NS_TRUNCATE = 0x20;
|
||||
const JS_FILE_NS_SYNC = 0x40;
|
||||
const JS_FILE_NS_EXCL = 0x80;
|
||||
/****************** Globals **********************/
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* void File(aPath) *
|
||||
* *
|
||||
* class constructor *
|
||||
* aPath is an argument of string local file path *
|
||||
* returns NS_OK on success, exception upon failure *
|
||||
* Ex: *
|
||||
* var p = '/tmp/foo.dat'; *
|
||||
* var f = new File(p); *
|
||||
* *
|
||||
* outputs: void(null) *
|
||||
****************************************************************/
|
||||
|
||||
function File(aPath) {
|
||||
|
||||
if (!aPath) {
|
||||
jslibError(null,
|
||||
"Please enter a local file path to initialize",
|
||||
"NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_FILE_FILE);
|
||||
throw - C.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS;
|
||||
}
|
||||
return this.initPath(arguments);
|
||||
} // constructor
|
||||
|
||||
File.prototype = new FileSystem();
|
||||
|
||||
// member vars
|
||||
File.prototype.mMode = null;
|
||||
File.prototype.mFileChannel = null;
|
||||
File.prototype.mTransport = null;
|
||||
File.prototype.mOutStream = null;
|
||||
File.prototype.mInputStream = null;
|
||||
File.prototype.mLineBuffer = null;
|
||||
File.prototype.mPosition = 0;
|
||||
|
||||
/********************* OPEN *************************************
|
||||
* bool open(aMode, aPerms) *
|
||||
* *
|
||||
* opens a file handle to read, write or append *
|
||||
* aMode is an argument of string 'w', 'a', 'r' *
|
||||
* returns true on success, null on failure *
|
||||
* Ex: *
|
||||
* var p='/tmp/foo.dat'; *
|
||||
* var f=new File(p); *
|
||||
* *
|
||||
* outputs: void(null) *
|
||||
****************************************************************/
|
||||
|
||||
File.prototype.open = function(aMode, aPerms)
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw C.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (!this.mPath) {
|
||||
jslibError(null, "open("+this.mMode+") (no file path defined)",
|
||||
"NS_ERROR_NOT_INITIALIZED",
|
||||
JS_FILE_FILE+":open");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.mMode) {
|
||||
jslibError(null, "open("+this.mMode+") (already open)",
|
||||
"NS_ERROR_NOT_INITIALIZED",
|
||||
JS_FILE_FILE+":open");
|
||||
this.close();
|
||||
return null;
|
||||
}
|
||||
|
||||
this.close();
|
||||
|
||||
if (!aMode)
|
||||
aMode=JS_FILE_READ_MODE;
|
||||
|
||||
if (this.exists() && this.mFileInst.isDirectory()) {
|
||||
jslibError(null, "open("+this.mMode+") (cannot open directory)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":open");
|
||||
return null;
|
||||
}
|
||||
|
||||
this.resetCache();
|
||||
var rv;
|
||||
|
||||
switch(aMode)
|
||||
{
|
||||
case JS_FILE_WRITE_MODE:
|
||||
case JS_FILE_APPEND_MODE: {
|
||||
try {
|
||||
if (!this.mFileChannel)
|
||||
this.mFileChannel=new JS_FILE_FileChannel();
|
||||
} catch(e) {
|
||||
jslibError(e, "open("+this.mMode+") (unable to get nsIFileChannel)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":open");
|
||||
return null;
|
||||
}
|
||||
if (aPerms) {
|
||||
if (!this.validatePermissions(aPerms)) {
|
||||
jslibError(null, "open("+this.mMode+") (invalid permissions)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILE_FILE+":open");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (!aPerms)
|
||||
aPerms=JS_FILE_DEFAULT_PERMS;
|
||||
// removing, i don't think we need this --pete
|
||||
//this.permissions = aPerms;
|
||||
try {
|
||||
var offSet=0;
|
||||
if (aMode == JS_FILE_WRITE_MODE) {
|
||||
this.mMode=JS_FILE_WRITE_MODE;
|
||||
// Create a file transport in write mode
|
||||
if (!this.mTransport) {
|
||||
this.mTransport = JS_FILE_FileTransportService.createTransport(this.mFileInst,
|
||||
JS_FILE_NS_WRONLY | JS_FILE_NS_CREATE_FILE | JS_FILE_NS_TRUNCATE, aPerms, true);
|
||||
}
|
||||
} else {
|
||||
this.mMode=JS_FILE_APPEND_MODE;
|
||||
if (!this.mTransport) {
|
||||
this.mTransport = JS_FILE_FileTransportService.createTransport(
|
||||
this.mFileInst,
|
||||
JS_FILE_NS_APPEND | JS_FILE_NS_RDWR |
|
||||
JS_FILE_NS_SYNC | JS_FILE_NS_CREATE_FILE,
|
||||
aPerms, true);
|
||||
}
|
||||
}
|
||||
} catch(e) {
|
||||
jslibError(e, "open("+this.mMode+") (unable to get transport)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":open");
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
// Use the previously created file transport to open an output
|
||||
// stream for writing to the file
|
||||
if (!this.mOutStream) {
|
||||
this.mOutStream = this.mTransport.openOutputStream(offSet, -1, 0);
|
||||
}
|
||||
} catch(e) {
|
||||
jslibError(e, "open("+this.mMode+") (unable to get outputstream)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":open");
|
||||
this.close();
|
||||
return null;
|
||||
}
|
||||
|
||||
rv = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case JS_FILE_READ_MODE: {
|
||||
if (!this.exists()) {
|
||||
jslibError(null, "open(r) (file doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":open");
|
||||
return null;
|
||||
}
|
||||
this.mMode=JS_FILE_READ_MODE;
|
||||
try {
|
||||
this.mFileChannel = new JS_FILE_FileChannel();
|
||||
this.mInputStream = new JS_FILE_InputStream();
|
||||
this.mLineBuffer = new Array();
|
||||
this.mFileChannel.init(this.mFileInst, JS_FILE_READ, this.permissions);
|
||||
this.mInputStream.init(this.mFileChannel.open());
|
||||
rv=true;
|
||||
} catch (e) {
|
||||
jslibError(e, "open(r) (error setting permissions)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":open");
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
jslibError(null, "open (must supply either w,r, or a)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILE_FILE+":open");
|
||||
return null;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/********************* READ *************************************
|
||||
* string read() *
|
||||
* *
|
||||
* reads a file if the file is binary it will *
|
||||
* return type ex: ELF *
|
||||
* takes no arguments needs an open read mode filehandle *
|
||||
* returns string on success, null on failure *
|
||||
* Ex: *
|
||||
* var p='/tmp/foo.dat'; *
|
||||
* var f=new File(p); *
|
||||
* f.open(p); *
|
||||
* f.read(); *
|
||||
* *
|
||||
* outputs: <string contents of foo.dat> *
|
||||
****************************************************************/
|
||||
|
||||
File.prototype.read = function()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw C.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (this.mMode != JS_FILE_READ_MODE) {
|
||||
jslibError(null, "(mode is write/append)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":read");
|
||||
this.close();
|
||||
return null;
|
||||
}
|
||||
var rv=null;
|
||||
try {
|
||||
if (!this.mFileInst || !this.mInputStream) {
|
||||
jslibError(null, "(no file instance or input stream) ",
|
||||
"NS_ERROR_NOT_INITIALIZED",
|
||||
JS_FILE_FILE+":read");
|
||||
return null;
|
||||
}
|
||||
rv = this.mInputStream.read(this.mFileInst.fileSize);
|
||||
this.mInputStream.close();
|
||||
} catch (e) {
|
||||
jslibError(e, "read (input stream read)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":read");
|
||||
return null;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/********************* READLINE**********************************
|
||||
* string readline() *
|
||||
* *
|
||||
* reads a file if the file is binary it will *
|
||||
* return type string *
|
||||
* takes no arguments needs an open read mode filehandle *
|
||||
* returns string on success, null on failure *
|
||||
* Ex: *
|
||||
* var p='/tmp/foo.dat'; *
|
||||
* var f=new File(p); *
|
||||
* f.open(); *
|
||||
* while(!f.EOF) *
|
||||
* dump("line: "+f.readline()+"\n"); *
|
||||
* *
|
||||
* outputs: <string line of foo.dat> *
|
||||
****************************************************************/
|
||||
|
||||
File.prototype.readline = function()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw C.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (!this.mInputStream) {
|
||||
jslibError(null, "(no input stream)",
|
||||
"NS_ERROR_NOT_INITIALIZED",
|
||||
JS_FILE_FILE+":readline");
|
||||
return null;
|
||||
}
|
||||
var rv = null;
|
||||
var buf = null;
|
||||
var tmp = null;
|
||||
try {
|
||||
if (this.mLineBuffer.length < 2) {
|
||||
buf = this.mInputStream.read(JS_FILE_CHUNK);
|
||||
this.mPosition = this.mPosition + JS_FILE_CHUNK;
|
||||
if (this.mPosition > this.mFileInst.fileSize)
|
||||
this.mPosition = this.mFileInst.fileSize;
|
||||
if (buf) {
|
||||
if (this.mLineBuffer.length == 1) {
|
||||
tmp = this.mLineBuffer.shift();
|
||||
buf = tmp+buf;
|
||||
}
|
||||
this.mLineBuffer = buf.split(/[\n\r]/);
|
||||
}
|
||||
}
|
||||
rv = this.mLineBuffer.shift();
|
||||
} catch (e) {
|
||||
jslibError(e, "(problems reading from file)",
|
||||
"NS_ERROR_NOT_INITIALIZED",
|
||||
JS_FILE_FILE+":readline");
|
||||
rv = null;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/********************* EOF **************************************
|
||||
* bool getter EOF() *
|
||||
* *
|
||||
* boolean check 'end of file' status *
|
||||
* return type boolean *
|
||||
* takes no arguments needs an open read mode filehandle *
|
||||
* returns true on eof, false when not at eof *
|
||||
* Ex: *
|
||||
* var p='/tmp/foo.dat'; *
|
||||
* var f=new File(p); *
|
||||
* f.open(); *
|
||||
* while(!f.EOF) *
|
||||
* dump("line: "+f.readline()+"\n"); *
|
||||
* *
|
||||
* outputs: true or false *
|
||||
****************************************************************/
|
||||
|
||||
File.prototype.__defineGetter__('EOF',
|
||||
function()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw C.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (!this.mInputStream) {
|
||||
jslibError(null, "(no input stream)",
|
||||
"NS_ERROR_NOT_INITIALIZED",
|
||||
JS_FILE_FILE+":EOF");
|
||||
throw C.results.NS_ERROR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
if ((this.mLineBuffer.length > 0) || (this.mInputStream.available() > 0))
|
||||
return false;
|
||||
|
||||
else
|
||||
return true;
|
||||
|
||||
})
|
||||
|
||||
/********************* WRITE ************************************
|
||||
* bool write() *
|
||||
* *
|
||||
* reads a file if the file is binary it will *
|
||||
* return type ex: ELF *
|
||||
* takes no arguments needs an open read mode filehandle *
|
||||
* returns string on success, null on failure *
|
||||
* Ex: *
|
||||
* var p='/tmp/foo.dat'; *
|
||||
* var f=new File(p); *
|
||||
* f.open(p); *
|
||||
* f.read(); *
|
||||
* *
|
||||
* outputs: <string contents of foo.dat> *
|
||||
****************************************************************/
|
||||
|
||||
File.prototype.write = function(aBuffer, aPerms)
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw C.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (this.mMode == JS_FILE_READ_MODE) {
|
||||
jslibError(null, "(in read mode)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":write");
|
||||
this.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.mFileInst) {
|
||||
jslibError(null, "(no file instance)",
|
||||
"NS_ERROR_NOT_INITIALIZED",
|
||||
JS_FILE_FILE+":write");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!aBuffer)
|
||||
throw(JS_FILE_FILE+":write:ERROR: must have a buffer to write!\n");
|
||||
|
||||
var rv=null;
|
||||
|
||||
try {
|
||||
this.mOutStream.write(aBuffer, aBuffer.length);
|
||||
this.mOutStream.flush();
|
||||
//this.mOutStream.close();
|
||||
rv=true;
|
||||
} catch (e) {
|
||||
jslibError(e, "write (nsIOutputStream write/flush)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILE_FILE+":write");
|
||||
rv=false;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/********************* COPY *************************************
|
||||
* void copy(aDest) *
|
||||
* *
|
||||
* void file close *
|
||||
* return type void(null) *
|
||||
* takes no arguments closes an open file stream and *
|
||||
* deletes member var instances of objects *
|
||||
* Ex: *
|
||||
* var p='/tmp/foo.dat'; *
|
||||
* var f=new File(p); *
|
||||
* fopen(); *
|
||||
* f.close(); *
|
||||
* *
|
||||
* outputs: void(null) *
|
||||
****************************************************************/
|
||||
|
||||
File.prototype.copy = function (aDest)
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw C.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (!aDest) {
|
||||
jslibError(null, "(no dest defined)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILE_FILE+":copy");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.exists()) {
|
||||
jslibError(null, "(file doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":copy");
|
||||
return false;
|
||||
}
|
||||
|
||||
var rv;
|
||||
try {
|
||||
var dest = new JS_FS_File_Path(aDest);
|
||||
var copyName = null;
|
||||
var dir = null;
|
||||
|
||||
if (dest.equals(this.mFileInst)) {
|
||||
jslibError(null, "(can't copy file to itself)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":copy");
|
||||
return false;
|
||||
}
|
||||
dest.append(this.leaf);
|
||||
if (dest.exists()) {
|
||||
jslibError(null, "(dest "+dest.path+" already exists)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":copy");
|
||||
return false;
|
||||
}
|
||||
if (this.mFileInst.isDirectory()) {
|
||||
jslibError(null, "(cannot copy directory)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":copy");
|
||||
return false;
|
||||
}
|
||||
if (!dest.exists()) {
|
||||
copyName = dest.leafName;
|
||||
dir = dest.parent;
|
||||
|
||||
if (!dir.exists()) {
|
||||
jslibError(null, "(dest "+dir.path+" doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":copy");
|
||||
return false;
|
||||
}
|
||||
if (!dir.isDirectory()) {
|
||||
jslibError(null, "(dest "+dir.path+" is not a valid path)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":copy");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!dir) {
|
||||
dir=dest;
|
||||
if (dest.equals(this.mFileInst)) {
|
||||
jslibError(null, "(can't copy file to itself)", "NS_ERROR_FAILURE", JS_FILE_FILE+":copy");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
this.mFileInst.copyTo(dir, copyName);
|
||||
jslib_debug(JS_FILE_FILE+":copy successful!");
|
||||
rv = true;
|
||||
} catch (e) {
|
||||
jslibError(e, "(unexpected error)", "NS_ERROR_UNEXPECTED", JS_FILE_FILE+":copy");
|
||||
rv=false;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/********************* CLOSE ************************************
|
||||
* void close() *
|
||||
* *
|
||||
* void file close *
|
||||
* return type void(null) *
|
||||
* takes no arguments closes an open file stream and *
|
||||
* deletes member var instances of objects *
|
||||
* Ex: *
|
||||
* var p='/tmp/foo.dat'; *
|
||||
* var f=new File(p); *
|
||||
* fopen(); *
|
||||
* f.close(); *
|
||||
* *
|
||||
* outputs: void(null) *
|
||||
****************************************************************/
|
||||
|
||||
File.prototype.close = function()
|
||||
{
|
||||
/***************** Destroy Instances *********************/
|
||||
if (this.mFileChannel) delete this.mFileChannel;
|
||||
if (this.mInputStream) delete this.mInputStream;
|
||||
if (this.mTransport) delete this.mTransport;
|
||||
if (this.mMode) this.mMode=null;
|
||||
if (this.mOutStream) {
|
||||
this.mOutStream.close();
|
||||
delete this.mOutStream;
|
||||
}
|
||||
if (this.mLineBuffer) this.mLineBuffer=null;
|
||||
this.mPosition = 0;
|
||||
/***************** Destroy Instances *********************/
|
||||
|
||||
return void(null);
|
||||
}
|
||||
|
||||
/********************* CREATE *****************************/
|
||||
File.prototype.create = function()
|
||||
{
|
||||
|
||||
// We can probably implement this so that it can create a
|
||||
// file or dir if a long non-existent mPath is present
|
||||
|
||||
if (!this.checkInst())
|
||||
throw C.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (this.exists()) {
|
||||
jslibError(null, "(file already exists)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":create");
|
||||
return null
|
||||
}
|
||||
|
||||
if (!this.mFileInst.parent.exists() && this.mFileInst.parent.isDirectory()) {
|
||||
jslibError(null, "(no such file or dir: '"+this.path+"' )",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILE_FILE+":create");
|
||||
return null
|
||||
}
|
||||
|
||||
var rv=null;
|
||||
try {
|
||||
rv = this.mFileInst.create(JS_FILE_FILE_TYPE, JS_FILE_DEFAULT_PERMS);
|
||||
} catch (e) {
|
||||
jslibError(e, "(unexpected)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILE_FILE+":create");
|
||||
rv=null;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/********************* REMOVE *******************************/
|
||||
File.prototype.remove = function ()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw C.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (!this.mPath) {
|
||||
jslibError(null, "(no path defined)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILE_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
this.close();
|
||||
var rv;
|
||||
try {
|
||||
if (!this.exists()) {
|
||||
jslibError(null, "(file doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
|
||||
// I think we should remove whatever is there, sym link, file, dir etc . .
|
||||
if (this.mFileInst.isSpecial()) {
|
||||
jslibError(null, "(file is not a regular file)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!this.isFile()) {
|
||||
jslibError(null, "(not a file)", "NS_ERROR_FAILURE", JS_FILE_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
// this is a recursive remove.
|
||||
this.isFile() ? rv = this.mFileInst.remove(true) : rv = null;
|
||||
} catch (e) {
|
||||
jslibError(e, "(unexpected)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILE_FILE+":remove");
|
||||
rv=null;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/********************* POS **************************************
|
||||
* int getter POS() *
|
||||
* *
|
||||
* int file position *
|
||||
* return type int *
|
||||
* takes no arguments needs an open read mode filehandle *
|
||||
* returns current position, default is 0 set when *
|
||||
* close is called *
|
||||
* Ex: *
|
||||
* var p='/tmp/foo.dat'; *
|
||||
* var f=new File(p); *
|
||||
* f.open(); *
|
||||
* while(!f.EOF){ *
|
||||
* dump("pos: "+f.pos+"\n"); *
|
||||
* dump("line: "+f.readline()+"\n"); *
|
||||
* } *
|
||||
* *
|
||||
* outputs: int pos *
|
||||
****************************************************************/
|
||||
|
||||
File.prototype.__defineGetter__('pos', function(){ return this.mPosition; })
|
||||
|
||||
/********************* SIZE *************************************
|
||||
* int getter size() *
|
||||
* *
|
||||
* int file size *
|
||||
* return type int *
|
||||
* takes no arguments a getter only *
|
||||
* Ex: *
|
||||
* var p='/tmp/foo.dat'; *
|
||||
* var f=new File(p); *
|
||||
* f.size; *
|
||||
* *
|
||||
* outputs: int 16 *
|
||||
****************************************************************/
|
||||
|
||||
File.prototype.__defineGetter__('size',
|
||||
function()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw C.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (!this.mPath) {
|
||||
jslibError(null, "size (no path defined)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILE_FILE);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!this.exists()) {
|
||||
jslibError(null, "size (file doesn't exist)", "NS_ERROR_FAILURE", JS_FILE_FILE);
|
||||
return null;
|
||||
}
|
||||
var rv=null;
|
||||
this.resetCache();
|
||||
try {
|
||||
rv=this.mFileInst.fileSize;
|
||||
} catch(e) {
|
||||
jslibError(e, "(problem getting file instance)", "NS_ERROR_UNEXPECTED", JS_FILE_FILE+":size");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
}) //END size Getter
|
||||
|
||||
/********************* EXTENSION ********************************
|
||||
* string getter ext() *
|
||||
* *
|
||||
* string file extension *
|
||||
* return type string *
|
||||
* takes no arguments a getter only *
|
||||
* Ex: *
|
||||
* var p='/tmp/foo.dat'; *
|
||||
* var f=new File(p); *
|
||||
* f.ext; *
|
||||
* *
|
||||
* outputs: dat *
|
||||
****************************************************************/
|
||||
|
||||
File.prototype.__defineGetter__('ext',
|
||||
function()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw C.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (!this.exists()) {
|
||||
jslibError(null, "(file doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILE_FILE+":ext");
|
||||
return null;
|
||||
}
|
||||
if (!this.mPath) {
|
||||
jslibError(null, "(no path defined)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILE_FILE+":ext");
|
||||
return null;
|
||||
}
|
||||
var rv=null;
|
||||
try {
|
||||
var leafName = this.mFileInst.leafName;
|
||||
var dotIndex = leafName.lastIndexOf('.');
|
||||
rv=(dotIndex >= 0) ? leafName.substring(dotIndex+1) : "";
|
||||
} catch(e) {
|
||||
jslibError(e, "(problem getting file instance)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILE_FILE+":ext");
|
||||
rv=null;
|
||||
}
|
||||
return rv;
|
||||
})// END ext Getter
|
||||
|
||||
File.prototype.super_help = FileSystem.prototype.help;
|
||||
|
||||
/********************* HELP *****************************/
|
||||
File.prototype.__defineGetter__('help',
|
||||
function()
|
||||
{
|
||||
const help = this.super_help() +
|
||||
|
||||
" open(aMode);\n" +
|
||||
" read();\n" +
|
||||
" readline();\n" +
|
||||
" EOF;\n" +
|
||||
" write(aContents, aPermissions);\n" +
|
||||
" copy(aDest);\n" +
|
||||
" close();\n" +
|
||||
" create();\n" +
|
||||
" remove();\n" +
|
||||
" size;\n" +
|
||||
" ext;\n" +
|
||||
" help;\n";
|
||||
|
||||
return help;
|
||||
})
|
||||
|
||||
jslib_debug('*** load: '+JS_FILE_FILE+' OK');
|
||||
|
||||
} // END BLOCK JS_LIB_LOADED CHECK
|
||||
|
||||
// If jslib base library is not loaded, dump this error.
|
||||
else
|
||||
{
|
||||
dump("JS_FILE library not loaded:\n" +
|
||||
" \tTo load use: chrome://jslib/content/jslib.js\n" +
|
||||
" \tThen: include('chrome://jslib/content/io/file.js');\n\n");
|
||||
}
|
|
@ -0,0 +1,744 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2; -*-
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is Collabnet code.
|
||||
The Initial Developer of the Original Code is Collabnet.
|
||||
|
||||
Portions created by Collabnet are Copyright (C) 2000 Collabnet.
|
||||
All Rights Reserved.
|
||||
|
||||
Contributor(s): Pete Collins,
|
||||
Doug Turner,
|
||||
Brendan Eich,
|
||||
Warren Harris,
|
||||
Eric Plaster,
|
||||
Martin Kutschker
|
||||
Philip Lindsay
|
||||
|
||||
|
||||
JS FileUtils IO API (The purpose of this file is to make it a little easier to do file IO from js)
|
||||
|
||||
fileUtils.js
|
||||
|
||||
Function List
|
||||
|
||||
chromeToPath(aPath) // Converts a chrome://bob/content uri to a path.
|
||||
// NOTE: although this gives you the
|
||||
// path to a file in the chrome directory, you will
|
||||
// most likely not have permisions
|
||||
// to create or write to files there.
|
||||
urlToPath(aPath) // Converts a file:// url to a path
|
||||
exists(aPath); // check to see if a file exists
|
||||
append(aDirPath, aFileName); // append is for abstracting platform specific file paths
|
||||
remove(aPath); // remove a file
|
||||
copy(aSource, aDest); // copy a file from source to destination
|
||||
leaf(aPath); // leaf is the endmost file string
|
||||
// eg: foo.html in /myDir/foo.html
|
||||
permissions(aPath); // returns the files permissions
|
||||
dateModified(aPath); // returns the last modified date in locale string
|
||||
size(aPath); // returns the file size
|
||||
ext(aPath); // returns a file extension if there is one
|
||||
parent(aPath) // returns the dir part of a path
|
||||
dirPath(aPath) // *Depriciated* use parent
|
||||
spawn(aPath, aArgs) // spawns another program
|
||||
nsIFile(aPath) // returns an nsIFile obj
|
||||
help; // currently returns a list of available functions
|
||||
|
||||
Deprecated
|
||||
|
||||
chrome_to_path(aPath); // synonym for chromeToPath
|
||||
URL_to_path(aPath) // synonym for use urlToPath
|
||||
rm(aPath); // synonym for remove
|
||||
extension(aPath); // synonym for ext
|
||||
|
||||
Instructions:
|
||||
|
||||
First include this js file
|
||||
|
||||
var file = new FileUtils();
|
||||
|
||||
Examples:
|
||||
|
||||
var path='/usr/X11R6/bin/Eterm';
|
||||
file.spawn(path, ['-e/usr/bin/vi']);
|
||||
*note* all args passed to spawn must be in the form of an array
|
||||
|
||||
// to list help
|
||||
dump(file.help);
|
||||
|
||||
Warning: these API's are not for religious types
|
||||
|
||||
*/
|
||||
|
||||
// Make sure jslib is loaded
|
||||
if(typeof(JS_LIB_LOADED)=='boolean')
|
||||
{
|
||||
|
||||
/****************** Globals **********************/
|
||||
|
||||
const JS_FILEUTILS_FILE = "fileUtils.js";
|
||||
const JS_FILEUTILS_LOADED = true;
|
||||
|
||||
const JS_FILEUTILS_LOCAL_CID = "@mozilla.org/file/local;1";
|
||||
const JS_FILEUTILS_FILESPEC_PROGID = '@mozilla.org/filespec;1';
|
||||
const JS_FILEUTILS_NETWORK_STD_CID = '@mozilla.org/network/standard-url;1';
|
||||
const JS_FILEUTILS_SIMPLEURI_PROGID = "@mozilla.org/network/simple-uri;1";
|
||||
const JS_FILEUTILS_CHROME_REG_PROGID = '@mozilla.org/chrome/chrome-registry;1';
|
||||
const JS_FILEUTILS_DR_PROGID = "@mozilla.org/file/directory_service;1";
|
||||
const JS_FILEUTILS_PROCESS_CID = "@mozilla.org/process/util;1";
|
||||
|
||||
const JS_FILEUTILS_I_LOCAL_FILE = "nsILocalFile";
|
||||
const JS_FILEUTILS_INIT_W_PATH = "initWithPath";
|
||||
const JS_FILEUTILS_I_PROPS = "nsIProperties";
|
||||
|
||||
const JS_FILEUTILS_CHROME_DIR = "AChrom";
|
||||
|
||||
const JS_FILEUTILS_OK = true;
|
||||
const JS_FILEUTILS_FilePath = new
|
||||
C.Constructor(JS_FILEUTILS_LOCAL_CID, JS_FILEUTILS_I_LOCAL_FILE, JS_FILEUTILS_INIT_W_PATH);
|
||||
|
||||
const JS_FILEUTILS_I_URI = C.interfaces.nsIURI;
|
||||
const JS_FILEUTILS_I_FILEURL = C.interfaces.nsIFileURL;
|
||||
const JS_FILEUTILS_I_PROCESS = C.interfaces.nsIProcess;
|
||||
|
||||
/****************** FileUtils Object Class *********************/
|
||||
function FileUtils() {
|
||||
} // constructor
|
||||
|
||||
FileUtils.prototype = {
|
||||
|
||||
mFileInst : null,
|
||||
|
||||
/********************* CHROME_TO_PATH ***************************/
|
||||
// this is here for backward compatability but is depreciated --pete
|
||||
chrome_to_path : function (aPath) { return this.chromeToPath(aPath); },
|
||||
|
||||
chromeToPath : function (aPath)
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":chromeToPath");
|
||||
return null;
|
||||
}
|
||||
|
||||
var uri = C.classes[JS_FILEUTILS_SIMPLEURI_PROGID].createInstance(JS_FILEUTILS_I_URI);
|
||||
var path;
|
||||
|
||||
if(aPath.search(/^chrome:/) == 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cr = C.classes[JS_FILEUTILS_CHROME_REG_PROGID].getService();
|
||||
if (cr)
|
||||
{
|
||||
cr = cr.QueryInterface(C.interfaces.nsIChromeRegistry);
|
||||
uri.spec = aPath;
|
||||
uri.spec = cr.convertChromeURL(uri);
|
||||
path = uri.path;
|
||||
}
|
||||
}
|
||||
|
||||
catch(e) {}
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
var dir = new C.Constructor(JS_FILEUTILS_DR_PROGID, JS_FILEUTILS_I_PROPS);
|
||||
rv = (new dir()).get(JS_FILEUTILS_CHROME_DIR, C.interfaces.nsIFile).path;
|
||||
rv = path.replace(/\/chrome/, rv);
|
||||
}
|
||||
|
||||
catch (e)
|
||||
{
|
||||
jslibError(e, "(problem getting file instance)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":chromeToPath");
|
||||
rv = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
if(aPath.search(/^file:/) == 0)
|
||||
{
|
||||
rv=this.urlToPath(aPath);
|
||||
}
|
||||
|
||||
else
|
||||
rv=null;
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* URL_TO_PATH ***************************/
|
||||
URL_to_path : function (aPath){ return this.urlToPath(aPath); },
|
||||
|
||||
urlToPath : function (aPath)
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":urlToPath");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
if(aPath.search(/^file:/) == 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var uri = C.classes[JS_FILEUTILS_NETWORK_STD_CID].createInstance(JS_FILEUTILS_I_FILEURL);
|
||||
uri.spec = aPath;
|
||||
rv = uri.file.path;
|
||||
}
|
||||
|
||||
catch (e)
|
||||
{
|
||||
jslibError(e, "(problem getting file instance)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":urlToPath");
|
||||
rv=null;
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* EXISTS ***************************/
|
||||
exists : function (aPath)
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":exists");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
try
|
||||
{
|
||||
var file = new JS_FILEUTILS_FilePath(aPath);
|
||||
rv=file.exists();
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
jslibError(e, "(problem getting file instance)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":exists");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* RM *******************************/
|
||||
rm : function (aPath) { return this.remove(aPath); },
|
||||
|
||||
remove : function (aPath)
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!this.exists(aPath))
|
||||
{
|
||||
jslibError(null, "(file doesn't exist)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
var fileInst = new JS_FILEUTILS_FilePath(aPath);
|
||||
if(fileInst.isDirectory())
|
||||
{
|
||||
jslibError(null, "path is a dir. use rmdir()", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
|
||||
fileInst.remove(false);
|
||||
rv = C.results.NS_OK;
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
jslibError(e, "(unexpected)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":urlToPath");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* COPY *****************************/
|
||||
copy : function (aSource, aDest)
|
||||
{
|
||||
if(!aSource || !aDest)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":copy");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!this.exists(aSource))
|
||||
{
|
||||
jslibError(null, "(file doesn't exist)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":copy");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
var fileInst = new JS_FILEUTILS_FilePath(aSource);
|
||||
var dir = new JS_FILEUTILS_FilePath(aDest);
|
||||
var copyName = fileInst.leafName;
|
||||
|
||||
if(fileInst.isDirectory())
|
||||
{
|
||||
jslibError(null, "(cannot copy directory)", "NS_ERROR_FAILURE", JS_FILEUTILS_FILE+":copy");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!this.exists(aDest) || !dir.isDirectory())
|
||||
{
|
||||
copyName = dir.leafName;
|
||||
dir = new JS_FILEUTILS_FilePath(dir.path.replace(copyName,''));
|
||||
|
||||
if(!this.exists(dir.path))
|
||||
{
|
||||
jslibError(null, "(dest "+dir.path+" doesn't exist)", "NS_ERROR_FAILURE", JS_FILEUTILS_FILE+":copy");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!dir.isDirectory())
|
||||
{
|
||||
jslibError(null, "(dest "+dir.path+" is not a valid path)", "NS_ERROR_FAILURE", JS_FILEUTILS_FILE+":copy");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if(this.exists(this.append(dir.path, copyName)))
|
||||
{
|
||||
jslibError(null, "(dest "+this.append(dir.path, copyName)+" already exists)", "NS_ERROR_FAILURE", JS_FILEUTILS_FILE+":copy");
|
||||
return null;
|
||||
}
|
||||
|
||||
rv=fileInst.copyTo(dir, copyName);
|
||||
|
||||
rv = C.results.NS_OK;
|
||||
}
|
||||
|
||||
catch (e)
|
||||
{
|
||||
jslibError(e, "(unexpected error)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":copy");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* LEAF *****************************/
|
||||
leaf : function (aPath)
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":leaf");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!this.exists(aPath))
|
||||
{
|
||||
jslibError(null, "(file doesn't exist)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":leaf");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
var fileInst = new JS_FILEUTILS_FilePath(aPath);
|
||||
rv=fileInst.leafName;
|
||||
}
|
||||
|
||||
catch(e)
|
||||
{
|
||||
jslibError(e, "(unexpected error)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":leaf");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* APPEND ***************************/
|
||||
append : function (aDirPath, aFileName)
|
||||
{
|
||||
if(!aDirPath || !aFileName)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":append");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!this.exists(aDirPath))
|
||||
{
|
||||
jslibError(null, "(file doesn't exist)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":append");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
var fileInst = new JS_FILEUTILS_FilePath(aDirPath);
|
||||
if(fileInst.exists() && !fileInst.isDirectory())
|
||||
{
|
||||
jslibError(null, aDirPath+" is not a dir", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":append");
|
||||
return null;
|
||||
}
|
||||
|
||||
fileInst.append(aFileName);
|
||||
rv=fileInst.path;
|
||||
delete fileInst;
|
||||
}
|
||||
|
||||
catch(e)
|
||||
{
|
||||
jslibError(e?e:null, "(unexpected error)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":append");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* VALIDATE PERMISSIONS *************/
|
||||
validatePermissions : function(aNum)
|
||||
{
|
||||
if ( parseInt(aNum.toString(10).length) < 3 )
|
||||
return false;
|
||||
|
||||
return JS_FILEUTILS_OK;
|
||||
},
|
||||
|
||||
/********************* PERMISSIONS **********************/
|
||||
permissions : function (aPath)
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":permissions");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!this.exists(aPath))
|
||||
{
|
||||
jslibError(null, "(file doesn't exist)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":permissions");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
rv=(new JS_FILEUTILS_FilePath(aPath)).permissions.toString(8);
|
||||
}
|
||||
|
||||
catch(e)
|
||||
{
|
||||
jslibError(e, "(unexpected error)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":permissions");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* MODIFIED *************************/
|
||||
dateModified : function (aPath)
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":dateModified");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!this.exists(aPath))
|
||||
{
|
||||
jslibError(null, "(file doesn't exist)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":dateModified");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
var date = new Date((new JS_FILEUTILS_FilePath(aPath)).lastModificationDate).toLocaleString();
|
||||
rv=date;
|
||||
}
|
||||
|
||||
catch(e)
|
||||
{
|
||||
jslibError(e, "(unexpected error)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":dateModified");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* SIZE *****************************/
|
||||
size : function (aPath)
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":size");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!this.exists(aPath))
|
||||
{
|
||||
jslibError(null, "(file doesn't exist)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":size");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
rv = (new JS_FILEUTILS_FilePath(aPath)).fileSize;
|
||||
}
|
||||
|
||||
catch(e)
|
||||
{
|
||||
jslibError(e, "(unexpected error)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":size");
|
||||
rv=0;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* EXTENSION ************************/
|
||||
extension : function (aPath){ return this.ext(aPath); },
|
||||
|
||||
ext : function (aPath)
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":ext");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!this.exists(aPath))
|
||||
{
|
||||
jslibError(null, "(file doesn't exist)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":ext");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
var leafName = (new JS_FILEUTILS_FilePath(aPath)).leafName;
|
||||
var dotIndex = leafName.lastIndexOf('.');
|
||||
rv=(dotIndex >= 0) ? leafName.substring(dotIndex+1) : "";
|
||||
}
|
||||
|
||||
catch(e)
|
||||
{
|
||||
jslibError(e, "(unexpected error)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":ext");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* DIRPATH **************************/
|
||||
dirPath : function (aPath){ return this.parent(aPath); },
|
||||
|
||||
parent : function (aPath)
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":parent");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
var fileInst = new JS_FILEUTILS_FilePath(aPath);
|
||||
|
||||
if(!fileInst.exists())
|
||||
{
|
||||
jslibError(null, "(file doesn't exist)", "NS_ERROR_FAILURE", JS_FILEUTILS_FILE+":parent");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(fileInst.isFile())
|
||||
rv=fileInst.parent.path;
|
||||
|
||||
else
|
||||
if(fileInst.isDirectory())
|
||||
rv=fileInst.path;
|
||||
|
||||
else
|
||||
rv=null;
|
||||
}
|
||||
|
||||
catch (e)
|
||||
{
|
||||
jslibError(e, "(problem getting file instance)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":parent");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* SPAWN ****************************/
|
||||
spawn : function (aPath, aArgs)
|
||||
/*
|
||||
* Trys to execute the requested file as a separate *non-blocking* process.
|
||||
*
|
||||
* Passes the supplied *array* of arguments on the command line if
|
||||
* the OS supports it.
|
||||
*
|
||||
*/
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":spawn");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!this.exists(aPath))
|
||||
{
|
||||
jslibError(null, "(file doesn't exist)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":spawn");
|
||||
return null;
|
||||
}
|
||||
|
||||
var len=0;
|
||||
|
||||
if(aArgs)
|
||||
len = aArgs.length;
|
||||
|
||||
else
|
||||
aArgs=null;
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
var fileInst = new JS_FILEUTILS_FilePath(aPath);
|
||||
|
||||
if(!fileInst.isExecutable())
|
||||
{
|
||||
jslibError(null, "(File is not executable)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":spawn");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(fileInst.isDirectory())
|
||||
{
|
||||
jslibError(null, "(File is not a program)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":spawn");
|
||||
return null;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
// Create and execute the process...
|
||||
/*
|
||||
* NOTE: The first argument of the process instance's 'run' method
|
||||
* below specifies the blocking state (false = non-blocking).
|
||||
* The last argument, in theory, contains the process ID (PID)
|
||||
* on return if a variable is supplied--not sure how to implement
|
||||
* this with JavaScript though.
|
||||
*/
|
||||
try
|
||||
{
|
||||
var theProcess = C.classes[JS_FILEUTILS_PROCESS_CID].createInstance(JS_FILEUTILS_I_PROCESS);
|
||||
|
||||
theProcess.init(fileInst);
|
||||
|
||||
return theProcess.run(false, aArgs, len, {});
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
jslibError(e, "(problem spawing process)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":spawn");
|
||||
rv=null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
catch (e)
|
||||
{
|
||||
jslibError(e, "(problem getting file instance)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":spawn");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* nsIFILE **************************/
|
||||
nsIFile : function (aPath)
|
||||
{
|
||||
if(!aPath)
|
||||
{
|
||||
jslibError(null, "(no path defined)", "NS_ERROR_INVALID_ARG", JS_FILEUTILS_FILE+":nsIFile");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rv;
|
||||
|
||||
try
|
||||
{
|
||||
rv = new JS_FILEUTILS_FilePath(aPath);
|
||||
}
|
||||
|
||||
catch (e)
|
||||
{
|
||||
jslibError(e, "(problem getting file instance)", "NS_ERROR_UNEXPECTED", JS_FILEUTILS_FILE+":nsIFile");
|
||||
rv=null;
|
||||
}
|
||||
|
||||
return rv;
|
||||
},
|
||||
|
||||
/********************* HELP *****************************/
|
||||
get help()
|
||||
{
|
||||
var help =
|
||||
|
||||
"\n\nFunction List:\n" +
|
||||
"\n" +
|
||||
" exists(aPath);\n" +
|
||||
" chromeToPath(aPath);\n" +
|
||||
" urlToPath(aPath);\n" +
|
||||
" append(aDirPath, aFileName);\n" +
|
||||
" remove(aPath);\n" +
|
||||
" copy(aSource, aDest);\n" +
|
||||
" leaf(aPath);\n" +
|
||||
" permissions(aPath);\n" +
|
||||
" dateModified(aPath);\n" +
|
||||
" size(aPath);\n" +
|
||||
" ext(aPath);\n" +
|
||||
" parent(aPath);\n" +
|
||||
" spawn(aPath, aArgs);\n" +
|
||||
" nsIFile(aPath);\n" +
|
||||
" help;\n";
|
||||
|
||||
return help;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
jslibDebug('*** load: '+JS_FILEUTILS_FILE+' OK');
|
||||
|
||||
} // END BLOCK JS_LIB_LOADED CHECK
|
||||
|
||||
// If jslib base library is not loaded, dump this error.
|
||||
else
|
||||
{
|
||||
dump("JS_FILE library not loaded:\n" +
|
||||
" \tTo load use: chrome://jslib/content/jslib.js\n" +
|
||||
" \tThen: include('chrome://jslib/content/io/fileUtils.js');\n\n");
|
||||
}
|
||||
|
|
@ -0,0 +1,680 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2;
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is Collabnet code.
|
||||
The Initial Developer of the Original Code is Collabnet.
|
||||
|
||||
Portions created by Collabnet are
|
||||
Copyright (C) 2000 Collabnet. All
|
||||
Rights Reserved.
|
||||
|
||||
Contributor(s): Pete Collins,
|
||||
Doug Turner,
|
||||
Brendan Eich,
|
||||
Warren Harris,
|
||||
Eric Plaster,
|
||||
Martin Kutschker
|
||||
***/
|
||||
|
||||
if (typeof(JS_LIB_LOADED)=='boolean') {
|
||||
|
||||
/***************************
|
||||
* Globals *
|
||||
***************************/
|
||||
|
||||
const JS_FILESYSTEM_LOADED = true;
|
||||
const JS_FILESYSTEM_FILE = "filesystem.js";
|
||||
const JS_FS_LOCAL_CID = "@mozilla.org/file/local;1";
|
||||
const JS_FS_DIR_CID = "@mozilla.org/file/directory_service;1";
|
||||
const JS_FS_NETWORK_CID = '@mozilla.org/network/standard-url;1';
|
||||
const JS_FS_URL_COMP = "nsIURL";
|
||||
const JS_FS_I_LOCAL_FILE = "nsILocalFile";
|
||||
const JS_FS_DIR_I_PROPS = "nsIProperties";
|
||||
const JS_FS_INIT_W_PATH = "initWithPath";
|
||||
const JS_FS_CHROME_DIR = "AChrom";
|
||||
const JS_FS_USR_DEFAULT = "DefProfRt";
|
||||
const JS_FS_PREF_DIR = "PrefD";
|
||||
const JS_FS_OK = true;
|
||||
const JS_FS_File_Path = new Components.Constructor
|
||||
( JS_FS_LOCAL_CID, JS_FS_I_LOCAL_FILE, JS_FS_INIT_W_PATH);
|
||||
const JS_FS_Dir = new Components.Constructor
|
||||
(JS_FS_DIR_CID, JS_FS_DIR_I_PROPS);
|
||||
const JS_FS_URL = new Components.Constructor
|
||||
(JS_FS_NETWORK_CID, JS_FS_URL_COMP);
|
||||
|
||||
/***************************
|
||||
* Globals *
|
||||
***************************/
|
||||
|
||||
/***************************
|
||||
* FileSystem Object Class *
|
||||
***************************/
|
||||
function FileSystem(aPath) {
|
||||
|
||||
return (aPath?this.initPath(arguments):void(null));
|
||||
|
||||
} // constructor
|
||||
|
||||
/***************************
|
||||
* FileSystem Prototype *
|
||||
***************************/
|
||||
FileSystem.prototype = {
|
||||
|
||||
mPath : null,
|
||||
mFileInst : null,
|
||||
|
||||
|
||||
/***************************
|
||||
* INIT PATH *
|
||||
***************************/
|
||||
initPath : function(args)
|
||||
{
|
||||
var fileURL;
|
||||
var i;
|
||||
|
||||
// check if the argument is a file:// url
|
||||
if(typeof(args)=='object') {
|
||||
for (i=0; i<args.length; i++) {
|
||||
if(args[i].search(/^file:/) == 0) {
|
||||
try {
|
||||
fileURL= new JS_FS_URL();
|
||||
fileURL.spec=args[i];
|
||||
args[i] = fileURL.path;
|
||||
} catch (e) {
|
||||
jslibError(e, "(problem getting file instance)",
|
||||
"NS_ERROR_UNEXPECTED", JS_FILESYSTEM_FILE+":initPath");
|
||||
rv=null;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(args.search(/^file:/) == 0) {
|
||||
try {
|
||||
fileURL= new JS_FS_URL();
|
||||
fileURL.spec=args;
|
||||
args = fileURL.path;
|
||||
} catch (e) {
|
||||
jslibError(e, "(problem getting file instance)",
|
||||
"NS_ERROR_UNEXPECTED", JS_FILESYSTEM_FILE+":initPath");
|
||||
rv=null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* If you are wondering what all this extra cruft is, well
|
||||
* this is here so you can reinitialize 'this' with a new path
|
||||
*/
|
||||
var rv = null;
|
||||
try {
|
||||
if (typeof(args)=='object') {
|
||||
this.mFileInst = new JS_FS_File_Path(args[0]?args[0]:this.mPath);
|
||||
if (args.length>1)
|
||||
for (i=1; i<args.length; i++)
|
||||
this.mFileInst.append(args[i]);
|
||||
(args[0] || this.mPath)?rv=this.mPath = this.mFileInst.path:rv=null;
|
||||
} else {
|
||||
this.mFileInst = new JS_FS_File_Path(args?args:this.mPath);
|
||||
this.mFileInst.path?rv=this.mPath = this.mFileInst.path:rv=null;
|
||||
}
|
||||
} catch(e) {
|
||||
jslibError(e?e:null,
|
||||
"initPath (nsILocalFile problem)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILESYSTEM_FILE+":initPath");
|
||||
rv = null;
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* CHECK INST *
|
||||
***************************/
|
||||
checkInst : function ()
|
||||
{
|
||||
|
||||
if (!this.mFileInst) {
|
||||
jslibError(null,
|
||||
"(no path defined)",
|
||||
"NS_ERROR_NOT_INITIALIZED",
|
||||
JS_FILESYSTEM_FILE+":checkInstance");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* PATH *
|
||||
***************************/
|
||||
get path()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
return this.mFileInst.path;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* EXISTS *
|
||||
***************************/
|
||||
exists : function ()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
var rv = false;
|
||||
try {
|
||||
rv = this.mFileInst.exists();
|
||||
} catch(e) {
|
||||
jslibError(e,
|
||||
"exists (nsILocalFile problem)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILESYSTEM_FILE+":exists");
|
||||
rv = false;
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* GET LEAF *
|
||||
***************************/
|
||||
get leaf()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
var rv = null;
|
||||
try {
|
||||
rv = this.mFileInst.leafName;
|
||||
} catch(e) {
|
||||
jslibError(e,
|
||||
"(problem getting file instance)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILESYSTEM_FILE+":leaf");
|
||||
rv = null;
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* SET LEAF *
|
||||
***************************/
|
||||
set leaf(aLeaf)
|
||||
{
|
||||
|
||||
if (!aLeaf) {
|
||||
jslibError(null,
|
||||
"(missing argument)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":leaf");
|
||||
return null;
|
||||
}
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
var rv = null;
|
||||
try {
|
||||
rv = (this.mFileInst.leafName=aLeaf);
|
||||
} catch(e) {
|
||||
jslibError(e,
|
||||
"(problem getting file instance)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILESYSTEM_FILE+":leaf");
|
||||
rv = null;
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* PARENT *
|
||||
***************************/
|
||||
get parent()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
var rv = null;
|
||||
try {
|
||||
if (this.mFileInst.parent.isDirectory()) {
|
||||
if (typeof(JS_DIR_LOADED)!='boolean')
|
||||
include(JS_LIB_PATH+'io/dir.js');
|
||||
rv = new Dir(this.mFileInst.parent.path);
|
||||
}
|
||||
} catch (e) {
|
||||
jslibError(e,
|
||||
"(problem getting file parent)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILESYSTEM_FILE+":parent");
|
||||
rv = null;
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* GET PERMISSIONS *
|
||||
***************************/
|
||||
get permissions()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
if (!this.exists()) {
|
||||
jslibError(null,
|
||||
"(file doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILESYSTEM_FILE+":permisions");
|
||||
return null;
|
||||
}
|
||||
var rv = null;
|
||||
try {
|
||||
rv = this.mFileInst.permissions.toString(8);
|
||||
} catch(e) {
|
||||
jslibError(e,
|
||||
"(problem getting file instance)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILESYSTEM_FILE+":permissions");
|
||||
rv = null;
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* SET PERMISSIONS *
|
||||
***************************/
|
||||
set permissions(aPermission)
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
|
||||
if (!aPermission) {
|
||||
jslibError(null,
|
||||
"(no new permission defined)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":permissions");
|
||||
return null;
|
||||
}
|
||||
if (!this.exists()) {
|
||||
jslibError(null,
|
||||
"(file doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILESYSTEM_FILE+":permisions");
|
||||
return null;
|
||||
}
|
||||
if (!this.validatePermissions(aPermission)) {
|
||||
jslibError(null,
|
||||
"(invalid permission argument)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":permissions");
|
||||
return null;
|
||||
}
|
||||
var rv = null;
|
||||
try {
|
||||
rv = this.mFileInst.permissions=aPermission;
|
||||
} catch(e) {
|
||||
jslibError(e,
|
||||
"(problem getting file instance)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILESYSTEM_FILE+":permissions");
|
||||
rv = null;
|
||||
}
|
||||
return rv;
|
||||
|
||||
},
|
||||
|
||||
/***************************
|
||||
* VALIDATE PERMISSIONS *
|
||||
***************************/
|
||||
validatePermissions : function (aNum)
|
||||
{
|
||||
if (typeof(aNum)!='number')
|
||||
return false;
|
||||
if (parseInt(aNum.toString(10).length) < 3 )
|
||||
return false;
|
||||
return true;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* MODIFIED *
|
||||
***************************/
|
||||
get dateModified()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
if (!this.exists()) {
|
||||
jslibError(null,
|
||||
"(file doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILESYSTEM_FILE+":dateModified");
|
||||
return null;
|
||||
}
|
||||
var rv = null;
|
||||
try {
|
||||
rv = (new Date(this.mFileInst.lastModificationDate));
|
||||
} catch(e) {
|
||||
jslibError(e,
|
||||
"(problem getting file instance)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILESYSTEM_FILE+":dateModified");
|
||||
rv = null;
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* RESET CACHE *
|
||||
***************************/
|
||||
resetCache : function()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
var rv = false;
|
||||
if (this.mPath) {
|
||||
delete this.mFileInst;
|
||||
try {
|
||||
this.mFileInst=new JS_FS_File_Path(this.mPath);
|
||||
rv = true;
|
||||
} catch(e) {
|
||||
jslibError(e,
|
||||
"(unable to get nsILocalFile)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILESYSTEM_FILE+":resetCache");
|
||||
rv = false;
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* nsIFILE *
|
||||
***************************/
|
||||
get nsIFile()
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
var rv = null;
|
||||
try {
|
||||
rv = this.mFileInst.clone();
|
||||
} catch (e) {
|
||||
jslibError(e,
|
||||
"(problem getting file instance)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILESYSTEM_FILE+":nsIFile");
|
||||
rv = null;
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* NOTE: after a move *
|
||||
* successful, 'this' will *
|
||||
* be reinitialized *
|
||||
* to the moved file! *
|
||||
***************************/
|
||||
move : function (aDest)
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
if (!aDest) {
|
||||
jslibError(null,
|
||||
"(no destination path defined)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":move");
|
||||
return false;
|
||||
}
|
||||
if (!this.mPath) {
|
||||
jslibError(null,
|
||||
"(no path defined)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":move");
|
||||
return false;
|
||||
}
|
||||
var rv = null;
|
||||
var newName=null;
|
||||
try {
|
||||
var f = new JS_FS_File_Path(aDest);
|
||||
if (f.exists() && !f.isDirectory()) {
|
||||
jslibError(null,
|
||||
"(destination file exists remove it)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":move");
|
||||
return false;
|
||||
}
|
||||
if (f.equals(this.mFileInst)) {
|
||||
jslibError(null,
|
||||
"(destination file is this file)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":move");
|
||||
return false;
|
||||
}
|
||||
if (!f.exists() && f.parent.exists())
|
||||
newName=f.leafName;
|
||||
if (f.equals(this.mFileInst.parent) && !newName) {
|
||||
jslibError(null,
|
||||
"(destination file is this file)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":move");
|
||||
return false;
|
||||
}
|
||||
var dir=f.parent;
|
||||
if (dir.exists() && dir.isDirectory()) {
|
||||
jslib_debug(newName);
|
||||
this.mFileInst.moveTo(dir, newName);
|
||||
jslib_debug(JS_FILESYSTEM_FILE+':move successful!\n');
|
||||
this.mPath=f.path;
|
||||
this.resetCache();
|
||||
delete dir;
|
||||
rv = true;
|
||||
} else {
|
||||
jslibError(null,
|
||||
"(destination "+dir.parent.path+" doesn't exists)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":move");
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
jslibError(e,
|
||||
"(problem getting file instance)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILESYSTEM_FILE+":move");
|
||||
rv = false;
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* APPEND *
|
||||
***************************/
|
||||
append : function(aLeaf)
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
if (!aLeaf) {
|
||||
jslibError(null,
|
||||
"(no argument defined)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":append");
|
||||
return null;
|
||||
}
|
||||
if (!this.mPath) {
|
||||
jslibError(null,
|
||||
"(no path defined)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":append");
|
||||
return null;
|
||||
}
|
||||
var rv = null;
|
||||
try {
|
||||
this.mFileInst.append(aLeaf);
|
||||
rv = this.mPath=this.path;
|
||||
} catch(e) {
|
||||
jslibError(null,
|
||||
"(unexpected error)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILESYSTEM_FILE+":append");
|
||||
rv = null;
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* APPEND RELATIVE *
|
||||
***************************/
|
||||
appendRelativePath : function(aRelPath)
|
||||
{
|
||||
|
||||
if (!this.checkInst())
|
||||
throw Components.results.NS_ERROR_NOT_INITIALIZED;
|
||||
if (!aRelPath) {
|
||||
jslibError(null,
|
||||
"(no argument defined)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":appendRelativePath");
|
||||
return null;
|
||||
}
|
||||
if (!this.mPath) {
|
||||
jslibError(null,
|
||||
"(no path defined)",
|
||||
"NS_ERROR_INVALID_ARG",
|
||||
JS_FILESYSTEM_FILE+":appendRelativePath");
|
||||
return null;
|
||||
}
|
||||
var rv = null;
|
||||
try {
|
||||
this.mFileInst.appendRelativePath(aRelPath);
|
||||
rv = this.mPath=this.path;
|
||||
} catch(e) {
|
||||
jslibError(null,
|
||||
"(unexpected error)",
|
||||
"NS_ERROR_UNEXPECTED",
|
||||
JS_FILESYSTEM_FILE+":appendRelativePath");
|
||||
rv = null;
|
||||
}
|
||||
return rv;
|
||||
},
|
||||
|
||||
/***************************
|
||||
* GET URL *
|
||||
***************************/
|
||||
get URL()
|
||||
{
|
||||
return (this.path?'file://'+this.path.replace(/\ /g, "%20").replace(/\\/g, "\/"):'');
|
||||
},
|
||||
|
||||
/***************************
|
||||
* ISDIR *
|
||||
***************************/
|
||||
isDir : function()
|
||||
{
|
||||
|
||||
if (!this.exists()) {
|
||||
jslibError(null,
|
||||
"(file doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILESYSTEM_FILE+":isDir");
|
||||
return false;
|
||||
}
|
||||
return this.mFileInst.isDirectory();
|
||||
},
|
||||
|
||||
/***************************
|
||||
* ISFILE *
|
||||
***************************/
|
||||
isFile : function()
|
||||
{
|
||||
|
||||
if (!this.exists()) {
|
||||
jslibError(null,
|
||||
"(file doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILESYSTEM_FILE+":isDir");
|
||||
return false;
|
||||
}
|
||||
return this.mFileInst.isFile();
|
||||
},
|
||||
|
||||
/***************************
|
||||
* ISEXEC *
|
||||
***************************/
|
||||
isExec : function()
|
||||
{
|
||||
|
||||
if (!this.exists()) {
|
||||
jslibError(null,
|
||||
"(file doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILESYSTEM_FILE+":isDir");
|
||||
return false;
|
||||
}
|
||||
return this.mFileInst.isExecutable();
|
||||
},
|
||||
|
||||
/***************************
|
||||
* ISSYMLINK *
|
||||
***************************/
|
||||
isSymlink : function()
|
||||
{
|
||||
|
||||
if (!this.exists()) {
|
||||
jslibError(null,
|
||||
"(file doesn't exist)",
|
||||
"NS_ERROR_FAILURE",
|
||||
JS_FILESYSTEM_FILE+":isDir");
|
||||
return false;
|
||||
}
|
||||
return this.mFileInst.isSymlink();
|
||||
},
|
||||
|
||||
/***************************
|
||||
* HELP *
|
||||
***************************/
|
||||
help : function()
|
||||
{
|
||||
|
||||
const help =
|
||||
"\n\nFunction and Attribute List:\n" +
|
||||
"\n" +
|
||||
" initPath(aPath);\n" +
|
||||
" path;\n" +
|
||||
" exists();\n" +
|
||||
" leaf;\n" +
|
||||
" parent;\n" +
|
||||
" permissions;\n" +
|
||||
" dateModified;\n" +
|
||||
" nsIFile;\n" +
|
||||
" move(aDest);\n" +
|
||||
" append(aLeaf);\n" +
|
||||
" appendRelativePath(aRelPath);\n" +
|
||||
" URL;\n" +
|
||||
" isDir();\n" +
|
||||
" isFile();\n" +
|
||||
" isExec();\n" +
|
||||
" isSymlink();\n";
|
||||
return help;
|
||||
}
|
||||
|
||||
}; // END FileSystem Class
|
||||
|
||||
jslib_debug('*** load: '+JS_FILESYSTEM_FILE+' OK');
|
||||
|
||||
} // END BLOCK JS_LIB_LOADED CHECK
|
||||
|
||||
else {
|
||||
dump("JS_FILE library not loaded:\n" +
|
||||
" \tTo load use: chrome://jslib/content/jslib.js\n" +
|
||||
" \tThen: include('chrome://jslib/content/io/filesystem.js');\n\n");
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2;
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is Collabnet code.
|
||||
The Initial Developer of the Original Code is Collabnet.
|
||||
|
||||
Portions created by Collabnet are
|
||||
Copyright (C) 2000 Collabnet. All
|
||||
Rights Reserved.
|
||||
|
||||
Contributor(s): Pete Collins, Doug Turner, Brendan Eich, Warren Harris
|
||||
|
||||
***/
|
||||
|
||||
if(typeof(JS_LIB_LOADED)=='boolean')
|
||||
{
|
||||
/********************* INCLUDED FILES **************/
|
||||
include(JS_LIB_PATH+'io/filesystem.js');
|
||||
include(JS_LIB_PATH+'io/file.js');
|
||||
include(JS_LIB_PATH+'io/dir.js');
|
||||
include(JS_LIB_PATH+'io/fileUtils.js');
|
||||
include(JS_LIB_PATH+'io/dirUtils.js');
|
||||
/********************* INCLUDED FILES **************/
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
dump("JSLIB library not loaded:\n" +
|
||||
" \tTo load use: chrome://jslib/content/jslib.js\n" +
|
||||
" \tThen: include('chrome://jslib/content/io/io.js');\n\n");
|
||||
}
|
||||
|
|
@ -0,0 +1,209 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2;
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is jslib team code.
|
||||
The Initial Developer of the Original Code is jslib team.
|
||||
|
||||
Portions created by jslib team are
|
||||
Copyright (C) 2000 jslib team. All
|
||||
Rights Reserved.
|
||||
|
||||
Original Author: Pete Collins <pete@mozdev.org>
|
||||
Contributor(s): Martin Kutschker <Martin.T.Kutschker@blackbox.net>
|
||||
|
||||
***/
|
||||
|
||||
/**
|
||||
* insure jslib base is not already loaded
|
||||
*/
|
||||
if (typeof(JS_LIB_LOADED)!='boolean') {
|
||||
try {
|
||||
|
||||
/*************************** GLOBALS ***************************/
|
||||
const JS_LIB_LOADED = true;
|
||||
|
||||
const JS_LIBRARY = "jslib";
|
||||
const JS_LIB_FILE = "jslib.js"
|
||||
const JS_LIB_PATH = "chrome://calendar/content/jslib/";
|
||||
const JS_LIB_VERSION = "0.00";
|
||||
const JS_LIB_AUTHORS = "\tPete Collins <petejc@optonline.net>\n" +
|
||||
"\tEric Plaster <plaster@urbanrage.com>\n" +
|
||||
"\tMartin.T.Kutschker <Martin.T.Kutschker@blackbox.net>\n";
|
||||
const JS_LIB_BUILD = "0.9.5";
|
||||
const JS_LIB_ABOUT = "\tThis is an effort to provide a fully " +
|
||||
"functional js library\n" +
|
||||
"\tfor mozilla package authors to use" +
|
||||
"in their applications\n";
|
||||
const JS_LIB_HOME = "http://jslib.mozdev.org/";
|
||||
|
||||
// Hopefully there won't be any global namespace collisions here
|
||||
const ON = true;
|
||||
const OFF = false;
|
||||
const C = Components;
|
||||
|
||||
var JS_LIB_DEBUG = ON;
|
||||
var JS_LIB_DEBUG_ALERT = OFF;
|
||||
var JS_LIB_ERROR = ON;
|
||||
var JS_LIB_ERROR_ALERT = OFF;
|
||||
|
||||
const JS_LIB_HELP = "\n\nWelcome to jslib version "+JS_LIB_VERSION+"\n\n"
|
||||
+ "Global Constants:\n\n"
|
||||
+ "JS_LIBRARY \n\t"+JS_LIBRARY +"\n"
|
||||
+ "JS_LIB_FILE \n\t"+JS_LIB_FILE +"\n"
|
||||
+ "JS_LIB_PATH \n\t"+JS_LIB_PATH +"\n"
|
||||
+ "JS_LIB_VERSION \n\t"+JS_LIB_VERSION +"\n"
|
||||
+ "JS_LIB_AUTHORS \n" +JS_LIB_AUTHORS
|
||||
+ "JS_LIB_BUILD \n\t"+JS_LIB_BUILD +"\n"
|
||||
+ "JS_LIB_ABOUT \n" +JS_LIB_ABOUT
|
||||
+ "JS_LIB_HOME \n\t"+JS_LIB_HOME +"\n\n"
|
||||
+ "Global Variables:\n\n"
|
||||
+ " JS_LIB_DEBUG\n JS_LIB_ERROR\n\n";
|
||||
|
||||
/*************************** GLOBALS ***************************/
|
||||
|
||||
/****************************************************************
|
||||
* void include(aScriptPath) *
|
||||
* aScriptPath is an argument of string lib chrome path *
|
||||
* returns NS_OK on success, 1 if file is already loaded and *
|
||||
* - errorno or throws exception on failure *
|
||||
* Ex: *
|
||||
* var path='chrome://jslib/content/io/file.js'; *
|
||||
* include(path); *
|
||||
* *
|
||||
* outputs: void(null) *
|
||||
****************************************************************/
|
||||
|
||||
function include(aScriptPath) {
|
||||
|
||||
if (!aScriptPath) {
|
||||
jslibError(null, "Missing file path argument\n",
|
||||
"NS_ERROR_XPC_NOT_ENOUGH_ARGS",
|
||||
JS_LIB_FILE+": include");
|
||||
throw - C.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS;
|
||||
}
|
||||
|
||||
if (aScriptPath==JS_LIB_PATH+JS_LIB_FILE) {
|
||||
jslibError(null, aScriptPath+" is already loaded!",
|
||||
"NS_ERROR_INVALID_ARG", JS_LIB_FILE+": include");
|
||||
throw - C.results.NS_ERROR_INVALID_ARG;
|
||||
}
|
||||
|
||||
var start = aScriptPath.lastIndexOf('/') + 1;
|
||||
var end = aScriptPath.lastIndexOf('.');
|
||||
var slice = aScriptPath.length - end;
|
||||
var loadID = aScriptPath.substring(start, (aScriptPath.length - slice));
|
||||
try {
|
||||
if (typeof(eval('JS_'+loadID.toUpperCase()+'_LOADED')) == 'boolean')
|
||||
return 1;
|
||||
} catch (e) {}
|
||||
|
||||
var rv;
|
||||
try {
|
||||
const PROG_ID = "@mozilla.org/moz/jssubscript-loader;1";
|
||||
const INTERFACE = "mozIJSSubScriptLoader";
|
||||
const Inc = new C.Constructor(PROG_ID, INTERFACE);
|
||||
(new Inc()).loadSubScript(aScriptPath);
|
||||
rv = C.results.NS_OK;
|
||||
} catch(e) {
|
||||
const msg = aScriptPath+" is not a valid path or is already loaded";
|
||||
jslibError(e, msg, "NS_ERROR_INVALID_ARG", JS_LIB_FILE+": include");
|
||||
rv = - C.results.NS_ERROR_INVALID_ARG;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/****************************************************************
|
||||
* void jslibDdebug(aOutString) *
|
||||
* aOutString is an argument of string debug message *
|
||||
* returns void *
|
||||
* Ex: *
|
||||
* var msg='Testing function'; *
|
||||
* jslibDebug(msg); *
|
||||
* *
|
||||
* outputs: Testing function *
|
||||
****************************************************************/
|
||||
|
||||
// this is here for backward compatability but is deprecated --masi
|
||||
function jslib_debug(aOutString) { return jslibDebug(aOutString); }
|
||||
|
||||
function jslibDebug(aOutString) {
|
||||
|
||||
if (!JS_LIB_DEBUG)
|
||||
return void(null);
|
||||
|
||||
if (!aOutString)
|
||||
aOutString="\n";
|
||||
|
||||
if (JS_LIB_DEBUG_ALERT)
|
||||
alert(aOutString);
|
||||
|
||||
return (dump(aOutString+'\n'));
|
||||
}
|
||||
|
||||
// Welcome message
|
||||
jslibDebug(JS_LIB_HELP);
|
||||
jslibDebug("\n\n*********************\nJS_LIB DEBUG IS ON\n*********************\n\n");
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* void jslibError(e, aType, aResults, aCaller) *
|
||||
* e - argument of results exception *
|
||||
* aType - argument of string error type message *
|
||||
* aResults - argument of string Components.results name *
|
||||
* aCaller - argument of string caller filename and func name *
|
||||
* returns void *
|
||||
* Ex: *
|
||||
* jslibError(null, "Missing file path argument\n", *
|
||||
* "NS_ERROR_XPC_NOT_ENOUGH_ARGS", *
|
||||
* JS_LIB_FILE+": include"); *
|
||||
* *
|
||||
* outputs: *
|
||||
* -----======[ ERROR ]=====----- *
|
||||
* Error in jslib.js: include: Missing file path argument *
|
||||
* *
|
||||
* NS_ERROR_NUMBER: NS_ERROR_XPC_NOT_ENOUGH_ARGS *
|
||||
* ------------------------------ *
|
||||
* *
|
||||
****************************************************************/
|
||||
|
||||
function jslibError(e, aType, aResults, aCaller) {
|
||||
|
||||
if (!JS_LIB_ERROR)
|
||||
return void(null);
|
||||
|
||||
if (arguments.length==0)
|
||||
return (dump("JS_LIB_ERROR=ON\n"));
|
||||
|
||||
var errMsg="ERROR: "+(aCaller?"in "+aCaller:"")+" "+aType+"\n";
|
||||
if (e) {
|
||||
var m = (e.message?e.message:e);
|
||||
var r = (typeof(e.result)!='undefined'?'0x'+e.result.toString(16):'');
|
||||
var l = (typeof(e.location)!='undefined'?e.location:'');
|
||||
errMsg+="Name: "+e.name+"\n" +
|
||||
"Message: "+m+"\n" +
|
||||
"Result: "+r+"\n" +
|
||||
"Location: "+l+"\n";
|
||||
}
|
||||
if (aResults)
|
||||
errMsg+="NS_ERROR_NUMBER: "+aResults+"\n";
|
||||
|
||||
if (JS_LIB_ERROR_ALERT)
|
||||
alert(errMsg);
|
||||
|
||||
errMsg = "\n-----======[ ERROR ]=====-----\n" + errMsg;
|
||||
errMsg += "------------------------------\n\n";
|
||||
|
||||
return (dump(errMsg));
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
} // end jslib load test
|
|
@ -0,0 +1,313 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2;
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is Urban Rage Software code.
|
||||
The Initial Developer of the Original Code is Eric Plaster.
|
||||
|
||||
Portions created by Urban Rage Software are
|
||||
Copyright (C) 2000 Urban Rage Software. All
|
||||
Rights Reserved.
|
||||
|
||||
Contributor(s): Eric Plaster <plaster@urbanrage.com)> (original author)
|
||||
Martin Kutschker <martin.t.kutschker@blackbox.net> (polishing)
|
||||
|
||||
*/
|
||||
|
||||
if(typeof(JS_LIB_LOADED)=='boolean')
|
||||
{
|
||||
// test to make sure rdf base classes are loaded
|
||||
if(typeof(JS_RDFBASE_LOADED)!='boolean')
|
||||
include(JS_LIB_PATH+'rdf/rdfBase.js');
|
||||
if(typeof(JS_RDFRESOURCE_LOADED)!='boolean')
|
||||
include(JS_LIB_PATH+'rdf/rdfResource.js');
|
||||
if(typeof(JS_RDFCONTAINER_LOADED)!='boolean')
|
||||
include(JS_LIB_PATH+'rdf/rdfContainer.js');
|
||||
|
||||
const JS_RDF_LOADED = true;
|
||||
const JS_RDF_FILE = "rdf.js";
|
||||
|
||||
const JS_RDF_FLAG_SYNC = 1; // load RDF source synchronously
|
||||
|
||||
function RDF(src, flags) {
|
||||
this.loaded = false;
|
||||
|
||||
if(src) {
|
||||
this._rdf_init(src, flags);
|
||||
}
|
||||
}
|
||||
|
||||
RDF.prototype = new RDFBase;
|
||||
|
||||
RDF.prototype.src = null;
|
||||
|
||||
RDF.prototype._rdf_init = function(src, flags) {
|
||||
flags = flags || 0;
|
||||
this.src = src;
|
||||
|
||||
var load = true; // load source
|
||||
|
||||
// Create an RDF/XML datasource using the XPCOM Component Manager
|
||||
this.dsource = Components
|
||||
.classes[JS_RDFBASE_RDF_DS_PROGID]
|
||||
.createInstance(Components.interfaces.nsIRDFDataSource);
|
||||
|
||||
// The nsIRDFRemoteDataSource interface has the interfaces
|
||||
// that we need to setup the datasource.
|
||||
var remote = this.dsource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
|
||||
|
||||
try {
|
||||
remote.Init(src); // throws an exception if URL already in use
|
||||
}
|
||||
catch(err) {
|
||||
// loading already
|
||||
load = false;
|
||||
|
||||
jslibDebug(JS_RDF_FILE+":_rdf_init: Init of "+src+" failed.");
|
||||
}
|
||||
|
||||
if (load) {
|
||||
try {
|
||||
remote.Refresh((flags & JS_RDF_FLAG_SYNC) ? true: false);
|
||||
}
|
||||
catch(err) {
|
||||
this.dsource = null;
|
||||
|
||||
jslibError(err, "Error refreshing remote rdf: "+src, "NS_ERROR_UNEXPECTED",
|
||||
JS_RDF_FILE+":_rdf_init");
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
this.dsource = this.RDF.GetDataSource(src);
|
||||
remote = this.dsource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
|
||||
}
|
||||
catch(err) {
|
||||
this.dsource = null;
|
||||
|
||||
jslibError(err, "Error getting datasource: "+src, "NS_ERROR_UNEXPECTED",
|
||||
JS_RDF_FILE+":_rdf_init");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (remote.loaded) {
|
||||
this.loaded = true;
|
||||
this.setValid(true);
|
||||
}
|
||||
else {
|
||||
var obs = {
|
||||
rdf: this, // backreference to ourselves
|
||||
|
||||
onBeginLoad: function(aSink)
|
||||
{
|
||||
},
|
||||
|
||||
onInterrupt: function(aSink)
|
||||
{},
|
||||
|
||||
onResume: function(aSink)
|
||||
{},
|
||||
|
||||
onEndLoad: function(aSink)
|
||||
{
|
||||
this.rdf.loaded = true;
|
||||
this.rdf.setValid(true);
|
||||
},
|
||||
|
||||
onError: function(aSink, aStatus, aErrorMsg)
|
||||
{
|
||||
jslibError(null,"Error loading datasource: "+aErrorMsg,
|
||||
"NS_ERROR_UNEXPECTED", JS_RDF_FILE+":_rdf_init (observer)");
|
||||
}
|
||||
};
|
||||
|
||||
// RDF/XML Datasources are all nsIRDFXMLSinks
|
||||
var sink = this.dsource.QueryInterface(Components.interfaces.nsIRDFXMLSink);
|
||||
|
||||
// Attach the observer to the datasource-as-sink
|
||||
sink.addXMLSinkObserver(obs);
|
||||
}
|
||||
}
|
||||
catch(err) {
|
||||
jslibError(err, "Error loading rdf!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDF_FILE+":_rdf_init");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
RDF.prototype.getSource = function()
|
||||
{
|
||||
return this.src;
|
||||
};
|
||||
|
||||
RDF.prototype.getNode = function(aPath)
|
||||
{
|
||||
if(this.isValid()) {
|
||||
var res = this.RDF.GetResource(aPath);
|
||||
return new RDFResource("node", res.Value, null, this.dsource);
|
||||
} else {
|
||||
jslibError(null, "RDF is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDF_FILE+":getNode");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
RDF.prototype.addRootSeq = function(aSeq)
|
||||
{
|
||||
return this.addRootContainer(aSeq, "seq");
|
||||
};
|
||||
|
||||
RDF.prototype.addRootAlt = function(aAlt)
|
||||
{
|
||||
return this.addRootContainer(aAlt, "alt");
|
||||
};
|
||||
|
||||
RDF.prototype.addRootBag = function(aBag)
|
||||
{
|
||||
return this.addRootContainer(aBag, "bag");
|
||||
};
|
||||
|
||||
RDF.prototype.addRootContainer = function(aContainer, aType)
|
||||
{
|
||||
if(this.isValid()) {
|
||||
if(!aContainer)
|
||||
jslibError(null, "Must supply a container path", null, JSRDFCONTAINER+":addRootContainer");
|
||||
|
||||
var res = this.RDF.GetResource(aContainer);
|
||||
|
||||
// FIXME: should test if exists and is already a container
|
||||
|
||||
if(aType == "bag") {
|
||||
this.RDFCUtils.MakeBag(this.dsource, res);
|
||||
} else if(aType == "alt") {
|
||||
this.RDFCUtils.MakeAlt(this.dsource, res);
|
||||
} else if(aType == "seq") {
|
||||
this.RDFCUtils.MakeSeq(this.dsource, res);
|
||||
} else {
|
||||
// FIXME: this.RDFCUtils.MakeContainer....
|
||||
}
|
||||
return new RDFContainer(aType, aContainer, null, this.dsource);
|
||||
} else {
|
||||
jslibError(null, "RDF is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDF_FILE+":addRootContainer");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
RDF.prototype.getRootSeq = function(aSeq)
|
||||
{
|
||||
return this.getContainer(aSeq, "seq");
|
||||
};
|
||||
|
||||
RDF.prototype.getRootAlt = function(aAlt)
|
||||
{
|
||||
return this.getContainer(aAlt, "alt");
|
||||
};
|
||||
|
||||
RDF.prototype.getRootBag = function(aBag)
|
||||
{
|
||||
return this.getContainer(aBag, "bag");
|
||||
};
|
||||
|
||||
RDF.prototype.getContainer = function(aContainer, aType)
|
||||
{
|
||||
var rv = null;
|
||||
if(this.isValid()) {
|
||||
var res = this.RDF.GetResource(aContainer);
|
||||
if(res) {
|
||||
rv = new RDFContainer(aType, aContainer, null, this.dsource);
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
};
|
||||
|
||||
RDF.prototype.getAllSeqs = function()
|
||||
{
|
||||
return this.getRootContainers("seq");
|
||||
};
|
||||
|
||||
RDF.prototype.getAllAlts = function()
|
||||
{
|
||||
return this.getRootContainers("alt");
|
||||
};
|
||||
|
||||
RDF.prototype.getAllBags = function()
|
||||
{
|
||||
return this.getRootContainers("bag");
|
||||
};
|
||||
|
||||
RDF.prototype.getAllContainers = function()
|
||||
{
|
||||
return this.getRootContainers("all");
|
||||
};
|
||||
|
||||
RDF.prototype.getRootContainers = function(aType)
|
||||
{
|
||||
var rv = null;
|
||||
if(this.isValid()) {
|
||||
var list = new Array;
|
||||
var elems = this.dsource.GetAllResources();
|
||||
while(elems.hasMoreElements()) {
|
||||
var elem = elems.getNext();
|
||||
elem = elem.QueryInterface(Components.interfaces.nsIRDFResource);
|
||||
if(aType == "bag") {
|
||||
if(this.RDFCUtils.IsBag(this.dsource, elem)) {
|
||||
list.push(new RDFContainer(aType, elem.Value, null, this.dsource));
|
||||
}
|
||||
} else if(aType == "alt") {
|
||||
if(this.RDFCUtils.IsAlt(this.dsource, elem)) {
|
||||
list.push(new RDFContainer(aType, elem.Value, null, this.dsource));
|
||||
}
|
||||
} else if(aType == "seq") {
|
||||
if(this.RDFCUtils.IsSeq(this.dsource, elem)) {
|
||||
list.push(new RDFContainer(aType, elem.Value, null, this.dsource));
|
||||
}
|
||||
} else if(aType == "all") {
|
||||
if(this.RDFCUtils.IsContainer(this.dsource, elem)) {
|
||||
list.push(new RDFContainer(aType, elem.Value, null, this.dsource));
|
||||
}
|
||||
} else {
|
||||
if(!this.RDFCUtils.IsContainer(this.dsource, elem)) {
|
||||
list.push(new RDFResource(aType, elem.Value, null, this.dsource));
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
} else {
|
||||
jslibError(null, "RDF is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDF_FILE+":getRootContainers");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
RDF.prototype.flush = function()
|
||||
{
|
||||
if(this.isValid()) {
|
||||
this.dsource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource).Flush();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
jslibDebug('*** load: '+JS_RDF_FILE+' OK');
|
||||
|
||||
} // END BLOCK JS_LIB_LOADED CHECK
|
||||
|
||||
else
|
||||
{
|
||||
dump("JS_RDF library not loaded:\n" +
|
||||
" \tTo load use: chrome://jslib/content/jslib.js\n" +
|
||||
" \tThen: include('chrome://jslib/content/rdf/rdf.js');\n\n");
|
||||
}
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2;
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is Urban Rage Software code.
|
||||
The Initial Developer of the Original Code is Eric Plaster.
|
||||
|
||||
Portions created by Urban Rage Software are
|
||||
Copyright (C) 2000 Urban Rage Software. All
|
||||
Rights Reserved.
|
||||
|
||||
Contributor(s): Eric Plaster <plaster@urbanrage.com)> (original author)
|
||||
Martin Kutschker <martin.t.kutschker@blackbox.net> (polishing)
|
||||
|
||||
*/
|
||||
|
||||
if(typeof(JS_LIB_LOADED)=='boolean')
|
||||
{
|
||||
const JS_RDFBASE_LOADED = true;
|
||||
const JS_RDFBASE_FILE = "rdfBase.js";
|
||||
|
||||
const JS_RDFBASE_CONTAINER_PROGID = '@mozilla.org/rdf/container;1';
|
||||
const JS_RDFBASE_CONTAINER_UTILS_PROGID = '@mozilla.org/rdf/container-utils;1';
|
||||
const JS_RDFBASE_LOCATOR_PROGID = '@mozilla.org/filelocator;1';
|
||||
const JS_RDFBASE_RDF_PROGID = '@mozilla.org/rdf/rdf-service;1';
|
||||
const JS_RDFBASE_RDF_DS_PROGID = '@mozilla.org/rdf/datasource;1?name=xml-datasource';
|
||||
|
||||
/***************************************
|
||||
* RDFBase is the base class for all RDF classes
|
||||
*
|
||||
*/
|
||||
function RDFBase(aDatasource) {
|
||||
this.RDF = Components.classes[JS_RDFBASE_RDF_PROGID].getService();
|
||||
this.RDF = this.RDF.QueryInterface(Components.interfaces.nsIRDFService);
|
||||
this.RDFC = Components.classes[JS_RDFBASE_CONTAINER_PROGID].getService();
|
||||
this.RDFC = this.RDFC.QueryInterface(Components.interfaces.nsIRDFContainer);
|
||||
this.RDFCUtils = Components.classes[JS_RDFBASE_CONTAINER_UTILS_PROGID].getService();
|
||||
this.RDFCUtils = this.RDFCUtils.QueryInterface(Components.interfaces.nsIRDFContainerUtils);
|
||||
if(aDatasource) {
|
||||
this._base_init(aDatasource);
|
||||
}
|
||||
}
|
||||
|
||||
RDFBase.prototype = {
|
||||
RDF : null,
|
||||
RDFC : null,
|
||||
RDFCUtils : null,
|
||||
dsource : null,
|
||||
valid : false,
|
||||
|
||||
_base_init : function(aDatasource) {
|
||||
this.dsource = aDatasource;
|
||||
},
|
||||
|
||||
getDatasource : function()
|
||||
{
|
||||
return this.dsource;
|
||||
},
|
||||
|
||||
isValid : function()
|
||||
{
|
||||
return this.valid;
|
||||
},
|
||||
|
||||
setValid : function(aTruth)
|
||||
{
|
||||
if(typeof(aTruth)=='boolean') {
|
||||
this.valid = aTruth;
|
||||
return this.valid;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
flush : function()
|
||||
{
|
||||
if(this.isValid()) {
|
||||
this.dsource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource).Flush();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
jslibDebug('*** load: '+JS_RDFBASE_FILE+' OK');
|
||||
|
||||
} // END BLOCK JS_LIB_LOADED CHECK
|
||||
|
||||
else
|
||||
{
|
||||
dump("JS_RDFBase library not loaded:\n" +
|
||||
" \tTo load use: chrome://jslib/content/jslib.js\n" +
|
||||
" \tThen: include('chrome://jslib/content/rdf/rdf.js');\n\n");
|
||||
}
|
||||
|
|
@ -0,0 +1,259 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2;
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is Urban Rage Software code.
|
||||
The Initial Developer of the Original Code is Eric Plaster.
|
||||
|
||||
Portions created by Urban Rage Software are
|
||||
Copyright (C) 2000 Urban Rage Software. All
|
||||
Rights Reserved.
|
||||
|
||||
Contributor(s): Eric Plaster <plaster@urbanrage.com)> (original author)
|
||||
Martin Kutschker <martin.t.kutschker@blackbox.net>
|
||||
|
||||
*/
|
||||
|
||||
if(typeof(JS_LIB_LOADED)=='boolean')
|
||||
{
|
||||
|
||||
// test to make sure filesystem base class is loaded
|
||||
if(typeof(JS_RDFRESOURCE_LOADED)!='boolean')
|
||||
include(JS_LIB_PATH+'rdf/rdfResource.js');
|
||||
|
||||
const JS_RDFCONTAINER_LOADED = true;
|
||||
const JS_RDFCONTAINER_FILE = "rdfContainer.js";
|
||||
|
||||
function RDFContainer(aType, aPath, aParent, aDatasource) {
|
||||
if(aDatasource) {
|
||||
this._container_init(aType, aPath, aParent, aDatasource);
|
||||
}
|
||||
}
|
||||
|
||||
RDFContainer.prototype = new RDFResource;
|
||||
|
||||
RDFContainer.prototype._container_init = function(aType, aPath, aParent, aDatasource)
|
||||
{
|
||||
this._resource_init(aType, aPath, aParent, aDatasource);
|
||||
};
|
||||
|
||||
RDFContainer.prototype.addSeq = function(aSeq) {
|
||||
return this.addContainer(aSeq, "seq");
|
||||
};
|
||||
|
||||
RDFContainer.prototype.addBag = function(aBag) {
|
||||
return this.addContainer(aBag, "bag");
|
||||
};
|
||||
|
||||
RDFContainer.prototype.addAlt = function(aAlt) {
|
||||
return this.addContainer(aAlt, "alt");
|
||||
};
|
||||
|
||||
RDFContainer.prototype.addContainer = function(aContainer, aType)
|
||||
{
|
||||
if(this.isValid()) {
|
||||
if(!aContainer || !aType)
|
||||
jslibError(null, "Must supply two arguments", null, JS_RDFCONTAINER_FILE+":addContainer");
|
||||
|
||||
var res = this.RDF.GetResource(this.subject+":"+aContainer);
|
||||
|
||||
if( this.resource ) {
|
||||
this.RDFC.Init(this.dsource, this.resource );
|
||||
|
||||
if(aType == "bag") {
|
||||
this.RDFCUtils.MakeBag(this.dsource, res);
|
||||
} else if(aType == "alt") {
|
||||
this.RDFCUtils.MakeAlt(this.dsource, res);
|
||||
} else {
|
||||
this.RDFCUtils.MakeSeq(this.dsource, res);
|
||||
}
|
||||
this.RDFC.AppendElement(res);
|
||||
}
|
||||
return new RDFContainer(aType, this.subject+":"+aContainer, this.parent, this.dsource);
|
||||
} else {
|
||||
jslibError(null, "RDFContainer is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDFCONTAINER_FILE+":addContainer");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
RDFContainer.prototype.getNode = function(aNode) {
|
||||
var rv = null;
|
||||
if(this.isValid()) {
|
||||
var res = this.RDF.GetResource(this.subject+":"+aNode);
|
||||
if(res) {
|
||||
return new RDFResource("node", this.subject+":"+aNode, this.subject, this.dsource);
|
||||
}
|
||||
} else {
|
||||
jslibError(null, "RDFContainer is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDFCONTAINER_FILE+":getNode");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
RDFContainer.prototype.addNode = function(aNode) {
|
||||
if(this.isValid()) {
|
||||
var res = this.RDF.GetResource(this.subject+":"+aNode);
|
||||
this.RDFC.Init(this.dsource, this.resource);
|
||||
this.RDFC.AppendElement(res);
|
||||
return new RDFResource("node", this.subject+":"+aNode, this.subject, this.dsource);
|
||||
} else {
|
||||
jslibError(null, "RDFContainer is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDFCONTAINER_FILE+":addNode");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// FIXME add a getSeq("relative:path");
|
||||
//
|
||||
RDFContainer.prototype.getSubSeqs = function()
|
||||
{
|
||||
return this.getSubResources("seq");
|
||||
};
|
||||
|
||||
RDFContainer.prototype.getSubBags = function()
|
||||
{
|
||||
return this.getSubResources("bag");
|
||||
};
|
||||
|
||||
RDFContainer.prototype.getSubAlts = function()
|
||||
{
|
||||
return this.getSubResources("alt");
|
||||
};
|
||||
|
||||
RDFContainer.prototype.getSubContainers = function()
|
||||
{
|
||||
return this.getSubResources("all");
|
||||
};
|
||||
|
||||
RDFContainer.prototype.getSubNodes = function()
|
||||
{
|
||||
return this.getSubResources("node");
|
||||
};
|
||||
|
||||
RDFContainer.prototype.getSubResources = function(aType)
|
||||
{
|
||||
if(this.isValid()) {
|
||||
var list = new Array;
|
||||
|
||||
this.RDFC.Init(this.dsource, this.resource);
|
||||
|
||||
var elems = this.RDFC.GetElements();
|
||||
while(elems.hasMoreElements()) {
|
||||
var elem = elems.getNext();
|
||||
elem = elem.QueryInterface(Components.interfaces.nsIRDFResource);
|
||||
if(aType == "bag") {
|
||||
if(this.RDFCUtils.IsBag(this.dsource, elem)) {
|
||||
list.push(new RDFContainer(aType, elem.Value, this.subject, this.dsource));
|
||||
}
|
||||
} else if(aType == "alt") {
|
||||
if(this.RDFCUtils.IsAlt(this.dsource, elem)) {
|
||||
list.push(new RDFContainer(aType, elem.Value, this.subject, this.dsource));
|
||||
}
|
||||
} else if(aType == "seq") {
|
||||
if(this.RDFCUtils.IsSeq(this.dsource, elem)) {
|
||||
list.push(new RDFContainer(aType, elem.Value, this.subject, this.dsource));
|
||||
}
|
||||
} else if(aType == "all") {
|
||||
if(this.RDFCUtils.IsContainer(this.dsource, elem)) {
|
||||
list.push(new RDFContainer(aType, elem.Value, this.subject, this.dsource));
|
||||
}
|
||||
} else {
|
||||
if(!this.RDFCUtils.IsContainer(this.dsource, elem)) {
|
||||
list.push(new RDFResource(aType, elem.Value, this.subject, this.dsource));
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
} else {
|
||||
jslibError(null, "RDFContainer is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDFCONTAINER_FILE+":getSubResources");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
RDFContainer.prototype.remove_recursive = function(aPath)
|
||||
{
|
||||
if(this.isValid()) {
|
||||
var res = this.RDF.GetResource(aPath);
|
||||
this.RDFC.Init(this.dsource, res);
|
||||
|
||||
var elems = this.RDFC.GetElements();
|
||||
while(elems.hasMoreElements()) {
|
||||
var elem = elems.getNext();
|
||||
if(this.RDFCUtils.IsContainer(this.dsource, elem)) {
|
||||
this.remove_recursive(elem.QueryInterface(Components.interfaces.nsIRDFResource).Value);
|
||||
this.RDFC.Init(this.dsource, res);
|
||||
}
|
||||
var arcs = this.dsource.ArcLabelsOut(elem);
|
||||
while(arcs.hasMoreElements()) {
|
||||
var arc = arcs.getNext();
|
||||
var targets = this.dsource.GetTargets(elem, arc, true);
|
||||
while (targets.hasMoreElements()) {
|
||||
var target = targets.getNext();
|
||||
this.dsource.Unassert(elem, arc, target, true);
|
||||
}
|
||||
}
|
||||
this.RDFC.RemoveElement(elem, false);
|
||||
}
|
||||
this.RDFC.RemoveElement(res, false);
|
||||
} else {
|
||||
jslibError(null, "RDFContainer is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDFCONTAINER_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
RDFContainer.prototype.remove = function(aDeep)
|
||||
{
|
||||
if(this.isValid()) {
|
||||
if(this.parent != null) {
|
||||
var parentres = this.RDF.GetResource(this.parent);
|
||||
this.RDFC.Init(this.dsource, parentres);
|
||||
}
|
||||
|
||||
if(aDeep) {
|
||||
this.remove_recursive(this.subject);
|
||||
}
|
||||
|
||||
var arcs = this.dsource.ArcLabelsOut(this.resource);
|
||||
while(arcs.hasMoreElements()) {
|
||||
var arc = arcs.getNext();
|
||||
var targets = this.dsource.GetTargets(this.resource, arc, true);
|
||||
while (targets.hasMoreElements()) {
|
||||
var target = targets.getNext();
|
||||
this.dsource.Unassert(this.resource, arc, target, true);
|
||||
}
|
||||
}
|
||||
|
||||
if(this.parent != null) {
|
||||
this.RDFC.RemoveElement(this.resource, false);
|
||||
}
|
||||
this.setValid(false);
|
||||
} else {
|
||||
jslibError(null, "RDFContainer is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDFCONTAINER_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
jslib_debug('*** load: '+JS_RDFCONTAINER_FILE+' OK');
|
||||
|
||||
} // END BLOCK JS_LIB_LOADED CHECK
|
||||
|
||||
else
|
||||
{
|
||||
dump("JS_RDF library not loaded:\n" +
|
||||
" \tTo load use: chrome://jslib/content/jslib.js\n" +
|
||||
" \tThen: include('chrome://jslib/content/rdf/rdf.js');\n\n");
|
||||
}
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2;
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is Urban Rage Software code.
|
||||
The Initial Developer of the Original Code is Eric Plaster.
|
||||
|
||||
Portions created by Urban Rage Software are
|
||||
Copyright (C) 2000 Urban Rage Software. All
|
||||
Rights Reserved.
|
||||
|
||||
Contributor(s): Eric Plaster <plaster@urbanrage.com)> (original author)
|
||||
Martin Kutschker <martin.t.kutschker@blackbox.net> (polishing)
|
||||
|
||||
*/
|
||||
|
||||
if(typeof(JS_LIB_LOADED)=='boolean')
|
||||
{
|
||||
|
||||
// test to make sure rdf base class is loaded
|
||||
if(typeof(JS_RDF_LOADED)!='boolean')
|
||||
include(JS_LIB_PATH+'rdf/rdf.js');
|
||||
|
||||
// test to make sure file class is loaded
|
||||
if (typeof(JS_FILE_LOADED)!='boolean')
|
||||
include(JS_LIB_PATH+'io/file.js');
|
||||
|
||||
|
||||
const JS_RDFFILE_FLAG_SYNC = 1; // load RDF source synchronously
|
||||
const JS_RDFFILE_FLAG_DONT_CREATE = 2; // don't create RDF file (RDFFile only)
|
||||
const JS_RDFFILE_FILE = "rdfFile.js";
|
||||
|
||||
function RDFFile(aPath, aFlags) {
|
||||
this.created = false;
|
||||
|
||||
if(aPath) {
|
||||
this._file_init(aPath, aFlags);
|
||||
}
|
||||
}
|
||||
RDFFile.prototype = new RDF;
|
||||
|
||||
RDFFile.prototype._file_init = function (aPath, aFlags, aNameSpace, aID) {
|
||||
aFlags = aFlags || JS_RDFFILE_FLAG_SYNC; // default to synchronous loading
|
||||
|
||||
if(aNameSpace == null) {
|
||||
aNameSpace = "http://jslib.mozdev.org/rdf#";
|
||||
}
|
||||
if(aID == null) {
|
||||
aID = "JSLIB";
|
||||
}
|
||||
|
||||
var filename = aPath;
|
||||
|
||||
if(filename.substr(0,7) == "file://") {
|
||||
var fu = new FileUtils();
|
||||
filename = fu.urltoPath(filename);
|
||||
}
|
||||
|
||||
dump("file: "+filename+"\n");
|
||||
// Ensure we have a base RDF file to work with
|
||||
var rdf_file = new File(aPath);
|
||||
|
||||
if (!rdf_file.exists() && !(aFlags & JS_RDFFILE_FLAG_DONT_CREATE)) {
|
||||
dump("here!\n");
|
||||
if (rdf_file.open("w") != JS_FILE_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
var filestr =
|
||||
'<?xml version="1.0" ?>\n' +
|
||||
'<RDF:RDF\n' +
|
||||
' xmlns:'+ aID +'="'+ aNameSpace +'"\n' +
|
||||
' xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#">\n' +
|
||||
'</RDF:RDF>\n';
|
||||
|
||||
if (rdf_file.write(filestr) != JS_FILE_OK) {
|
||||
rdf_file.close();
|
||||
return;
|
||||
}
|
||||
|
||||
this.created = true;
|
||||
}
|
||||
rdf_file.close();
|
||||
|
||||
// Get a reference to the available datasources
|
||||
this._rdf_init("file://"+filename, aFlags);
|
||||
};
|
||||
|
||||
jslib_debug('*** load: '+JS_RDFFILE_FILE+' OK');
|
||||
|
||||
} // END BLOCK JS_LIB_LOADED CHECK
|
||||
|
||||
// If jslib base library is not loaded, dump this error.
|
||||
else
|
||||
{
|
||||
dump("JS_RDFFILE library not loaded:\n" +
|
||||
" \tTo load use: chrome://jslib/content/jslib.js\n" +
|
||||
" \tThen: include('chrome://jslib/content/rdf/rdfFile.js');\n\n");
|
||||
}
|
|
@ -0,0 +1,246 @@
|
|||
/*** -*- Mode: Javascript; tab-width: 2;
|
||||
|
||||
The contents of this file are subject to the Mozilla Public
|
||||
License Version 1.1 (the "License"); you may not use this file
|
||||
except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS
|
||||
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
implied. See the License for the specific language governing
|
||||
rights and limitations under the License.
|
||||
|
||||
The Original Code is Urban Rage Software code.
|
||||
The Initial Developer of the Original Code is Eric Plaster.
|
||||
|
||||
Portions created by Urban Rage Software are
|
||||
Copyright (C) 2000 Urban Rage Software. All
|
||||
Rights Reserved.
|
||||
|
||||
Contributor(s): Eric Plaster <plaster@urbanrage.com)> (original author)
|
||||
Martin Kutschker <martin.t.kutschker@blackbox.net> (polishing)
|
||||
|
||||
*/
|
||||
|
||||
if(typeof(JS_LIB_LOADED)=='boolean')
|
||||
{
|
||||
// test to make sure rdfBase base class is loaded
|
||||
if(typeof(JS_RDFBASE_LOADED)!='boolean')
|
||||
include(JS_LIB_PATH+'rdf/rdfBase.js');
|
||||
|
||||
const JS_RDFRESOURCE_LOADED = true;
|
||||
const JS_RDFRESOURCE_FILE = "rdfResource.js";
|
||||
|
||||
function RDFResource(aType, aPath, aParentPath, aDatasource) {
|
||||
if(aDatasource) {
|
||||
this._resource_init(aType, aPath, aParentPath, aDatasource);
|
||||
}
|
||||
}
|
||||
|
||||
RDFResource.prototype = new RDFBase;
|
||||
|
||||
RDFResource.prototype.type = null;
|
||||
RDFResource.prototype.parent = null;
|
||||
RDFResource.prototype.resource = null;
|
||||
RDFResource.prototype.subject = null;
|
||||
|
||||
RDFResource.prototype._resource_init = function(aType, aPath, aParentPath, aDatasource) {
|
||||
this.type = aType;
|
||||
this.parent = aParentPath;
|
||||
this.subject = aPath;
|
||||
this.resource = this.RDF.GetResource(aPath);
|
||||
|
||||
this._base_init(aDatasource);
|
||||
if(this.resource) {
|
||||
this.setValid(true);
|
||||
}
|
||||
};
|
||||
|
||||
RDFResource.prototype.getResource = function() {
|
||||
return this.resource;
|
||||
};
|
||||
|
||||
RDFResource.prototype.getSubject = function() {
|
||||
return this.subject;
|
||||
};
|
||||
|
||||
RDFResource.prototype.makeSeq = function(aSeq) {
|
||||
return this.makeContainer("seq");
|
||||
};
|
||||
|
||||
RDFResource.prototype.makeBag = function(aBag) {
|
||||
return this.makeContainer("bag");
|
||||
};
|
||||
|
||||
RDFResource.prototype.makeAlt = function(aAlt) {
|
||||
return this.makeContainer("alt");
|
||||
};
|
||||
|
||||
RDFResource.prototype.makeContainer = function(aType) {
|
||||
this.RDFC.Init(this.dsource, this.resource );
|
||||
|
||||
if(aType == "bag") {
|
||||
this.RDFCUtils.MakeBag(this.dsource, this.resource);
|
||||
} else if(aType == "alt") {
|
||||
this.RDFCUtils.MakeAlt(this.dsource, this.resource);
|
||||
} else {
|
||||
this.RDFCUtils.MakeSeq(this.dsource, this.resource);
|
||||
}
|
||||
|
||||
return new RDFContainer(aType, this.resource_path+":"+aContainer, this.parent, this.dsource);
|
||||
this.setValid(false);
|
||||
};
|
||||
|
||||
RDFResource.prototype.setAttribute = function(aName, aValue)
|
||||
{
|
||||
if(this.isValid()) {
|
||||
var oldvalue = this.getAttribute(aName);
|
||||
|
||||
if(oldvalue) {
|
||||
this.dsource.Change(this.resource,
|
||||
this.RDF.GetResource(aName),
|
||||
this.RDF.GetLiteral(oldvalue),
|
||||
this.RDF.GetLiteral(aValue) );
|
||||
} else {
|
||||
this.dsource.Assert(this.resource,
|
||||
this.RDF.GetResource(aName),
|
||||
this.RDF.GetLiteral(aValue),
|
||||
true );
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
RDFResource.prototype.getAttribute = function(aName)
|
||||
{
|
||||
if(this.isValid()) {
|
||||
var itemRes = this.RDF.GetResource(aName);
|
||||
if (!itemRes) return null;
|
||||
|
||||
var target = this.dsource.GetTarget(this.resource, itemRes, true);
|
||||
if (target) target = target.QueryInterface(Components.interfaces.nsIRDFLiteral);
|
||||
if (!target) return null;
|
||||
return target.Value;
|
||||
} else {
|
||||
jslibError(null, "RDFResource is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDFRESOURCE_FILE+":getAttribute");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
RDFResource.prototype.removeAttribute = function(aName)
|
||||
{
|
||||
if(this.isValid()) {
|
||||
var itemRes = this.RDF.GetResource(aName, true);
|
||||
var target = this.dsource.GetTarget(this.resource, itemRes, true);
|
||||
this.dsource.Unassert(this.resource, itemRes, target);
|
||||
} else {
|
||||
jslibError(null, "RDFResource is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDFRESOURCE_FILE+":removeAttribute");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
RDFResource.prototype.setAllAttributes = function(aList)
|
||||
{
|
||||
var length = 0;
|
||||
try {
|
||||
length = aList.length;
|
||||
} catch(e) {
|
||||
return false;
|
||||
}
|
||||
if(this.isValid()) {
|
||||
var arcs = this.dsource.ArcLabelsOut(this.resource);
|
||||
while(arcs.hasMoreElements()) {
|
||||
var arc = arcs.getNext();
|
||||
arc = arc.QueryInterface(Components.interfaces.nsIRDFResource);
|
||||
var obj = new Object;
|
||||
var l = arc.Value.split("#");
|
||||
obj.name = l[l.length-1];
|
||||
var targets = this.dsource.GetTargets(this.resource, arc, true);
|
||||
while (targets.hasMoreElements()) {
|
||||
var target = targets.getNext();
|
||||
this.dsource.Unassert(this.resource, arc, target, true);
|
||||
}
|
||||
}
|
||||
for(var i=0; i<length; i++) {
|
||||
this.setAttribute(aList[i].name, aList[i].value);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
RDFResource.prototype.getAllAttributes = function()
|
||||
{
|
||||
var list = new Array;
|
||||
if(this.isValid()) {
|
||||
var arcs = this.dsource.ArcLabelsOut(this.resource);
|
||||
while(arcs.hasMoreElements()) {
|
||||
var arc = arcs.getNext();
|
||||
arc = arc.QueryInterface(Components.interfaces.nsIRDFResource);
|
||||
var obj = new Object;
|
||||
var l = arc.Value.split("#");
|
||||
obj.name = l[l.length-1];
|
||||
var targets = this.dsource.GetTargets(this.resource, arc, true);
|
||||
while (targets.hasMoreElements()) {
|
||||
var target = targets.getNext();
|
||||
if(target) {
|
||||
try {
|
||||
target = target.QueryInterface(Components.interfaces.nsIRDFLiteral);
|
||||
}
|
||||
catch(e) {
|
||||
target = target.QueryInterface(Components.interfaces.nsIRDFResource);
|
||||
}
|
||||
obj.value = target.Value;
|
||||
list.push(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
} else {
|
||||
jslibError(null, "RDFResource is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDFRESOURCE_FILE+":getAllAttributes");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
RDFResource.prototype.remove = function()
|
||||
{
|
||||
if(this.isValid()) {
|
||||
// FIXME: if we get this node from RDF, it has no parent...
|
||||
// try just removing all arcs and targets...
|
||||
// var parentres = this.RDF.GetResource(this.parent);
|
||||
// this.RDFC.Init(this.dsource, parentres);
|
||||
|
||||
var arcs = this.dsource.ArcLabelsOut(this.resource);
|
||||
while(arcs.hasMoreElements()) {
|
||||
var arc = arcs.getNext();
|
||||
var targets = this.dsource.GetTargets(this.resource, arc, true);
|
||||
while (targets.hasMoreElements()) {
|
||||
var target = targets.getNext();
|
||||
this.dsource.Unassert(this.resource, arc, target, true);
|
||||
}
|
||||
}
|
||||
|
||||
// this.RDFC.RemoveElement(this.resource, false);
|
||||
this.setValid(false);
|
||||
} else {
|
||||
jslibError(null, "RDFResource is no longer valid!\n", "NS_ERROR_UNEXPECTED",
|
||||
JS_RDFRESOURCE_FILE+":remove");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
jslib_debug('*** load: '+JS_RDFRESOURCE_FILE+' OK');
|
||||
|
||||
} // END BLOCK JS_LIB_LOADED CHECK
|
||||
|
||||
else
|
||||
{
|
||||
dump("JS_RDFResource library not loaded:\n" +
|
||||
" \tTo load use: chrome://jslib/content/jslib.js\n" +
|
||||
" \tThen: include('chrome://jslib/content/rdf/rdf.js');\n\n");
|
||||
}
|
||||
|
Загрузка…
Ссылка в новой задаче