I have a file that tells me what to do on runtime.
Notation is as below;
<Service name="Service2">
<Request>
<User value="admin">
<Pass value="1234">
</Request>
Is it possible to parse it with standard rules, without writing custom parser?
Thanks
As the above text are in file, you will read it as NSString. After reading the file you can either use some algorithm to fetch the value like, breaking the string into arrays separated by < and >
or, you can create your own parser.
As you dont want to form your custom parser, then you can use the first way to find the values, but that will be fixed for the above file contents.
Related
I have a following situation: in some of my i18n property files there are properties containing a special word:
prop.example=specialword just for example
prop.test=just for test specialword
I want to have a possibility of having a property somewhere in my Config.groovy that would contain a specific value for this specialword so that if I specify:
specialword=Value of special word
in a Config.groovy then I want my i18n properties to be resolved like:
prop.example=Value of special word just for example
prop.test=just for test Value of special word
for that purpose, when building the project, I want to access property files in order to look for occurences of specialword and to replace them with value of specialwordvalue from Config.groovy.
Is that possible somehow? Perhaps, someone faced similar situation? I would really appreciate any help.
Thanks, Cheers
Instead of trying to change the way the properties are compiled, you would be better off passing the special value as an argument to your message code (as discussed in the comments to your question).
For instance:
<g:message code="my.key.code" args="[someVariableWithAValueFromConfig]" />
If your message code doesn't use the argument it will simply be ignored. This seems like the best approach to the problem you are trying to solve.
I have an XML where I have several attributes for one node:
var
row : IXMLNode;
rowattr : IXMLAttr;
xml : IXMLDocument;
begin
xml := ConstructXMLDocument('xml');
SetNodeAttr(xml.DocumentElement, 'version', '1.0');
SetNodeAttr(xml.DocumentElement, 'encoding', 'UTF-8');
row := AppendNode(xml, 'Links');
rowattr:=xml.CreateAttribute('Link1');
rowattr.Value:='http:\\wwww.somelink1.com';
row.Attributes.SetNamedItem(rowattr);
rowattr:=xml.CreateAttribute('Link2');
rowattr.Value:='http:\\wwww.somelink2.com';
row.Attributes.SetNamedItem(rowattr);
rowattr:=xml.CreateAttribute('Link3');
rowattr.Value:='http:\\wwww.somelink3.com';
row.Attributes.SetNamedItem(rowattr);
XMLSaveToFile(xml, 'C:\Test1.xml', ofIndent);
end;
I wish to have every link on a separate line like this:
<xml version="1.0" encoding="UTF-8">
<Links
link1="http://www.somelink1.com"
link2="http://www.somelink2.com"
link3="http://www.somelink3.com"
/>
</xml>
OmniXML does not offer such fine grained control of output formatting. You could perhaps look to find an external XML pretty printer that will do what you need. Or you could even write your own XML library.
Before you go any further though I would like to make the point that XML was never intended to be read by humans. Its design makes no effort to be readable, and if you continue trying to make your XML as readable as possible, then you will be swimming against the tide. If you want a human readable structured file format then you might look instead at YAML which was designed with that in mind.
Another avenue to consider is the structure of the XML. Using node attributes to specify an array of values is a poor decision. Attributes are intended to be used with name/value mapping pairs. If you want to specify an array of values then you might do so like this:
<links>
<item>http://www.somelink1.com</item>
<item>http://www.somelink2.com</item>
<item>http://www.somelink3.com</item>
</links>
This is clearer than your XML and much easier to parse. Try writing code to parse your attributes and you will see what I mean.
Now, just to illustrate my point above, in YAML this would be:
Links:
- http://www.somelink1.com
- http://www.somelink2.com
- http://www.somelink3.com
Of course, all this is moot if somebody other than you is defining the format of the XML.
I Have a few text strings that contain well formed XML.
I would like to be able to (1) turn these strings into IXMLNodes then (2) append them to an existing XMLDocument. Preferably without declaring a new XMLDocument first.
This doesn't seem possible?
Is there any easy way to accomplish something equivalent though? My initial thought was to use the IXMLNode.XML (string) property and insert the new strings. No such luck as IXMLNode.XML is Read Only.
Here is an example, if I had the following strings in a TStringList,
<Property Name="Version" RttiType="tkString"></Property>
<Property Name="ShowSubunit" RttiType="tkBoolean"></Property>
And I had the following XML, already loaded into a TXMLDocument, how could I easily append the two lines above into the TXMLDocument below?
<Program Name="PFOO">
<Class Name="CFOO">
<Property Name="DBN" RttiType="tkString"/>
<Property Name="SDate" RttiType="tkClass" ClassType="TXSDATE">12/30/1899</Property>
<Property Name="XForm" RttiType="tkEnumeration">xfXML</Property>
<Property Name="Singleton" RttiType="tkBoolean">True</Property>
</Class>
</Program>
Any other (simple) ways to achieve this (no protected hack on the XML property please)?
Thank you!
Unless you parse the XML fragments manually and then construct the relevant child nodes/attributes manually, you will have to load the fragments into a temp XMLDocument and then move its nodes to the main XMLDocument as needed.
Update: For example:
Node := XmlDocument1.DocumentElement.ChildNodes[0]; // <Class> node
Node.ChildNodes.Add(LoadXMLData('<Property Name="Version" RttiType="tkString"></Property>').DocumentElement);
Node.ChildNodes.Add(LoadXMLData('<Property Name="ShowSubunit" RttiType="tkBoolean"></Property>').DocumentElement);
Check out SimpleStorage. For now its tied to OmniXML, but its powerfull. What you want would look like this:
CurrentNode.Append(StorageFromXML('<Node>Content</Node>'));
One line of code.
Like the asker of the question here
Variable substitution JSF Resource Bundle property file
I'm slightly aghast at the inability to reference the value of other property keys in the message bundle.
Although I see how easy to write my own rubbish handler[0] that can do what I want in a custom component, that would leave expressions in templates calling the message bundle still using the default JSF implementation.
Is it possible to override the default JSF handling of the message bundle?
[0] Or better, to use code referenced in one of the answers to the above question
https://code.google.com/p/reflectiveresourcebundle/
You can provide the fully qualified name of a concrete ResourceBundle implementation as "base name" instead of alone the path and filename of the properties files.
E.g.
public class YourCustomResourceBundle extends ResourceBundle {
// ...
}
which can be registered as follows
<application>
<resource-bundle>
<base-name>com.example.YourCustomResourceBundle</base-name>
<var>text</var>
</resource-bundle>
</application>
or declared on a per-view/template basis as follows
<f:loadBundle baseName="com.example.YourCustomResourceBundle" var="text" />
Here are several related questions/answers which contain some concrete code which you could use as a kickoff example:
How to remove the surrounding ??? when message is not found in bundle
internationalization in JSF with ResourceBundle entries which are loaded from database
i18n with UTF-8 encoded properties files in JSF 2.0 appliaction
Everything is possible for those who try. The question is not whether it is is possible but should you do it. And the answer to that question is: probably not.
Referencing other message in a message bundle means you want to build a compound message. So you can re-use part of the message many times just to save small fraction of the disk space or small fraction of development time.
If that is the case, I have a message for you. What you plan to do is called a concatenation and it is the second most common I18n defect. And its implications are as bad as those of hardcoded strings.
Why? Because target languages do not follow the English grammar rules. First, it is common need to re-order the sentence while translating. This might be easy to fix by using (numbered or named) placeholders. On the other hand though, the translation might differ depending on the context. That is, it might need to be translated in totally other way, or simply the word endings might need to be different depending on a grammar case, mood or gender.
My advice is, do not use such shortcuts, it will create more problems than it fixes.
Now you should know why "those stupid Romans" didn't implement it like this: it is against I18n best practices.
<propertyfile file="${build.dir}/MyProperties.properties">
<entry key="releaseInformation"
type="string"
value="${build.time}"/>
</propertyfile>
When Ant copies my properties file over to the bin directory there is a property in it that has something like "samplePathName=C\:\Users\SomeUser\". But the property from the original file was "samplePathName=C:\Users\SomeUser\". How would the additional backslash end up there? I don't see anything that could possibly cause this to happen. Where should I begin looking other than the build.xml which only contains (relevant) the above line?
This is a common problem - the format of property files is defined by Sun (Oracle). Ant is conforming to this, which is why the escaping happens. There's no way round this using the propertyfile task - that's the way it's intended to work. If the file is genuinely a Java property file, then it shouldn't matter - the escaping should be handled correctly when the file is read.
However if you are hoping to use propertyfile to write a name-value config file that's for something else - where the escaping is not wanted - you'll need to adopt a different approach. As mentioned in the answer to a related question - you might use the Ant replace or replaceregexp tasks for this.