You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xap-commits@incubator.apache.org by bb...@apache.org on 2007/01/04 22:51:24 UTC

svn commit: r492796 - in /incubator/xap/trunk/src/xap/xml: ParserFactory.js firefox/ firefox/Parser.js firefox/ParserException.js firefox/ParserExceptionRes.js

Author: bbuffone
Date: Thu Jan  4 14:51:23 2007
New Revision: 492796

URL: http://svn.apache.org/viewvc?view=rev&rev=492796
Log:
added native firefox and mozilla parser.

Added:
    incubator/xap/trunk/src/xap/xml/firefox/
    incubator/xap/trunk/src/xap/xml/firefox/Parser.js
    incubator/xap/trunk/src/xap/xml/firefox/ParserException.js
    incubator/xap/trunk/src/xap/xml/firefox/ParserExceptionRes.js
Modified:
    incubator/xap/trunk/src/xap/xml/ParserFactory.js

Modified: incubator/xap/trunk/src/xap/xml/ParserFactory.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/xml/ParserFactory.js?view=diff&rev=492796&r1=492795&r2=492796
==============================================================================
--- incubator/xap/trunk/src/xap/xml/ParserFactory.js (original)
+++ incubator/xap/trunk/src/xap/xml/ParserFactory.js Thu Jan  4 14:51:23 2007
@@ -22,6 +22,7 @@
 Xap.require("xap.xml.sax.SaxContentHandler") ;
 Xap.require("xap.xml.sax.SaxParser") ;
 Xap.require("xap.xml.ie.Parser"); 
