wood burning stoves 2.0*
The moose likes Java in General and the fly likes taking a particular data from a number of lines Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "taking a particular data from a number of lines" Watch "taking a particular data from a number of lines" New topic
Author

taking a particular data from a number of lines

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

I am having an output like:

[org.asteriskjava.manager.event.StatusEvent[dateReceived=Fri Jan 09 17:29:49 IST 2009,privilege='Call',callerid='703',link='null',state='Ringing',timestamp='null',calleridname='',extension='null',uniqueid='1231502369.3',actionid='null',internalactionid='25116828_4',seconds='null',priority='null',context='null',account='null',calleridnum='703',channel='SIP/703-09728350',systemHashcode=22808536], org.asteriskjava.manager.event.StatusEvent[dateReceived=Fri Jan 09 17:29:49 IST 2009,privilege='Call',callerid='701',link='null',state='Ring',timestamp='null',calleridname='701',extension='s',uniqueid='1231502368.2',actionid='null',internalactionid='25116828_4',seconds='24',priority='7',context='macro-dial',account='null',calleridnum='701',channel='SIP/701-096e9ec0',systemHashcode=3098834], org.asteriskjava.manager.event.StatusCompleteEvent[dateReceived=Fri Jan 09 17:29:49 IST 2009,timestamp='null',actionid='null',internalactionid='25116828_4',systemHashcode=5626173]]


I need to take the state and channel alone from the above output.How could i take th eparticular datas from this much of lines?
Any help would be much appreciated.

Thanks.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi there, have you taken a look at the RegEx APIs in Java?


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Can you get your hands on the actual object and get the data from that?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

The Asterisk PBX doesn't provide a remote interface that supports direct transfer of Java objects like RMI, as far as I know.
If you're interested in reading PBX events you'll have to make due with a TCP/IP stream based interface that uses a basic key/value based protocol for this kind of data exchange, just like the data segment in the OP.
Luckily there's a Java API that allows you to easily interact with this manager interface, called Asterisk-Java. This API includes support for conveniently reading the data of a StatusEvent


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

Thank you for your reply.I have tried a simple example using regex to find out the word.But i am unable to find the dynamically changing word .(ie) my out put is:

[org.asteriskjava.manager.event.StatusEvent[dateReceived=Sat Jan 10 10:35:48 IST 2009,privilege='Call',callerid='703',link='SIP/701-0a1bad40',state='Up',timestamp='null',calleridname='<unknown>',extension='null',uniqueid='1231563940.3',actionid='null',internalactionid='11180085_4',seconds='null',priority='null',context='null',account='null',calleridnum='703',channel='SIP/703-0a1c13c8',systemHashcode=26988606], org.asteriskjava.manager.event.StatusEvent[dateReceived=Sat Jan 10 10:35:48 IST 2009,privilege='Call',callerid='701',link='SIP/703-0a1c13c8',state='Up',timestamp='null',calleridname='701',extension='s',uniqueid='1231563939.2',actionid='null',internalactionid='11180085_4',seconds='11',priority='7',context='macro-dial',account='null',calleridnum='701',channel='SIP/701-0a1bad40',systemHashcode=8694729], org.asteriskjava.manager.event.StatusCompleteEvent[dateReceived=Sat Jan 10 10:35:48 IST 2009,timestamp='null',actionid='null',internalactionid='11180085_4',systemHashcode=2765838]]


From the output i can able to take the state and channel.But the state may dynamically changed like "up","busy","ringing"etc.I dont know how to take the dynamically changing state.my code which i have tried tio take the state is as follows:

the output i got is:
state
state

But how could i get the status of the state whether busy,up or ringing?please help me to solve this.

Thanks.
[/code]
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

The regular expression is going to help you get to the place in the String where you want to get you info, but in fact I think you want to use String.split to split the string by ',' then grab the value after the '=' from the element that starts with "state". Does that make sense?

Note I'm not looking at the Java API, so treat this as pseudo code and forgive the less than best practices

Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
preethi Ayyappan wrote:...
the output i got is:
state
state

But how could i get the status of the state whether busy,up or ringing?please help me to solve this.

Thanks.


Use the following regex:



and instead of grouping your entire pattern, only get group 1 by doing:

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks piet.It helps me.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

I can able to get the states and channels continuously without specifying which state is for channel?I have to separate the output like state ring is for the channel sip/703, and state ringing is for sip/701 and then i have to store them in database.How could i do this?

Thanks
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Pseudo code:

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thank you for your reply.I can able to get the state in a variable like
String state=m.group(1);

But if i try to store the state in the database like

I am getting the following error:



ava.sql.SQLException: Syntax error or access violation message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Dialing''' at line 1"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2001)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1168)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1279)
at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1225)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2225)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1259)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1205)
at com.example.servlets.Call2.doGet(Call2.java:239)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)

Here extnum is the variable i got from the table contacts in the database people .
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
This is something entirely different as you original question. I suggest you create a new thread about your database problem.

Good luck.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

You should take a look at java.sql.PreparedStatement. It makes sure you can put just about anything in your SQL query and the statement will escape it for you where needed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: taking a particular data from a number of lines