Regular Expression for ContentProvider's Uri's

Quickly share your Android Code Snippets here...

Regular Expression for ContentProvider's Uri's

Postby Anm » Sat Oct 18, 2008 10:10 pm

This RegEx should deal with a standard content:// Uri, separating out type string, instance id, and arguments into different groups.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. StringBuffer sb = new StringBuffer();
  3.  
  4. sb.append( "\\A" );                               // Start of input
  5.  
  6. sb.append( "content://"+URI_AUTHORITY );          // content://provider
  7.  
  8. sb.append( "/(type1|type2|type/with/subtype)" );  // First path segment should be a recognized type (in a capture group)
  9.  
  10. sb.append( "(?:/(\\d*))?");                       // Optional second path segment should be digits (in a capture group)
  11.  
  12. sb.append( "/?");                                 // Optional end path separator
  13.  
  14. sb.append( "(?:\\?(.*))?" );                      // Optional argument list (in a capture group) prefixed with '?'
  15.  
  16. sb.append( "\\Z" );                               // End of input
  17.  
  18. Pattern regex = Pattern.compile( sb.toString(), Pattern.CASE_INSENSITIVE );
  19.  
  20.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Why? My app's single ContentProvider supports several types, kinda like the Contacts app. This seemed easier than digging into the pre-parsed URI with lots of nested if/else statements. Using this, I can quickly check if the URI matches and pull out the informative data in a single pass. It also allowed me to be more flexible in my inputs (ending path separators, etc.).

Usage Example 1: getType(..):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4. public String getType( Uri uri ) {
  5.  
  6.         Matcher matcher = URI_PATTERN.matcher( uri.toString() );
  7.  
  8.        
  9.  
  10.         String mime_type = null;
  11.  
  12.         if( matcher.matches() ) {
  13.  
  14.                 String type = matcher.group( 1 ).toLowerCase();
  15.  
  16.                 boolean has_ref_id = (matcher.group( 2 )!=null);
  17.  
  18.                
  19.  
  20.                 if( "task".equals( type ) ) {
  21.  
  22.                         if( has_ref_id ) {
  23.  
  24.                                 mime_type = MIME_TYPE_TASK;
  25.  
  26.                         } else {
  27.  
  28.                                 mime_type = MIME_TYPE_TASKS;
  29.  
  30.                         }
  31.  
  32.                 }  // else.. test against other types
  33.  
  34.         }
  35.  
  36.        
  37.  
  38.         return mime_type;
  39.  
  40. }
  41.  
  42.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Usage Example 2: query(..):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4. public Cursor query(
  5.  
  6.                 Uri uri,
  7.  
  8.                 String[] projection,
  9.  
  10.                 String selection,
  11.  
  12.                 String[] selectionArgs,
  13.  
  14.                 String sortOrder
  15.  
  16. ) {
  17.  
  18.         Cursor cursor = null;
  19.  
  20.        
  21.  
  22.         Matcher matcher = URI_PATTERN.matcher( uri.toString() );
  23.  
  24.         if( matcher.matches() ) {
  25.  
  26.                 String type = matcher.group( 1 ).toLowerCase();
  27.  
  28.                 String ref_id = matcher.group( 2 );
  29.  
  30.                 String uri_args = matcher.group( 3 );
  31.  
  32.                
  33.  
  34.                 if( "task".equals( type ) ) {
  35.  
  36.                         cursor = queryTask( ref_id, uri_args, projection, selection, selectionArgs, sortOrder );
  37.  
  38.                 }  // else.. test against other types
  39.  
  40.         }
  41.  
  42.        
  43.  
  44.         return cursor;
  45.  
  46. }
  47.  
  48.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


One thing you might consider changing is making the reference id mutually exclusive with the selection arguments.
Attachments
TestAndroidUriRegex.java
Example with tests against positive and negative examples.
(3.05 KiB) Downloaded 95 times
Anm
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 15, 2008 8:21 am

Top

Postby Anm » Fri Oct 24, 2008 7:47 am

Silly me. I just found the UriMatcher.
Anm
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 15, 2008 8:21 am

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 3 guests