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/09/22 02:58:16 UTC
svn commit: r448779 - in /incubator/xap/trunk/src/xap:
data/bridge/DataAttributeConverter.js
taghandling/AttributeConversionException taghandling/AttributeConverter.js
Author: mturyn
Date: Thu Sep 21 19:58:15 2006
New Revision: 448779
URL: http://svn.apache.org/viewvc?view=rev&rev=448779
Log:
Attribute conversion classes and exception; far from fully functional, but getting there and won't break the xapcore build.
Added:
incubator/xap/trunk/src/xap/data/bridge/DataAttributeConverter.js
incubator/xap/trunk/src/xap/taghandling/AttributeConversionException
incubator/xap/trunk/src/xap/taghandling/AttributeConverter.js
Added: incubator/xap/trunk/src/xap/data/bridge/DataAttributeConverter.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/data/bridge/DataAttributeConverter.js?view=auto&rev=448779
==============================================================================
--- incubator/xap/trunk/src/xap/data/bridge/DataAttributeConverter.js (added)
+++ incubator/xap/trunk/src/xap/data/bridge/DataAttributeConverter.js Thu Sep 21 19:58:15 2006
@@ -0,0 +1,164 @@
+
+/*
+ * 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.DataAttributeConverter");
+Xap.require("xap.taghandling.AttributeConversionException");
+Xap.require("xap.tagshandling.AttributeConverter");
+Xap.require("xap.data.DataServiceImpl");
+Xap.require("xap.data.controller.BindingType");
+Xap.require("xap.data.datasource.DataSourceImpl");
+// TODO: turn this back on when
+//Xap.require("xap.data.formatter.Formatter")
+/*private static final String[]*/
+xap.data.bridge.DataAttributeConverter.BINDING_NAMES = [xap.data.controller.BindingType.ONE_WAY.getTypeId(), xap.data.controller.BindingType.ONE_TIME.getTypeId()];
+/*private static final xap.data.controller.BindingType[]*/
+xap.data.bridge.DataAttributeConverter.BINDING_VALUES = [xap.data.controller.BindingType.ONE_WAY, xap.data.controller.BindingType.ONE_TIME];
+
+// Only static methods, no need here:
+//Xap.setupClassAsSubclassOf("xap.data.bridge.DataAttributeConverter", "xap.taghandling.AttributeConverter");
+/**
+ * @public
+ * @return {xap.data.controller.BindingType}
+ *
+ * @param s{String}
+ * @throws AttributeConversionException
+**/
+xap.data.bridge.DataAttributeConverter.toBindingType = function (s) {
+ return xap.taghandling.AttributeConverter.toObjectFromEnum(s, xap.data.bridge.DataAttributeConverter.BINDING_NAMES, xap.data.bridge.DataAttributeConverter.BINDING_VALUES);
+};
+
+/**
+ * @public
+ * @return {DataSource}
+ *
+ * @param s{String}
+ * @param dataService{DataService}
+ * @throws AttributeConversionException{
+**/
+xap.data.bridge.DataAttributeConverter.toDataSource = function (s, dataService) {
+ /*DataSource*/
+ var dataSource = null;
+ if (s != null) {
+ dataSource = dataService.getDataSourceContainer().getDataSource(s);
+ }
+ if (!dataSource) {
+ throw new AttributeConversionException(s, "DATA_SOURCE", null); //XmlDataTokens.DATA_SOURCE
+ }
+ return dataSource;
+};
+/**
+ * @public
+ * @return {Formatter}
+ *
+ * @param s{String}
+ * @param dataService{DataServiceImpl}
+ * @throws AttributeConversionException
+**/
+xap.data.bridge.DataAttributeConverter.toFormatter = function (s, dataService) {
+ /*Formatter*/
+ var formatter = null;
+ if (s != null) {
+ formatter = dataService.getFormatterContainer().getFormatter(s);
+ }
+ if (formatter == null) {
+ throw new AttributeConversionException(s, XmlDataTokens.FORMATTER, null);
+ }
+ return formatter;
+};
+/**
+ * @public
+ * @return {Object}
+ *
+ * @param s{String}
+ * @param classType{Class}
+ * @throws AttributeConversionException
+**/
+xap.data.bridge.DataAttributeConverter.toClassInstance = function (s, classType) {
+ if (s == null) {
+ throw new AttributeConversionException(s, "instance of " + classType.getName(), null);
+ }
+ try {
+ /*Object*/
+ var o = Class.forName(s).newInstance();
+ if (!classType.isAssignableFrom(o.getClass())) {
+ throw new AttributeConversionException(s, "instance of " + classType.getName(), new ClassCastException());
+ }
+ return o;
+ }
+ catch (e) {//ClassNotFoundException, InstantiationException, IllegalAccessException
+ throw new AttributeConversionException(s, "instance of " + classType.getName(), e);
+ }
+};
+/**
+ * Converts a string like "jp" or "jp_JP" to a Locale object.
+ * @param s
+ * @return
+ * @throws AttributeConversionException
+ *
+ * @public
+ * @return {Locale}
+ *
+ * @param s{String}
+ * @throws AttributeConversionException
+**/
+xap.data.bridge.DataAttributeConverter.toLocale = function (s) {
+ if (s == null) {
+ throw new AttributeConversionException(s, "locale", null);
+ }
+ /*int*/
+ var iof = s.indexOf("_");
+ /*String*/
+ var country = "";
+ try {
+ if (iof > 0) {
+ country = s.substring(iof + 1);
+ s = s.substring(0, iof);
+ }
+ return new Locale(s, country);
+ }
+ catch (e) { //Exception
+ throw new AttributeConversionException(s, "locale", e);
+ }
+};
+/**
+ * Converts a string like "PST" to a TimeZone object.
+ * Note that if the string argument is null or cannot be converted to a
+ * TimeZone object, this method will throw AttributeConversionException
+ * (as opposed to returning the default TimeZone). Deciding whether it's
+ * appropriate to return a default TimeZone does not belong here.
+ *
+ * @param s - the string to be converted into a TimeZone object
+ * @return TimeZone
+ * @throws AttributeConversionException
+ *
+ * @public
+ * @return {TimeZone}
+ * @param s{String }
+ * @throws AttributeConversionException
+**/
+xap.data.bridge.DataAttributeConverter.toTimeZone = function (s) {
+ if (s == null) {
+ throw new AttributeConversionException(s, "TimeZone", null);
+ }
+ /*TimeZone*/
+ var zone = TimeZone.getTimeZone(s);
+ if (zone == null) {
+ throw new AttributeConversionException(s, "TimeZone", null);
+ }
+ return zone;
+};
+
Added: incubator/xap/trunk/src/xap/taghandling/AttributeConversionException
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/taghandling/AttributeConversionException?view=auto&rev=448779
==============================================================================
--- incubator/xap/trunk/src/xap/taghandling/AttributeConversionException (added)
+++ incubator/xap/trunk/src/xap/taghandling/AttributeConversionException Thu Sep 21 19:58:15 2006
@@ -0,0 +1,46 @@
+/*
+ * 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.taghandling.AttributeConversionException");
+Xap.require("xap.util.Exception") ;
+
+
+/**
+ * An exception for reporting problems with the formatting of attributes
+ * on elements.
+ *
+ * @author James Margaris
+ * @param attribute{String}
+ * @param dataType{String}
+ * @param rootException{Exception}
+ *
+ *
+ */
+xap.taghandling.AttributeConversionException = function(attribute,dataType,rootException){
+ xap.util.Exception.call(
+ this,
+ xap.taghandling.AttributeConversionException.COULD_NOT_CONVERT,
+ [attribute,dataType],
+ rootException
+ );
+}
+
+ /*private static final String*/
+ xap.taghandling.AttributeConversionExceptionCOULD_NOT_CONVERT="couldNotConvert";
+
+}
\ No newline at end of file
Added: incubator/xap/trunk/src/xap/taghandling/AttributeConverter.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/taghandling/AttributeConverter.js?view=auto&rev=448779
==============================================================================
--- incubator/xap/trunk/src/xap/taghandling/AttributeConverter.js (added)
+++ incubator/xap/trunk/src/xap/taghandling/AttributeConverter.js Thu Sep 21 19:58:15 2006
@@ -0,0 +1,739 @@
+/*
+ * 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.require("xap.session.ClientSession");
+//Xap.require("xap.XmlTokens") ;
+//Xap.require("xap.bridges.GradientBridge") ;
+//Xap.require("xap.component.NPanel") ;
+//Xap.require("xap.stylesheet.Style") ;
+Xap.require("xap.xml.dom.Document");
+
+//Xap.require("xap.xml.DocumentRegistry") ;
+Xap.require("xap.xml.DocumentContainer");
+Xap.require("xap.xml.dom.XapElement");
+/**
+ *
+ * A helper class for converting Strings (usually attributes)
+ * into other data types. This should be used by tag-handlers
+ * to convert attributes into the property data types, typically
+ * in the attributeSet() method.
+ *
+ * @author James Margaris
+ * @author Stan
+ *
+ */
+xap.taghandling.AttributeConverter = function () {
+};
+Xap.setupClassAsSubclassOf("xap.taghandling.AttributeConverter", "Object");
+/**
+ * Indicates that a value is not set. This can be useful
+ * for tracking things like "fontBold", which can be set to true
+ * or false or not set, which maked in inherit the style
+ * from the parent.
+ *public static final int*/
+xap.taghandling.AttributeConverter.NOT_SET = null;
+/**
+ * Indicates that a value is set to false.
+ *public static final int*/
+xap.taghandling.AttributeConverter.FALSE = 0;
+/**
+ * Indicates that a value is set to true.
+ *public static final int*/
+xap.taghandling.AttributeConverter.TRUE = 1;
+/*private static final Color[]*/
+xap.taghandling.AttributeConverter.s_colorValues = [];
+// TODO: Add these?
+// Color.black, Color.blue, Color.cyan,
+// Color.darkGray, Color.gray, Color.green,
+// Color.lightGray, Color.magenta, Color.orange,
+// Color.pink, Color.red, Color.white,
+// Color.yellow, SystemColor.activeCaptionBorder, SystemColor.activeCaption,
+// SystemColor.control, SystemColor.activeCaptionText, SystemColor.inactiveCaptionBorder,
+// SystemColor.inactiveCaptionText, SystemColor.controlHighlight, SystemColor.infoText,
+// SystemColor.info, SystemColor.menu, SystemColor.menuText,
+// SystemColor.controlDkShadow, SystemColor.desktop, SystemColor.scrollbar,
+// SystemColor.textHighlightText, SystemColor.textHighlight, SystemColor.window,
+// SystemColor.windowText, SystemColor.controlShadow,SystemColor.inactiveCaption
+/*private static final int[]*/
+xap.taghandling.AttributeConverter.s_cursorConstants = [];
+// TODO: Add these?
+// Cursor.HAND_CURSOR, Cursor.CROSSHAIR_CURSOR, Cursor.E_RESIZE_CURSOR,
+// Cursor.W_RESIZE_CURSOR, Cursor.S_RESIZE_CURSOR, Cursor.N_RESIZE_CURSOR,
+// Cursor.NE_RESIZE_CURSOR, Cursor.NW_RESIZE_CURSOR, Cursor.SE_RESIZE_CURSOR,
+// Cursor.SW_RESIZE_CURSOR, Cursor.TEXT_CURSOR, Cursor.MOVE_CURSOR,
+// Cursor.DEFAULT_CURSOR, Cursor.WAIT_CURSOR
+/**
+ * Converts the string to a java.awt.Cursor.
+ * @param s The name of a cursor.
+ * @return A Cursor.
+ * @throws xap.taghandling.AttributeConversionException Throws if the cursor name is invalid.
+ *
+ * @public
+ * @return {Cursor}
+ *
+ * @param s{String}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toCursor = function (s) {
+ if (xap.taghandling.isEmptyOrNull(s)) {
+ throw new xap.taghandling.AttributeConversionException(s, "cursor", null);
+ }
+ if (s.equals("")) {
+ return null;
+ } //TODO does this make sense?
+ for (var i = 0; i < XmlTokens.CURSOR_NAMES.length; i++) {
+ if (s.equals(XmlTokens.CURSOR_NAMES[i])) {
+ return Cursor.getPredefinedCursor(s_cursorConstants[i]);
+ }
+ }
+ throw new xap.taghandling.AttributeConversionException(s, "cursor", null);
+};
+/**
+ * Converts the string to a boolean.
+ * @param s The string to convert.
+ * @return A boolean.
+ * @throws xap.taghandling.AttributeConversionException If the string is empty, null,
+ * or otherwise cannot be converted.
+ *
+ * @public
+ * @return {boolean}
+ *
+ * @param s{String}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toBoolean = function (s) {
+ if (xap.taghandling.isEmptyOrNull(s)) {
+ throw new xap.taghandling.AttributeConversionException(s, "boolean", null);
+ }
+ return (new Boolean(s)).booleanValue();
+};
+/**
+ * Convert a String "true", "false", "", or null to integer TRUE,
+ * FALSE, NOT_SET, or NOT_SET, throwing an exception if any other value encountered.
+ * Tristate values are used to track things that can be true, false,
+ * or not set by the user. For example, the bold style of a component
+ * can be set to false, true, or left unspecified.
+ * @param s String to parse
+ * @return TRUE, FALSE, or NOT_SET
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {int}
+ *
+ * @param s{String}
+ * @throws xap.taghandling.AttributeConversionException{
+**/
+xap.taghandling.AttributeConverter.toTriState = function (s) {
+ var result = xap.taghandling.AttributeConverter.NOT_SET;
+ if (!xap.taghandling.isEmptyOrNull(s)) {
+ if (s == "true") {
+ result = xap.taghandling.AttributeConverter.TRUE;
+ } else if (s == "false") {
+ result = xap.taghandling.AttributeConverter.FALSE;
+ } else {
+ throw new xap.taghandling.AttributeConversionException(s,"tristate",null);
+ }
+ }
+ return result;
+};
+/**
+ * Convert an int TRUE, FALSE, or NOT_SET to a boolean,
+ * using the passed default value in the last case.
+ * @param ts an int TRUE, FALSE, or NOT_SET
+ * @param defalt default value to use
+ * @return passed default value or forced true or false
+ *
+ * @public
+ * @param ts{integer}
+ * @param defalt{boolean} Use default value?
+ * @return {boolean}
+**/
+xap.taghandling.AttributeConverter.toBooleanFromTriStateInt = function (ts, defalt) {
+ var defalt = null;
+ switch (ts) {
+ case xap.taghandling.AttributeConverter.TRUE:
+ defalt = true;
+ break;
+ case xap.taghandling.AttributeConverter.FALSE:
+ defalt = false;
+ break;
+ }
+ return defalt;
+};
+/**
+ * Convert a String "true", "false", "", or null to a boolean,
+ * using the passed default value in the last 2 cases.
+ * @param s String to parse
+ * @param defalt default value to use
+ * @return passed default value or forced true or false
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {boolean}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toBooleanFromTriState = function (s, defalt) {
+ return toBooleanFromTriStateInt(this.toTriState(s), defalt);
+};
+/**
+ * Converts a string to a date given the SimpleDateFormat
+ * @param s The String to convert
+ * @param dateFormat The dateFormat to use in conversion.
+ * @return A Date object.
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {Date}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toDate = function (s, dateFormat) {
+ try {
+ return dateFormat.parse(s);
+ }
+ catch (e) { //ParseException
+ //TODO include data format info?
+ throw new xap.taghandling.AttributeConversionException(s, "date", e);
+ }
+};
+/**
+ * Coverts the string to an integer.
+ * @param s The String to convert.
+ * @return An int.
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {int}
+ *
+ * @param s{String}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toInteger = function (s) {
+ try {
+ return ((new Integer(s))).intValue();
+ }
+ catch (e) { //Exception
+ //don't wrap the exception we get, it makes the stack trace
+ //way too long and the problem is obvious anyway
+ throw new xap.taghandling.AttributeConversionException(s, "integer", null);
+ }
+};
+/**
+ * Converts the string to a float.
+ * @param s The string to convert.
+ * @return A float.
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {float}
+ *
+ * @param s{String}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toFloat = function (s) {
+ try {
+ return ((new Float(s))).floatValue();
+ }
+ catch (e) { //Exception
+ //don't wrap the exception we get, it makes the stack trace
+ //way too long and the problem is obvious anyway
+ throw new xap.taghandling.AttributeConversionException(s, "float", null);
+ }
+};
+/**
+ * Converts a string into an array of length number.
+ * If the string is just one value, the array is all duplicates
+ * If th string is number different comma separated value and
+ * length is not number this throws an error
+ * @param s The String to convert
+ * @param number The length of the array to return.
+ * @return An int[] of length number.
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {int[]}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toIntegers = function (s, number) {
+ if (xap.taghandling.isEmptyOrNull(s)) {
+ throw new xap.taghandling.AttributeConversionException(s, "" + number + " integers", null);
+ }
+ /*StringTokenizer*/
+ var st = new StringTokenizer(s, ",");
+ /*int*/
+ var tokens = st.countTokens();
+ /*int[]*/
+ var ints = new Array(number);
+ if (tokens == 1) {
+ /*int*/
+ var intValue = this.toInteger(st.nextToken());
+ for (var i = 0; i < number; i++) {
+ ints[i] = intValue;
+ }
+ } else if (tokens == number) {
+ for (var i = 0; i < tokens; i++) {
+ ints[i] = toInteger(st.nextToken());
+ }
+ } else {
+ throw new xap.taghandling.AttributeConversionException(s, ""+ number + " integers", null);
+ }
+ return ints;
+};
+/**
+ * Converts a comma separated list into an array of floats of any size,
+ * can return null.
+ * @param s The String to convert.
+ * @return A float[].
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {float[]}
+ *
+ * @param s{String}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toFloats = function (s) {
+ if (xap.taghandling.isEmptyOrNull(s)) {
+ throw new xap.taghandling.AttributeConversionException(s, "float array", null);
+ }
+ /*StringTokenizer*/
+ var st = new StringTokenizer(s, ",");
+ /*int*/
+ var tokens = st.countTokens();
+ /*float[]*/
+ var floats = new Array(tokens);
+ try {
+ for (var i = 0; i < tokens; i++) {
+ floats[i] = new Float(st.nextToken().trim()).floatValue();
+ }
+ }
+ catch (e) { //Exception
+ throw new xap.taghandling.AttributeConversionException(s, "float array", null);
+ }
+ return floats;
+};
+/**
+ * Converts the string to an array of integers.
+ * @param s A comma-separated list of integers.
+ * @return An int[].
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {int[]}
+ *
+ * @param s{String}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toIntegers = function (s) {
+ if (xap.taghandling.isEmptyOrNull(s)) {
+ throw new xap.taghandling.AttributeConversionException(s, "integer array", null);
+ }
+ /*StringTokenizer*/
+ var st = new StringTokenizer(s, ",");
+ /*int*/
+ var tokens = st.countTokens();
+ /*int[]*/
+ var ints = new Array(tokens);
+ try {
+ for (var i = 0; i < tokens; i++) {
+ ints[i] = Integer.parseInt(st.nextToken().trim());
+ }
+ }
+ catch (e) { //Exception
+ throw new xap.taghandling.AttributeConversionException(s, "integer array", null);
+ }
+ return ints;
+};
+/**
+ * Expects a string like "78.4%", returns a float
+ * between 0 and 1.
+ * @param s The String to convert.
+ * @return A float between 0 and 1.
+ *
+ * @public
+ * @return {float}
+ *
+ * @param s{String}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toPercentage = function (s) {
+ if (s == null || !s.endsWith("%") || s.length() <= 1) {
+ throw new xap.taghandling.AttributeConversionException(s, "percentage", null);
+ }
+ try {
+ s = s.substring(0, s.length() - 1);
+ /*Float*/
+ var i = new Float(s);
+ if (i.floatValue() < 0 || i.floatValue() > 100) {
+ throw new xap.taghandling.AttributeConversionException(s, "percentage", null);
+ }
+ /*float*/
+ var value = parseFloat(i); //i.floatValue()/100.0f;
+ return Math.min(1, Math.max(value, 0));
+ }
+ catch (e) { //Exception
+ throw new xap.taghandling.AttributeConversionException(s, "percentage", null);
+ }
+};
+/**
+ * Converts the string to a color. The string should be of the form
+ * #HHHHHH where H is a hex digit, or an r,g,b string,
+ * or a proper color name.
+ * @param s The String to convert. "transparent" will return null.
+ * @return An AWT Color.
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {Color}
+ *
+ * @param s{String}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toColor = function (s) {
+ try {
+ return toColorInternal(s);
+ }
+ catch (e) { //IllegalArgumentException
+ throw new xap.taghandling.AttributeConversionException(s, "color", null);
+ }
+};
+/**
+ * @private
+ * @return {Color}
+ *
+ * @param s{String}
+ * @throws IllegalArgumentException
+**/
+xap.taghandling.AttributeConverter.toColorInternal = function (s) {
+ if (xap.taghandling.isEmptyOrNull(s)) {
+ throw new IllegalArgumentException();
+ }
+ try {
+ if (s.equals(XmlTokens.TRANSPARENT)) {
+ return null;
+ }
+ if (s.startsWith("#") && s.trim().length() == 7) {
+ return Color.decode(s);
+ }
+ for (var i = 0; i < XmlTokens.COLOR_NAMES.length; i++) {
+ if (s.equals(XmlTokens.COLOR_NAMES[i])) {
+ return s_colorValues[i];
+ }
+ }
+// TODO: un-punt this:
+ /*StringTokenizer*/
+//var st = new StringTokenizer(s,",");
+// /*Integer r*/
+//var = new Integer(st.nextToken());
+// /*Integer g*/
+//var = new Integer(st.nextToken());
+// /*Integer b*/
+//var = new Integer(st.nextToken());
+//
+// return new Color(r.intValue(),g.intValue(),b.intValue());
+ }
+ catch (e) { // Exception
+ throw new IllegalArgumentException();
+ }
+};
+/**
+ * If passed attribute string is in the passed String array,
+ * return the corresponding int from the passed int array,
+ * else throw an xap.taghandling.AttributeConversionException. This can be used
+ * to turn an enumerated type into a constant, for example turn
+ * "left","center","right" into 1,2,3
+ * @param s The string to convert
+ * @param names Array of enumeration names
+ * @param values Array of enumeration values
+ * @return int from values matching parallel name
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toIntFromEnum = function (s, names, values) {
+ for (var i = 0; i < values.length; ++i) {
+ if (names[i].equals(s)) {
+ return values[i];
+ }
+ }
+
+ //if there was an error produce a string like:
+ //could not convert to datatype: enumeration of{ true false }
+ /*StringBuffer*/
+ var stringBuffer = new StringBuffer("enumeration of{ ");
+ for (var i = 0; i < names.length; i++) {
+ stringBuffer.append(names[i]);
+ stringBuffer.append(" ");
+ }
+ stringBuffer.append("}");
+ throw new xap.taghandling.AttributeConversionException(s, stringBuffer.toString(), null);
+};
+/**
+ * If passed attribute string is in the passed String array,
+ * return the corresponding Object from the passed Object array,
+ * else throw an xap.taghandling.AttributeConversionException. This can be used
+ * to turn an enumerated type into a constant, for example turn
+ * "left","center","right" into 1,2,3
+ * @param s The string to convert
+ * @param names{String []} Array of enumeration names
+ * @param values{Object[] } Array of enumeration values
+ * @return Object from values matching parallel name
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {Object}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toObjectFromEnum = function (s, names, values) {
+ for (var i = 0; i < values.length; ++i) {
+ if (names[i].equals(s)) {
+ return values[i];
+ }
+ }
+
+ //if there was an error produce a string like:
+ //could not convert to datatype: enumeration of{ true false }
+ /*StringBuffer*/
+ var stringBuffer = new StringBuffer("enumeration of{ ");
+ for (var i = 0; i < names.length; i++) {
+ stringBuffer.append(names[i]);
+ stringBuffer.append(" ");
+ }
+ stringBuffer.append("}");
+ throw new xap.taghandling.AttributeConversionException(s, stringBuffer.toString(), null);
+};
+/**
+ * Converts the string s to an array of number length of colors.
+ * @param s The String to convert. "transparent" will return null.
+ * @param number The length of the array to return.
+ * @return A Color[] of length number.
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {Color[]}
+ *
+ * @param s, int number{String}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toColors = function (s, number) {
+ if (xap.taghandling.isEmptyOrNull(s)) {
+ throw new xap.taghandling.AttributeConversionException(s, "" + number + " colors", null);
+ }
+ /*StringTokenizer*/
+ var st = new StringTokenizer(s, ",");
+ /*int*/
+ var tokens = st.countTokens();
+ /*Color[]*/
+ var colors = new Color[number];
+ if (tokens == 1) {
+ /*Color*/
+ var color = this.toColor(st.nextToken());
+ for (var i = 0; i < number; i++) {
+ colors[i] = color;
+ }
+ } else if (tokens == number) {
+ for (var i = 0; i < tokens; i++) {
+ colors[i] = toColor(st.nextToken());
+ }
+ } else {
+ throw new xap.taghandling.AttributeConversionException(s, "" + number + " colors", null);
+ }
+ return colors;
+};
+/**
+ * Converts an element reference in the form of #ID to an Element.
+ * @param s{String} The element reference.
+ * @param d{Document } The document to look the element up in.
+ * @return An Element.
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {Element}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toElement = function (s, d) {
+ /*String*/
+ var errorString = "element reference (check element being referenced is declared before" + " being referenced).";
+ if (xap.taghandling.isEmptyOrNull(s)) {
+ throw new xap.taghandling.AttributeConversionException(s, errorString, null);
+ }
+ if (!s.startsWith("#")) {
+ throw new xap.taghandling.AttributeConversionException(s, errorString, null);
+ }
+ /*Element*/
+ var e = null;
+ try {
+ e = d.getElementById(s.substring(1));
+ }
+ catch (ex) { //Exception
+ throw new xap.taghandling.AttributeConversionException(s, errorString, null);
+ }
+ if (e != null) {
+ return e;
+ }
+ throw new xap.taghandling.AttributeConversionException(s, errorString, null);
+};
+/**
+ * @public
+ * @return {Component}
+ *(String s, ClientSession session, Class componentClass,
+ * String logicalClassName)
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toComponent = function (s, session, componentClass, logicalClassName) {
+ /*String*/
+ var errorString = "element reference (check element being referenced is declared before" + " being referenced).";
+ if (xap.taghandling.isEmptyOrNull(s)) {
+ throw new xap.taghandling.AttributeConversionException(s, errorString, null);
+ }
+ if (!s.startsWith("#")) {
+ throw new xap.taghandling.AttributeConversionException(s, errorString, null);
+ }
+ /*Element*/
+ var e = null;
+ try {
+ e = session.getDocumentRegistry().findDocument(DocumentRegistry.UI_DOCUMENT_NAME).getElementById(s.substring(1));
+ }
+ catch (ex) { //Exception
+ throw new xap.taghandling.AttributeConversionException(s, errorString, null);
+ }
+ if (e != null) {
+ /*Object*/
+ var o = session.getUiContentHandler().getHandlerForElement(e);
+ if (o instanceof AwtComponentBridge) {
+ /*Component*/
+ var popup = (o).getUiComponent(); //(AwtComponentBridge)
+ if (!componentClass.isAssignableFrom(popup.getClass())) {
+ throw new xap.taghandling.AttributeConversionException(s, logicalClassName + " reference.", null);
+ }
+ return popup;
+ }
+ }
+ throw new xap.taghandling.AttributeConversionException(s, errorString, null);
+};
+/**
+ * Returns either a Color, Style.TRANSPARENT_COLOR, or a gradient
+ * This must take a session so it can look up a gradient reference
+ * @param s The String to convert.
+ * @param session{ClientSession} The current ClientSession.
+ * @return A Color, gradient on Style.TRANSPARENT_COLOR.
+ *
+ * @public
+ * @return {Object}
+ *
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toPattern = function (s, session) {
+ if (xap.taghandling.isEmptyOrNull(s)) {
+ throw new xap.taghandling.AttributeConversionException(s, "pattern", null);
+ }
+
+ //do this because toCOlor(transparent) returns null
+ if (s.equals("transparent")) {
+ return Style.TRANSPARENT_COLOR;
+ }
+ try {
+ return toColorInternal(s);
+ }
+ catch (ex) { //IllegalArgumentException
+ try {
+ if (s.startsWith("#")) {
+ /*Element*/
+ var e = session.getDocumentRegistry().findDocument(DocumentRegistry.UI_DOCUMENT_NAME).getElementById(s.substring(1));
+ /*Object*/
+ var o = session.getUiContentHandler().getHandlerForElement(e);
+ if (o instanceof GradientBridge) {
+ return (o).getGradient();
+ } //(GradientBridge)
+ }
+ }
+ catch (e) { //Exception
+ throw new xap.taghandling.AttributeConversionException(s, "pattern", null);
+ }
+ }
+ throw new xap.taghandling.AttributeConversionException(s, "pattern", null);
+};
+/**
+ * Converts a comma-separated string into an array of string.
+ * @param s The String to convert.
+ * @param number The length of the array to return.
+ * @param validValues An array of acceptable strings, or null for all strings
+ * @return A String[] of length number.
+ * @throws xap.taghandling.AttributeConversionException
+ *
+ * @public
+ * @return {String[]}
+ *
+ * @param s, int number, String[] validValues{String}
+ * @throws xap.taghandling.AttributeConversionException
+**/
+xap.taghandling.AttributeConverter.toStrings = function (s, number, validValues) {
+ if (xap.taghandling.isEmptyOrNull(s)) {
+ throw new xap.taghandling.AttributeConversionException(s, "" + number + " strings", null);
+ }
+ /*StringTokenizer*/
+ var st = new StringTokenizer(s, ",");
+ /*int*/
+ var tokens = st.countTokens();
+ /*String[]*/
+ var strings = new String[number];
+ if (tokens == 1) {
+ /*String*/
+ var oneString = st.nextToken();
+ for (var i = 0; i < number; i++) {
+ strings[i] = oneString;
+ }
+ } else if (tokens == number) {
+ for (var i = 0; i < tokens; i++) {
+ strings[i] = st.nextToken().intern();
+ }
+ } else {
+ throw new xap.taghandling.AttributeConversionException(s, "" + number + " strings", null);
+ }
+ if (validValues != null) {
+ for (var i = 0; i < strings.length; i++) {
+ /*boolean*/
+ var wasValid = false;
+ for (var j = 0; j < validValues.length && !wasValid; j++) {
+ if (strings[i].equals(validValues[j])) {
+ wasValid = true;
+ }
+ }
+ if (!wasValid) {
+ throw new xap.taghandling.AttributeConversionException(s, ""+number + " strings", null);
+ }
+ }
+ }
+ return strings;
+};
+/**
+ * Returns true if the string is an empty string
+ * or is null, does not perform a trim first.
+ * @param s The string to check.
+ * @return True if the string is null or equals "".
+ *
+ * @public
+ * @return {boolean}
+ *
+ * @param s{String}
+**/
+xap.taghandling.AttributeConverter.isEmptyOrNull = function (s) {
+ if (s == null || s == "" ) {
+ return true;
+ }
+ return false;
+};
+