imatix / gsl

iMatix GSL code generator
http://www.imatix.com
GNU General Public License v3.0
538 stars 107 forks source link

Inclusions process for XML files #64

Open canercandan opened 9 years ago

canercandan commented 9 years ago

Something that I am using quite a lot with my models is the ability to include XML files into others. I was usually doing it in hand-writing every time a GSL script to include a specific file (similarly to the example provided by the GSL guide) but honestly that sucks ;-) and I guess it's not in the MOP spirit. That's why I created a recursive function that includes every single node named include with an attribute file.

Here's an example of model in XML including other XML files:

script.gsl:

.template 0
process_inclusion()
.endtemplate

project.xml:

<project script="script.gsl">
  <include file="file1"/>
  <include file="file2"/>
</project>

file1.xml:

<file1_markup/>

file2.xml:

<file2_markup/>
<file2_markup/>
<file2_markup/>
<include file="file3/file3"/>

file3/file3.xml:

<file3_markup/>
<file3_markup/>

Here is the actual source code of the function:

.template 0

function global.process_inclusion(context)
  my.context ?= project

  my.found = 1
  while my.found
    my.found = 0
    for my.context.include as i
      # include file [i.file].xml
      xml to my.context from "$(i.file:).xml"
      delete i
      my.found = 1
    endfor
  endwhile

  for my.context. as ii where ii.name()?"include" <> "include"
    process_inclusion(ii)
  endfor
endfunction

.endtemplate

Another solution could be to use the XInclude XML inclusion mechanism, here is the w3c rfc: http://www.w3.org/TR/xinclude/

hintjens commented 9 years ago

This is really neat, your other pieces of code too.

Why not add these to the GSL repository, perhaps in a new directory?

On Fri, Dec 12, 2014 at 2:46 PM, Caner Candan notifications@github.com wrote:

Something that I am using quite a lot with my models is the ability to include XML files into others. I was usually doing it in hand-writing every time a GSL script to include a specific file (similarly to the example provided by the GSL guide) but honestly that sucks ;-) and I guess it's not in the MOP spirit. That's why I created a recursive function that includes every single node named include with an attribute file.

Here's a example of model in XML including other XML files:

script.gsl:

.template 0 process_inclusion() .endtemplate

project.xml:

file1.xml:

file2.xml:

file3/file3.xml:

Here is the actual source code of the function:

.template 0

function global.process_inclusion(context) my.context ?= project

my.found = 1 while my.found my.found = 0 for my.context.include as i

include file [i.file].xml

  xml to my.context from "$(i.file:).xml"
  delete i
  my.found = 1
endfor

endwhile

for my.context. as ii where ii.name()?"include" <> "include" process_inclusion(ii) endfor endfunction

.endtemplate

— Reply to this email directly or view it on GitHub https://github.com/imatix/gsl/issues/64.

canercandan commented 9 years ago

Hi @hintjens,

Thanks I appreciate, regarding your suggestion, I have no idea about the best place in the GSL repository to host them.

BTW, is there an easy way to add this new function as a builtin GSL function ? Maybe it could even be better to reimplement it in C code. What do you think ?

hintjens commented 9 years ago

GSL uses .gxl files to build new classes in C, generating the hooks to plug that into the GSL main. It's not trivial though.

On Sat, Dec 13, 2014 at 10:13 AM, Caner Candan notifications@github.com wrote:

Hi @hintjens https://github.com/hintjens,

Thanks I appreciate, regarding your suggestion, I have no idea about the best place in the GSL repository to host them.

BTW, is there an easy way to add this new function as a builtin GSL function ? Maybe it could even be better to reimplement it in C code. What do you think ?

— Reply to this email directly or view it on GitHub https://github.com/imatix/gsl/issues/64#issuecomment-66870381.