Regex ILE RPG Part 2

465 pts.
Tags:
iSeries
Java
RPG
So after my failed attempt to get the Regex C APIs working (see earlier post), I rewrote the service program as a wrapper to facilitate the calling of java.util.regex based functionality. All code fragments below. Make a call passing some simple stuff. No luck whatsoever getting a result. Try using the same within a test harness within the Eclipse environment that I used to create the class and it works just fine. Grrrr.. Any ideas? Can anybody please see anything obviously wrong with my coding? Is character sets an issue? I thought objects underwent auto-conversion? Thanks in advance (again)

Answer Wiki

Thanks. We'll let you know when a new response is added.

Test RPG to call ILE Service Program

     Hdebug(*yes)
      *
     dRegex            pr          4500      extproc('REGEX')
     dStringIn                     2000      const
     dRegExpIn                      500      const
      *
     d
     d retval          ds          4500
     d  result1                1   2000
     d  result2             2001   4000
     d  error               4001   4500
      *
     dtests            s           2000
     dtestr            s            500
      *
     c                   eval      tests = 'I have a cat, but I like my dog -
     c                             better. Pity the mouse is dead' + x'00'
     c                   eval      testr = '(mouse|cat|dog|wolf|bear|human)'
      *
     c                   eval      retval = regex(tests:testr)
     c                   dump
     c                   move      '1'           *inlr
     c                   return

Test Service Program (ILE RPG)

     h debug thread(*serialize) nomain bnddir('QC2LE')
      *
      *****************************************************************
      * Procedure Interface(s)                                        *
      *****************************************************************
      *
     dRegex            pr          4500
     dStringIn                     2000
     dRegExpIn                      500
      *
      *****************************************************************
      * Sub-procedure Regex                                           *
      *****************************************************************
      *
     PRegex            b                   export
     d                 pi          4500
     dStringIn                     2000
     dRegExpIn                      500
      *
      *****************************************************************
      * Variable Specifications                                       *
      *****************************************************************
      *
     d retval          ds
     d  result1                1   2000
     d  result2             2001   4000
     d  error               4001   4500
      *
      *  Define classname constants
      *
     Dclass1           c                   'com.robinsonway.uk.regex.Regex'
      *
      *
      *  Prototype constructor/method calls
      *
     DRegex            PR              O   ExtProc(*JAVA:Class1:*constructor)
     D                                     Class(*JAVA:Class1)
      *  constructor parameters
     D  SearchString                   O   Class(*JAVA:'java.lang.String')
     D                                     Const
     D  RegExp                         O   Class(*JAVA:'java.lang.String')
     D                                     Const
      *
     DgetTotalMatches  PR             5I 0 ExtProc(*JAVA:Class1:'getTotalMatche-
     D                                     s')
      *
     DgetStart         PR             5I 0 ExtProc(*JAVA:Class1:'getStart')
      *  method parameters
     Dindex                           5I 0 Value
      *
     DgetEnd           PR             5I 0 ExtProc(*JAVA:Class1:'getEnd')
      *  method parameters
     Dindex                           5I 0 Value
      *
      *  newString constructor that accepts a byte array
      *  (an alphnumeric variable in RPG speak). It
      *  returns a string object.
      *
     D newString       PR              O   ExtProc(*JAVA:
     D                                             'java.lang.String':
     D                                             *CONSTRUCTOR)
     D                                     Class(*JAVA:'java.lang.String')
     D  bytes                      9999A   Const Varying
      *
      * Method variables
      *
     D sSearchString   S               O   Class(*JAVA:'java.lang.String')
     D sRegExp         S               O   Class(*JAVA:'java.lang.String')
     D RegObj          S               O   Class(*JAVA:class1)
     d entries         s              5i 0
     d start           s              5i 0
     d end             s              5i 0
     d idx             s              5i 0
      *
      * Set the Java classpath etc
      *
     c                   call      'SETJVAENV'
     c                   parm      'TALP'        app              10
     c                   parm      'A'           aorr              1
      *
      * Instantiation of Regex Class
      *
     c                   eval      sSearchString =
     c                             newstring(%trim(StringIn))
     c                   eval      sRegExp =
     c                             newstring(RegExpIn)
     c                   eval      RegObj = Regex(sSearchString:sRegExp)
      *
     c                   eval      entries = getTotalMatches(RegObj)
      *
     c                   eval      idx = 0
     c     1             do        entries
     c                   eval      start = getStart(RegObj:idx)
     c                   eval      end = getEnd(RegObj:idx)
     c                   add       1             idx
     c                   enddo
      *
     c                   dump
      *
     c                   move      '1'           *inlr
     c                   return    retval
      *
     p                 e

Test Java Program (compiled into Jar using Eclipse for PC)

import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;

public class Regex 
{
	private List <String> results;
	private List <String> results_start;
	private List <String> results_end;
	private Matcher m;
	private short matches;

	public Regex(String searchString, String regExp)
	{
		System.out.println(searchString + ":" + regExp);
		Pattern p = Pattern.compile(regExp);
		System.out.println(p.pattern());
		m = p.matcher(searchString);

		results = new ArrayList<String>();
		results_start = new ArrayList<String>();
		results_end = new ArrayList<String>();
		while (m.find()) 
		{
			System.out.println("Found a " + m.group() + ".");
			results.add(m.group());
			results_start.add("" + m.start());
			results_end.add("" + m.end());
			matches++;
		}
	}
	public short getStart(short idx)
	{
		String startString = results_start.get(idx);
		short start = Short.valueOf(startString);
		start++;
		return start;
	}
	public short getEnd(short idx)
	{
		String endString = results_end.get(idx);
		return Short.valueOf(endString);
	}
	public short getTotalMatches()
	{
		return matches;
	}
}

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • andyhumphreys
    Test RPG to call ILE RPG Service Program     Hdebug(*yes)      *     dRegex            pr          4500      extproc('REGEX')     dStringIn                     2000      const     dRegExpIn                      500      const      *     d     d retval          ds          4500     d  result1                1   2000     d  result2             2001   4000     d  error               4001   4500      *     dtests            s           2000     dtestr            s            500      *     c                   eval      tests = 'I have a cat, but I like my dog -     c                             better. Pity the mouse is dead' + x'00'     c                   eval      testr = '(mouse|cat|dog|wolf|bear|human)'      *     c                   eval      retval = regex(tests:testr)     c                   dump     c                   move      '1'           *inlr     c                   return
    465 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following