Injectable, Poppable Stack

Background

A project involves converting a relational expression map (rxm) to an XML/SQL statement. The map resembles:

root               > people,            # "root" keyword starts the document
person             > person,            # maps table context to a node
.first_name        > first,             # maps a column to a node
.last_name         > name/last,         # maps a column to an ancestor node
.age               > @age,              # @ maps a column to an attribute node
account.person_id +> person.person_id,  # +> performs an INNER JOIN
account            > account,           # context is now "account" node
.id                > @id,               # account id attribute
^,                                      # pop stack to previous context
address            > address,           # switch context to "address" node
.*,                                     # .* globs all columns
account.person_id -> company.person_id, # -> performs an OUTER JOIN
;                                       # Starts the optional WHERE clause

The rxm is converted into Java classes using ANTLR.

Walking through the AST uses a Visitor pattern that calls methods for entering and exiting various parts of the tree. For example:

  @Override
  public void enterRoot( QueryParser.RootContext context ) {
    System.out.println( "Root: " + context.getChild(2).getText() );
  }

  @Override
  public void exitRoot( QueryParser.RootContext context ) {
    System.out.println( "<< root" );
  }

Problem

The code needs to generate an XML/SQL statement along the lines of:

XMLROOT (
  XMLELEMENT (
    NAME people,
    XMLELEMENT (
      NAME person,
      XMLATTRIBUTE( ... )
    )
  ),
  VERSION ’1.0’,
  STANDALONE YES
)

At the time that either enterRoot or exitRoot are called, it is not known whether there will be another line mapped using rxm. Ideally, I’d like to write:

  @Override
  public void enterRoot( QueryParser.RootContext context ) {
    String root = context.getChild(2).getText();
    getStack().push( "SELECT XMLROOT( XMLELEMENT( NAME " + root );
    getStack().push( "VERSION '1.0', STANDALONE YES )" );
  }

Question

Using a stack seems to be a good way to generate the query based on the visitor pattern.

What data structure would you use to convert a linear map into a hierarchical query?

Would it be feasible, for example, to just pop the last element from the stack (i.e., the line containing the closing parenthesis), store it locally, then push it back on afterwards?

2

Use a Tree data structure then convert it to a string using recursive polymorphic toString methods.


Let me reiterate your question in my own words to make sure I understand you correctly:

It appears that you are writing a Domain Specific Language (DSL) that you wish to convert directly your desired output via string manipulation without any intermediate representation. Your question is about your current solution and your choice of data structure required for building of the final string.

If your recursion calls get too deep, you can build a set of classes to walk the tree and build up the string as they visit each node using a depth first search. All this is assuming you have no circular references in your tree.

After re-reading the question, I think I have a better understanding of what you have implemented and what tools you are using. I had assumed you were skipping the whole process of AST generation because I missed that on my first few reads. Can you control how the visitors walk the tree? If start building your string with the deepest child and before you start walking back up the tree, you should just always be wrapping the previous string with opening and closing tags of the current node. Then there is no need to track whether or not you have to insert a closing tag or not, you just always do.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật