commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbe...@apache.org
Subject svn commit: r956931 - in /commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf: Transformer.js util/base.js
Date Tue, 22 Jun 2010 15:52:25 GMT
Author: jbeard
Date: Tue Jun 22 15:52:25 2010
New Revision: 956931

URL: http://svn.apache.org/viewvc?rev=956931&view=rev
Log:
Forgot to check in two important modules.

Added:
    commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/Transformer.js
  (with props)
    commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/util/base.js
  (with props)

Added: commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/Transformer.js
URL: http://svn.apache.org/viewvc/commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/Transformer.js?rev=956931&view=auto
==============================================================================
--- commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/Transformer.js
(added)
+++ commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/Transformer.js
Tue Jun 22 15:52:25 2010
@@ -0,0 +1,169 @@
+require.def("src/javascript/scxml/cgf/Transformer",
+["src/javascript/scxml/cgf/util/xml"],
+function(xmlUtil){
+
+	
+
+	return !require.isBrowser ?
+			//assume Rhino
+			(function(){
+				var TransformerFactory = javax.xml.transform.TransformerFactory,
+					XMLReaderFactory = Packages.org.xml.sax.helpers.XMLReaderFactory,
+					ByteArrayOutputStream = java.io.ByteArrayOutputStream,
+					StreamResult = javax.xml.transform.stream.StreamResult,
+					DOMSource = javax.xml.transform.dom.DOMSource,
+					SAXResult = javax.xml.transform.sax.SAXResult,
+					DOMResult = javax.xml.transform.dom.DOMResult;
+
+				//do transforms
+				var tFactory = TransformerFactory.newInstance();
+
+				function transform(xsl,xml,params,output){
+						// Use the DOM Document to define a DOMSource object.
+						var xslDomSource = new DOMSource(xsl);
+
+						// Process the stylesheet DOMSource and generate a Transformer.
+						var transformer = tFactory.newTransformer(xslDomSource);
+
+						//set parameters
+						for(paramName in params){
+							 transformer.setParameter(paramName,params[paramName]);
+						}
+
+						// Use the DOM Document to define a DOMSource object.
+						var xmlDomSource = new DOMSource(xml);
+
+						var toReturn;
+						switch(output){
+							case "xml":
+								// Create an empty DOMResult for the Result.
+								var domResult = new DOMResult();
+								// Perform the transformation, placing the output in the DOMResult.
+								transformer.transform(xmlDomSource, domResult);
+								toReturn = domResult.getNode();
+								break;
+
+							case "text":
+								var baos = new ByteArrayOutputStream();
+								var streamResult = new StreamResult(baos);
+								transformer.transform(xmlDomSource, streamResult);
+								toReturn = String(new java.lang.String(baos.toByteArray()));
+								break;
+						}
+
+						return toReturn;
+				}
+
+				return function(sourceDocument,transformList,params,output){
+					transformList = transformList 
+							&& !(transformList instanceof Array)	? 
+								[transformList]			:
+								transformList;
+
+					var docToTransform = sourceDocument;
+
+					for(var i=0; i < transformList.length-1; i++){	
+						var t = transformList[i];
+						docToTransform  = transform(t,docToTransform,params,"xml");
+					}
+
+					var lastTransform = transformList[transformList.length-1];
+
+					// Parse the source XML, and send the parse events to the
+					// TransformerHandler.
+					var toReturn = transform(lastTransform,docToTransform,params,output);
+		
+					return toReturn;	//string or node
+				}	
+			})()		:
+				window.ActiveXObject 	? 
+				//IE
+				function(sourceDocument,transformList,params,output){
+					transformList = transformList && !transformList.length	? 
+								[transformList]			:
+								transformList;
+
+					//transform to IR
+					var docToTransform = sourceDocument;
+					for(var i=0; i < transformList.length-1; i++){
+						var t = transformList[i];
+
+						var txt = docToTransform.transformNode(t); 
+						docToTransform = xmlUtil.parseFromString(txt);
+					}
+
+					var lastFilter = transformList[transformList.length-1];
+
+					var txt = docToTransform.transformNode(lastFilter ); 
+
+					var toReturn;
+					switch(output){
+						case "text":
+							toReturn = txt;
+							break;
+						case "xml":
+							toReturn = xmlUtil.parseFromString(txt);
+							break;
+					}
+		
+					return toReturn;	//string or node
+				}			:
+				//everything else
+				function(sourceDocument,transformList,params,output){
+					transformList = transformList && !transformList.length	? 
+								[transformList]			:
+								transformList;
+
+					function getProcessorFromStylesheetDocument(d){
+						var processor = new XSLTProcessor();  
+						processor.importStylesheet(d); 
+						return processor;
+					}
+
+
+					function getResultTextFromDoc(d){
+						var resultText;
+						try{
+							//chrome
+							resultText  = d.body.children[0].textContent
+						} catch (e){
+							try {
+								//firefox
+								resultText = d.documentElement.firstChild.textContent;
+							} catch (e) {
+								console.log("Cannot get resultText out of object " + d);
+							}
+						}
+						return resultText;
+					}
+
+					//transform to IR
+					var processors = transformList.map(getProcessorFromStylesheetDocument); 
+	
+					//set parameters
+					processors.forEach(function(p){
+						for(paramName in params){
+							 p.setParameter(null,paramName,params[paramName]);
+						}
+					});
+
+					var docToTransform  = sourceDocument;
+					processors.forEach(function(p){
+						docToTransform = p.transformToDocument(docToTransform);
+					});
+		
+
+					var toReturn;
+					switch(output){
+						case "text":
+							toReturn = getResultTextFromDoc(docToTransform);;
+							break;
+						case "xml":
+							toReturn = docToTransform;
+							break;
+					}
+		
+					return toReturn;	//string or node
+				};
+});
+

Propchange: commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/Transformer.js
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/util/base.js
URL: http://svn.apache.org/viewvc/commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/util/base.js?rev=956931&view=auto
==============================================================================
--- commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/util/base.js
(added)
+++ commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/util/base.js
Tue Jun 22 15:52:25 2010
@@ -0,0 +1,35 @@
+require.def("src/javascript/scxml/cgf/util/base",
+function(){
+	//some compatibility stuff for IE
+	//TODO: move out into another file
+	if (!Array.indexOf) {
+	    Array.prototype.indexOf = function(obj) {
+		for (var i = 0; i < this.length; i++) {
+		    if (this[i] == obj) {
+			return i;
+		    }
+		}
+		return -1;
+	    }
+	}
+
+	if (!Array.map) {
+	    Array.prototype.map = function(fn) {
+		var toReturn = [];
+		for (var i = 0; i < this.length; i++) {
+		    toReturn[i] = fn(this[i]);
+		}
+		return toReturn;
+	    }
+	}
+
+	if (!Array.forEach) {
+	    Array.prototype.forEach = function(fn) {
+		for (var i = 0; i < this.length; i++) {
+		    fn(this[i]);
+		}
+		return undefined;
+	    }
+	}
+});
+

Propchange: commons/sandbox/gsoc/2010/scxml-js/branches/browser-tests.1.ie/src/javascript/scxml/cgf/util/base.js
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message