Results 1 to 2 of 2
  1. #1
    Steve641 is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default Java idiom to define a call patttern matching a CFG

    Assume a trivial XML-like document format with two terminal elements <A/> and <B/> and one recursively nestable element <Z>...</Z>. Assume the objective is to construct strings using the Java type system to allow new (syntactically valid) documents to be defined as cleanly as possible using Java. I'm envisioning a base class that can be hidden away in a class library - then for the definition of individual documents to be as intuitive and neat as possible.

    Ignoring, for a moment, the recursively nestable element - this code represents one 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() { return doc.toString(); }
    }
    --

    Using StaticDocument as a base class, it is possible to define documents using only Java to describe their abstract structure. An example document with sequential composition of A and B elements:-
    --
    public class ExampleDocument extends StaticDocument
    {
    public ExampleDocument()
    {
    nonNestedA();
    nonNestedB();
    nonNestedA();
    }
    }
    --

    Things get a bit more complicated when trying to extend this approach to embrace the recursively nestable element <Z>...</Z>. In C++ I might have used a protected inner-class of StaticDocument to define the Z element - then used the enclosing blocks in ExampleDocument to ensure every <Z> is matched with a </Z> - employing the RAII idiom/pattern. In C#, I could exploit the IDisposable interface and 'using' to a similar end. Another possibility is to pass an argument to a nestedZ method and have that argument encapsulate what to execute to generate the document surrounded by the Z element... but this is somewhat cumbersome - especially as Java(6) doesn't support either delegates or lambda expressions natively.

    [Aside: I'm aware of tools like JaxB for XML... and that I can embed XML files in jars - but the question here is intended to capture a more involved objective - the XML just exhibits the same abstract structure.]

    So.. finally... the question: Is there consensus among the Java community as to the best way to tackle problems like this? The objectives are to minimise extraneous intermediate objects during construction of documents - and for the construction of documents to be both as compact and as 'natural' (i.e. easily recognisable as matching the corresponding XML document) as possible.

    I'd be interested to hear some expert opinions... and/or pointers to relevant articles.

  2. #2
    jennifer is offline Member
    Join Date
    Sep 2013
    Location
    United Kingdom
    Posts
    1
    Rep Power
    0

    Default Re: Java idiom to define a call patttern matching a CFG

    So.. finally... the question: Is there consensus among the Java community as to the best way to tackle problems like this? The objectives are to minimize extraneous intermediate objects during construction of documents - and for the construction of documents to be both as compact and as 'natural' (i.e. easily recognizable as matching the corresponding XML document) as possible.

    I'd be interested to hear some expert opinions... and/or pointers to relevant articles.
    I am amazed to know that, my problem is exactly what you asked.... The objectives are to minimize extraneous intermediate objects during construction of documents.... Any help please.

Similar Threads

  1. call by value and call by reference in java
    By sandeepsai39 in forum New To Java
    Replies: 7
    Last Post: 08-12-2010, 12:03 PM
  2. Replies: 1
    Last Post: 07-03-2010, 03:56 AM
  3. Replies: 0
    Last Post: 06-29-2010, 09:16 PM
  4. How to define constants in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:39 PM
  5. What is an interface in the java? how to define
    By sivasayanth in forum New To Java
    Replies: 3
    Last Post: 01-14-2008, 05:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •