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 mt...@apache.org on 2006/10/06 17:40:46 UTC

svn commit: r453704 - in /incubator/xap/trunk/src/xap/data: bridge/DecimalFormatterBridge.js bridge/FormatterBridge.js bridge/FormatterChainBridge.js formatter/DecimalFormatter.js

Author: mturyn
Date: Fri Oct  6 10:40:46 2006
New Revision: 453704

URL: http://svn.apache.org/viewvc?view=rev&rev=453704
Log: (empty)

Added:
    incubator/xap/trunk/src/xap/data/bridge/DecimalFormatterBridge.js
    incubator/xap/trunk/src/xap/data/bridge/FormatterBridge.js
    incubator/xap/trunk/src/xap/data/bridge/FormatterChainBridge.js
    incubator/xap/trunk/src/xap/data/formatter/DecimalFormatter.js

Added: incubator/xap/trunk/src/xap/data/bridge/DecimalFormatterBridge.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/data/bridge/DecimalFormatterBridge.js?view=auto&rev=453704
==============================================================================
--- incubator/xap/trunk/src/xap/data/bridge/DecimalFormatterBridge.js (added)
+++ incubator/xap/trunk/src/xap/data/bridge/DecimalFormatterBridge.js Fri Oct  6 10:40:46 2006
@@ -0,0 +1,99 @@
+/*
+ * 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.data.bridge.DecimalFormatterBridge");
+Xap.require("xap.data.bridge.FormatterBridge");
+Xap.require("xap.taghandling.AttributeConversionException");
+Xap.require("xap.xml.InvalidXmlException"); 
+//Xap.require("xap.data.InitializationException") 
+Xap.require("xap.data.formatter.DecimalFormatter");
+Xap.require("xap.data.formatter.JsFormatterImpl");
+
+Xap.require("xap.xml.XmlTokens");
+
+
+
+/**
+ * DecimalFormatterBridge class handles the configuration element for a
+ * DecimalFormatter instance.
+ *
+ * @author ikaplansky
+ */
+xap.data.bridge.DecimalFormatterBridge = function () {
+	xap.data.bridge.FormatterBridge.call(this);
+};
+Xap.setupClassAsSubclassOf("xap.data.bridge.DecimalFormatterBridge", "xap.data.bridge.FormatterBridge");
+	
+	//-----------------------------------------------------------------------
+	// Protected Methods.
+	//-----------------------------------------------------------------------
+/**
+ * @see FormatterBridge#createFormatter()
+ *
+ * @protected
+ * @return {Formatter}
+**/
+xap.data.bridge.DecimalFormatterBridge.prototype.createFormatter = function () {
+	/*String*/
+	var className = this.getElement().getAttribute(xap.xml.XmlTokens.CLASS);
+
+	/*DecimalFormatter*/
+	var formatter = null;
+	try {
+//			if ( className != null &&
+//				 className.trim().length() != 0 ) {
+//				formatter =
+//					xap.data.bridgeDataAttributeConverter.
+//						 toClassInstance( className, DecimalFormatter.class );
+//			} else {
+
+		formatter = new xap.data.formatter.DecimalFormatter();
+
+//			}
+	}
+	catch (e) { // AttributeConversionException
+	alert(e);
+		this.reportInvalidXmlException(xap.data.bridge.XmlDataTokens.CLASS, "decimal formatter", this.getElement(), e);
+	}
+	return formatter;
+};
+/**
+ * @see FormatterBridge#initializeFormatter()
+ *
+ * @protected
+ * @return {void}
+ * @throws InitializationException
+**/
+xap.data.bridge.DecimalFormatterBridge.prototype.initializeFormatter = function () {
+	xap.data.bridge.FormatterBridge.prototype.initializeFormatter.call(this);
+	try {
+		/*Locale*/
+		var locale = this.getLocale();
+		/*String*/
+		var format = this.getFormatPattern();
+		// TODO: Implement using the actual formatting object
+		// in the bridge class, not here---but implement it:
+		/*DecimalFormat*/
+		//var theDecimalFormat = new xap.data.formatter.DecimalFormatter(format);
+		this.getFormatter().setFormat(format);
+	}
+	catch (e) { // InvalidXmlException
+//			throw new InitializationException(
+//					InitializationException.INVALID_XML_MSGID,
+//					DecimalFormatterBridge.class.getName(), e );
+	}
+};
+

