|
Letzte
Bearbeitung dieses Dokuments: |
Code
Erklärungen
und Anwendungsbeispiele
Verwandte
Dokumentation
package
js_base.utilities;
import java.util.*;
import js_base.utilities.JSBS_Formatter;
/**
*
* @author kurt@javascout.biz
*
@date 2007-08-09
*
* @description
*
Class with static methods that provide services to generate
JavaScript-code.
* For the beginning, the
implemented method is:
* * convert a
multidimensional vector to JavaScript-code that creates and fills
* an array with the equivalent data.
*
* @change-log
*
when who why
*
--------------------------------------------------------
*
*/public
class
JSBS_JavaScriptServices
{
/*
*
--------------------
* Method to create JavaScript-code that
generates an array equivalent to the
* Java-vector
passed as parameter and a JavaScript-function that loads this
array.
* The (variable-)name for the array and the
function-name are passed as parameters too.
* The
JavaScript-code generated is returned within a type of String.
*/
public
static
String
codeJavaScriptArray(String
parmArrayName,
String
parmFunctionName,
Vector
parmArrayVector) {/*
*
Check if all parameters are valid; otherwise return empty String.
*/
if
(parmArrayName ==
null
)
return
""
;
if
(parmFunctionName ==
null
)
return
""
;
if
(parmArrayVector ==
null
)
return
""
;
/*
*
Define a StringBuffer for the Return-value as manipulating a
StringBuffer is faster
* than manipulating a
String.
* Fill this StringBuffer already with the
beginning of the generated JavaScript-code. */ StringBuffer
strReturnValue =
new
StringBuffer(
"//"
+
System.getProperty(
"line.separator"
)
)
;
strReturnValue.append("var
"
+ parmArrayName +
";"
+
System.getProperty(
"line.separator"
)
)
;
strReturnValue.append("function
"
+ parmFunctionName +
"(){"
+ System.getProperty(
"line.separator"
)
)
;
/*
* The
code for creation of the multi-dimensions of the JavaScript-array and
* the filling of the array is written in method that
processes the Vector passed as parameter
* recursive.
*/ strReturnValue.append(generateCodeForArray(parmArrayVector,
parmArrayName)
)
;
/*
* Append
the closing parenthesis for the JavaScriptFunction.
*/ strReturnValue.append(
"}"
+ System.getProperty(
"line.separator"
)
)
;
/*
* Append
the number of elements (members) of the array and a line of
comment.
* For multi-dimensional arrays only the
members of the highest dimension are documented.
*/ strReturnValue.append(
"var
"
+ parmArrayName +
"_Members
=
")
;
strReturnValue.append(JSBS_Formatter.toFormattedString(parmArrayVector.size()))
;
strReturnValue.append(";"
+
System.getProperty(
"line.separator"
)
)
;
strReturnValue.append("//"
+ System.getProperty(
"line.separator"
)
)
;
/*
* Return
the String with the generated JavaScript-code that defines and fills
the array. */
return
strReturnValue.toString();
}/*
* --------------------
* Method
to create JavaScript-code to construct a multidimensional
array
* and fill it with the values passed int the
parameter-Vector.
*
* With each recursive call
of this method an additional dimension for
* the array
in the JavaScript-code is created.
* The number of
elements within the processed dimensions is defined by the passed
Vector.
*
* To understand the algorithm
it is helpful to watch the growing of 'strReturnValue'
* with
a debugger. */
private
static
StringBuffer
generateCodeForArray(Vector parmArrayVector,
String
parmArrayNameWithDimension) {/*
*
Check if all parameters are valid; otherwise return empty String.
*/
if
(parmArrayVector ==
null
)
return
new
StringBuffer(
"")
;
if
(parmArrayNameWithDimension ==
null
)
return
new
StringBuffer(
"")
;
/*
* Define
the return-value. */ StringBuffer
strReturnValue =
new
StringBuffer(
"")
;
/*
* Inspect
the size of the passed Vector.
* This is the number of
elements in the array for the processed dimension. */
int
intVectorSize
= parmArrayVector.size();
/*
* Continue
processing only if the Vector contains elements.
* This
is to avoid to generate faulty JavaScript-code if an empty Vector is
passed. */
if
(intVectorSize
> 0) {
/*
* Create
JavaScript-code to build an array representing the number of elements
* in the passed Vector.
*/ strReturnValue.append(parmArrayNameWithDimension
+
"
= new Array("
+
JSBS_Formatter.toFormattedString(intVectorSize)
+
");"
+
System.getProperty(
"line.separator"
)
)
;
/*
* Use
a for-loop to inspect each element of the Vector passed as parameter.
*/
for
(
int
intVectorIndex
= 0; intVectorIndex < intVectorSize; intVectorIndex++)
{
/*
* Enlarge
the variable-name for the JavaScript-array (passed within the
parameter
* 'parmArrayNameWithDimension') by another
dimension.
* This String is either used to pass it as
parameter at a recursive call of this method
* or to
fill one array-element with a value. */ String
strForwardedArrayNameWithDimension = parmArrayNameWithDimension +
"["
+ JSBS_Formatter.toFormattedString(intVectorIndex)
+
"]"
;
/*
* The
indexed element of the Vector is processed as type Object as it is
not known now
* if it will be another Vector
(representing an addidional dimension of the JavaScript-array
* to
be built) or a String (representing a value within the
JavaScript-array). */ Object
objVectorElement = parmArrayVector.elementAt(intVectorIndex);
/*
* Inspect
the type represented by the Object. */
if
(objVectorElement
instanceof
Vector) {
/* An
additional dimension of the JavaScript-array has to be generated;
* call this method recursively.
*/ strReturnValue.append(
generateCodeForArray((Vector)
objVectorElement,
strForwardedArrayNameWithDimension));
}
if
(objVectorElement
instanceof
String) {
/* The
Vector-element represents a value to be transferred to the
JavaScript-code.
* Assign it to the
JavaScript-array-element.
*/ strReturnValue.append(
strForwardedArrayNameWithDimension
+ "
= '"
+ (String) objVectorElement +
"';"
+
System.getProperty(
"line.separator"
));
}
}
}/*
* Return
the String with the generated JavaScript-code.
* This
string contains now all dimensions with values as represented by the
passed Vector. */
return
strReturnValue;
}}
xxx
Dokument |
Inhalt |
Dieser
Leitfaden enthält die notwendigen Tätigkeiten für
die Entwicklung eines StartFrame (auch als Command-Center
bekannt). |