Filehelpers Quoted Tab-Separated-Values error - filehelpers

I am getting an error using Quoted, Tab-Delimited input. The same input with no Quotes works perfectly. Here is a sample input line with '!' denoting a TAB character:
CN!"000012"!"Contents page 1 of 000012"
Here is the error message:
Line: 1 Column: 12. The field itemID is quoted but the quoted char: " not is just before the separator (You can use [FieldTrim] to avoid this error)
I am using the ClassBuilder, and here is the C# output for my generated Class:
[DelimitedRecord(" ")]
public sealed class CN
{
[FieldTrim(TrimMode.Both, " ")]
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
public System.String opCode;
[FieldOptional()]
[FieldNullValue(typeof(System.Int32), "0")]
[FieldConverter(typeof(blah.FileHelperClassBuilder+ItemIDConverter))]
[FieldTrim(TrimMode.Both, " ")]
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
public System.Int32 itemID;
[FieldOptional()]
[FieldTrim(TrimMode.Both, " ")]
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
public System.String itemContents;
[FieldOptional()]
[FieldNullValue(typeof(System.Int32), "0")]
[FieldConverter(typeof(blah.FileHelperClassBuilder+AccountConverter))]
[FieldTrim(TrimMode.Both, " ")]
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
public System.Int32? accountID;
[FieldOptional()]
[FieldTrim(TrimMode.Both, " ")]
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
public System.String alternateCode;
[FieldOptional()]
[FieldNotInFile()]
[FieldNullValue(typeof(System.Int32), "0")]
[FieldTrim(TrimMode.Both, " ")]
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
public System.Int32 sequence;
[FieldOptional()]
[FieldNotInFile()]
[FieldNullValue(typeof(System.Byte), "0")]
[FieldTrim(TrimMode.Both, " ")]
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
public System.Byte updateItemTime;
}
The DelimitedRecord Attribute indeed has a TAB char in-between the double-quotes.
I am using FileHelpers version 2.9.9.0.
I tried making the Quotes required for all Fields but the first, but no help. It is doing the same thing for several similar Classes. Basically, the error points to the first character after the close-quote for the first Quoted Field, which is a TAB character.
Thanks.