Added: incubator/xap/trunk/src/xap/data/bridge/FormatterBridge.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/data/bridge/FormatterBridge.js?view=auto&rev=453704
==============================================================================
--- incubator/xap/trunk/src/xap/data/bridge/FormatterBridge.js (added)
+++ incubator/xap/trunk/src/xap/data/bridge/FormatterBridge.js Fri Oct  6 10:40:46 2006
@@ -0,0 +1,172 @@
+/*
+ * 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.data.bridge.FormatterBridge");
+Xap.require("xap.data.bridge.DataFrameworkBridge");
+Xap.require("xap.util.Exception");
+//Xap.require("xap.client.ExceptionContext") ;
+Xap.require("xap.taghandling.AttributeConversionException");
+Xap.require("xap.xml.InvalidXmlException");
+Xap.require("xap.data.DataServiceImpl");
+//Xap.require("xap.data.InitializationException") ;
+Xap.require("xap.data.formatter.JsFormatterImpl");
+/**
+ * FormatterBridge is a generic formatter bridge which uses
+ * the <code>class</code> attribute to look up an implementation of 
+ * a <code>com.nexaweb.data.Formatter</code> interface, creates
+ * and initializes and registers an instance of it in the formatter 
+ * container with the specified <code>id</code> attribute.
+ *
+ * @author jmargaris
+ */
+xap.data.bridge.FormatterBridge = function () {
+	xap.data.bridge.DataFrameworkBridge.call(this) ;
+};
+Xap.setupClassAsSubclassOf("xap.data.bridge.FormatterBridge", "xap.data.bridge.DataFrameworkBridge");
+	
+//-----------------------------------------------------------------------
+// Constants.
+//-----------------------------------------------------------------------
+/*protected static final String*/
+xap.data.bridge.FormatterBridge.FORMAT_PATTERN_ATTRIBUTE = "format";
+/*protected static final String*/
+xap.data.bridge.FormatterBridge.LOCALE_ATTRIBUTE = "locale";
+	
+//-----------------------------------------------------------------------
+// Instance Variables.
+//-----------------------------------------------------------------------
+/**
+ * @private Formatter
+ **/
+xap.data.bridge.FormatterBridge.prototype._formatter = null;
+	
+//-----------------------------------------------------------------------
+// Public Methods.
+//-----------------------------------------------------------------------
+/**
+ * @public
+ * @return {Formatter}
+ **/
+xap.data.bridge.FormatterBridge.prototype.getFormatter = function () {
+	return this._formatter;
+};
+/**
+ * Creates and initializes a formatter based on the configuration
+ * specified in the formatter element tag.
+ *
+ * @public
+ * @return {void}
+ **/
+xap.data.bridge.FormatterBridge.prototype.init = function () {
+	xap.data.bridge.DataFrameworkBridge.prototype.init.call(this);
+	try {
+		this._formatter = this.createFormatter();
+		this.initializeFormatter();
+	}
+	catch (e) { // InitializationException e
+		this.getSession().handleException(xap.data.DataFramework.DATA_FRAMEWORK_EXCEPTION_TYPE, e);
+	}
+	/*catch () { // InvalidXmlException e
+	this.getSession().handleException( ExceptionContext.INVALID_XML_EXCEPTION, e );
+	} */
+};
+	
+//-----------------------------------------------------------------------
+// Protected Methods.
+//-----------------------------------------------------------------------
+/**
+ * Calls initialize() on the formatter and adds it to the formatterContainer
+ * if the initialize call was successfull.
+ * @throws InitializationException
+ *
+ * @protected
+ * @return {void}
+ * @throws InitializationException 
+ **/
+xap.data.bridge.FormatterBridge.prototype.initializeFormatter = function () {
+	/*String*/
+	var id = this.getElement().getAttribute(xap.data.bridge.XmlDataTokens.ID);
+	this._formatter.initialize(id, this.getSession(), this.getElement());
+	this.getFormatterContainer().addFormatter(id, this._formatter);
+};
+/**
+ * Creates the formatter. The default implementation looks at the 
+ * "class" attribute of the element and returns an instance of
+ * that class. Subclasses should override this method
+ * to return their own formatter implementations.
+ * @return Formatter instance
+ * @throws InvalidXmlException
+ *
+ * @protected
+ * @return {Formatter}
+ * @throws InvalidXmlException 
+ **/
+xap.data.bridge.FormatterBridge.prototype.createFormatter = function () {
+	/*String*/
+	var className = this.getElement().getAttribute(xap.xml.XmlDataTokens.CLASS);
+	/*Formatter*/
+	var formatter = null;
+	try {
+//	formatter = xap.data.bridge.DataAttributeConverter.toClassInstance(
+//									  className, Formatter.class );
+		return formatter;
+	}
+	catch (e) { // AttributeConversionException
+		throw this.constructInvalidXmlException(xap.xml.XmlDataTokens.CLASS, className, this.getElement(), e);
+	}
+};
+/**
+ * Constructs a Locale object from the value of the LOCAL_ATTRIBUTE attribute.
+ * Since most formatters will support specifying locales in one form or 
+ * another it's useful to have this on the base bridge.  An example locale
+ * attribute is specified as follows: locale="en_US"
+ * 
+ * @return Locale instance
+ * @throws InvalidXmlException
+ *
+ * @protected
+ * @return {Locale}
+ * @throws InvalidXmlException
+ **/
+xap.data.bridge.FormatterBridge.prototype.getLocale = function () {
+// TODO: Get real.
+	return {s:"en_us", country:"us"} ;
+
+
+	/*Locale*/
+	var locale = Locale.getDefault();
+	/*String*/
+	var localeAttr = this.getElement().getAttribute(LOCALE_ATTRIBUTE);
+	if (localeAttr != null) {
+		try {
+			locale = xap.data.bridge.DataAttributeConverter.toLocale(localeAttr);
+		}
+		catch (e) { //AttributeConversionException
+			throw this.constructInvalidXmlException(LOCALE_ATTRIBUTE, localeAttr, this.getElement(), e);
+		}
+	}
+	return locale;
+};
+/**
+ * @return The value of the FORMAT_PATTERN_ATTRIBUTE
+ *
+ * @protected
+ * @return {String}
+ **/
+xap.data.bridge.FormatterBridge.prototype.getFormatPattern = function () {
+	return this.getElement().getAttribute(xap.data.bridge.FormatterBridge.FORMAT_PATTERN_ATTRIBUTE);
+};
+

