|
4 | 4 |
|
5 | 5 | import java.nio.*; |
6 | 6 | import java.nio.charset.*; |
| 7 | +import java.util.regex.Pattern; |
| 8 | +import java.util.Arrays; |
7 | 9 |
|
8 | 10 | /** |
9 | 11 | * <type><name>0<data> |
@@ -106,15 +108,67 @@ public static boolean cameFromDB( DBObject o ){ |
106 | 108 | } |
107 | 109 |
|
108 | 110 | public static int patternFlags( String flags ){ |
109 | | - if ( flags.length() == 0 ) |
110 | | - return 0; |
111 | | - throw new RuntimeException( "can't convert flags yet" ); |
| 111 | + flags = flags.toLowerCase(); |
| 112 | + Flag f[] = Flag.values(); |
| 113 | + int fint = 0; |
| 114 | + int count = 0; |
| 115 | + |
| 116 | + for( Flag flag : f ) { |
| 117 | + if( flags.indexOf( flag.flagChar ) >= 0 ) { |
| 118 | + fint |= flag.javaFlag; |
| 119 | + count++; |
| 120 | + if( flag.unsupported != null ) |
| 121 | + _warnUnsupported( flag.unsupported ); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if( count < flags.length() ) |
| 126 | + throw new RuntimeException( "some flags could not be converted: "+flags ); |
| 127 | + |
| 128 | + return fint; |
112 | 129 | } |
113 | 130 |
|
114 | 131 | public static String patternFlags( int flags ){ |
115 | | - if ( flags == 0 ) |
116 | | - return ""; |
117 | | - throw new RuntimeException( "can't convert flags yet" ); |
| 132 | + Flag f[] = Flag.values(); |
| 133 | + byte b[] = new byte[ f.length ]; |
| 134 | + int count = 0; |
| 135 | + |
| 136 | + for( Flag flag : f ) { |
| 137 | + if( ( flags & flag.javaFlag ) > 0 ) { |
| 138 | + b[ count++ ] = (byte)flag.flagChar; |
| 139 | + flags -= flag.javaFlag; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if( flags > 0 ) |
| 144 | + throw new RuntimeException( "some flags could not be recognized." ); |
| 145 | + |
| 146 | + Arrays.sort( b ); |
| 147 | + return new String( b, 0, count ); |
| 148 | + } |
| 149 | + |
| 150 | + private static enum Flag { |
| 151 | + CANON_EQ( Pattern.CANON_EQ, 'c', "PATTERN.CANON_EQ" ), |
| 152 | + DOTALL( Pattern.DOTALL, 'd', "Pattern.DOTALL" ), |
| 153 | + CASE_INSENSITIVE( Pattern.CASE_INSENSITIVE, 'i', null ), |
| 154 | + UNIX_LINES( Pattern.UNIX_LINES, 'l', "Pattern.UNIX_LINES" ), |
| 155 | + MULTILINE(Pattern.MULTILINE, 'm', null ), |
| 156 | + LITERAL( Pattern.LITERAL, 't', "Pattern.LITERAL" ), |
| 157 | + UNICODE_CASE( Pattern.UNICODE_CASE, 'u', "Pattern.UNICODE_CASE" ), |
| 158 | + COMMENTS( Pattern.COMMENTS, 'x', null ); |
| 159 | + |
| 160 | + public final int javaFlag; |
| 161 | + public final char flagChar; |
| 162 | + public final String unsupported; |
| 163 | + Flag( int f, char ch, String u ) { |
| 164 | + javaFlag = f; |
| 165 | + flagChar = ch; |
| 166 | + unsupported = u; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private static void _warnUnsupported( String flag ) { |
| 171 | + System.out.println( "flag " + flag + " not supported by db." ); |
118 | 172 | } |
119 | 173 |
|
120 | 174 | static final String NO_REF_HACK = "_____nodbref_____"; |
|
0 commit comments