parser for lproj string files - java

I need to read string/translations files from lproj folders ( apple style localisation ) with java. Before writing my own parser I would like to know if I missed an existing one - the style looks like this
"string1_key" = "string 1 value";
/** multi line following - and showing comment here **/
"string2_key" = "string 1
multi line value";
at the end I want something like aHashMap<String,String> - anyone knows an existing lib for this kind of parsing or do I have to roll my own?

I ended up writing a parser for this:
https://github.com/ligi/PassAndroid/blob/master/src/main/java/org/ligi/passandroid/model/AppleStylePassTranslation.java

Related

Remove custom tag from a string then format its content

I need help to parse, modify and show a string on an Android App (Java language, max API level 22)
This is a example string I'm getting from an API which contains only custom tag:
<BOLD> Something <RED> went wrong </RED> </BOLD> <NEWLINE> Server unreachable </NEWLINE>
I need to remove all this custom tags then format its content based on the tags that were wrapping that substring (so I'm expeting, for example, to get "went wrong" in red color and bold). I already tried looking up for similar problems but can't get to the final result.
The string (cleaned and formatted) will then be used to set the Text of a TextView inside a List View
One way of doing this is like this....
String testString="<BOLD> Something <RED> went wrong </RED> </BOLD> <NEWLINE> Server unreachable </NEWLINE>";
testString=testString.replaceAll("<BOLD>","<font> <b>");
testString=testString.replaceAll("</BOLD>","</b> </font>");
testString=testString.replaceAll("<RED>","<font color =\"#FF0000\">"); //#FF0000 is hex code for red color
testString=testString.replaceAll("</RED>","</font> ");
testString=testString.replaceAll("<NEWLINE>","<br>");
testString=testString.replaceAll("</NEWLINE>","");
TextView textView=findViewById(R.id.text);
textView.setText(Html.fromHtml(testString));
Output :
Using Regex (Regular Expressions)
Just give your string to the Regex Pattern and it removes all the extra tags for you.
Kotlin
This removes all the HTML tags inside your String:
val result = yourString.replace(Regex("(<[a-z]*>)|(<.[a-z]*>)"), "")
Java
String result = yourString.replaceAll("(<[a-z]*>)|(<.[a-z]*>)", "");

Regex: Read value between multiple brackets

I currently working on translating a website (Smarty) with Poedit. To get all the text from the .tpl files i'm using regex to get the data between the {t} and {/t}. so an example:
{t}Password incorrect, please try again{/t}
The regex will read Password incorrect, please try again and place it in a .po file. This is all working fine. It goes wrong when it gets a little more advanced.
Sometimes the text between the {t} tags uses a parameter. this looks like this:
{t 1=$email|escape 2=$mailbox}No $1 given, please check your $2{/t}
This is also working great.
The real problem start when i use brackets inside the parameter like this:
{t 1={site info='name'} 2=$mailbox}visit %1 or go to your %2{/t}
My regex will close when it sees the first closing brackets so the result will be 2=$mailbox}visit %1 or go to your %2.
My regex looks like this:
\{t.*?\}?[}]([^\{]+)\{\/t\}|\{t\}([^\{]+)\{\/t\}
The regex is used inside a java program.
Does anybody has a way to fix this problem?
The easiest solution I see on this is to normalize the .tpl files. Just use a regex which matches all tags something like this one:
{[^}]*[^{]*}
I had the same issue to solve and it worked pretty good with the normalizing.
The normalizing-method would look like this:
final String regex = "\\{[^\\}]*[^\\{]*\\}";
private String normalizeContent(String content) {
return content.replaceAll(regex, "");
}

Parse file and delete bracket content using Java

I have a configuration file which has multiple records respecting the syntax below :
# some comment
Job {
Name = "Job1"
Include {
Where = /etc
}
}
I'm writing a Java program to parse this file, if user chooses to delete "Job1", then the program will delete the entire bracket of "Job1".
The hard part is how to find bracket that matches the first open one, as shows there are several brackets inside the first one. And sometimes we have records not respecting 100% the syntax like :
# some comment
Job {
Include {
Where = /etc
}
Name = "Job1"
}
So it makes the parsing even harder. Could anyone give me some ideas? Thanks.
You cannot do that with regular expressions, you need a grammar parser such as Antlr.

How can i parse the given string?

Hi guys i have been given a task to parse a string which will be coming from the server.
The string looks like:
<first name=$Jon$ last name=$Doe$/><first name=$Doe$ last name=$Jon$/><first name=$r$ last name=$k$/>
and the output needed is:
first name: Jon
last name: Doe
-------------------
first name: Doe
last name: Jon
-------------------
first name: r
last name: k
-------------------
i.e.,
key: value
I have done some simple text-parsing which included a simple delimiter like a $ or a %.
but in this case i don't understand how to parse the text. Your help will be very helpful.
Matcher keys = Pattern.compile("[<\\s)](.*?)[=]").matcher(string);
Matcher values = Pattern.compile("[$](.*?)[$]").matcher(string);
while(keys.find() && values.find()) {
System.out.println(keys.group(1)+" : "+values.group(1));
}
replace $ in xml string from server with ", load it as xml document, use XPath or some other mechanism to parse the information you need
There can be multiple ways to reach to the solution
Can use XSLT with Java. (Java provides apis like TransformerFactory, Transformer etc.)
Can use XSLT in IDE like eclipse. Several plugins available.
Can check this out www.vogella.com/articles/XSLT/article.html
can use unix script to do the same.
How to convert xml file in to a property file using unix shell script
It is not the exact solution to your problem but solutions you can try. Similarly there can be many other ways for sure.

Preprocessing with Javacc/ push front some chars in the stream?

Using javacc can I push some new characters in front of the inputstream ?
for example let's say that my parser parses the following syntax:
#define Paragraphs "Paragraph+"
#define Volume "(Title,(Chapter,${Paragraphs})+)"
Book=${Volume}+;
How can I tell javacc that its scanner should preprocess ${Volume} to (Title,(Chapter,Paragraph+)+) before invoking the parser ?
Can It be achieved using the MORE statement ?
Thanks
Token.image is a public field, so you could also just set it directly. Here's an example in my JavaCC book's tokenizer chapter:
TOKEN : {
{matchedToken.image = image.append("B").toString();}
}
You can download all the book's example source code here.
OK, I think I've found the solution: Some java statements can be added in the TOKEN section and the current buffer is defined in a StringBuilder named 'image':
| <Y:"${"(<NAME)+ "}" >
{
String oldValue=image.toString();
image.setLength(0);
image.append(my_dict.get(oldValue));
}

Categories

Resources