How does Spring convert a String into an array? - java

I have a standard Spring class that accepts a String array. I know that Spring will take care of splitting a comma separated String into an array, but I'm unaware of how it does it under the hood.
The question that led me to this is whether the Strings will become trimmed after being split, but I would like to know how to see this under the hood activity as well if other future issues arise.
<bean id="allServicersDelimitedLineTokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="col1,col2,col3,col4" />
</bean>
is the same as?
<bean id="allServicersDelimitedLineTokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="col1 , col2, col3, col4" />
</bean>

Related

How to Parse XML Document By Class Name Using Java

I'm writing a parsing tool to compare the textual content of two bean XML files in Java. The text content changes and we need a way to run a script to make sure the textual content is the same. I know we have org.w3c.dom which has a method getElementsByTagName("tag_name") and that returns a node list in the XML document. I'm wondering if anyone knows of a way to do this using the class name? I've been searching around but haven't been able to solve this yet.
<bean class="com.mycompany.myText" id="Q1.4">
<property name="name">
<value>Q1.4</value>
</property>
<property name="text">
<value>This is text one</value>
</property>
<property name="retired">
<value>true</value>
</property>
</bean>
<bean class="com.mycompany.myText" id="Q1.5">
<property name="name">
<value>Q1.5</value>
</property>
<property name="text">
<value>This is text two</value>
</property>
<property name="retired">
<value>true</value>
</property>
</bean>
I can't use the 'bean' element name as there are several other beans whith non relevant stuff I need only the ones with the class com.mycompany.myText and the value I'm trying to extract is property name=text - the text content
Any help here would be appreciated. Also as a side note I should mention that we have no direct control of how the XML file is managed and structured as it's fed to us from a 3rd party.
As you say, you need to select tags by their attributes.
You could use XPath to achieve this. See those resources:
http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/
https://stackoverflow.com/a/24220968/6377268
The expression would be something like this
/bean[#class='com.mycompany.myText']/property[#name='text']/value
I hope I could help at least a little bit.

Java Spring MultiLineItemReader To read Complex file having multiple records of same type

I am new to Spring and trying to read a complex file using FlatFileItemReader in Spring batch. The input file has structure as below. The sample below is one records and file has many records like this. Line starting with H is a Header Record and with F is a footer records. In between there are transaction records of type 10500, 10501, 405, 505. I am able to read this file using MultiLineItemReader.
I have created FieldSetMapper and Tokenizer and able to parse the file and get to individual fields. Only issue I am facing is that for records types 10500 and 10501 they can appear multiple times in between Header and Footer and when I am reading the file I am just getting the last read values and the previous records are just not appearing in reading. I know I am doing something silly but not able to understand. Has anybody had any experience reading such type of file.
H00025888222 444233DD 33232323232
105000000 0000 0
10501 1 2222222
105000000 0000 1
10501 1 2222223
40500 1222222
50500 21
F00555 5555 0
If the you just have multiple types of lines, but the lines are each standalone, then PatternMatchingCompositeLineMapper is probably what you're looking for.
<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="#{jobParameters['input.file']}" />
<property name="lineMapper" ref="patternMatchingMapper" />
<property name="strict" value="true" />
</bean>
<bean id="patternMatchingMapper" class="org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper">
<property name="tokenizers">
<map>
<entry key="LINEA*" value-ref="tokenizerA" />
<entry key="LINEB*" value-ref="tokenizerB" />
</map>
</property>
<property name="fieldSetMappers">
<map>
<entry key="*" value-ref="beanWrapperFieldSetMapper" />
</map>
</property>
</bean>
If they are related (multiple lines need to be combined to make one object, you make consider SingleItemPeekableItemReader, described briefly here).

Substring of a string based on condition

I have a string like below (read from a file and stored in a String)
<textmessages>
<textMessage timestamp="1424708212905">
<property name="tcs_car_kind" value="M32"/>
<property name="prev_cmdy_abrv" value="AUTOS"/>
<text><![CDATA[event_code="AP"]]></text>
</textMessage>
<textMessage timestamp="1424708212902">
<property name="shp_prim" value=""/>
<property name="prev_cmdy_abrv" value="AUTOS"/>
<text><![CDATA[event_code="CP"]]></text>
</textMessage>
<textMessage timestamp="1424708212902">
<property name="co_part_frm_nbr" value=""/>
<property name="prev_cmdy_abrv" value="AUTOS"/>
<text><![CDATA[event_code="LP"]]></text>
</textMessage>
</textmessages>
Requirement:
If string values matches to 'event_code="CP"' then I need to return complete data in between <textmessage> ---- </textmessage> as shown below.
<textMessage timestamp="1424708212902">
<property name="co_part_frm_nbr" value=""/>
<property name="shp_prim" value=""/>
<property name="prev_cmdy_abrv" value="AUTOS"/>
<text><![CDATA[event_code="CP"]]></text>
</textMessage>
A combination of those might help http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html
Search for 'event_code="CP"
with find() or match()
if is there...
Pattern p = Pattern.compile("pattern here");
if(p.matcher(content).find()){...}
Other option is to use xml parses, but regex are really simple for what you want.
if the pattern is really constant, yo can even use indexOf to go faster on your searches...
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)
if they are on the same location, then just reading from there is enough..
Why don't you try to parse that String as a XML using DOM?
With this you just have to compare the value of each "text" node and if it's value matches you return the whole parent node, as String for example...
Here is a basic sample of how it DOM works in Java: http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

Difference of property definition in component.xml (OSGI DS)

Could someone tell me the difference between this two property definitions?
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="blablaService">
<property name="dbUrl" type="String">jdbc:h2:url</property>
<property name="dbUser" type="String" value="user" />
</scr:component>
I'm using Dictionary properties = context.getProperties(); to get the values. It seems to be that if the property given surround with xxx is treated as an Object holding a String[1] and the property which value is specified in the value attribute is treated as an Object which is de facto a String. For the first example (String) throws and Exception as the latter does not.
I'm developing using Eclipse and the Component Context is from org.osgi.service.component.ComponentContext.
<property name="dbUser" type="String" value="user" /> yields a scalar String.
<property name="dbUrl" type="String">jdbc:h2:url</property> yields a String[1].
This is per the spec. See 112.4.6 in the Compendium Spec.

Spring inject values

I am very new to spring and started using.
I have a requirement where i have something like properties
like regions..US,UK
Regions
-------
US
UK
And when i read US it shud have values something like
US
----
(KEY)primary----VALUE(primaryValue)
(KEY)secondary----VALUE(secondaryValue)
.
.
similarly
UK
--
(KEY)primary----VALUE(primaryValue)
(KEY)secondary----VALUE(secondaryValue)
.
.
and the regions might increase as requirement changes and the key value pairs below it too
Someone hint me so i can proceed
Thank you in advance
You need to create two bean a List and a Map, in other word List<Map> is you need
<bean id="regions" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="usMap" />
<ref bean="ukMap" />
</list>
</constructor-arg>
</bean>
and
<util:map id="usMap" map-class="java.util.HashMap">
<entry key="primary" value="someValue"/>
<entry key="secondary" value="someValue"/>
</util:map>
You can make different properties in according to region , when server will start all the properties file will load.You can make PropertiesFileReader.java file which will read u'r properties.

Categories

Resources