public static void handle ( String s , AnnotationHandler ah ) { Map < String , Map < String , String > > l = new LinkedHashMap < String , Map < String , String > > ( ) ; Matcher m = annPattern . matcher ( s ) ; while ( m . find ( ) ) { // for (int i = 1; i <= m.groupCount(); i++) { // System.out.println("Group " + i + " '" + m.group(i) + "'"); // } String rest = s . substring ( m . end ( 0 ) ) . trim ( ) ; String srcLine = null ; if ( rest . indexOf ( ' ' ) > - 1 ) { srcLine = rest . substring ( 0 , rest . indexOf ( ' ' ) ) ; } else { srcLine = rest ; } Map < String , String > val = new LinkedHashMap < String , String > ( ) ; String annArgs = m . group ( 3 ) ; if ( annArgs == null ) { // no annotations arguments // e.g '@Function' } else if ( annArgs . indexOf ( ' ' ) > - 1 ) { // KVP annotation // e.g. '@Function(name="test", scope="global")' StringTokenizer t = new StringTokenizer ( annArgs , "," ) ; while ( t . hasMoreTokens ( ) ) { String arg = t . nextToken ( ) ; String key = arg . substring ( 0 , arg . indexOf ( ' ' ) ) ; String value = arg . substring ( arg . indexOf ( ' ' ) + 1 ) ; val . put ( key . trim ( ) , value . trim ( ) ) ; } } else { // single value annotation // e.g. '@Function("test"); val . put ( AnnotationHandler . VALUE , annArgs ) ; } l . put ( m . group ( 1 ) , val ) ; // If the next line also has an annotation // no source line will be passed into the handler if ( ! annTestPattern . matcher ( srcLine ) . find ( ) ) { ah . handle ( l , srcLine ) ; ah . log ( " Ann -> " + l ) ; ah . log ( " Src -> " + srcLine ) ; l = new LinkedHashMap < String , Map < String , String > > ( ) ; } } }
Handle a string with an annotation handler