Added: incubator/xap/trunk/src/xap/data/bridge/FormatterChainBridge.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/data/bridge/FormatterChainBridge.js?view=auto&rev=453704
==============================================================================
--- incubator/xap/trunk/src/xap/data/bridge/FormatterChainBridge.js (added)
+++ incubator/xap/trunk/src/xap/data/bridge/FormatterChainBridge.js Fri Oct  6 10:40:46 2006
@@ -0,0 +1,148 @@
+/*
+ * 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.data.bridge.FormatterChainBridge");
+Xap.require("xap.taghandling.AttributeConversionException");
+Xap.require("xap.data.InitializationException");
+Xap.require("xap.data.formatter.JsFormatterImpl");
+Xap.require("xap.data.formatter.FormatterChain");
+//Xap.require("xap.data.formatter.FormatterInitializationException") ;
+Xap.require("xap.xml.dom.XapElement");
+Xap.require("xap.util.Vector");
+/**
+ * FormatterChainBridge is the bridge class for the formatterChain tag used
+ * by the data framework to declare a chain of formatters.
+ * 
+ * @author ikaplansky
+ */
+xap.data.bridge.FormatterChainBridge = function () {
+};
+Xap.setupClassAsSubclassOf("xap.data.bridge.FormatterChainBridge", "xap.data.bridge.FormatterBridge");
+	
+	//-----------------------------------------------------------------------
+	// Constants.
+	//-----------------------------------------------------------------------
+/*private static final String*/
+xap.data.bridge.FormatterChainBridge.FORMATTER_REFERENCE_ELEMENT_NAME = "formatterReference";
+/*private static final String*/
+xap.data.bridge.FormatterChainBridge.FORMATTER_ID_ATTRIBUTE = "formatter";
+	
+	//-----------------------------------------------------------------------
+	// Protected Methods.
+	//-----------------------------------------------------------------------
+/**
+ * @see FormatterBridge#createFormatter()
+ *
+ * @protected
+ * @return {Formatter}
+**/
+xap.data.bridge.FormatterChainBridge.prototype.createFormatter = function () {
+	/*String*/
+	var className = getElement().getAttribute(XmlDataTokens.CLASS);
+	/*FormatterChain*/
+	var formatter = null;
+	try {
+//			if ( className != null &&
+//				 className.trim().length() != 0 ) {
+//				 formatter = xap.data.bridge.DataAttributeConverter.toClassInstance(className, FormatterChain.class );
+//
+//			} else {
+		formatter = new xap.data.formatter.FormatterChain();
+//			}
+	}
+	catch (e) { // AttributeConversionException
+		this.reportInvalidXmlException(XmlDataTokens.CLASS, className, this.getElement(), e);
+	}
+	return formatter;
+};
+/**
+ * @see FormatterBridge#initializeFormatter()
+ *
+ * @protected
+ * @return {void}
+ * @throws InitializationException 
+**/
+xap.data.bridge.FormatterChainBridge.prototype.initializeFormatter = function () {
+	superclass.initializeFormatter.call(this);
+	/*FormatterChain*/
+	var chain = /*((FormatterChain))*/
+	this.getFormatter();
+	/*Vector*/
+	var children = this.getElement().getChildren();
+	for (var i = 0; i < children.size(); i++) {
+		/*Object*/
+		var child = children.elementAt(i);
+		if (child instanceof xap.xml.dom.XapElement) {
+			/*Element*/
+			var formatterRef = /*((Element))*/
+			child;
+			this.appendFormatter(chain, formatterRef);
+		} else {
+//			   throw new FormatterInitializationException(
+//					   FormatterInitializationException.
+//						   ILLEGAL_FORMATTER_CHAIN_CHILD_TYPE_MSGID,
+//						   new String[]{
+//							   child.toString(),
+//							   FORMATTER_REFERENCE_ELEMENT_NAME + " element"
+//					   }); 
+			throw new xap.util.Exception(child.toString());
+		}
+	}
+};
+	
+	//-----------------------------------------------------------------------
+	// Private Methods.
+	//-----------------------------------------------------------------------
+/**
+ * @private
+ * @return {void}
+ *
+ * @param chain{FormatterChain}
+ * @param formatterRef{Element}
+ * @throws InitializationException  
+**/
+xap.data.bridge.FormatterChainBridge.prototype.appendFormatter = function (chain, formatterRef) {
+	if (formatterRef.getLocalName().equals(xap.data.bridge.FormatterChainBridge.FORMATTER_REFERENCE_ELEMENT_NAME)) {
+		/*String*/
+		var formatterId = formatterRef.getAttribute(xap.data.bridge.FormatterChainBridge.FORMATTER_ID_ATTRIBUTE);
+		if (formatterId != null) {
+			/*Formatter*/
+			var f = getFormatterContainer().getFormatter(formatterId);
+			if (f != null) {
+				chain.appendFormatter(f);
+			} else {
+//					throw new FormatterInitializationException(
+//							FormatterInitializationException.
+//												xap.data.bridge.FormatterChainBridge.FORMATTER_IS_NOT_DEFINED_MSGID,
+//												formatterId );
+				throw new xap.util.Exception("appendFormatter:" + formatterId);
+			}
+		} else {
+//				throw new FormatterInitializationException(
+//						FormatterInitializationException.NULL_FORMATTER_ID_MSGID,
+//						formatterRef.toXml( true ));
+			throw new xap.util.Exception("nullFormatter: " + formatterRef.toXml(true));
+		}
+	} else {
+//			throw new FormatterInitializationException(
+//						FormatterInitializationException.
+//								xap.data.bridge.FormatterChainBridge.ILLEGAL_FORMATTER_CHAIN_CHILD_TAG_MSGID,
+//						new String[]{
+//							formatterRef.getLocalName(), xap.data.bridge.FormatterChainBridge.FORMATTER_REFERENCE_ELEMENT_NAME });
+		throw new xap.util.Exception("illegal format");
+	}
+};
+

