1、bxml详解教程BXML is an XML-based markup language for simplifying the construction of Java object hierarchies. While it is most often used to define the user interface of an Apache Pivot application, it is not limited to user interface construction, and can actually be used to create hierarchies of any o
2、bject type. This document introduces the BXML language and explains how it can be used to create and configure a collection of Java objects. BXMLSerializerThe org.apache.pivot.beans.BXMLSerializer class is used to load a structure defined in a BXML file. This class implements the org.apache.pivot.se
3、rialization.Serializer interface, which defines the readObject() method for reading an object value from an input stream: ?123456789public interface Serializer public T readObject(InputStream inputStream) throws IOException, SerializationException; public void writeObject(T object, OutputStream outp
4、utStream) throws IOException, SerializationException; public String getMIMEType(T object); For example, the following code snippet loads a Window object declared in a file named my_window.bxml: ?1234BXMLSerializer bxmlSerializer = new BXMLSerializer(); Window window = (Window)bxmlSerializer.readObje
5、ct(getClass().getResource(my_window.bxml); BXMLSerializer does not implement writeObject(), but does provide some additional conveniece methods for reading BXML as well as for localizing the deserialized structure using resource bundles. NamespacesIn BXML, an XML namespace represents a Java package.
6、 Declaring a namespace associates the namespace prefix with the package, similar to how the import keyword is used in Java. For example, the following simple BXML associates the package com.foo with the foo namespace prefix: ?123 If this file were deserialized using BXMLSerializer, an instance of co
7、m.foo.Bar would be returned by the call to readObject(). The following BXML, which maps the com.foo package to the default namespace, would produce the same results with slightly less verbose markup: ?123; More complex examples may use classes defined in multiple packages; multiple namespace prefixe
8、s can be used for this purpose. The bxml NamespaceThe bxml namespace prefix is reserved and defines a number of elements and attributes that are used for internal processing. It is generally declared on the root element of a BXML document: ?123456 . The bxml namespace includes the following: The bxm
9、l:id attribute, which is used to assign a variable name to an element declared in a BXML file. For example, the following markup would associate an instance of the Foo class with the ID myFoo. This variable is added to the documents variable namespace (which is different from the XML namespace used
10、to import Java packages), and can then be referenced elsewhere in the file or by code that deserializes the file, via the BXMLSerializer#getNamespace() method: ?123 The tag, which is used to include external resources, including nested BXML documents. For example, the following markup would embed th
11、e contents of the my_include.bxml file in the current document:?123; BXML includes are often used for partitioning content into manageable pieces (for example, when working on large applications or with multiple developers, or when defining reusable content templates). By default, each include is as
12、signed its own variable namespace to avoid naming collisions with ancestor documents; however, this behavior can be overridden by adding an inline attribute with a value of true to the tag. The tag, which defines a block of script code within a BXML file. For example, the following script block defi
13、nes a function named foo(), which can then be called from other script blocks in the document: ?1234567 function foo() . The default scripting language is JavaScript, but any JVM-compatible scripting language can be used. The language processing instruction is used to specify a different language; f
14、or example, Groovy: ?123 Script blocks can also be defined in event handler attributes and elements. Scripting and event handling are discussed in more detail below. The tag, which is used to declare objects that will exist outside of the hierarchy but may be referred to elsewhere. For example, the
15、following define block declares an instance of Foo named myFoo that is not processed as part of the document flow (in other words, would not be added to its parent element, if it had one) but can be referred to by variable name later in the document: ?12345 The tag, used to dereference a page variab
16、le. For example, the Foo instance in the previous example could be dereferenced as follows elsewhere in the document: ?123 Wherever this tag appears, it will effectively be replaced by the value of the myFoo variable. The variable deference operator ($) can also be used to dereference page variables
17、. This is discussed in more detail below. ElementsIn BXML, an XML element that does not begin with the reserved bxml namespace prefix represents one of the following: A class instance A property of a class instance A static propertyEach of these is discussed in more detail below.Class InstancesIf an
18、 elements tag name begins with an uppercase letter (and it is not a static property setter; see below), it is considered a class instance. When BXMLSerializer encounters such an element, it creates an instance of that class. As discussed above, the XML namespace prefix is used to determine the Java
19、package to which the class belongs. For example, the following BXML would produce an instance of the org.apache.pivot.wtk.Label class populated with the text Hello, World!: ?123 Class instance elements in a BXML file will often represent instances of Java bean types. Internally, BXMLSerializer uses
20、an instance of org.apache.pivot.beans.BeanAdapter to wrap the instantiated class and invoke its setter methods. This class implements the org.apache.pivot.collections.Dictionary interface and allows a caller to get and set bean property values as key/value pairs. However, if the element represents a
21、 type that already implements the Dictionary interface (such as org.apache.pivot.collections.HashMap), it is not wrapped and its dictionary methods are used directly. For example, the following BXML creates an instance of org.apache.pivot.collections.HashMap and sets its foo and bar values to 123 an
22、d 456, respectively: ?123 How the foo and bar attributes are handled is discussed in more detail below. Instance PropertiesElements whose tag names begin with a lowercase letter represent instance properties. An instance property element may represent one of the following: A property setter A read-o
23、nly sequence A read-only dictionary An event listener listProperty SettersIf the element represents a property setter, the contents of the element (which must be either a text node or a nested class instance element) are passed as the value to the setter for the property. For example, the following
24、BXML creates an instance of the Label class and sets the value of the labels text property to Hello, World!: ?12345 Hello, World! This produces the same result as the earlier example which used an attribute to set the text property: ?123 The following example creates an instance of ListView and sets
25、 the value of its listData property to an instance of org.apache.pivot.collections.ArrayList that has been populated with several instances of org.apache.pivot.wtk.content.ListItem: ?12345678910111213 Read-Only SequencesIf the property represents a read-only sequence (a bean property whose getter re
26、turns an instance of org.apache.pivot.collections.Sequence and has no corresponding setter method), the contents of the element are added to the sequence. For example, the tabs property of the org.apache.pivot.wtk.TabPane class is a read-only sequence representing the tab panes tab components. Tabs
27、can be added to a TabPane in BXML as follows: ?12345678 Read-Only DictionariesA property element may also represent a read-only dictionary (a bean property whose getter returns an instance of org.apache.pivot.collections.Dictionary but has no corresponding setter method). For example, the userData property of the org.apache.pivot.wtk.Component class represents a read-only dictionary: ?123456 userData foo=12