bxml详解教程.docx
- 文档编号:26161332
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:22
- 大小:23.35KB
bxml详解教程.docx
《bxml详解教程.docx》由会员分享,可在线阅读,更多相关《bxml详解教程.docx(22页珍藏版)》请在冰豆网上搜索。
bxml详解教程
BXMLisanXML-basedmarkuplanguageforsimplifyingtheconstructionofJavaobjecthierarchies.WhileitismostoftenusedtodefinetheuserinterfaceofanApachePivotapplication,itisnotlimitedtouserinterfaceconstruction,andcanactuallybeusedtocreatehierarchiesofanyobjecttype.
ThisdocumentintroducestheBXMLlanguageandexplainshowitcanbeusedtocreateandconfigureacollectionofJavaobjects.
BXMLSerializer
Theorg.apache.pivot.beans.BXMLSerializerclassisusedtoloadastructuredefinedinaBXMLfile.Thisclassimplementstheorg.apache.pivot.serialization.Serializerinterface,whichdefinesthereadObject()methodforreadinganobjectvaluefromaninputstream:
?
1
2
3
4
5
6
7
8
9
publicinterfaceSerializer
publicTreadObject(InputStreaminputStream)
throwsIOException,SerializationException;
publicvoidwriteObject(Tobject,OutputStreamoutputStream)
throwsIOException,SerializationException;
publicStringgetMIMEType(Tobject);
}
Forexample,thefollowingcodesnippetloadsaWindowobjectdeclaredinafilenamed"my_window.bxml":
?
1
2
3
4
BXMLSerializerbxmlSerializer=newBXMLSerializer();
Windowwindow=(Window)bxmlSerializer.readObject(getClass().getResource("my_window.bxml"));
BXMLSerializerdoesnotimplementwriteObject(),butdoesprovidesomeadditionalconveniecemethodsforreadingBXMLaswellasforlocalizingthedeserializedstructureusingresourcebundles.
Namespaces
InBXML,anXMLnamespacerepresentsaJavapackage.Declaringanamespaceassociatesthenamespaceprefixwiththepackage,similartohowtheimportkeywordisusedinJava.
Forexample,thefollowingsimpleBXMLassociatesthepackage"com.foo"withthe"foo"namespaceprefix:
?
1
2
3
Barxmlns: foo="com.foo"/> IfthisfileweredeserializedusingBXMLSerializer,aninstanceofcom.foo.BarwouldbereturnedbythecalltoreadObject().ThefollowingBXML,whichmapsthecom.foopackagetothedefaultnamespace,wouldproducethesameresultswithslightlylessverbosemarkup: ? 1 2 3 Morecomplexexamplesmayuseclassesdefinedinmultiplepackages;multiplenamespaceprefixescanbeusedforthispurpose. The"bxml"Namespace The"bxml"namespaceprefixisreservedanddefinesanumberofelementsandattributesthatareusedforinternalprocessing.ItisgenerallydeclaredontherootelementofaBXMLdocument: ? 1 2 3 4 5 6 bxml="http: //pivot.apache.org/bxml" xmlns="com.foo"> ... The"bxml"namespaceincludesthefollowing: ∙Thebxml: idattribute,whichisusedtoassignavariablenametoanelementdeclaredinaBXMLfile.Forexample,thefollowingmarkupwouldassociateaninstanceoftheFooclasswiththeID"myFoo".Thisvariableisaddedtothedocument'svariablenamespace(whichisdifferentfromtheXMLnamespaceusedtoimportJavapackages),andcanthenbereferencedelsewhereinthefileorbycodethatdeserializesthefile,viatheBXMLSerializer#getNamespace()method: ? 1 2 3 id="myFoo"/> ∙The include>tag,whichisusedtoincludeexternalresources,includingnestedBXMLdocuments.Forexample,thefollowingmarkupwouldembedthecontentsofthe"my_include.bxml"fileinthecurrentdocument: ? 1 2 3 includesrc="my_include.bxml"/>; BXMLincludesareoftenusedforpartitioningcontentintomanageablepieces(forexample,whenworkingonlargeapplicationsorwithmultipledevelopers,orwhendefiningreusablecontenttemplates).Bydefault,eachincludeisassigneditsownvariablenamespacetoavoidnamingcollisionswithancestordocuments;however,thisbehaviorcanbeoverriddenbyaddingan"inline"attributewithavalueof"true"tothe include>tag. ∙The script>tag,whichdefinesablockofscriptcodewithinaBXMLfile.Forexample,thefollowingscriptblockdefinesafunctionnamedfoo(),whichcanthenbecalledfromotherscriptblocksinthedocument: ? 1 2 3 4 5 6 7 script> functionfoo(){ .... } script> ThedefaultscriptinglanguageisJavaScript,butanyJVM-compatiblescriptinglanguagecanbeused.Thelanguageprocessinginstructionisusedtospecifyadifferentlanguage;forexample,Groovy: ? 1 2 3 languagegroovy? > Scriptblockscanalsobedefinedineventhandlerattributesandelements.Scriptingandeventhandlingarediscussedinmoredetailbelow. ∙The define>tag,whichisusedtodeclareobjectsthatwillexistoutsideofthehierarchybutmaybereferredtoelsewhere. Forexample,thefollowingdefineblockdeclaresaninstanceofFoonamed"myFoo"thatisnotprocessedaspartofthedocumentflow(inotherwords,wouldnotbeaddedtoitsparentelement,ifithadone)butcanbereferredtobyvariablenamelaterinthedocument: ? 1 2 3 4 5 define> id="myFoo"/> define> ∙The reference>tag,usedtodereferenceapagevariable.Forexample,theFooinstanceinthepreviousexamplecouldbedereferencedasfollowselsewhereinthedocument: ? 1 2 3 referenceid="myFoo"/> Whereverthistagappears,itwilleffectivelybereplacedbythevalueofthe"myFoo"variable. Thevariabledeferenceoperator("$")canalsobeusedtodereferencepagevariables.Thisisdiscussedinmoredetailbelow. Elements InBXML,anXMLelementthatdoesnotbeginwiththereserved"bxml"namespaceprefixrepresentsoneofthefollowing: ∙Aclassinstance ∙Apropertyofaclassinstance ∙A"static"property Eachoftheseisdiscussedinmoredetailbelow. ClassInstances Ifanelement'stagnamebeginswithanuppercaseletter(anditisnota"static"propertysetter;seebelow),itisconsideredaclassinstance.WhenBXMLSerializerencounterssuchanelement,itcreatesaninstanceofthatclass.Asdiscussedabove,theXMLnamespaceprefixisusedtodeterminetheJavapackagetowhichtheclassbelongs. Forexample,thefollowingBXMLwouldproduceaninstanceoftheorg.apache.pivot.wtk.Labelclasspopulatedwiththetext"Hello,World! ": ? 1 2 3 "xmlns="org.apache.pivot.wtk"/> ClassinstanceelementsinaBXMLfilewilloftenrepresentinstancesofJavabeantypes.Internally,BXMLSerializerusesaninstanceoforg.apache.pivot.beans.BeanAdaptertowraptheinstantiatedclassandinvokeitssettermethods.Thisclassimplementstheorg.apache.pivot.collections.Dictionaryinterfaceandallowsacallertogetandsetbeanpropertyvaluesaskey/valuepairs. However,iftheelementrepresentsatypethatalreadyimplementstheDictionaryinterface(suchasorg.apache.pivot.collections.HashMap),itisnotwrappedanditsdictionarymethodsareuseddirectly.Forexample,thefollowingBXMLcreatesaninstanceoforg.apache.pivot.collections.HashMapandsetsits"foo"and"bar"valuesto"123"and"456",respectively: ? 1 2 3 Howthe"foo"and"bar"attributesarehandledisdiscussedinmoredetailbelow. InstanceProperties Elementswhosetagnamesbeginwithalowercaseletterrepresentinstanceproperties.Aninstancepropertyelementmayrepresentoneofthefollowing: ∙Apropertysetter ∙Aread-onlysequence ∙Aread-onlydictionary ∙Aneventlistenerlist PropertySetters Iftheelementrepresentsapropertysetter,thecontentsoftheelement(whichmustbeeitheratextnodeoranestedclassinstanceelement)arepassedasthevaluetothesetterfortheproperty.Forexample,thefollowingBXMLcreatesaninstanceoftheLabelclassandsetsthevalueofthelabel's"text"propertyto"Hello,World! ": ? 1 2 3 4 5 Thisproducesthesameresultastheearlierexamplewhichusedanattributetosetthe"text"property: ? 1 2 3 "xmlns="org.apache.pivot.wtk"/> ThefollowingexamplecreatesaninstanceofListViewandsetsthevalueofits"listData"propertytoaninstanceoforg.apache.pivot.collections.ArrayListthathasbeenpopulatedwithseveralinstancesoforg.apache.pivot.wtk.content.ListItem: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 xmlns: collections="org.apache.pivot.collections" xmlns: content="org.apache.pivot.wtk.content"> ArrayList> ListItemtext="A"/> ListItemtext="B"/> ListItemtext="C"/> ArrayList> Read-OnlySequences Ifthepropertyrepresentsaread-onlysequence(abeanpropertywhosegetterreturnsaninstanceoforg.apache.pivot.collections.Sequenceandhasnocorrespondingsettermethod),thecontentsoftheelementareaddedtothesequence.Forexample,the"tabs"propertyoftheorg.apache.pivot.wtk.TabPaneclassisaread-onlysequencerepresentingthetabpane'stabcomponents.TabscanbeaddedtoaTabPaneinBXMLasfollows: ? 1 2 3 4 5 6 7 8 Read-OnlyDictionaries Apropertyelementmayalsorepresentaread-onlydictionary(abeanpropertywhosegetterreturnsaninstanceoforg.apache.pivot.collections.Dictionarybuthasnocorrespondingsettermethod).Forexample,the"userData"propertyoftheorg.apache.pivot.wtk.Componentclassrepresentsaread-onlydictionary: ? 1 2 3 4 5 6 " xmlns="org.apache.pivot.wtk">
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- bxml 详解 教程