Linq default value for tags


#1

I am using linq.
In my template “docx_in.docx” there is tag:
<<[Dummy]>>

java code:
InputStream inputStream = new FileInputStream(new File("/home/aspose/img/docx_in.docx"));
Document document = new Document(inputStream);
ReportingEngine engine = new ReportingEngine();
engine.buildReport(document, values, keys);
document.save("/home/aspose/img/docx_out.docx");

if I do not give parameter “Dummy” into keys I take error :

java.lang.IllegalStateException: An error has been encountered at the end of expression ‘Dummy]>’. Can not get the value of member ‘Dummy’ on type ‘class com.aspose.words.net.System.Data.DataRowCollection’.

Is there way to take in document empty string instead <<[Dummy]>>?
Or to take list of all tags to process them manually?
Like document.getMailMerge().getFieldNames()?
Thanks!


#2

@rubcs,

While using the latest version of Aspose.Words i.e. 18.7, we managed to reproduce this issue on our end. We have logged this issue in our bug tracking system. The ID of this issue is WORDSNET-17129. Your thread has also been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.


#3

@rubcs,

Please see these input/output Word document: Docs.zip (17.5 KB)

Please try running the following code with latest version of Aspose.Words for Java i.e. 18.7. Hope, this helps.

Document doc = new Document("D:\\Temp\\Dummy.docx");

ReportingEngine engine = new ReportingEngine();
engine.setOptions(ReportBuildOptions.ALLOW_MISSING_MEMBERS);
engine.buildReport(doc, "A VALUE", "");

doc.save("D:\\Temp\\awjava-18.7.docx");

#4

Many thanks! Everything is working!


#5

Hello!

Usually (<<[Dummy]>>) is working good.
However, if this placeholder is used:

  • in users functions
  • in linq procedures: foreach; if etc.

For template:

<body>
    <pre>
    <table>
        <tr>
            <td><< foreach [item in ProductTable]>><<[item.Id]>><< /foreach>></td>
        </tr>
        </table>
    </pre>
</body>

and value of placeholder ‘ProductTable’ is not filled, we get an error like:

java.lang.IllegalStateException: An error has been encountered at the end of expression ‘item in ProductTable]>’. Can not get the value of member ‘ProductTable’ on type ‘class …’.

Is it possible to have the following: when there is no ‘ProductTable’ value to interpret it like an empty DataSource?

Thanks!


#6

@rubcs,

Please ZIP and attach your input Word document and piece of source code to reproduce the same problem on our end. We will then investigate the issue on our end and provide you more information.


#7

[quote=“awais.hafeez, post:3, topic:179483”]…
[/quote]

My mistake… It seems everything works. Excuse for troubling.


#8

Hi!

With empty placeholders, everything works good.
But now we have problems with empty variables.
If we assign the value of a placeholder to the variable, and then we try to use it, we get errors.

A simple example.
Variable ‘varCollection’, placeholder ‘collection’ is not filled.

Template:
<< var [varCollection = collection] >>
<< foreach [element in varCollection] >> element << / foreach >>

Error:
An error has been encountered at the end of the expression ‘element in varCollection]>’. Can not iterate over an instance of type ‘class java.lang.Object’.

Is it possible not to generate an exception in this situation?

Thank you!


#9

@rubcs,

Please ZIP and attach your simplified Word document and piece of source code to reproduce the same problem on our end. We will then investigate the issue on our end and provide you more information.


#10

Hi,

I created unit test with this problem: https://gitlab.com/pegoopik/aspose-research/merge_requests/4
error:>

java.lang.IllegalStateException: An error has been encountered at the end of expression 'element in varCollection] '. Can not iterate over an instance of type ‘class java.lang.Object’.

at asposewobfuscated.zzP3.zzZu(Unknown Source)
at asposewobfuscated.zzP3.zzZ(Unknown Source)
at asposewobfuscated.zzP3.zzY(Unknown Source)
at asposewobfuscated.zzOP.zzOC(Unknown Source)
at asposewobfuscated.zzVT.zzWb(Unknown Source)
at asposewobfuscated.zzOP.zzZ(Unknown Source)
at asposewobfuscated.zzDA.zzZ(Unknown Source)
at com.aspose.words.ReportingEngine.buildReport(Unknown Source)
at ru.pegoopik.aspose.linq.tests.empty_placeholder.EmptyVarTest.testEmptyCollection(EmptyVarTest.java:58)


#11

@rubcs,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-17670. We will further look into the details of this problem and will keep you updated on the status of the linked issue.


#12

Hi! We see that the ticket is WORDSNET-17670 transferred to the status closed. Can you tell me the solution?


#13

@rubcs,

WORDSNET-17670 is actually not a bug in Aspose.Words. The exception is intended as it indicates wrong template syntax.

The reason for the exception in this case is that ReportBuildOptions.ALLOW_MISSING_MEMBERS makes the engine to treat missing members as null literals (see the option’s description for more information). But a variable is not a null literal, even if its value is set to null through a literal. Since the type for the variable in the template is not set explicitly and it cannot be determined implicitly (from a null literal), its type is considered to be Object. But a foreach tag requires an expression of type implementing Iterable. So the exception is thrown.

However, there is a simple way to make it work. Please check the attached modified template (html (3) modified.html). Please note that it is also required to add a known type for the engine as shown in the following code snippet:

engine.getKnownTypes().add(Iterable.class);

Attachment: html (3) modified.zip (296 Bytes)


#14

I didn’t know that I could explicitly specify a type of the variable. This solution is appropriate. Thanks!


#15

@rubcs,

Thanks for your feedback. It is great that the solution worked for you. In case you have any further inquiries or need any help, please let us know.


#16

A post was split to a new topic: Can not iterate over an instance of type ‘System.Object’. ’