Script (tag)

From JAXXWiki

The <script> tag allows arbitrary Java code to be embedded anywhere in a JAXX file. The script may include import statements, field, method and constructor definitions, class definitions, and executable statements.

Example scripts:

<script><![CDATA[
import java.util.List;

private List info = new ArrayList();
for (int i = 0; i < 10; i++)
    info.add(new Integer(i));

public List getInfo() {
    return info;
}
]]></script>

It is important to note that while all the pieces of this script are valid Java code, they aren't valid Java code together. There is no context in Java that you can have an import statement (which must appear outside of a class body), field and method declarations (which must appear inside a class body), and bare executable statements (which must appear inside a method body) together in this fashion.

JAXX, on the other hand, allows this. All of the individual pieces of the script are routed to where they actually belong in the resulting compiled .java file.

CDATA sections

As scripts may contain characters that are not valid in XML text (primarily '<'), it is necessary to either escape the individual characters using either XML entities (e.g. &lt;) or the entire script using a <![CDATA[]]> block.

As the XML entity approach is not particularly attractive and errors resulting from forgetting to escape a character can be misleading, it is customary to wrap all scripts in <![CDATA[]]> blocks whether they need it or not.

External script files

The source attribute specifies a path to an external script file to be loaded.

<script source='scripts/foo.script'/>

The script tag is processed exactly as if the scripts/foo.script file had been included inline.

It is an error to specify both a source attribute and an inline script within the same <script> tag.