Added: incubator/xap/trunk/src/xap/data/formatter/DecimalFormatter.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/data/formatter/DecimalFormatter.js?view=auto&rev=453704
==============================================================================
--- incubator/xap/trunk/src/xap/data/formatter/DecimalFormatter.js (added)
+++ incubator/xap/trunk/src/xap/data/formatter/DecimalFormatter.js Fri Oct  6 10:40:46 2006
@@ -0,0 +1,122 @@
+/*
+ * 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.data.formatter.DecimalFormatter"); 
+
+//Xap.require("xap.util.TypeConversionException");
+Xap.require("xap.util.Exception");
+Xap.require("xap.data.formatter.JsFormatterImpl");
+
+
+
+
+
+//-----------------------------------------------------------------------
+// Constructors.
+//-----------------------------------------------------------------------
+/**
+ * A DecimalFormatter formats numbers into decimals according to the 
+ * specified format. 
+ * 
+ * <p/>
+ * For example, given the format "000.0", the number 123.423432322 can be 
+ * formatted to display as 123.4
+ *
+ * @author ikaplansky
+ */
+xap.data.formatter.DecimalFormatter = function () {
+};
+Xap.setupClassAsSubclassOf("xap.data.formatter.DecimalFormatter", "xap.data.formatter.JsFormatterImpl");
+
+//-----------------------------------------------------------------------
+// Public Methods.
+//-----------------------------------------------------------------------
+/**
+ * @return the underlying text object used by this formatter.
+ *
+ * @public
+ * @return {DecimalFormat}
+**/
+xap.data.formatter.DecimalFormatter.prototype.getDecimalFormat = function () {
+	return this.getFormat();
+};
+//-----------------------------------------------------------------------
+// Protected Methods.
+//-----------------------------------------------------------------------
+/**
+ * @see AbstractJavaFormatter#formatObject(Object)
+ *
+ * @protected
+ * @return {Object}
+**/
+xap.data.formatter.DecimalFormatter.prototype.formatObject = function (object) {
+	/*Object*/
+	var fObject = object;
+	var sObject = "" + object;
+	if (typeof nObject != "number") {
+		try {
+	//			toFormat
+	//			= this.getSession().getTypeConversionService().convert(object, Number.class );
+			fObject = parseFloat(sObject);
+		}
+		catch (e) { // TypeConversionException
+			if (this.getLog().isDebug()) {
+				this.getLog().exception(e);
+			}
+		}
+	}
+	return xap.data.formatter.DecimalFormatter.format(fObject, this.getDecimalFormat());
+};
+/**
+ * This class method is essentially a
+ * replacement for a separate formatter class
+ * ---it obviously needs to be broken out into
+ * a separate class as soon as possible.
+ * @param aNumber{float}
+ * @param aFormatString{str}---String in the form a0b0b0c0.00,
+ * where a,b,and c can be any character beside 0 and the decimal
+ * separator char.
+ *
+**/
+xap.data.formatter.DecimalFormatter.format = function (pfNumber, psFormatString) {
+//	var formArr = psFormatString.split("0") ;
+//	var sNumber = "" + pfNumber ;
+//
+//	// Sorry, half the world---we're making
+//	// "." the decimal separator; locales will come later
+//	var sepString = "." ;
+//
+//	var formWhole = "" ;
+//	var sepIndex = -1 ;
+//	while( ++sepIndex < sNumber.length){
+//		if( formArr[sepIndex].indexOf(sepString) != -1 ){
+//			break ;
+//		}
+//	}
+// Never throw away leading integer digits:
+//	var leadingDigits
+
+	// testing:
+	var nDecis = psFormatString.length - psFormatString.indexOf(".") - 1;
+	var nAcc = nDecis ;
+	var acc = 0.05 ;
+	while( --nAcc > 0 ){
+		acc = acc /10 ;
+	}
+	var sNumber = ""+pfNumber ;
+	return (pfNumber-acc).toFixed(nDecis);
+};
+