+Xap.require("xap.xml.firefox.Parser"); 
 
 
 xap.xml.ParserFactory = function(){
@@ -34,6 +35,10 @@
 xap.xml.ParserFactory.getParser = function(){
 	if (dojo.render.html.ie){
 		var parser = new xap.xml.ie.Parser();
+		return parser;
+	}else if (document.implementation && 
+				document.implementation.createDocument){
+		var parser = new xap.xml.firefox.Parser();
 		return parser;
 	}else{
 	  	var parser = new xap.xml.sax.SaxParser( new xap.xml.sax.SaxContentHandler() );

Added: incubator/xap/trunk/src/xap/xml/firefox/Parser.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/xml/firefox/Parser.js?view=auto&rev=492796
==============================================================================
--- incubator/xap/trunk/src/xap/xml/firefox/Parser.js (added)
+++ incubator/xap/trunk/src/xap/xml/firefox/Parser.js Thu Jan  4 14:51:23 2007
@@ -0,0 +1,158 @@
+/*
+ * Copyright  2006 The Apache Software Foundation.
+ *
+ *  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. 
+ *
+ */
+Xap.provide("xap.xml.firefox.Parser"); 
+Xap.require("xap.xml.dom.Document"); 
+Xap.require("xap.xml.dom.XapElement"); 
+Xap.require("xap.xml.firefox.ParserException");
+
+xap.xml.firefox.Parser = function(){
+}
+
+/**
+ * Called to get a XML parser.  Will determine how which parser to 
+ * use either the Native Firefox parser or the Sax Parser for the rest of the browsers
+ */
+xap.xml.firefox.Parser.prototype.parse = function(xmlContent, baseUrl){
+	var parser=new DOMParser();
+	var nativeDoc = null;
+	try{
+		nativeDoc = parser.parseFromString(xmlContent,"text/xml");
+	}catch(exception){
+		//the current implementation doesn't throw an exception but we will 
+		//check just in case this dumb behavior changes.
+		throw new xap.xml.firefox.ParserException(xap.xml.firefox.ParserException.PARSER_EXCEPTION, 
+					["Parser Exception Occured", exception], null);
+	}
+	return this._parse(nativeDoc, baseUrl);
+}
+
+
+/**
+ * Called to get a XML parser.  Will determine how which parser to 
+ * use either the Native Firefox parser or the Sax Parser for the rest of the browsers
+ */
+xap.xml.firefox.Parser.prototype._parse = function(nativeDoc, baseUrl){
+	
+	//check to see if there was an error parsing the document.
+	//if there was an error in the document it returns a valid document
+	//if the root node name being "parsererror"
+	var errorNamespace = "http://www.mozilla.org/newlayout/xml/parsererror.xml";	
+	if (nativeDoc === null){
+		throw new xap.xml.firefox.ParserException(xap.xml.firefox.ParserException.PARSER_EXCEPTION, 
+					["Document Null"], null) ;
+
+	} else if (nativeDoc.documentElement.nodeName == "parsererror" && 
+			   nativeDoc.documentElement.namespaceURI == errorNamespace){
+        
+        var sourceText = nativeDoc.documentElement.getElementsByTagNameNS(errorNamespace, 'sourcetext')[0];
+      	if (sourceText !== null) {
+        	sourceText = sourceText.firstChild.data;
+		}
+		throw new xap.xml.firefox.ParserException(xap.xml.firefox.ParserException.PARSER_EXCEPTION,
+					[nativeDoc.documentElement.firstChild.data, sourceText], null) ;
+	}else{
+		//create the xap document
+		var xapDocument = new xap.xml.dom.Document( null );
+		
+		//replicate the complete Native Document into a XAP document.	
+		this._importNativeDocument(xapDocument, nativeDoc, baseUrl);	
+		return xapDocument;
+	}	
+}
+
+/**
+ * Function is responsible for taking the native document and create 
+ * XAP document will the same information.  This method will normalize
+ * for any difference between the document.
+ */
+xap.xml.firefox.Parser.prototype._importNativeDocument = function(xapDocument, nativeDoc, baseUrl){
+
+	//create the root element.
+	var xapElement = this._createElement(nativeDoc.documentElement, xapDocument);
+	
+	//copy over the document.
+	this._importNativeElement(xapElement, nativeDoc.documentElement, xapDocument, baseUrl);	
+
+	//store the root element.	
+	xapDocument.setRootElement(xapElement);
+}
+
+/**
+ * Function is responsible for taking the native document and create 
+ * XAP document will the same information.  This method will normalize
+ * for any difference between the document.
+ */
+xap.xml.firefox.Parser.prototype._importNativeElement = function(xapParentElement, nativeElement, xapDocument, baseUrl){	
+	//create move over all the attributes.
+	var length = nativeElement.attributes.length;
+	for (var index = 0; index < length; index++){
+		var attribute = nativeElement.attributes[index];
+		if (attribute.name.indexOf("xmlns") != 0){
+			xapParentElement.setAttribute(attribute.name, 
+									  attribute.value);									  
+		}
+	}
+	
+	if (baseUrl){
+		xapParentElement.baseUrl = baseUrl;
+	}
+	
+	//replicate all the child elements for this native element.
+	length = nativeElement.childNodes.length;
+	for (var index = 0; index < length; index++){
+		var childNativeElement = nativeElement.childNodes[index];
+	   	if (childNativeElement.nodeType == google.DOM_ELEMENT_NODE){
+			var xapElement = this._createElement(childNativeElement, xapDocument);
+			//add the new element to the parent.
+			this._importNativeElement(xapElement, childNativeElement, xapDocument, baseUrl);	
+			xapParentElement.appendChild(xapElement);
+		}else if (childNativeElement.nodeType == google.DOM_TEXT_NODE ||
+				  childNativeElement.nodeType == google.DOM_CDATA_SECTION_NODE){
+			if (childNativeElement.nodeValue != null && 
+				this.trim(childNativeElement.nodeValue) != ""){
+				var childCopy = google.XNode.create(google.DOM_TEXT_NODE,null,childNativeElement.nodeValue,null);
+				xapParentElement.appendChild(childCopy);
+			}
+		}
+	}
+}
+
+xap.xml.firefox.Parser.prototype.trim = function(string) { return string.replace(/^\s+|\s+$/, ''); };
+
+/**
+ * Function is responsible for taking the native document and create 
+ * XAP document will the same information.  This method will normalize
+ * for any difference between the document.
+ */
+xap.xml.firefox.Parser.prototype._createElement = function(nativeElement, xapDocument){	
+	var xapElement = null;
+	if (nativeElement.namespaceURI == null || nativeElement.namespaceURI == ""){
+		xapElement = xapDocument.createElement(nativeElement.nodeName);
+	}else{
+		var prefixIndex = nativeElement.nodeName.indexOf(":");
+		var nodeName = nativeElement.nodeName;
+		var prefix = null;
+		if (prefixIndex != -1){
+			prefix = nodeName.substring(0, prefixIndex);
+			nodeName = nodeName.substring(prefixIndex + 1);
+		}
+		xapElement = xapDocument.createElement(nodeName, nativeElement.namespaceURI, prefix);
+	}
+	return xapElement;
+}
+
+

Added: incubator/xap/trunk/src/xap/xml/firefox/ParserException.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/xml/firefox/ParserException.js?view=auto&rev=492796
==============================================================================
--- incubator/xap/trunk/src/xap/xml/firefox/ParserException.js (added)
+++ incubator/xap/trunk/src/xap/xml/firefox/ParserException.js Thu Jan  4 14:51:23 2007
@@ -0,0 +1,57 @@
+/*
+ * Copyright  2006 The Apache Software Foundation.
+ *
+ *  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.
+ *
+ */
+
+
+Xap.provide('xap.xml.firefox.ParserException'); 
+
+Xap.require("xap.util.XapException");
+
+ 
+ /**
+ * @fileoverview An exception thrown during parsing when using the Native IE parser.
+ * @class An exception that will append the Line and Column numbers of a supplied 
+ * parser to the message. A sample xap.xml.sax.ParserException message might look like this: <br>
+ * "A parser exception occurred (Line: 123 Column: 456)".  The message attending
+ * the exception will be one found via an index into the strings held by 
+ * a xap.xml.sax.ParserExceptionRes instance, suitable for internationalisation there.
+ * <p>
+ * @author ikaplansky
+ * @see xap.xml.firefox.Parser
+ * @see xap.xml.firefox.ParserExceptionRes
+ *
+ * Creates a new xap.xml.sax.ParserException object
+ * @param msgId
+ * @param args
+ * @param cause
+ * @constructor
+ * 
+ */
+xap.xml.firefox.ParserException = function( msgId, args, cause ) {
+	xap.util.XapException.call( this, msgId, args, cause );
+}
+xap.xml.firefox.ParserException.prototype = new xap.util.XapException();   
+
+xap.xml.firefox.ParserException.PARSER_EXCEPTION = "parserException";
+xap.xml.firefox.ParserException.CLASSNAME = "xap.xml.firefox.ParserException" ;
+
+//-----------------------------------------------------------------------
+// Public Methods.
+//-----------------------------------------------------------------------
+xap.xml.firefox.ParserException.prototype.getClassName = function(){	
+	return xap.xml.firefox.ParserException.CLASSNAME ;
+}
+

Added: incubator/xap/trunk/src/xap/xml/firefox/ParserExceptionRes.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/xml/firefox/ParserExceptionRes.js?view=auto&rev=492796
==============================================================================
--- incubator/xap/trunk/src/xap/xml/firefox/ParserExceptionRes.js (added)
+++ incubator/xap/trunk/src/xap/xml/firefox/ParserExceptionRes.js Thu Jan  4 14:51:23 2007
@@ -0,0 +1,30 @@
+/*
+ * Copyright  2006 The Apache Software Foundation.
+ *
+ *  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.
+ *
+ */
+ 
+Xap.provide('xap.xml.ie.ParserExceptionRes'); 
+
+
+
+ /**
+ * @fileoverview A resource file for the xap.xml.sax.ParserException.js.
+ * @class A resource file for the xap.xml.sax.ParserException.js.
+ */
+xap.xml.firefox.ParserExceptionRes = function() {
+	this.messages = {};
+    this.messages[xap.xml.firefox.ParserException.PARSER_EXCEPTION] = "Parse Error\n\rReason: {0}\n\r{1}" ;
+}
+