In my app, I'm reading a xml file (with JAXB) but there is times that I have empty segments.
I put some examples
<tuv>
<seg>Unknown: the action taken should always be known</seg>
</tuv>
Here I read well the tag "seg", I can get all text "Unknown: the action taken should always be known" but if this text is like this:
<tuv>
<seg><bpt i="1" x-wb-tag="b1" />Unknown<ept i="1" x-wb-tag="/b1" />: the action taken should always be known</seg>
</tuv>
I don't get anything, my variable is empty and I want to get all text "Unknown: the action taken should always be known"
Could I get this text "Unknown: the action taken should always be known"? (I want this like a string)
Note: my variable's value is a "string"
Thanks!
I answer to my question if anybody needs it.
I have used #XmlMixed that return a object list.
Here there are more information: mixed element
Related
I have a Java/JSP webapp that has started showing a bug after I added fn:escapeXml() to a tag that constructs a URL.
The original code was
<p id="provide-link">
Follow this link to see the source data:
<q:link href="../resources/source-data.jsp"
sourceData="${chart.parameters.source}">
Source Data
</q:link>
</p>
q:link is a custom tag that creates a hyperlink out of href as the base and sourceData as a GET parameter. For example, if chart.parameters.source = 'dataset03', then the link embedded in the page is
../resources/source-data.jsp&sourceData=dataset03.
The custom tag q:link is written to handle cases where chart.parameters.source is a Collection of multiple data sources, which is an allowed case.
This code works normally, but is vulnerable to script insertion attacks due to the GET parameter. Thus, I've added the JSTL function escapeXml() to the code to prevent this:
<p id="provide-link">
Follow this link to see the source data:
<q:link href="../resources/source-data.jsp"
sourceData="${fn:escapeXml(chart.parameters.source)}">
Source Data
</q:link>
</p>
When I do this, square brackets get added to the URL:
../resources/source-data.jsp&sourceData=[dataset03]
This is a problem because [dataset03] is not a valid value for the parameter.
The only thing I've found online about this problem is this question, which doesn't provide a full answer but suggests that ${fn:escapeXml(chart.parameters.source)} might be outputting as an Array now that it includes fn:escapeXml(). The theory is that q:link then catches and retains the Array brackets when it converts to String.
Anyone know what's happening and how to stop it?
Your last suggestion seems correct to me, the fn:escapeXml function has an argument of type String. If the parameter passed is a Collection, it will be converted through a toString() method (that add the []). It appends before the fn:escapeXml() call, so it’s not added inside it.
Rem: it doesn’t append without the escapeXml function because your custom tag retrieve and work on the Collection directly (iterating on its values inside probably).
One solution to convert the Collection passed as argument before passing it to the escapeXml fct; through a tag or EL expression (map()...)
Rem2: instead of using a custom tag to encode your url parameters, there are already these standard JSTL tags that do a similar job :
https://www.tutorialspoint.com/jsp/jstl_core_param_tag.htm
https://www.tutorialspoint.com/jsp/jstl_core_url_tag.htm
Hope that will help,
Kind regards,
Cedric
I have a Vaadin 8 Grid where I would like to set one column as editable. For this I have where Food.calories is a long (yes in this case it could be an int but keep in mind this is an example and my specific use case requires a long):
Binder<Food> binder = foodGrid.getEditor().getBinder();
TextField caloriesTextField = new TextField();
binder.forField(caloriesTextField)
.withValidator(CustomCaloryValidator::isValidValue, "Must be valid a positive amount")
.withConverter(new StringToCaloriesConverter("Must be an integer"))
.bind(Food::getCalories, Food::setCalories);
// This line fails with the error because the types do not match.
foodGrid.addColumn(Food::getCalories, new NumberRenderer(myNumberFormat))
.setEditorComponent(new TextField(), Food::setCalories);
Unfortunately this doesn't work and has the following error:
Inferred type 'C' for type parameter 'C' is not within its bound; should implement 'com.vaadin.data.HasValue'
I looked everywhere I could and couldn't find any example of anything beyond simple edits. The demo sampler did have a more complex example using a slider but I couldn't figure out how to extrapolate from that example...
I understand the error, it's trying to map a long to a String. However I can't find a way to add a converter to the addColumn to make it work...
Firstly the main issue was that the Binder did not specify the generic type, it needed to be:
Binder<Food> binder = foodGrid.getEditor().getBinder();
And NOT:
Binder binder = foodGrid.getEditor().getBinder();
That being said there were several other gotchas. First when you do a forField() you need to keep track of that binding to be able to set it later with the column. This wasn't clear at all for me. Specifically you need to:
Binder.Binding<Food, Long> caloriesBinder = binder.forField(caloriesTextField)
.withValidator(CustomCaloryValidator::isValidValue, "Must be valid a positive amount")
.withConverter(new StringToCaloriesConverter("Must be an integer"))
.bind(Food::getCalories, Food::setCalories);
I'm not 100% sure on the caloriesBinder because my code is different and this is an example, but you need that binding. You then take that binding and do:
foodGrid.getColumn("calories").setEditorBinding(caloriesBinding);
This allows the correct editor to work. This is in the documentation but the example is very simple so I missed that.
The next step which is extremely important depending on what you're displaying, is to add a renderer otherwise you can run into some weird issues. For example if you're using long to store a currency then you need to convert it to display a currency amount. Similarly if you're using a date then you probably also want to format it. You then need to add a renderer. The only way I could find how to do it without compilation errors (mismatched types) was:
((Grid.Column<Food, Long>)foodGrid.getColumn("calories")).setRenderer(new CaloriesRenderer());
Just for completeness you then need to enable the editor with:
foodGrid.getEditor().setEnabled(true);
Lastly, if the table is part of a bigger bean then you need to call foodGrid.setItems(), you cannot just rely on binder.readBean() because it cannot accept a list. So for example if instead of food the bean was a meal which consisted of a number of ingredients, then you could not do binder.readBean(meal) nor could you do binder.readBean(meal.getIngredients) even though you can do binder.readBean(meal) for the rest of the form. The only way I could make it work was to do:
binder.readBean(meal);
foodGrid.setItems(meal.getIngredients);
I'm using the available transformation functions in the org.json library to transform json to xml. It's very simple to do like this.
String xmlStr = XML.toString(new JSONObject(jsonStr));
Everything was perfect until I needed to process some json that contained the content property like this.
{
"content": "X",
...
}
I expected this to convert to
<content>X</content>
but instead it converts to simply X without the opening and closing tags. So I checked the source code for XML.toString and "content" is treated special. The comment in the code says this.
// Emit content in body
I Googled and also found this.
Content text may be placed in a "content" member
However I can't find an explanation of what this is all about. What's the purpose and why would someone want this to be treated in a special way? Also If you can point me to a good explanation that would be quite helpful.
This seems to be a non-optimal implementation decision. The most recent discussion have taken place in org.json issue #394:
"content" is an unfortunately-named keyword in the XML <-> Java transformation. For the history of this issue, please see #344, #286, and #108. For more information about how the keyword works, see XMLTest.java contentOperations() in https://github.com/stleary/JSON-Java-unit-test.
No objection if someone wants to propose a workaround along the lines of #108, or any other approach that does not break existing applications.
There is underscore-java library with static method U.jsonToXml(json). It supports content key name. I am the maintainer of the project.
{
"content": "X"
}
Output:
<?xml version="1.0" encoding="UTF-8"?>
<content>X</content>
I'm a beginner to both quickfix and java.
Usually when I want to remove a Field in a quickfix Message, I use the removeField method with a tag as an argument, but this doesn't seem to work with the 2 automatically generated fields- BodyLength (tag 9) and CheckSum (tag 10).
For example, I have created a message, then I print.
System.out.println(message)
gives
8=FIX.4.29=8635=149=WFSComp23452=20130613-21:45:22.28256=ClientComp1234109=default112=default10=067
then I remove a field, say tag number 8,
with the following
message.getHeader().removeField(8);
and print again, I get
9=7435=149=WFSComp23452=20130613-22:06:32.81956=ClientComp1234112=default10=105
where the field is removed, but when I try to remove the 9 tag the same way with:
message.getHeader().removeField(9);
the output when I print yields the same code without the field removed:
9=7435=149=WFSComp23452=20130613-22:06:32.81956=ClientComp1234112=default10=105
the 9 field is still there !
My guess is that it's because quickfix automatically generates the bodylength, but how do you remove it? Thank you.
Links to quickfix:
javadoc:
http://www.quickfixj.org/quickfixj/javadoc/1.5.3/
http://www.quickfixj.org/
Yes, QuickFIX automatically generates the BodyLength and Checksum.
I can't imagine why in the world you think you need to remove these fields, but if you really want the message string with those fields removed, you can kludge it with a regex applied after the fact:
message.toString().replaceAll("\09=[0-9]*","").replaceAll("\010=[0-9]*","")
Again, though, I don't really see any good reason to do this.
I'm trying to change the values in my strings.xml using Java. It's to change certain labels in an app dynamically on certain events. Any help would be appreciated
You can't change values in strings.xml as Strings are immutable (unless you do some dynamic compilation). You can have enums of Strings that can change the answer depending on passed-in variables
I think the best solution for your need would be to define all possible strings within the XML File. Based on the event in your application you can then choose the correct string. If the event is something like a user input then the changing the XML file is not possible. But in this case it should be fine to change the String directly without using XML references.
You might want to take a look at Java String Formatter it simply let you format a string in your string.xml but not changing the whole String , as other answers said, you can't change the value of a String in strings.xml from your java code.