Hello,
Requirement: I need to extract all the tags used in a word template (docx) and then find the user defined identifiers used in the expressions of the tags. Then I have to prepare JSON schema of these identifiers so that it can be checked against master data model. I am going to use JSON data source to generate documents from the template.
Thus identifier used in foreach must be array. The identifier with dot in it would be property of object.
<<foreach [in node.Items]>>
here Items
is an object array inside node
object.
<<if [node.Active]>>
here Active
is boolean
property of node
object.
I note that the Aspose Java library give list of nodes in Word document, but it does not give list of tags used in template. Also I see that there is no model to represent various tags that can be used in a template as per template syntax. For example, I was hoping to see wrapper model classes like IfTag, ElseIfTag, ElseTag, ForEachTag, ScriptTag, etc.
These tags would internally store the details so that there is no need to parse the text of the tag body.
Parsing using hand coded logic would be very difficult and error prone. Probably we will never it get it correct. It is about writing a language parser. And the grammar of tag is subset of C# syntax.
-
Am I missing anything? Is there already such model present in the Aspose Words Java library?
-
Is such model populated when loading the word template or at the time of document generation?
-
Or there is other way used to validate the syntax on the fly without need to populate first such model for representing the tags used in a template?
If not, probably we need to generate parser for the template tag syntax using tools like ANTLR.
- Do we have lexical grammar available for tags syntax of the template?
- Can we use lexical grammar of language like C# if that works for Aspose tags syntax?
Please guide me,
Sachin Raverkar
Pune, India
@sagaofsilence.dev
There is no built-in feature to address that at the moment. WORDSNET-17454 was already logged to implement it. However, its development is postponed and there is no ETA.
This comment provides a basic idea on how to collect contents of simple expression tags. It can be further extended to take all possible tags into account.
No, and we have no plans to implement such a feature.
If your sole goal is to validate template syntax, then the only proper way to do this is trying to build a report, because LINQ Reporting Engine needs actual data sources to answer the question whether template syntax is guaranteedly valid. For example, the engine cannot validate tag <<if [obj.SomeValue]>>
until actual data sources are present and it becomes clear whether obj
has SomeValue
as its member or not.
By default, while building a report, the engine throws an exception when it encounters invalid template syntax. However, syntax errors can be inlined to templates instead. See Inlining Syntax Error Messages into Templates for details on how to achieve this.
Also, we have plans to support visual assistance for template preparation using MS Word (logged as WORDSNET-17417), but there is no ETA at the moment.
Syntax of all tags is described in detail at sections of Template Syntax.
LINQ Reporting Engine supports a limited subset of C# language in its template syntax. See Composing Expressions and its subsections to know more about the limitations.
@sagaofsilence.dev
Your post is now linked to the following ticket(s) in our internal issue tracking system. We will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-17454,WORDSNET-17417
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
Thanks a lot for quick response. It is highly appreciated.
I have seen the idea to parse the tag with regular expressions. But coming up with regular expressions that can parse all the scenarios allowed by LINQ syntax would be huge effort.
And probably I would use that as a last option.
Proper way would be to generate a parser from the lexical grammar for the tags / LINQ.
The template syntax link in Aspose documentation is not in grammar formats like lexical grammar. It would not be useful for generating the parser using tools like ANTLR.
For example, C# lexical grammar for if statement:
if-statement:
if ( boolean-expression ) embedded-statement
if ( boolean-expression ) embedded-statement else embedded-statement
We need syntax in this lexical grammar form.
Probably Aspose Word for Java library does not need such lexical grammar to find syntax errors.
My task is not just finding the syntax errors but to transform the variables used in the expressions of the tags into JSON schema. For that I need to dissect the tag into parts and make sense of it.
I have been reading the Aspose documentation and forum threads for last one month, so far I did not find any answer to level of parsing I need, so I thought I should post my query.
Regards
Sachin
@sagaofsilence.dev
Internally, LINQ Reporting Engine does not use a grammar format, so unfortunately, we cannot provide what is requested. However, as mentioned earlier, syntax of tags and expressions is described in detail in our documentation, so one may transform this information to any grammar format they need.
Unfortunately, besides of already suggested options, we have nothing new to add in this regard. You may either wait for resolution of WORDSNET-17454 or try to implement extraction of syntax tags using an approach that best suits your needs. We apologize for any inconvenience.
Hi @sagaofsilence.dev,
Just checking in – were you able to find a solution to the problem? I’m dealing with a similar challenge and seeking a way to validate the syntax. Since regex might not suffice due to potential complexities in statements like:
<<foreach [ client in clients]>>
<<if [client.Country == "New Zealand"]>>
<<[client.Name]>> <<[client.LocalAddress]>>
<<else>>
<<[client.Name]>> <<[client.Country]>> <<[client.LocalAddress]>>
<</if>>
<</foreach>>
Currently, I’m using the ANTLR tool (http://lab.antlr.org/
) for tag validation using lexer and parser. Could you assist me in resolving it?
Thank you,
Abhishek
Hello @sagaofsilence.dev ,
we have a similar problem, see my recent message : Building templates - #7 by miniseb31
did you succeed to use ANTLR tool ? did you integrate it in ms word ?