/java/sample/authsub/web/javascript/gdata_condensed.js
JavaScript | 5235 lines | 3217 code | 726 blank | 1292 comment | 629 complexity | 8680b83de9fa03df2564f8ef06ae2ae5 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- /* Copyright (c) 2006 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @fileoverview
- * This file contains some common helper functions
- */
- // Based on <http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/
- // core.html#ID-1950641247>
- var DOM_ELEMENT_NODE = 1;
- var DOM_ATTRIBUTE_NODE = 2;
- var DOM_TEXT_NODE = 3;
- var DOM_CDATA_SECTION_NODE = 4;
- var DOM_ENTITY_REFERENCE_NODE = 5;
- var DOM_ENTITY_NODE = 6;
- var DOM_PROCESSING_INSTRUCTION_NODE = 7;
- var DOM_COMMENT_NODE = 8;
- var DOM_DOCUMENT_NODE = 9;
- var DOM_DOCUMENT_TYPE_NODE = 10;
- var DOM_DOCUMENT_FRAGMENT_NODE = 11;
- var DOM_NOTATION_NODE = 12;
- //
- // regular expression for xsd date-time
- //
- var UTIL_PARSE_XMLDATE = /(\d{4})-(\d{2})-(\d{2})/;
- var UTIL_PARSE_XMLDATETIME =
- /(\d{4})-?(\d{2})-?(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))(Z)?(([+-])(\d{2}):(\d{2}))?/;
- function UTIL_inherits(subClass, superClass) {
- subClass.prototype = new superClass();
- subClass.prototype.Base = superClass.prototype;
- subClass.prototype.constructor = superClass.constructor;
- }
- /**
- * helper to detect if a variable is persistable
- * @param toTest the value to test
- * @return true if peristable
- */
- function UTIL_isPersistable(toTest) {
- if (typeof(toTest) == 'string') {
- if (toTest !== null && toTest.length > 0 ) {
- return true;
- }
- }
- return false;
- }
- /**
- * helper to convert a date to RFC3339 string presentation
- * @param date {Date} the date to convert
- * @return {string} date formatted according to RFC3339
- */
- function UTIL_dateToRFC3339(dateToConvert) {
- var strReturn = null;
- strReturn = dateToConvert.getUTCFullYear() + "-" +
- UTIL_pad((dateToConvert.getUTCMonth()+1).toString(), 2, "0") + "-" +
- UTIL_pad((dateToConvert.getUTCDate()).toString(), 2, "0");
- if (dateToConvert.getTime() > 0) {
- strReturn += "T" +
- UTIL_pad(dateToConvert.getUTCHours().toString(), 2, "0") + ":" +
- UTIL_pad(dateToConvert.getUTCMinutes().toString(), 2, "0") + ":" +
- UTIL_pad(dateToConvert.getUTCSeconds().toString(), 2, "0");
- // convert to hours
- var timediff = dateToConvert.getTimezoneOffset();
- timediff /= 60;
- strReturn += timediff > 0 ? "+" : "-";
- strReturn += UTIL_pad(Math.abs(timediff).toFixed(0), 2, "0") + ":00";
- }
- return strReturn;
- }
- /**
- * helper to convert a string in RFC3339 presentation to a date
- * @param dateString the string date value to convert
- * @return the date
- */
- function UTIL_parseXmlDateTime(dateString) {
- var d = null;
- var dateParts = dateString.match(UTIL_PARSE_XMLDATETIME);
- if (dateParts == null) {
- //
- // if dateTime didn't parse, try date
- //
- return UTIL_parseXmlDate(dateString);
- } else {
- d = new Date();
- var year = parseInt(dateParts[1],10);
- var month = parseInt(dateParts[2],10);
- var day = parseInt(dateParts[3],10);
- var hour = parseInt(dateParts[4],10);
- var min = parseInt(dateParts[5],10);
- var sec = parseInt(dateParts[6],10);
-
- if ((dateParts[8] || dateParts[9]) && !(hour==0 && min==0 && sec==0)) {
- // utc mode
- d.setUTCFullYear(year);
- d.setUTCMonth(month-1);
- d.setUTCDate(day);
- d.setUTCHours(hour);
- d.setUTCMinutes(min);
- d.setUTCSeconds(sec);
- d.setUTCMilliseconds(0);
- if (dateParts[8] === 'Z') {
- // so the time is in UTC
- LOG_TRACE(dateParts[8], "UTIL_parseXmlDateTime", dateString);
- } else {
- // should be an offset now
- var timeOffset = 0;
- if (dateParts[10]) {
- timeOffset = Number(dateParts[11]) * 60;
- timeOffset += Number(dateParts[12]);
- timeOffset *= ((dateParts[10] === '-') ? 1 : -1);
- }
- timeOffset *= 60 * 1000;
- d = new Date(Number(d) + timeOffset);
- }
-
- //
- // BUGBUG - apply +/- bias from part 8
- //
- } else {
- d.setFullYear(year);
- d.setMonth(month-1);
- d.setDate(day);
- d.setHours(hour);
- d.setMinutes(min);
- d.setSeconds(sec);
- d.setMilliseconds(0);
- }
- }
- LOG_TRACE(dateString + "----" + d, "UTIL_parseXmlDateTime", dateString);
- return d;
- }
-
-
- /**
- * helper to convert a string in RFC3339 presentation to a date
- * @param dateString the string date value to convert, no timezone
- * @return the date
- */
- function UTIL_parseXmlDate(dateString) {
- var d;
- var dateParts = dateString.match(UTIL_PARSE_XMLDATE);
- if (dateParts != null) {
- d = new Date();
- d.setHours(0);
- d.setMinutes(0);
- d.setSeconds(0);
- d.setMilliseconds(0);
-
- var year = parseInt(dateParts[1],10);
- var month = parseInt(dateParts[2],10);
- var day = parseInt(dateParts[3],10);
-
- d.setFullYear(year);
- d.setMonth(month-1);
- d.setDate(day);
- }
- return d;
- }
- /**
- * helper to pad a string with a given char
- * @param str the string to pad
- * @param length the length to pad to
- * @param chr the char to use for padding
- * @return the padded string
- */
- function UTIL_pad(str, length, chr) {
- while (length > str.length) {
- str = chr + str;
- }
- return str;
- }
- /**
- * helper to encode a string that is passed in to URL encoding
- * this is used for requests to a webserver, so do not use
- * encodeUriComponent here
- * @param sStr the stirng to encode
- * @return the encoded string
- */
- function UTIL_urlEncodeString(sStr) {
- return escape(sStr).
- replace(/\+/g, '%2B').
- replace(/\"/g,'%22').
- replace(/\'/g, '%27').
- replace(/\//g,'%2F');
- }
- /**
- * helper to attach an event to a target object
- * @param target the object to attach the event to
- * @param type the event type
- * @param callback the event callback
- */
- function UTIL_domEventHandler(target, type, callback) {
- if (target.addEventListener) {
- target.addEventListener(type, callback, false);
- } else {
- target.attachEvent("on" + type, callback);
- }
- }
- /**
- * helper to create a DIV element, currently used for debugging
- * @param opt_text the text in the div
- * @param opt_className the css class to use
- */
- function UTIL_domCreateDiv(opt_text, opt_className) {
- var el = document.createElement("div");
- if (opt_text) {
- UTIL_domAppendChild(el, document.createTextNode(opt_text));
- }
- if (opt_className) {
- el.className = opt_className;
- }
- return el;
- }
- /**
- * helper to append a child in the dom
- * @param parent the node to append to
- * @param child the node to append
- */
- function UTIL_domAppendChild(parent, child) {
- try {
- parent.appendChild(child);
- } catch (e) {
- LOG_ERROR("UTIL_domAppendChild: " + e);
- }
- return child;
- }
- /**
- * Applies the given function to each element of the array, preserving
- * this, and passing the index.
- */
- function UTIL_mapExec(array, func) {
- for (var i = 0; i < array.length; ++i) {
- func.call(this, array[i], i);
- }
- }
- /**
- * Returns an array that contains the return value of the given
- * function applied to every element of the input array.
- */
- function UTIL_mapExpr(array, func) {
- var ret = [];
- for (var i = 0; i < array.length; ++i) {
- ret.push(func(array[i]));
- }
- return ret;
- }
- /**
- * Reverses the given array in place.
- */
- function UTIL_reverseInplace(array) {
- for (var i = 0; i < array.length / 2; ++i) {
- var h = array[i];
- var ii = array.length - i - 1;
- array[i] = array[ii];
- array[ii] = h;
- }
- }
- /**
- * Shallow-copies an array.
- */
- function UTIL_copyArray(dst, src) {
- for (var i = 0; i < src.length; ++i) {
- dst.push(src[i]);
- }
- }
- /**
- * Returns the text value of a node; for nodes without children this
- * is the nodeValue, for nodes with children this is the concatenation
- * of the value of all children.
- */
- function UTIL_xmlValue(node) {
- if (!node) {
- return '';
- }
- var ret = '';
- if (node.nodeType == DOM_TEXT_NODE ||
- node.nodeType == DOM_CDATA_SECTION_NODE ||
- node.nodeType == DOM_ATTRIBUTE_NODE) {
- ret += node.nodeValue;
- } else if (node.nodeType == DOM_ELEMENT_NODE ||
- node.nodeType == DOM_DOCUMENT_NODE ||
- node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE) {
- for (var i = 0; i < node.childNodes.length; ++i) {
- ret += arguments.callee(node.childNodes[i]);
- }
- }
- return ret;
- }
- /**
- * Returns the representation of a node as XML text.
- */
- function UTIL_xmlText(node, opt_cdata) {
- var buf = [];
- xmlTextR(node, buf, opt_cdata);
- return buf.join('');
- }
- /**
- * worker function for UTIL_xmlText()
- */
- function xmlTextR(node, buf, cdata) {
- if (node.nodeType == DOM_TEXT_NODE) {
- buf.push(xmlEscapeText(node.nodeValue));
- } else if (node.nodeType == DOM_CDATA_SECTION_NODE) {
- if (cdata) {
- buf.push(node.nodeValue);
- } else {
- buf.push('<![CDATA[' + node.nodeValue + ']]>');
- }
- } else if (node.nodeType == DOM_COMMENT_NODE) {
- buf.push('<!--' + node.nodeValue + '-->');
- } else if (node.nodeType == DOM_ELEMENT_NODE) {
- buf.push('<' + xmlFullNodeName(node));
- for (var i = 0; i < node.attributes.length; ++i) {
- var a = node.attributes[i];
- if (a && a.nodeName && a.nodeValue) {
- buf.push(' ' + xmlFullNodeName(a) + '="' +
- xmlEscapeAttr(a.nodeValue) + '"');
- }
- }
- if (node.childNodes.length === 0) {
- buf.push('/>');
- } else {
- buf.push('>');
- for (i = 0; i < node.childNodes.length; ++i) {
- arguments.callee(node.childNodes[i], buf, cdata);
- }
- buf.push('</' + xmlFullNodeName(node) + '>');
- }
- } else if (node.nodeType == DOM_DOCUMENT_NODE ||
- node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE) {
- for (i = 0; i < node.childNodes.length; ++i) {
- arguments.callee(node.childNodes[i], buf, cdata);
- }
- }
- }
- /**
- * helper for xmlTextR
- */
- function xmlFullNodeName(n) {
- if (n.prefix && n.nodeName.indexOf(n.prefix + ':') !== 0) {
- return n.prefix + ':' + n.nodeName;
- } else {
- return n.nodeName;
- }
- }
- /**
- * Escape XML special markup chracters: tag delimiter < > and entity
- * reference start delimiter &. The escaped string can be used in XML
- * text portions (i.e. between tags).
- */
- function xmlEscapeText(s) {
- return ('' + s).replace(/&/g, '&').replace(/</, '<').
- replace(/>/, '>');
- }
- /**
- * Escape XML special markup characters: tag delimiter < > entity
- * reference start delimiter & and quotes ". The escaped string can be
- * used in double quoted XML attribute value portions (i.e. in
- * attributes within start tags).
- */
- function xmlEscapeAttr(s) {
- return xmlEscapeText(s).replace(/\"/g, '"');
- }
- /**
- * Escape markup in XML text, but don't touch entity references. The
- * escaped string can be used as XML text (i.e. between tags).
- */
- function xmlEscapeTags(s) {
- return s.replace(/</, '<').replace(/>/g, '>');
- }
- // remove this if you merge
- if (window.GD_Loader) {
- // continue loading
- window.GD_Loader();
- }
- // end
- /* Copyright (c) 2006 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @fileoverview
- * BrowserDetector (object)
- *
- * A class for detecting version 5 browsers by the Javascript objects
- * they support and not their user agent strings (which can be
- * spoofed).
- *
- * Warning: Though slow to develop, browsers may begin to add
- * DOM support in later versions which might require changes to this
- * file.
- *
- * Typical usage:
- * Detect = new BrowserDetector();
- * if (Detect.IE()) //IE-only code...
- */
- /**
- * @constructor
- */
- function BrowserDetector() {
- //IE 4+
- this.IE = function() {
- try {
- return this.Run(document.all && window.ActiveXObject) !==false;
- } catch(e) {
- /* IE 5.01 doesn't support the 'contains' object and
- fails the first test */
- if (document.all) {
- return true;
- }
- return false;
- }
- };
-
- //IE 5.5+
- this.IE_5_5_newer = function(){
- try {
- return this.Run(this.IE() && Array.prototype.pop)!==false;
- } catch(e) {return false;}
- };
-
- //IE 5, Macintosh
- this.IE_5_Mac = function(){
- try {
- return(true === undefined);
- } catch(e) {
- return(
- document.all &&
- document.getElementById &&
- !document.mimeType &&
- !window.opera)!==false;
- }
- };
- //Opera 7+
- this.OPERA = function(){
- try {
- return this.Run(document.all && document.contains)!==false;
- } catch(e) {return false;}
- };
- //Gecko, actually Mozilla 1.2+
- this.MOZILLA = function() {
- try {
- return this.Run(
- document.implementation &&
- document.implementation.createDocument &&
- document.evaluate &&
- !document.contains )!==false;
- } catch(e) {return false;}
- };
- //Safari
- this.SAFARI = function(){
- try {
- return this.Run(
- document.implementation &&
- document.implementation.createDocument &&
- document.contains)!==false;
- } catch(e) {return false;}
- };
- //Any browser which supports the W3C DOM
- this.DOM = function() {
- return(document.getElementById);
- };
- this.Run = function(test) {
- if (test===undefined) {
- return false;
- } else {
- return test;
- }
- };
- this.XPathSupport = function() {
- return this.IE_5_5_newer() || document.implementation.hasFeature('XPath','3.0');
- };
- }
- var DOM = (document.getElementById);
- var Detect;
- if (DOM) {
- Detect = new BrowserDetector();
- }
- // remove this if you merge
- if (window.GD_Loader) {
- // continue loading
- window.GD_Loader();
- }
- // end
- /* Copyright (c) 2006 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- //------------------------------------------------------------------------------
- function DumpError(msg) {}
- function DumpException(msg) {}
- //------------------------------------------------------------------------------
- // Logging
- //------------------------------------------------------------------------------
- function LOG_LEVEL() {}
- LOG_LEVEL.NONE = 0;
- LOG_LEVEL.ERROR = 1;
- LOG_LEVEL.DEBUG = 2;
- LOG_LEVEL.TRACE = 3;
- LOG_LEVEL.ALL = 4;
- function DEBUG_ENABLE_CROSSSITE() {}
- function DEBUG_CROSSSITE() { return false;}
- //------------------------------------------------------------------------------
- // Inline log functions
- //------------------------------------------------------------------------------
- function LOG_CLASS(classDef, name) {}
- function LOG(msg, opt_level) {}
- function LOG_DEBUG(msg, opt_show) {}
- function LOG_ERROR(msg) {}
- function LOG_EVENT(msg) {}
- function LOG_TRACE(obj, fn, args) {}
- function LOG_SEPARATOR(opt_text) {}
- function LOG_TIMER_START(name) {}
- function LOG_TIMER_STOP(name) {}
- function LOG_XML_PRINT(doc, opt_level) {}
- function LOG_DUMP_FEED(feed) {}
- function VARJ_INC(name) {}
- //------------------------------------------------------------------------------
- // Inline debug functions
- //------------------------------------------------------------------------------
- function DBG_ALERT(msg, opt_prefix) {}
- function DBG_ASSERT(cond, opt_msg) {}
- function DBG_ABSTRACT(obj, fn, args) {}
- function DBG_NOT_IMPLEMENTED(str) {}
- // remove this if you merge
- if (window.GD_Loader) {
- // continue loading
- window.GD_Loader();
- }
- // end
- /* Copyright (c) 2006 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /** @filecomment
- * transport.js provides the basic server related operations:
- * get a feed, create an item, update & delete an item
- * we abstract this out, so that a unit test framework based on FILE transport
- * can be developed later
- */
- /** returns the appropriate transportobject based on
- * the passed in type
- */
- function GD_getTransport(transportType) {
- switch (transportType) {
- case GD_Transport.TRANSPORT_FILE:
- // File Transport object
- DBG_ALERT("File transport NYI");
- return null;
- case GD_Transport.TRANSPORT_HTTP:
- return new GD_HttpTransport();
- case GD_Transport.TRANSPORT_GOOGLECALENDAR:
- return new GD_GaiaTransport("cl");
- default:
- return new GD_GaiaTransport("cl");
- }
- }
- /**
- * base transport, for the common things in life
- */
- function GD_Transport() {
- this.userName_ = null;
- this.pwd_ = null;
- }
- GD_Transport.TRANSPORT_FILE = 1;
- GD_Transport.TRANSPORT_HTTP = 2;
- GD_Transport.TRANSPORT_GOOGLECALENDAR = 3;
- GD_Transport.AUTH_HANDLER = "https://www.google.com/accounts/ClientLogin";
- GD_Transport.AUTH_HEADER = "Authorization";
- GD_Transport.AUTH_HEADERVALUE = "GoogleLogin auth=";
- GD_Transport.AUTH_TOKEN = "Auth";
- GD_Transport.AUTH_SERVICE = "service=";
- GD_Transport.AUTH_SOURCE = "source=desktop_Feed_reader";
- GD_Transport.AUTH_USER = "Email=";
- GD_Transport.AUTH_PWD = "Passwd=";
- GD_Transport.METHOD_OVERRIDE = "X-HTTP-Method-Override";
- GD_Transport.NOREDIRECT_HEADER = "X-If-No-Redirect";
- GD_Transport.SETCOOKIE_HEADER = "Set-Cookie";
- GD_Transport.COOKIE_HEADER = "Cookie";
- /**
- * set method GD_Transport.userName_
- */
- GD_Transport.prototype.setUserName = function(stringValue) {
- this.userName_ = stringValue;
- };
- /**
- * get method GD_Transport.userName_
- */
- GD_Transport.prototype.getUserName = function() {
- return this.userName_;
- };
- /**
- * set method GD_Transport.pwd_
- */
- GD_Transport.prototype.setPassword = function(stringValue) {
- this.pwd_ = stringValue;
- };
- /**
- * get method GD_Transport.pwd_
- */
- GD_Transport.prototype.getPassword = function() {
- return this.pwd_;
- };
- /**
- * prototype for the GetDocument method
- * @param docUri {string} the URI of the XML document to load
- * @param opt_callback {function} takes one argument,
- * the loaded XMLDocument for the documentUri
- */
- GD_Transport.prototype.GetDomDocument = function(docUri, opt_callback) {};
- /**
- * prototype for the InsertEntry method
- */
- GD_Transport.prototype.InsertEntry = function(feed, entry) {
- };
- /**
- * prototype for the UpdateEntry method
- */
- GD_Transport.prototype.UpdateEntry = function(entry) {
- };
- /**
- * prototype for the DeleteEntry method
- */
- GD_Transport.prototype.DeleteEntry = function(entry) {
- };
- /**
- * file transport section, prototype only
- */
- function GD_FileTransport() {
- GD_Transport.call(this);
- }
- UTIL_inherits(GD_FileTransport, GD_Transport);
- GD_FileTransport.prototype.toString = function(opt_verbose) {
- return "GD_FileTransport()";
- };
- /**
- * http transport
- */
- function GD_HttpTransport() {
- GD_Transport.call(this);
- this.shardingCookie_ = null;
- }
- UTIL_inherits(GD_HttpTransport, GD_Transport);
- GD_HttpTransport.prototype.toString = function(opt_verbose) {
- return "GD_HttpTransport()";
- };
- /**
- * set method GD_HttpTransport.shardingCookie_
- */
- GD_HttpTransport.prototype.setCookie = function(str) {
- this.shardingCookie_ = str;
- };
- /**
- * get method GD_HttpTransport.shardingCookie_
- */
- GD_HttpTransport.prototype.getCookie = function() {
- return this.shardingCookie_;
- };
- /**
- * gets the DomDocument for the HTTP transport
- * @param docUri {string} the URI of the XML document to load
- * @param opt_callback {function} takes one argument,
- * the loaded XMLDocument for the documentUri
- */
- GD_HttpTransport.prototype.GetDomDocument = function(docUri, opt_callback) {
- LOG_TRACE(docUri, "GD_HttpTransport => GetDomDocument", null);
- var callback = (opt_callback)
- ? function(request) { opt_callback.call(null, request.responseXML); }
- : undefined;
- var params = new XmlHttpParameters("GET", docUri, 200, null, callback);
- this.CallXmlHttp(params);
- };
- /**
- * DeleteEntry for the HTTP transport,
- * gets the target URI from the passed in entry
- */
- GD_HttpTransport.prototype.DeleteEntry = function(gdEntry, opt_callBack) {
- var deleteUri = gdEntry.getEditUri();
- LOG_TRACE(deleteUri, "GD_HttpTransport => DeleteEntry", null);
- var params = new XmlHttpParameters("DELETE", deleteUri, 204,
- null, opt_callBack);
- this.CallXmlHttp(params);
- };
- /**
- * UpdateEntry for the HTTP transport,
- * gets the target URI from the passed in entry
- */
- GD_HttpTransport.prototype.UpdateEntry = function(gdEntry, opt_callBack) {
- var updateUri = gdEntry.getEditUri();
- LOG_TRACE(updateUri, "GD_HttpTransport => UpdateEntry", null);
- var params = new XmlHttpParameters("PUT", updateUri, -1,
- gdEntry.save(), opt_callBack);
- this.CallXmlHttp(params);
- };
- /**
- * InsertEntry for the HTTP transport,
- * sets the target URI from the passed in feed
- */
- GD_HttpTransport.prototype.InsertEntry = function(ofFeed, gdEntry,
- opt_callBack) {
- var insertUri = ofFeed.getPostUri();
- if (insertUri != null) {
- LOG_TRACE(insertUri, "GD_HttpTransport => InsertEntry", null);
- var params = new XmlHttpParameters("POST", insertUri, -1,
- gdEntry.save(), opt_callBack);
- this.CallXmlHttp(params);
- }
- else {
- throw "this is a read only feed";
- }
- };
- /**
- * SetHeaders for the HTTP transport,
- * just sets the accept content type here
- */
- GD_HttpTransport.prototype.SetHeaders = function(xmlHttpRequestObject) {
- LOG_TRACE("Entering...", "HTTP.SetHeaders", null);
- xmlHttpRequestObject.setRequestHeader("Accept", "text/xml");
- xmlHttpRequestObject.setRequestHeader("content-type", "application/atom+xml");
- xmlHttpRequestObject.setRequestHeader("Cache-Control", "no-cache");
- };
- /**
- * @param params {XmlHttpParameters}
- */
- GD_HttpTransport.prototype.PrepareAuthentication = function(params) {
- this.DoRequest(params);
- };
- /**
- * Bundle a collection of parameters that are used when making an
- * XMLHttpRequest. The state of a transaction should be stored in
- * the XmlHttpParameters rather than the GD_HttpTransport, as the
- * GD_HttpTransport is likely a singleton, so requests it facilitates
- * should not share state.
- *
- * @param verb {string} must be "GET" or "DELETE" or "PUT" or "POST"
- * @param uri {string}
- * @param allowedStatus {int}
- * @param payload {string?}
- * @param opt_callback {function} will receive one argument,
- * the loaded XMLHttpRequest, and its return value will be ignored
- * @param opt_retrying {boolean} indicates whether this is a retry attempt;
- * defaults to false
- */
- function XmlHttpParameters(verb, uri, allowedStatus, payload,
- opt_callback, opt_retrying) {
- this.verb = verb;
- this.uri = uri;
- this.allowedStatus = allowedStatus;
- this.payload = payload;
- this.callback = opt_callback;
- this.retrying = !!opt_retrying;
- }
- /**
- * method do create the actual request. This one just calls PrepareAuth
- * which will continue calling DoRequest after auth is done
- * @param params {XmlHttpParameters}
- */
- GD_HttpTransport.prototype.CallXmlHttp = function(params) {
- try {
- LOG_TRACE("Entering...", "CallXmlHttp", null);
- this.PrepareAuthentication(params);
- } catch (e) {
- DBG_ALERT(e.toString());
- DBG_ALERT('The exception happened in CallXmlHttp for ' + params.verb +
- ' at URI: ' + params.uri);
- throw e;
- }
- LOG_TRACE("Exiting...", "CallXmlHttp", null);
- };
- /**
- * @param params {XmlHttpParameters}
- */
- GD_HttpTransport.prototype.DoRequest = function(params) {
- try {
- LOG_TRACE("Entering...", "CallXmlHttp", null);
-
- var xmlRequest = XMLX_getHttpRequest();
- var fAsync = false;
- if (params.callback) {
- xmlRequest.onreadystatechange =
- function() { GD_HandleAsyncData(xmlRequest, params); };
- fAsync = true;
- }
- if (DEBUG_CROSSSITE()) {
- netscape.security.PrivilegeManager.
- enablePrivilege("UniversalBrowserRead");
- netscape.security.PrivilegeManager.
- enablePrivilege("UniversalBrowserWrite");
- }
-
- LOG_TRACE("Using default XlmRequest open", "CallXmlHttp", null);
- xmlRequest.open(params.verb, params.uri, fAsync);
- // thwart the cache
- if (params.verb == "GET") {
- xmlRequest.setRequestHeader("If-Modified-Since",
- "Sat, 1 Jan 2000 00:00:00 GMT");
- }
- this.SetHeaders(xmlRequest);
- if (xmlRequest.overrideMimeType) {
- //only need to do this for mozilla based browsers. IE would throw
- xmlRequest.overrideMimeType('text/xml');
- }
- LOG_TRACE("Sending..." + xmlRequest, "CallXmlHttp", null);
- xmlRequest.send(params.payload);
- LOG_TRACE("Returned, readyState:" + xmlRequest.readyState,
- "CallXmlHttp", null);
- if (fAsync === false) {
- this.HandleOnXmlRequestFinished(xmlRequest, params);
- }
- } catch (e) {
- DBG_ALERT(e.toString());
- DBG_ALERT('The exception happened in CallXmlHttp for ' + params.verb_ +
- ' at URI: ' + params.uri_);
- throw e;
- }
- LOG_TRACE("Exiting...", "CallXmlHttp", null);
- };
- /**
- * async callback method
- */
- function GD_HandleAsyncData(request, params) {
- if (request.readyState != 4) {
- return;
- }
- LOG_TRACE("Callback done...", "GD_HandleAsyncData", null);
- GoogleDataFactory.getTransport().
- HandleOnXmlRequestFinished(request, params);
- }
- /**
- * handling of errors/results of the xmlhttprequest
- */
- GD_HttpTransport.prototype.HandleOnXmlRequestFinished =
- function(xmlRequest, params) {
- switch (xmlRequest.readyState) {
- case 1:
- case 2:
- case 3:
- DBG_ALERT('Bad Ready State: ' + xmlRequest.status);
- return false;
- case 4:
- if (xmlRequest.status > 299 ||
- (params.allowedStatus !== -1 &&
- xmlRequest.status != params.allowedStatus)) {
- if (params.retrying !== true) {
- /*
- * this might be a redirect/sharding issue, redo the whole thing
- * in javascript you can not handle the 302 return yourself,
- * so what happens is we get our auth token, we try to go to the
- * server, we get a redirect back now the redirect does not redo
- * the "postdata" nor the custom auth header, so it fails with
- * 401. Hence, redo it. But only once.
- */
- if (xmlRequest.status === 401) {
- params = true;
- this.CallXmlHttp(params);
- } else if (xmlRequest.status === 412) {
- // the server want's to redirect us to a shard !!
- // get the cookie and save it, then retry
- var cookie = xmlRequest.getResponseHeader(
- GD_Transport.SETCOOKIE_HEADER);
- DBG_ASSERT(cookie != null,
- "we got no cookie back from the redirection server");
-
- if (cookie != null) {
- this.setCookie(cookie);
- }
- // now retry
- params.retrying = true;
- this.CallXmlHttp(params);
- }
- }
- DBG_ALERT('Request resulted in a bad status code for the operation: '
- + xmlRequest.status + " " + params.allowedStatus);
- }
- break;
- }
- if (params.callback) {
- LOG_TRACE("calling callback to feed code", "HandleOnXmlRequestFinished",
- null);
- params.callback.call(null, xmlRequest);
- }
- }
- /**
- * Gaia authentication transport.
- */
- function GD_GaiaTransport(serviceName) {
- GD_HttpTransport.call(this);
- this.serviceName_ = serviceName;
- this.gaiaToken_ = null;
- }
- UTIL_inherits(GD_GaiaTransport, GD_HttpTransport);
- GD_GaiaTransport.prototype.toString = function(opt_verbose) {
- return "GD_GaiaTransport() for " + this.serviceName_;
- };
- /**
- * set method GD_GaiaTransport.gaiaToken_
- */
- GD_GaiaTransport.prototype.setToken = function(string) {
- this.gaiaToken_ = string;
- };
- /**
- * get method GD_GaiaTransport.gaiaToken_
- */
- GD_GaiaTransport.prototype.getToken = function() {
- if (/\r|\n/.test(this.gaiaToken_) === true) {
- alert("potential attack pattern");
- }
- return this.gaiaToken_;
- };
- /**
- * function to get an authtoken from the gaia servers
- * @param params {XmlHttpParameters}
- */
- GD_GaiaTransport.prototype.QueryAuthToken = function(params) {
- // Create a new request to the authentication URL.
- LOG_TRACE("Entering...", "QueryAuthToken()", null);
- var token = null;
- try {
- var xmlRequest = XMLX_getHttpRequest();
- var response = null;
- // construct the payload
-
- var postData = GD_Transport.AUTH_USER +
- encodeURIComponent(this.getUserName()) + "&" +
- GD_Transport.AUTH_PWD +
- encodeURIComponent(this.getPassword()) + "&" +
- GD_Transport.AUTH_SOURCE + "&" +
- GD_Transport.AUTH_SERVICE +
- encodeURIComponent(this.serviceName_);
- LOG_TRACE("postData = " + postData, "QueryAuthToken()", null);
- var responseData = null;
-
- var request = xmlRequest;
- xmlRequest.onreadystatechange = function() {
- GD_GaiaHandleAsyncData(request, params);
- }
-
- xmlRequest.open("POST", GD_Transport.AUTH_HANDLER, true);
- xmlRequest.setRequestHeader("content-type",
- "application/x-www-form-urlencoded");
- LOG_TRACE("sending..." + xmlRequest, "QueryAuthToken()", null);
- xmlRequest.send(postData);
- } catch (e) {
- DBG_ALERT(e.toString());
- DBG_ALERT('The exception happened in QueryAuthData');
- }
- };
- /**
- * callback for the QueryAuthToken
- * @param params {XmlHttpParameters}
- */
- function GD_GaiaHandleAsyncData(request, params) {
- LOG_TRACE("Entering callback..", "GD_GaiaHandleAsyncData", null);
- if (request.readyState != 4) {
- return;
- }
- GoogleDataFactory.getTransport().HandleOnQueryAuthRequestFinished(
- request, params);
- LOG_TRACE("QueryAuthCallback done...", "GD_GaiaHandleAsyncData", null);
- }
- /**
- * handling of errors/results of the xmlhttprequest
- * @param request {XMLHttpRequest}
- * @param params {XmlHttpParameters}
- */
- GD_GaiaTransport.prototype.HandleOnQueryAuthRequestFinished = function(
- request, params) {
- var response = null;
- var token = null;
- LOG_TRACE("Entering...", "HandleOnQueryAuthRequestFinished", null);
- if (request.status > 299) {
- DBG_ALERT('Bad server response during auth operation: ' +
- this.xmlRequest.status);
- DBG_ALERT('body: ' + params.postData);
- } else {
- response = request.responseText;
- }
- DBG_ASSERT(response !== null,
- "we got no data back from the authentication server");
-
- if (response !== null) {
- // now parse the result
- var result = response.match(/^Auth=(.*)/m);
- DBG_ASSERT(result !== null);
- DBG_ASSERT(result[0] !== null);
- DBG_ASSERT(result[1] !== null);
- if (result !== null) {
- token = result[1];
- }
- }
- LOG_TRACE(token, "HandleOnQueryAuthRequestFinished()", null);
- this.setToken(token);
- // stack needs to unwind, as you can not call from XmlHttp.onreadystate
- // into another xmlHttp object
- window.setTimeout(function() { GD_GaiaContinue(params) }, 0);
- LOG_TRACE("exiting...", "HandleOnQueryAuthRequestFinished", null);
- }
- /**
- * callback for the QueryAuthToken
- */
- function GD_GaiaContinue(params) {
- LOG_TRACE("GD_GaiaContinue...", "GD_GaiaContinue", null);
- GoogleDataFactory.getTransport().DoRequest(params);
- }
- /**
- * check if we need to get a token
- * @returns true if we need to get a token
- */
- GD_GaiaTransport.prototype.NeedToken = function() {
- if (UTIL_isPersistable(this.getUserName()) && this.getToken() == null) {
- return true;
- }
- return false;
- };
- /**
- * if a token is needed, will get one and set it
- */
- GD_GaiaTransport.prototype.PrepareAuthentication = function(params) {
- if (this.NeedToken()) {
- this.QueryAuthToken(params);
- } else {
- this.DoRequest(params);
- }
- };
- /**
- * sets the auth header for Gaia, if we have a token
- * @param xmlHttpRequestObject object to set the header on
- */
- GD_GaiaTransport.prototype.SetHeaders = function(xmlHttpRequestObject) {
- LOG_TRACE("Entering...", "Gaia.SetHeaders", null);
- xmlHttpRequestObject.setRequestHeader(GD_Transport.NOREDIRECT_HEADER, "1");
- if (this.getCookie() !== null) {
- // set a previously stored shard cookie
- LOG_TRACE("setting a stored cookie..." + this.getCookie(),
- "SetHeaders", null);
- xmlHttpRequestObject.setRequestHeader(GD_Transport.COOKIE_HEADER,
- this.getCookie(), null);
- }
- if (this.getToken() !== null) {
- xmlHttpRequestObject.setRequestHeader(GD_Transport.AUTH_HEADER,
- GD_Transport.AUTH_HEADERVALUE + this.getToken());
- }
- this.Base.SetHeaders(xmlHttpRequestObject);
- };
- /**
- * GD_Factory() is used to access the underlying transport layer
- * one instance of the factory exists as a global
- */
- function GD_Factory() {
- this.transPort_ = GD_getTransport(GD_Transport.TRANSPORT_GOOGLECALENDAR);
- };
- // set method GD_Factory.transPort_
- GD_Factory.prototype.setTransport = function(value) {
- this.transPort_ = value;
- };
- // get method GD_Factory.transPort_
- GD_Factory.prototype.getTransport = function() {
- return this.transPort_;
- };
- /**
- * declaration of our global GoogleDataFactory, so that the rest
- * of the code can share this...
- */
- var GoogleDataFactory = new GD_Factory();
- // remove this if you merge
- if (window.GD_Loader) {
- // continue loading
- window.GD_Loader();
- }
- // end
- /* Copyright (c) 2006 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @fileoverview
- * this file relies on detect.js to do the browser detection
- * it abstracts a minimal set of xpath methods so that
- * you can do cross browser xpath
- * the code seems to work fine in IE6+, Mozilla, Opera8.5+ and Safari2.0+
- */
- var XMLX_ieProgId_ = undefined;
- /** initialize XMLX_ieProgId_ */
- (function () {
- // Nobody (on the web) is really sure which of the progid's listed is totally
- // necessary. It is known, for instance, that certain installations of IE
- // will not work with only Microsoft.XMLHTTP, as well as with MSXML2.XMLHTTP.
- // Safest course seems to be to do this -- include all known progids for
- // XmlHttp.
- if (typeof XMLHttpRequest == 'undefined' &&
- typeof ActiveXObject != 'undefined') {
- var activeXIdents = [
- "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0",
- "MSXML2.XMLHTTP", "MICROSOFT.XMLHTTP.1.0", "MICROSOFT.XMLHTTP.1",
- "MICROSOFT.XMLHTTP" ];
- for (var i = 0; i < activeXIdents.length; ++i) {
- var candidate = activeXIdents[i];
- try {
- new ActiveXObject(candidate);
- XMLX_ieProgId_ = candidate;
- return;
- } catch (e) {
- // do nothing; try next choice
- }
- }
- // couldn't find any matches
- throw ("Could not create ActiveXObject. ActiveX might be disabled, or " +
- "msxml might not be installed");
- }
- })();
- /**
- * helper function to return the correct XMLHttpRequest() object
- */
- function XMLX_getHttpRequest() {
- if (XMLX_ieProgId_ !== undefined) {
- return new ActiveXObject(XMLX_ieProgId_);
- } else {
- return new XMLHttpRequest();
- }
- };
- /**
- * helper to return a brand new XMLDomDocument
- */
- function XMLX_createDomDocument(optXml) {
- var doc = null;
- if (optXml) {
- if (typeof(optXml) != 'string') {
- optXml = XMLX_serializeNode(optXml);
- }
- }
- if (document.implementation.createDocument) {
- if (optXml) {
- var domParser = new DOMParser();
- doc = domParser.parseFromString(optXml, "text/xml");
- } else {
- doc = document.implementation.createDocument("", "", null);
- }
- } else {
- doc = new ActiveXObject("MSXML.DOMDocument");
- if (optXml) {
- doc.loadXML(optXml);
- }
- }
- DBG_ASSERT(doc !== null, "Could not create a DOM document");
- return doc;
- };
- /**
- * helper to serialize an xmlnode into text
- */
- function XMLX_serializeNode(xmlNode) {
- var text = null;
- try {
- // this is for Gecko based browsers
- LOG_TRACE("Trying serializer", "XMLX_serializeNode", null);
- var xmlSerializer = new XMLSerializer();
- // safari 2.02 seems to work fine with this.
- text = xmlSerializer.serializeToString(xmlNode);
- }
- catch ( e ) {
- try {
- // Internet Explorer.
- LOG_TRACE("Trying IE way to serialize", "XMLX_serializeNode", null);
- text = xmlNode.xml;
- }
- catch (e) {
- // everyone else
- LOG_TRACE("Trying UTIL_xmlText(node)", "XMLX_serializeNode", null);
- text = UTIL_xmlText(xmlNode);
- }
- }
- return text;
- };
- /**
- * encapsulates the xpath methods we are exposing
- */
- function XMLX_getNodes(xmlNode, xpath) {
- DBG_ASSERT(xmlNode, "XML_getNodes: " + xpath);
- LOG_DEBUG("XML_getNodes: " + xpath);
- try {
- var document;
- if (xmlNode.ownerDocument !== null) {
- document = xmlNode.ownerDocument;
- } else {
- document = xmlNode;
- }
- if (Detect.XPathSupport()) {
- if (Detect.IE_5_5_newer()) {
- LOG_DEBUG("XML_getNodes: Inside IE5.5 path");
- document.setProperty("SelectionLanguage", "XPath");
- document.setProperty("SelectionNamespaces", XML.NAMESPACES);
- return xmlNode.selectNodes(xpath);
- } else {
- LOG_DEBUG("XML_getNodes: Inside MOZILLA path");
- var nsResolver = function(prefix){
- var s = XML.NAMESPACE_MAP[prefix];
- if (s) {
- return s;
- } else {
- throw "Unknown prefix: '" + prefix+"'";
- }
- };
-
-
- var tempResult = document.evaluate(xpath, xmlNode, nsResolver,
- XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
-
- // now make it an array
- var nodeList = new Array(tempResult.snapshotLength);
- for (var i = 0; i < nodeList.length; ++i) {
- nodeList[i] = tempResult.snapshotItem(i);
- }
-
- return nodeList;
- }
- } else {
- return XMLX_parse(xmlNode, xpath);
- }
- } catch (e) {
- // LOG_XML_PRINT(xmlNode);
- // this is everyone else
- LOG_DEBUG("XML_getNodes: catch path - " + e.toString());
- return XMLX_parse(xmlNode, xpath);
- }
- };
- /**
- * helper method to call the javascript parser
- */
- function XMLX_parse(xmlNode, xpath) {
- LOG_DEBUG("XML_parse: no xpath support, hence i am here");
- var expr = xpathParse(xpath);
- var ctx = new ExprContext(xmlNode);
- var ret = expr.evaluate(ctx);
- return ret.nodeSetValue();
- }
- /**
- * @param xmlNode the node to search from
- * @xpath the xpath to search for
- * @return the first xmlnode found
- */
- function XMLX_getNode(xmlNode, xpath) {
- var result = XMLX_getNodes(xmlNode, xpath);
- if (result) {
- return result[0];
- }
- return null;
- }
- // remove this if you merge
- if (window.GD_Loader) {
- // continue loading
- window.GD_Loader();
- }
- // end
- /* Copyright (c) 2006 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- // All Rights Reserved
- //
- // An XPath parser and evaluator written in JavaScript. The
- // implementation is complete except for functions handling
- // namespaces.
- //
- // Reference: [XPATH] XPath Specification
- // <http://www.w3.org/TR/1999/REC-xpath-19991116>.
- //
- //
- // The API of the parser has several parts:
- //
- // 1. The parser function xpathParse() that takes a string and returns
- // an expession object.
- //
- // 2. The expression object that has an evaluate() method to evaluate the
- // XPath expression it represents. (It is actually a hierarchy of
- // objects that resembles the parse tree, but an application will call
- // evaluate() only on the top node of this hierarchy.)
- //
- // 3. The context object that is passed as an argument to the evaluate()
- // method, which represents the DOM context in which the expression is
- // evaluated.
- //
- // 4. The value object that is returned from evaluate() and represents
- // values of the different types that are defined by XPath (number,
- // string, boolean, and node-set), and allows to convert between them.
- //
- // These parts are near the top of the file, the functions and data
- // that are used internally follow after them.
- //
- function assert(condition) { DBG_ASSERT(condition); }
- // The entry point for the parser.
- //
- // @param expr a string that contains an XPath expression.
- // @return an expression object that can be evaluated with an
- // expression context.
- // right now, only used for safari
- function xpathParse(expr) {
- LOG_DEBUG('XPath parse ' + expr);
- xpathParseInit();
- var cached = xpathCacheLookup(expr);
- if (cached) {
- LOG_DEBUG('XPath ....cached ');
- return cached;
- }
- // Optimize for a few common cases: simple attribute node tests
- // (@id), simple element node tests (page), variable references
- // ($address), numbers (4), multi-step path expressions where each
- // step is a plain element node test
- // (page/overlay/locations/location).
-
- if (expr.match(/^(\$|@)?\w+$/i)) {
- var ret = makeSimpleExpr(expr);
- xpathParseCache[expr] = ret;
- LOG_DEBUG('XPath ... simple');
- return ret;
- }
- if (expr.match(/^\w+(\/\w+)*$/i)) {
- ret = makeSimpleExpr2(expr);
- xpathParseCache[expr] = ret;
- LOG_DEBUG('XPath ... simple 2 ');
- return ret;
- }
- var cachekey = expr; // expr is modified during parse
- var stack = [];
- var ahead = null;
- var previous = null;
- var done = false;
- var parse_count = 0;
- var lexer_count = 0;
- var reduce_count = 0;
-
- while (!done) {
- parse_count+=1;
- expr = expr.replace(/^\s*/, '');
- previous = ahead;
- ahead = null;
- var rule = null;
- var match = '';
- for (var i = 0; i < xpathTokenRules.length; ++i) {
- var result = xpathTokenRules[i].re.exec(expr);
- lexer_count+=1;
- if (result && result.length > 0 && result[0].length > match.length) {
- rule = xpathTokenRules[i];
- match = result[0];
- break;
- }
- }
- // Special case: allow operator keywords to be element and
- // variable names.
- // NOTE: The parser resolves conflicts by looking ahead,
- // and this is the only case where we look back to
- // disambiguate. So this is indeed something different, and
- // looking back is usually done in the lexer (via states in the
- // general case, called "start conditions" in flex(1)). Also,the
- // conflict resolution in the parser is not as robust as it could
- // be, so I'd like to keep as much off the parser as possible (all
- // these precedence values should be computed from the grammar
- // rules and possibly associativity declarations, as in bison(1),
- // and not explicitly set.
- if (rule &&
- (rule == TOK_DIV ||
- rule == TOK_MOD ||
- rule == TOK_AND ||
- rule == TOK_OR) &&
- (!previous ||
- previous.tag == TOK_AT ||
- previous.tag == TOK_DSLASH ||
- previous.tag == TOK_SLASH ||
- previous.tag == TOK_AXIS ||
- previous.tag == TOK_DOLLAR)) {
- rule = TOK_QNAME;
- }
- if (rule) {
- expr = expr.substr(match.length);
- LOG_DEBUG('XPath token: parse ' + match + ' -- ' + rule.label);
- ahead = {
- tag: rule,
- match: match,
- prec: rule.prec ? rule.prec : 0, // || 0 is removed by the compiler
- expr: makeTokenExpr(match) };
- } else {
- LOG_DEBUG('XPath DONE');
- done = true;
- }
- while (xpathReduce(stack, ahead)) {
- reduce_count+=1;
- LOG_DEBUG('XPATH stack: ' + stackToString(stack));
- }
- }
- LOG_DEBUG('XPATH stack:' + stackToString(stack));
- if (stack.length != 1) {
- throw 'XPath parse error ' + cachekey + ':\n' + stackToString(stack);
- }
- result = stack[0].expr;
- xpathParseCache[cachekey] = result;
- LOG_DEBUG('XPath parse: ' + parse_count + ' / ' +
- lexer_count + ' / ' + reduce_count);
- return result;
- }
- var xpathParseCache = {};
- function xpathCacheLookup(expr) {
- return xpathParseCache[expr];
- }
- function xpathReduce(stack, ahead) {
- var cand = null;
- if (stack.length > 0) {
- var top = stack[stack.length-1];
- var ruleset = xpathRules[top.tag.key];
- if (ruleset) {
- for (var i = 0; i < ruleset.length; ++i) {
- var rule = ruleset[i];
- var match = xpathMatchStack(stack, rule[1]);
- if (match.length) {
- cand = {
- tag: rule[0],
- rule: rule,
- match: match };
- cand.prec = xpathGrammarPrecedence(cand);
- break;
- }
- }
- }
- }
- var ret;
- if (cand && (!ahead || cand.prec > ahead.prec ||
- (ahead.tag.left && cand.prec >= ahead.prec))) {
- for (i = 0; i < cand.match.matchlength; ++i) {
- stack.pop();
- }
- LOG_DEBUG('reduce ' + cand.tag.label + ' ' + cand.prec +
- ' ahead ' + (ahead ? ahead.tag.label + ' ' + ahead.prec +
- (ahead.tag.left ? ' left' : '') : ' none '));
- var matchexpr = UTIL_mapExpr(cand.match, function(m) { return m.expr; });
- cand.expr = cand.rule[3].apply(null, matchexpr);
- stack.push(cand);
- ret = true;
- } else {
- if (ahead) {
- LOG_DEBUG('shift ' + ahead.tag.label + ' ' + ahead.prec +
- (ahead.tag.left ? ' left' : '') +
- ' over ' + (cand ? cand.tag.label + ' ' +
- cand.prec : ' none'));
- stack.push(ahead);
- }
- ret = false;
- }
- return ret;
- }
- function xpathMatchStack(stack, pattern) {
- // NOTE: The stack matches for variable cardinality are
- // greedy but don't do backtracking. This would be an issue only
- // with rules of the form A* A, i.e. with an element with variable
- // cardinality followed by the same element. Since that doesn't
- // occur in the grammar at hand, all matches on the stack are
- // unambiguous.
- var S = stack.length;
- var P = pattern.length;
- var p, s;
- var match = [];
- match.matchlength = 0;
- var ds = 0;
- for (p = P - 1, s = S - 1; p >= 0 && s >= 0; --p, s -= ds) {
- ds = 0;
- var qmatch = [];
- if (pattern[p] == Q_MM) {
- p -= 1;
- match.push(qmatch);
- while (s - ds >= 0 && stack[s - ds].tag == pattern[p]) {
- qmatch.push(stack[s - ds]);
- ds += 1;
- match.matchlength += 1;
- }
- } else if (pattern[p] == Q_01) {
- p -= 1;
- match.push(qmatch);
- while (s - ds >= 0 && ds < 2 && stack[s - ds].tag == pattern[p]) {
- qmatch.push(stack[s - ds]);
- ds += 1;
- match.matchlength += 1;
- }
- } else if (pattern[p] == Q_1M) {
- p -= 1;
- match.push(qmatch);
- if (stack[s].tag == pattern[p]) {
- while (s - ds >= 0 && stack[s - ds].tag == pattern[p]) {
- qmatch.push(stack[s - ds]);
- ds += 1;
- match.matchlength += 1;
- }
- } else {
- return [];
- }
- } else if (stack[s].tag == pattern[p]) {
- match.push(stack[s]);
- ds += 1;
- match.matchlength += 1;
- } else {
- return [];
- }
- UTIL_reverseInplace(qmatch);
- qmatch.expr = UTIL_mapExpr(qmatch, function(m) { return m.expr; });
- }
- UTIL_reverseInplace(match);
- if (p == -1) {
- return match;
- } else {
- return [];
- }
- }
- function xpathTokenPrecedence(tag) {
- return tag.prec || 2;
- }
- function xpathGrammarPrecedence(frame) {
- var ret = 0;
- if (frame.rule) { /* normal reduce */
- if (frame.rule.length >= 3 && frame.rule[2] >= 0) {
- ret = frame.rule[2];
- } else {
- for (var i = 0; i < frame.rule[1].length; ++i) {
- var p = xpathTokenPrecedence(frame.rule[1][i]);
- ret = Math.max(ret, p);
- }
- …
Large files files are truncated, but you can click here to view the full file