How to output a String with JRecord - java

I am trying to convert a XML/Bean (either one) to a fixed length flatfile with JRecord. I am not able to output it correctly to a String in file. I can only get an output as binary.
So I will just convert a XML/Beant to String. Not to a Binary Fixed Length and so on.
Any chance of someone who have solved this issue with JRecord?
Any other Framework with example´you can reccomend? And with an example.

I'm the author of JRecordBind, a Java library that does the same as JRecord (I think) and that it's based on XML Schema
The are a couple of examples on the homepage, while more are available as input for the tests
https://github.com/ffissore/jrecordbind/tree/master/jrecordbind-test/src/test/resources

Related

Overflow when inserting an specific arithmetic value

I'm currently working on generating a Word document file from a Word Template using data from a JSON variable in java. I'm using the ASPOSE library.
I have a problem with a specific variable. This variable should print a numeric(float) value. However, when the value is 100.0 ,it is sometimes printed as -2147483648 (-2^31) instead of 100.0.
I have to mention that when the value is a double digit float (for example 35.5 and 99.9),the value of the variable is printed correctly. Also when the input value is integer(e.g 100),the correct value is also printed correctly.
I have also tried to use some format strings(<<[variableName]:"0.##">>) inside the expression tag in the Word Template file, but it didn't fix it.
Here is the DOCX template
This is the input value from JSON file
Here is the generated result
#aggelos991
Using the latest available version of Aspose.Words for Java (i.e. 22.2), I was unable to reproduce the issue. Moreover, we had a similar issue in the past, so it seems like you are using an outdated version of Aspose.Words for Java. Could you please upgrade to the latest version and let us know if the issue is still present?
Float type is in the 32bit format, and 100.0 is in the 64bit format(double). 100.0f must be used for proper use.

IIB - convert BLOB to String using Java Compute Node

So I have a simple message flow with a File Read node, parsing a .txt (saying whatever) to BLOB, which I have to convert to a string in a Java Compute Node. Never used JAVA, how do I go about this?
Then I have to give the string a new value (whatever) and switch the logical tree body element to the new value.
Should be simple, but still a steep learning curve for me, out of nowhere. All helps are appreciated. :)
When parsing to BLOB, you end up with a byte array in assembly.getMessage().getRootElement().getLastChild().getLastChild(), and converting that to String should be easy:
String(byte[] bytes, Charset charset)
You can get the charset from the Preperties subtree.
You can read about accessing the message tree parts here:
https://www.ibm.com/support/knowledgecenter/en/SSMKHH_9.0.0/com.ibm.etools.mft.doc/ac30330_.htm
Just in case, one other way to do it would be to parse the input file with a dedicated parser directly (DFDL, ...). If one day your document is not in the format you expected, it will throw a proper error and won't crash on the java line trying to cast as a string something that is not a string. It might be too complex for your case (and also useless), but if you are learning, I would recommand you to play with the parsers so you won't have to learn about it for futur cases.
But reading as a BLOB is totally fine as long as you keep in minds that it means litteraly anything could be read, so the java solution is totally fine as long as you handle it properly (try/catch/throw).

Kaitai code writing

I recently started kaitai-struct for dealing with arbitrary binary formats. I have created the .ksy file for my data and parsed it to targeted language that is java. Now can anyone point me how to pass the input file that has the data and how to get the data that is parsed as output so that I can write code to manipulate that data to my requirements? Is there any tutorial on how to write code depending on the data we get.
Thanks in advance.
First you have to generate Java classes from the .ksy file using the Kaitai Struct Compiler or the WebIDE. You can find more information how to use the compiler in the Kaitai user guide.
If you use the WebIDE then just simply right-click on your .ksy file and select the Generate parser > Java menu item.
After you have the generated Java code, you can parse a structure directly from a local file like this:
AnExampleClass output = AnExampleClass.fromFile("an_example.data");
// ... manipulate output ...
Or you can parse a structure from a byte array (byte[]):
AnExampleClass output = new AnExampleClass(new KaitaiStream(byteArray));
// ... manipulate output ...
Note that parsing from non-seekable streams (i.e. FileInputStream, BufferedInputStream, etc) is not supported and probably won’t be supported, as a lot of parsing functionality in KS relies on seek support.
You can read the generic documentation how to use the API here and you can find the Java-specific documentation here.
The answer from koczkatamas is outdated.
There are now specific implementations.
The snippet would be
AnExampleClass output = new AnExampleClass(new ByteBufferKaitaiStream(byteArray));
See this issue for more details

There is any way/method to append a File(e.g: jpg,png,pdf) in csv file with Java programming language

We currently doing research on how append files in CSV file with Java programming language.
Therefore, we looking for some input & guidance on this, which will much useful for me.
Your help & guidance will be much appreciated.
Thanks in advance.
No, there is not, but the reason why is not specific to the Java programming language. Rather, it pertains to the nature of CSV in general.
CSV stands for "comma separated values", and it is a way to store database information in plaintext. If you read RFC 4180, you'll see a formal definition outlined there.
Part of the definition of CSV is that it is stored in plain text. That means that only numbers and strings can be stored in CSV files.
Thus, it is impossible to store image files in CSV files, unless you serialize your images into strings. More info on how to do that is here: Serialize image to a StringHow can I convert an image to a base64 string using Java?.
Of course it's possible. All you need is to convert your binary data to a Base 64 string representation.
Apache commons codec does this for you:
Just use:
byte[] binaryData = someMethodToReadYourFileIntoBytes();
String encodedData = Base64.encodeBase64String(binaryData);`
Refer to the javadoc for more information and other options on how to go about it.
Hope it helps.

thrift character encoding, perl to java

I have a complex situation that I'm trying to deal with involving character encoding.
I have a perl program which is communicating with a java endpoint via thrift, the java is then using the data to make a request to a legacy php service. It's ugly, but part of a migration plan so needs to work for a short while.
In perl a thrift object is created where some of the fields of the thrift object are json encoded strings.
The problem is that when perl makes the request to java, one of the strings is as follows (this is from data:dumper and is subsequently json encoded and added to thrift):
'offer_message' => "<<>>
&&
\x{c3}\x{82}\x{c2}\x{a9}©
<script>alert(\"XSS\");</script>
https://url.com/imghp?hl=uk",
However, when this data is received on the java side the sequence \x{c3}\x{82}\x{c2}\x{a9} has been converted so in java we receive the following:
<<>>\\n&&\\nÃ�Â�Ã�©©\\n<script>alert(\"XSS\");</script>\\nhttps://www.google.com.ua/imghp?hl=uk
The problem is that if I pass the second string to the legacy php program, it fails, if I pass the string taken from the dump of the perl hash, it succeeds. So my assumption is that I need to convert the received string to another encoding (correct me if I'm wrong, I'm not sure that this is the right solution).
I've tried taking the parameters received in java and converting them to every encoding I can think of, however it doesn't work. So for example:
byte[] utf8 = templateParams.getBytes("UTF8");
normallisedTemplateParams = new String(utf8, "UTF8");
I've been varying the encoding schemes in the hope I find something that works.
What is the correct way to solve this? For a short time this messy solution is my only option while other re-engineering is happening.
The problem in the end difficult to diagnose but simple to resolve. It turned out that the package I was using to convert in Java was using java's default encoding of UTF-16. I had to modify the package and force it to use UTF-8. After that, everything worked.

Categories