nk3 rewrite -> nk4 gramar

Poster Content
nk4um User
Posts: 111
January 8, 2010 18:04prototype
Actually after your previous comment I was abot to get it working as a prototype:
<overlay>
  <prototype>JavascriptPrototype</prototype>
  <script>res:/wrapper/jsreq.js</script>
  <grammar>jsrun:/
    <group>
      <regextype="relative-path" />
      <regextype="nmtoken" />.js
    </group>
    <groupmin="0" max="*">+
      <groupname="argName">
        <regextype="nmtoken" />
      </group>@
      <groupname="argValue">
        <regextype="active-escaped-uri-loose" />
      </group>
    </group>
  </grammar>
</overlay>


I had to change my implementation a bit but it made it mostly cleaner.  To get the original script name I now need to parse the request uri, but that''s the right thing to do in the first place and something I was not able to do previously.  But 2 questions remain: 
- is there a utility class for parsing active-type uris?  org.netkernel.layer0.urii.ParsedIdentifierImpl if ok for extracting the arguments, but doesn''t have a way to get the scheme or scheme-specific part (i.e., java.net.URI gives you the scheme, scheme-specific-part, and query as per the URI spec where the query is everything after ''?'', but in a nk active-type uri the query is everything after the first ''+'').  I could use a grammar to parse this, but I don''t want the identifier as a separate argument.
- what is the difference between arguments (RequestReadOnly.getArgument*) and parameters (RequestContest.getParamValue)?  I did find that in this prototype, the "script" argument to the prototype is a parameter and the grammar groups are arguments, but otherwise the distinction isn''t terribly clear.
nk4um Moderator
Posts: 498
January 8, 2010 13:56I'll just shut up!
Hmmm. I think you have found the best approach without my meddling. Using the generic args group is the best way to recreate the NK3 rewrite. The prototype approach won''t work here because you need to dynamically specify the javascript source on each request.

Cheers,
Tony
nk4um Moderator
Posts: 498
January 7, 2010 13:30
Hi Jeff,

looking at this in fresh light after the holiday I realise that the mapper will not pass arbirary arguments through. This is a limitation of the design because of the way it maps arguments from the inbound grammar to the outbound request.

My comment on your grammar not fitting the active syntax wasn''t a criticism - sorry if that was how it was interpretted :-) It was just to contextualise the problem of trying to get your grammar to work.

What you are trying to do as absolutely within what NK4 should be cable of. I''m going to send you another post with details of how this might be done easier with a Javascript runtime prototype rather than using the mapper. This functionality isn''t really document well at the moment but it allows you to instantiate a javascript endpoint within your application module without need to map the inbound request.

Let me work on an example...

Tony
nk4um User
Posts: 111
December 30, 2009 22:00
using argName and argValue stil didn''t seem to pass the arbitrary arguments through to the declarative request.  I found another solution, by putting the "other" arguments into a generic "args" argument, and then parsing that within my wrapper.

My identifier doesn''t fit the active specification, because from the conceptual level I''m not calling the javascript runtime and passing it an argument, I''m just calling an accessor implemented in javascript.  I''m also following what I had to do in nk3, where I could not just rewrite ffcpl:.*.js -> active:javascript+operator@$1, because then I would have no way to request the source file I was working with to begin with (because THAT is also a request matching ffcpl:*.js, and would itself get rewritten ...)

The ability to do transformations on the script source is a rather nice capability that deserves more exploration.  It lets you move all the repeated stuff elsewhere and just write what is needed for the specific functionality, as well as (in the case of javascript at least) setting up an environment and standard libraries, etc. 
nk4um Moderator
Posts: 498
December 24, 2009 10:44
Hi Jeff,

It''s a shame your identifier doesn''t quite fit into the capabilities of the active grammar specification because the first part of your identifier changes i.e. jsrun:*, if was fixed to jsrun:whatever and the script was a standard argument i.e. +script@script.js then you''d be fine. :-(

However you can always resort to the lower level raw grammar, here is how you can do it:
<grammar>jsrun:
  <groupname="script">/
    <regextype="relative-path" />.js
  </group>
  <groupmin="0" max="*">+
    <groupname="argName">
      <regextype="nmtoken" />
    </group>@
    <groupname="argValue">
      <regextype="active-escaped-uri-loose" />
    </group>
  </group>
</grammar>

The trick you were missing was the syntax of the standard varags group. This is what the the active grammar uses and the names are picked up by NKF. It is undocumented at the moment but the argName,argValue pairs are converted into arguments within NKF.

Cheers,
Tony
nk4um User
Posts: 111
December 23, 2009 23:39nk3 rewrite -> nk4 gramar
I''m trying to convert a nk3 module to nk4 and I can''t figure out how to make my simple rewrite rule:
<rewrite>
  <match>jsrun:([^+]*.js)(.*)</match>
  <to>active:javascript+operator@ffcpl:/wrapper/jsreq.js$2+script@ffcpl:$1</to>
</rewrite>

into a proper nk4 grammar.  I''m trying
<config>
  <endpoint>
    <id>urn:jsrun:wrapper</id>
    <name>JSRun handler</name>
    <grammar>jsrun:/
      <groupname="script">
        <regextype="relative-path" />
        <regextype="nmtoken" /> .js
      </group>
      <groupmin="0" max="*">+
        <groupname="args">
          <regextype="alphanum" />@
          <regextype="alphanum" />
        </group>
      </group>
    </grammar>
    <request>
      <identifier>active:javascript</identifier>
      <argumentname="operator">res:/wrapper/jsreq.js</argument>
      <argumentname="script">res:/[[arg:script]]</argument>
    </request>
  </endpoint>
</config>


The grammar works for simple calls, but how do I pass through arbitrary arguments in the declarative request?  The active grammar has the <varargs/> wildcard match, but the more general form doesn''t seem to be able to pass those.