private String convertPropertyFileLineToJSon ( String line , String json ) { if ( line == null ) return json ; String l = line . trim ( ) ; if ( l . length ( ) == 0 || l . startsWith ( "//" ) ) return json ; boolean inString = l . startsWith ( "\"" ) ; String english ; String rest ; if ( inString ) { boolean esc = false ; int i = 1 ; for ( ; i < l . length ( ) ; i ++ ) { if ( esc ) esc = false ; else if ( l . charAt ( i ) == ' ' ) esc = true ; else if ( l . charAt ( i ) == ' ' ) { i ++ ; break ; } } english = l . substring ( 1 , i - 1 ) . replace ( "\\\"" , "\"" ) ; rest = l . substring ( i ) . trim ( ) ; } else { int pos = l . indexOf ( ' ' ) ; if ( pos < 0 ) { LOGGER . severe ( "Illegal entry in language file: \"" + line + "\"" ) ; return json ; } else { english = l . substring ( 0 , pos ) . trim ( ) ; rest = l . substring ( pos ) ; } } if ( ! rest . startsWith ( "=" ) ) { LOGGER . severe ( "Illegal entry in language file: \"" + line + "\"" ) ; } else { String translation ; rest = rest . substring ( 1 ) . trim ( ) ; if ( rest . startsWith ( "\"" ) ) { int pos = rest . lastIndexOf ( "\"" ) ; if ( pos > 1 ) translation = rest . substring ( 1 , pos ) . replace ( "\\\"" , "\"" ) . trim ( ) ; else translation = rest . replace ( "\\\"" , "\"" ) ; // treat incorrect string gracefully } else translation = rest ; this . translations . put ( english , translation ) ; json += ", \"" + english + "\"" + "," + "\"" + translation + "\"" ; } return json ; }
Note this method has a side effect - it also fills the HashMap of translations .