Posted By

tgunr on 03/14/07


Tagged

objc spotlight


Versions (?)

Objective-C Memo


 / Published in: Objective C
 

URL: http://homepage.mac.com/mnishikata/page2/page2.html

  1. mdimporter、kMDItemTextContent
  2.  
  3.  
  4. ------------ SpotlightTextContentRetriever.h
  5.  
  6. @interface SpotlightTextContentRetriever : NSObject {
  7.  
  8. }
  9. +(void)initialize;
  10. +(NSMutableArray* )metaDataOfFileAtPath:(NSString*)targetFilePath;
  11. +(NSString* )textContentOfFileAtPath:(NSString*)targetFilePath;
  12. +(NSMutableDictionary*)executeMDImporterAtPath:(NSString*)mdimportPath forPath:(NSString*)path uti:(NSString*)uti;
  13.  
  14. @end
  15.  
  16. ------------ SpotlightTextContentRetriever.m
  17. //
  18. // SpotlightTextContentRetriever.m
  19. // SpotInside
  20. //
  21. // Created by Masatoshi Nishikata on 06/11/22.
  22. // Copyright 2006 __MyCompanyName__. All rights reserved.
  23. //
  24.  
  25.  
  26. /*
  27. Using codes from Apple's BasicPlugin
  28. ----------------------------------
  29.  
  30. Description: Basic CFPlugIn sample code shell, Carbon API
  31.  
  32. Copyright: © Copyright 2001 Apple Computer, Inc. All rights reserved.
  33.  
  34. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
  35. ("Apple") in consideration of your agreement to the following terms, and your
  36. use, installation, modification or redistribution of this Apple software
  37. constitutes acceptance of these terms. If you do not agree with these terms,
  38. please do not use, install, modify or redistribute this Apple software.
  39.  
  40. In consideration of your agreement to abide by the following terms, and subject
  41. to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  42. copyrights in this original Apple software (the "Apple Software"), to use,
  43. reproduce, modify and redistribute the Apple Software, with or without
  44. modifications, in source and/or binary forms; provided that if you redistribute
  45. the Apple Software in its entirety and without modifications, you must retain
  46. this notice and the following text and disclaimers in all such redistributions of
  47. the Apple Software. Neither the name, trademarks, service marks or logos of
  48. Apple Computer, Inc. may be used to endorse or promote products derived from the
  49. Apple Software without specific prior written permission from Apple. Except as
  50. expressly stated in this notice, no other rights or licenses, express or implied,
  51. are granted by Apple herein, including but not limited to any patent rights that
  52. may be infringed by your derivative works or by other works in which the Apple
  53. Software may be incorporated.
  54.  
  55. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
  56. WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  57. WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  58. PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  59. COMBINATION WITH YOUR PRODUCTS.
  60.  
  61. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  62. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  63. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  64. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  65. OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  66. (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  67. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  68.  
  69. */
  70.  
  71.  
  72.  
  73. #include <Carbon/Carbon.h>
  74. #include <CoreFoundation/CoreFoundation.h>
  75. #include <CoreFoundation/CFPlugInCOM.h>
  76. #import "SpotlightTextContentRetriever.h"
  77.  
  78.  
  79. typedef struct PlugInInterfaceStruct {
  80. IUNKNOWN_C_GUTS;
  81. Boolean (*GetMetadataForFile)(void* myInstance,
  82. CFMutableDictionaryRef attributes,
  83. CFStringRef contentTypeUTI,
  84. CFStringRef pathToFile);
  85.  
  86. } MDImporterInterfaceStruct;
  87.  
  88.  
  89. static MDImporterInterfaceStruct **mdimporterInterface = NULL;
  90.  
  91. /*
  92. typedef Boolean (*getMetadataForFileFunction)(void* thisInterface,
  93. CFMutableDictionaryRef attributes,
  94. CFStringRef contentTypeUTI,
  95. CFStringRef pathToFile);
  96.  
  97. */
  98.  
  99.  
  100. @implementation SpotlightTextContentRetriever
  101.  
  102. static NSArray* mdimporterArray;
  103.  
  104. +(void)initialize
  105. {
  106. // Get and store MDImporter list
  107. NSTask *task = [[NSTask alloc] init];
  108. NSPipe *messagePipe = [NSPipe pipe];
  109.  
  110. [task setLaunchPath:@"/usr/bin/mdimport"];
  111. [task setArguments:[NSArray arrayWithObjects: @"-L" ,nil]];
  112.  
  113.  
  114. [task setStandardError : messagePipe];
  115. [task launch];
  116. [task waitUntilExit];
  117.  
  118.  
  119.  
  120. NSData *messageData = [[messagePipe fileHandleForReading] availableData];
  121.  
  122.  
  123. NSString* message;
  124. message = [[[NSString alloc] initWithData:messageData
  125. encoding:NSUTF8StringEncoding] autorelease];
  126.  
  127. [task release];
  128.  
  129.  
  130.  
  131. // Cut unwanted string
  132.  
  133. NSRange firstReturn = [message rangeOfString:@"¥n"];
  134. NSString* arrayStr = [message substringFromIndex: firstReturn.location-1];
  135.  
  136.  
  137. // Convert string to array
  138. mdimporterArray = [[arrayStr propertyList] retain];
  139.  
  140. /*
  141. // Convert percentage encoded string
  142. int hoge;
  143. NSMutableArray* array = [NSMutableArray array];
  144. for( hoge = 0; hoge < [tempArray count]; hoge++ )
  145. {
  146. NSString* aPath = [tempArray objectAtIndex:hoge];
  147. aPath = [aPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
  148.  
  149. [array addObject:aPath ];
  150.  
  151. }
  152. mdimporterArray = [[NSArray arrayWithArray: array ] retain];
  153. */
  154. //NSLog(@"mdimporterArray %@", [mdimporterArray description]);
  155.  
  156. }
  157.  
  158. +(NSMutableArray* )metaDataOfFileAtPath:(NSString*)targetFilePath
  159. {
  160. // Get UTI of the given file
  161.  
  162. targetFilePath = [targetFilePath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
  163. NSURL* anUrl = [NSURL URLWithString: targetFilePath];
  164. FSRef ref;
  165. CFURLGetFSRef(anUrl,&ref);
  166. CFTypeRef outValue;
  167. LSCopyItemAttribute (
  168. &ref,
  169. kLSRolesAll,
  170. kLSItemContentType,
  171. &outValue
  172. );
  173.  
  174. if( outValue == nil ) return nil;
  175.  
  176. NSString* uti = [NSString stringWithString:outValue];
  177. CFRelease(outValue);
  178.  
  179. //NSLog(@"uti %@",uti);
  180.  
  181.  
  182. //----------------
  183.  
  184.  
  185. //Get handlers that can handle the file
  186. CFArrayRef ha = LSCopyAllRoleHandlersForContentType (
  187. uti,
  188. kLSRolesAll
  189. );
  190. NSArray* handlerArray = [NSArray arrayWithArray: ha];
  191. CFRelease(ha);
  192.  
  193. //NSLog(@"handlers %@",[handlerArray description]);
  194.  
  195.  
  196. //----------------
  197.  
  198. //Evaluate
  199.  
  200. int hoge;
  201. for( hoge = 0; hoge < [mdimporterArray count]; hoge++ )
  202. {
  203. NSString* mdimporterPath = [mdimporterArray objectAtIndex:hoge];
  204. NSBundle* bndl = [NSBundle bundleWithPath: mdimporterPath ];
  205.  
  206. if( bndl != nil )
  207. {
  208.  
  209. int piyo;
  210. for( piyo = 0; piyo < [handlerArray count]; piyo++ )
  211. {
  212. NSString* aHandler = [handlerArray objectAtIndex:piyo];
  213.  
  214.  
  215. //NSLog(@"Compareing %@, %@ ",aHandler, [bndl bundleIdentifier]);
  216.  
  217.  
  218. if( [aHandler isEqualToString:[bndl bundleIdentifier] ] )
  219. {
  220.  
  221. //NSLog(@"Executing mdimporterPath %@ for targetFilePath %@",mdimporterPath,targetFilePath);
  222.  
  223. // found one mdimporter
  224. NSMutableDictionary* attributes =
  225. [SpotlightTextContentRetriever executeMDImporterAtPath:mdimporterPath
  226. forPath:targetFilePath
  227. uti:uti];
  228.  
  229.  
  230. if( [attributes objectForKey:kMDItemTextContent] != nil )
  231. return attributes;
  232.  
  233. }
  234. }
  235.  
  236. }else
  237. {
  238. //NSLog(@"bndl is null");
  239.  
  240. }
  241.  
  242. }
  243. /*
  244. // use text mdimporter if appropriate importer cannot be found
  245. ///System/Library/Spotlight/RichText.mdimporter
  246.  
  247. //NSLog(@"%@",uti);
  248. NSMutableDictionary* attributes =
  249. [SpotlightTextContentRetriever executeMDImporterAtPath:@"/System/Library/Spotlight/RichText.mdimporter"
  250. forPath:targetFilePath
  251. uti:uti];
  252.  
  253. return attributes;
  254. */
  255.  
  256. return nil;
  257.  
  258. }
  259.  
  260. +(NSString* )textContentOfFileAtPath:(NSString*)targetFilePath
  261. {
  262.  
  263. NSMutableDictionary* attributes =
  264. [SpotlightTextContentRetriever metaDataOfFileAtPath:targetFilePath];
  265.  
  266. id textContent = [attributes objectForKey:kMDItemTextContent];
  267. if( [textContent isKindOfClass:[NSString class]] )
  268. {
  269. //NSLog(@"%@",textContent);
  270. return textContent;
  271. }
  272.  
  273. return nil;
  274. }
  275.  
  276.  
  277. +(NSMutableDictionary*)executeMDImporterAtPath:(NSString*)mdimportPath forPath:(NSString*)path uti:(NSString*)uti
  278. {
  279. mdimportPath = [mdimportPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
  280.  
  281. NSMutableDictionary* attributes = nil;
  282. CFBundleRef bundle;
  283.  
  284.  
  285.  
  286. CFURLRef url = CFURLCreateWithString (
  287. nil,
  288. mdimportPath,
  289. nil
  290. );
  291.  
  292. if( url == nil ) return nil;
  293.  
  294. /*
  295. NSLog(@"start executeMDImporterAtPath");
  296.  
  297. NSLog(@"Execute GetMetadataForFile");
  298. NSLog(@"uti %@",uti);
  299. NSLog(@"path %@",path);
  300. NSLog(@"mdimport %@",mdimportPath );
  301. */
  302.  
  303. // Create CFPlugInRef
  304.  
  305. CFPlugInRef plugin = CFPlugInCreate(NULL, url);
  306. CFRelease(url);
  307.  
  308.  
  309. if (!plugin)
  310. {
  311. //NSLog(@"Could not create CFPluginRef.¥n");
  312. return nil;
  313. }
  314.  
  315.  
  316. // The plug-in was located. Now locate the interface.
  317.  
  318. BOOL foundInterface = NO;
  319. CFArrayRef factories;
  320.  
  321. // See if this plug-in implements the Test type.
  322. factories = CFPlugInFindFactoriesForPlugInTypeInPlugIn( kMDImporterTypeID, plugin );
  323.  
  324. // If there are factories for the Test type, attempt to get the IUnknown interface.
  325. if ( factories != NULL )
  326. {
  327. CFIndex factoryCount;
  328. CFIndex index;
  329.  
  330. factoryCount = CFArrayGetCount( factories );
  331. if ( factoryCount > 0 )
  332. {
  333. for ( index = 0 ; (index < factoryCount) && (foundInterface == false) ; index++ )
  334. {
  335. CFUUIDRef factoryID;
  336.  
  337. // Get the factory ID for the first location in the array of IDs.
  338. factoryID = (CFUUIDRef) CFArrayGetValueAtIndex( factories, index );
  339. if ( factoryID )
  340. {
  341. IUnknownVTbl **iunknown;
  342.  
  343. // Use the factory ID to get an IUnknown interface. Here the plug-in code is loaded.
  344. iunknown = (IUnknownVTbl **) CFPlugInInstanceCreate( NULL, factoryID, kMDImporterTypeID );
  345.  
  346. if ( iunknown )
  347. {
  348. // If this is an IUnknown interface, query for the test interface.
  349. (*iunknown)->QueryInterface( iunknown, CFUUIDGetUUIDBytes( kMDImporterInterfaceID ), (LPVOID *)( &mdimporterInterface ) );
  350.  
  351. // Now we are done with IUnknown
  352. (*iunknown)->Release( iunknown );
  353.  
  354. if ( mdimporterInterface )
  355. {
  356. // We found the interface we need
  357. foundInterface = true;
  358. }
  359. }
  360. }
  361. }
  362. }
  363. }
  364.  
  365. CFRelease( factories );
  366.  
  367.  
  368.  
  369. if ( foundInterface == false )
  370. {
  371. }
  372. else
  373. {
  374. attributes = [NSMutableDictionary dictionary];
  375. path = [path stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
  376.  
  377. //NSLog(@"start getMetadataForFile CFPlugin");
  378.  
  379. (*mdimporterInterface)->GetMetadataForFile( mdimporterInterface,
  380. attributes,
  381. uti,
  382. path);
  383. //NSLog(@"%@",[attributes description]);
  384.  
  385. (*mdimporterInterface)->Release( mdimporterInterface );
  386.  
  387. }
  388.  
  389. // Finished
  390.  
  391. CFRelease( plugin );
  392. plugin = NULL;
  393.  
  394. return attributes;
  395. }
  396.  
  397. @end

Report this snippet  

You need to login to post a comment.