I solved the problem. I had begun to create my own transient Assembly from the same metadata which I was formerly feeding to the Filehelpers Class Builder (which produced the C# code shown here). However, I was not setting the FieldTrimAttribute TrimChars correctly, and was getting a default set of TrimChars supplied by Filehelpers.

Related

java.lang.NullPointerException while trying to update sql table

i'm trying to update "PA49_Debut"column with :
- the"PA49_Debut" value from the second table if the product exists there
- else "PA49_Debut" from the same table if the product exists in the previous rows
- else "PA49_Actuel" from the same table
take a look to the code in the repository
public interface FSrepository extends JpaRepository<FSmodel, String> {
#Transactional
#Modifying
#Query("Update FSmodel FS Set FS.PA49_Debut = CASE WHEN EXISTES (SELECT 1 FROM ISmodel SI where FS.Partnumber=SI.Partnumber)" +
" THEN SI.PA49 " +
"WHEN EXISTS (SELECT 1 FROM FSmodel FS2 WHERE FS.Partnumber=FS2.Partnumber AND FS.id>FS2.id )" +
" THEN FS2.PA49_Debut "+
" ELSE (FS.PA49_Actuel) "
+ "END" )
void setPA49Debut(); ```
these are my ISmodel and FSmodel entities
#Entity
public class FSmodel {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long id;
public String Partnumber;
public String Materialgroup;
public String Warehouse;
public Float Value;
public Float Stock;
public Float PA49_Actuel;
public Float PA49_Debut;
public String article_fg;
public Float Priceperunit;}
#Entity
public class ISmodel {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long id;
public String Partnumber;
public String Materialgroup;
public String Warehouse;
public Float Stock;
public Float Value;
public Float PA49;
public Float STPA49; }
and that is the error
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controller': Unsatisfied dependency expressed through field 'FSrepos'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'FSrepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract void com.example.demo2.repository.FSrepository.setPA49Debut()!
Caused by: java.lang.NullPointerException: null
Any help would be greatly appreciated. Thanks.
the solution to NullPointerException: null is to
try #Query(vaue="your query goes here",nativeQuery = true)
as #stacker said but i found out that there is a lot of mistakes in my code,i'll try to explain them in hope it helps someone else.
The first
CASE WHEN EXISTES spelled wrongly. Have to be written CASE WHEN EXISTS #alex
The second is using SI out of the select statement i resolved this problem by adding a new select statement.
The third mistake is to update my table by subquery the same table in those lines "WHEN EXISTS (SELECT 1 FROM FSmodel FS2 WHERE FS.Partnumber=FS2.Partnumber AND FS.id>FS2.id )" +
" THEN FS2.PA49_Debut "
and i found the solution as well as the explanation here
Eventually this is my final code and it works great
#Transactional
#Modifying
#Query(value="Update FSmodel FS Set FS.PA49_Debut = CASE" +
" WHEN exists(SELECT SI.Partnumber FROM ISmodel SI where FS.Partnumber=SI.Partnumber)"
+ " THEN (select distinct S.PA49 from ISmodel S where FS.Partnumber=S.Partnumber)" +
"WHEN exists(SELECT 1 FroM (SELECT * FROM FSmodel) AS FSd WHERE FS.Partnumber=FSd.Partnumber AND FS.id>FSd.id )"
+ " THEN (select distinct F.PA49_Debut from (SELECT * FROM FSmodel) AS F where FS.Partnumber=F.Partnumber AND FS.id>F.id)+1" +
" ELSE (FS.PA49_Actuel )" + "END",nativeQuery = true)
void setPA49Debut();

Spring data with custom query - like, between, like and order by

how I can pass this string "sql" for a Spring Data #Query?
My Class Resources:
public void findMessage(String startDate, String endDate, String id, String values, String fieldName, String sort){
String sql = "SELECT e FROM message e WHERE (e.create_at BETWEEN '"+
startDate+ "' AND "+"'"+ endDate +"') AND (e.id = '"+id+"')
AND (e.category LIKE '%"+values+"%') ORDER BY
e."+fieldName+" "+sortParam;
}
My Custom Repository:
public interface CustomMessageRepository extends PagingAndSortingRepository<Message,Long>{
#Query(value = **MyStringSql**)
public Page<Message> find(Pageable page);
}
I'm using JHipster + Spring Data. Using the examples I found, just can not run queries are passed parameter as fieldName and sort.
What is the solution?
Thank you.

FileHelpers: The delimiter '|' can´t be found after the field

I have huge CSV file (1GB) which looks like that:
1140|2017|0213065852|2001|99|Ä‚/ŮŌçlš˝Ă_Âá'ÄǸ|-3858,18|4015,36|100,17|19,34|0,00|0,00|0,00|40,00|0,00|0,00|0,00|32,57|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|12|12|74|18|24620,65|11903,96|29385,35|10993,94|0,00|-2520,88|0|0100|301|02|0302|N|N|N| | |2001-12-27 00:00:00|N|1140|1313|BG4CSXF0| |2002-01-01 00:28:13| |0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00
1140|2017|0213065852|2002|99|ľë¸Â‰_gÂýľ)Ľ¸ů_°Ë
…p'™m'î'Ä_|-3799,76|3982,22|461,66|1,48|0,00|0,00|0,00|0,00|0,00|0,00|0,00|100,23|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|27|43|189|20|40140,74|48835,00|69605,32|19312,00|0,00|-3427,87|0|0100|301|02|0303|N|N|N| | |2002-12-27 00:00:00|N|1140|1313|BG4CSXF0| |2003-01-01 01:35:56| |0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00
When I try to read this file using FileHelpers using following code:
FileHelperEngine engine = new FileHelperEngine(typeof(BGDTMDF));
BGDTMDF[] tab = (BGDTMDF[])engine.ReadFile(path);
I get an error:
The delimiter '|' can´t be found after the field 'MDF_SALDOS_MEDIOS' at line 2 (the record has less fields, the delimiter is wrong or the next field must be marked as optional).
My class looks like:
[DelimitedRecord("|")]
public class BGDTMDF
{
public string MDF_ENTIDAD;
public string MDF_CENTRO_ALTA;
public string MDF_CUENTA;
public string MDF_ANIO;
public string MDF_MES;
[FieldNullValue("def")]
public string MDF_SALDOS_MEDIOS;
[FieldNullValue("def")]
public string MDF_SALDO_DISPUE;
public string MDF_SALDO_MAX_DEU;
public string MDF_INTE_COBRAD;
public string MDF_INTE_PAGADO;
public string MDF_IMPU_RETENI;
}
(there is only part of this class (so huge).)
Any help?
My solution is mark all fileds in class as [FieldOptional].

Issue with Antlr Stringtemplate V4 ArrayList iteration

heres my test in java
public class person
{
public String name;
public int age;
public String getName() { return name; }
public int getAge() { return age; }
}
In my function I create a number of person objects
and add it into an list
ArrayList<person> arr = new ArrayList<person>()
arr.add(person1);
arr.add(person2); etc etc
in the string template group file I have got
test(arr) ::= <<
<table>
$arr: {a|
<tr><td>$a.name$</td><td>$a.age$</td></tr>
}$
</table>
>>
this is called from my list template
list (arr) ::= <<
$test(arr)$
... and other page details etc
>>
in version4 for I get template not found message with some stack trace as follows
Caused by: java.lang.ClassCastException: java.util.ArrayList
at org.antlr.runtime.tree.RewriteRuleTokenStream.nextNode(RewriteRuleTokenStream.java:58)
at org.stringtemplate.v4.compiler.STParser.subtemplate(STParser.java:1563)
at org.stringtemplate.v4.compiler.STParser.mapTemplateRef(STParser.java:3692)
but I can list an array as follows and it prints the object representation in string format
(also I can use a map - ie key value pairs works ok too)
in string template
test(arr) :: =<<
<p>
$arr; seperator="</br>"$
</p>
>>
how do we iterate list of object to print field values using string template grop in V4
am I using it in a wrong format/syntax?
any help/points would be greateful
note: we set $ as delimiter using new STGroupString("", templateGroup, '$', '$');
You are missing the terminating $ on your expression: $a.age should be $a.age$

Get name of the parsed file?

I would like to parse a file where the first line may or may not contain a definition of a "project" name (like with Pascal's program keyword), and if not, use the name of the file that is being parsed as default. Simplified:
#members{ String projectName; }
project : {projectName = ...} // name of parsed file as default
( PROJECTNAMEKEYWORD ID {projectName = $ID.text;} )?
otherstuff {/*...*/};
Is this even possible? I found this surprisingly hard to find out using google or the antlr manual. By its documentation, input.getSourceName() should be what I am looking for, but it always returns null, and debugging lead me to the class ANTLRStringStream that has a name field whose value is returned by this method but never set.
Simply create a custom constructor in your parser that takes a string that represents your file.
A demo:
grammar T;
#parser::members {
private String projectName;
public TParser(String fileName) throws java.io.IOException {
super(new CommonTokenStream(new TLexer(new ANTLRFileStream(fileName))));
projectName = fileName;
}
public String getProjectName() {
return projectName;
}
}
parse
: (PROJECT ID {projectName = $ID.text;})? EOF
;
PROJECT : 'project';
ID : ('a'..'z' | 'A'..'Z')+;
SPACE : (' ' | '\t' | '\r' | '\n') {skip();};
which can be tested with:
import org.antlr.runtime.*;
public class Main {
public static void main(String[] args) throws Exception {
// 'empty.txt' is, as the name suggests, an empty file
TParser parser = new TParser("empty.txt");
parser.parse();
System.out.println(parser.getProjectName());
// 'test.txt' contains a single line with the words: 'project JustATest'
parser = new TParser("test.txt");
parser.parse();
System.out.println(parser.getProjectName());
}
}
If you run the Main class, the following will be printed to your console:
empty.txt
JustATest