Java idiom to define a call patttern matching a CFG -



Java idiom to define a call patttern matching a CFG -

assume trivial xml-like document format 2 terminal elements , , 1 recursively nestable element .... assume objective build strings using java type scheme allow new (syntactically valid) documents defined cleanly possible using java. i'm envisioning base of operations class can hidden away in class library - definition of individual documents intuitive , neat possible.

ignoring, moment, recursively nestable element - code represents 1 potentially viable approach:-

public class staticdocument { private stringbuilder doc; protected staticdocument() {} protected void nonnesteda() { doc.append("<a/>"); } protected void nonnestedb() { doc.append("<b/>"); } public string tostring() { homecoming doc.tostring(); } }

using staticdocument base of operations class, possible define documents using java describe abstract structure. illustration document sequential composition of , b elements:-

public class exampledocument extends staticdocument { public exampledocument() { nonnesteda(); nonnestedb(); nonnesteda(); } }

things bit more complicated when trying extend approach encompass recursively nestable element .... in c++ might have used protected inner-class of staticdocument define z element - used enclosing blocks in exampledocument ensure every matched - employing raii idiom/pattern. in c#, exploit idisposable interface , 'using' similar end. possibility pass argument nestedz method , have argument encapsulate execute generate document surrounded z element... cumbersome - java(6) doesn't back upwards either delegates or lambda expressions natively.

[aside: i'm aware of tools jaxb xml... , can embed xml files in jars - question here intended capture more involved objective - xml exhibits same abstract structure.]

so.. finally... question: there consensus among java community best way tackle problems this? objectives minimise extraneous intermediate objects during construction of documents - , construction of documents both compact , 'natural' (i.e. recognisable matching corresponding xml document) possible.

i'd interested hear expert opinions... and/or pointers relevant articles.

you utilize builder design pattern , create nestable builders fluent interface represent desired structure.

this examples uses them create test info objects. applies scenario well.

java idioms

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -