/ Published in: Objective C
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
mdimporterã€kMDItemTextContent ------------ SpotlightTextContentRetriever.h } +(void)initialize; +(NSMutableDictionary*)executeMDImporterAtPath:(NSString*)mdimportPath forPath:(NSString*)path uti:(NSString*)uti; @end ------------ SpotlightTextContentRetriever.m // // SpotlightTextContentRetriever.m // SpotInside // // Created by Masatoshi Nishikata on 06/11/22. // Copyright 2006 __MyCompanyName__. All rights reserved. // /* Using codes from Apple's BasicPlugin ---------------------------------- Description: Basic CFPlugIn sample code shell, Carbon API Copyright: © Copyright 2001 Apple Computer, Inc. All rights reserved. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in consideration of your agreement to the following terms, and your use, installation, modification or redistribution of this Apple software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, modify or redistribute this Apple software. In consideration of your agreement to abide by the following terms, and subject to these terms, Apple grants you a personal, non-exclusive license, under Apple’s copyrights in this original Apple software (the "Apple Software"), to use, reproduce, modify and redistribute the Apple Software, with or without modifications, in source and/or binary forms; provided that if you redistribute the Apple Software in its entirety and without modifications, you must retain this notice and the following text and disclaimers in all such redistributions of the Apple Software. Neither the name, trademarks, service marks or logos of Apple Computer, Inc. may be used to endorse or promote products derived from the Apple Software without specific prior written permission from Apple. Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your derivative works or by other works in which the Apple Software may be incorporated. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include <Carbon/Carbon.h> #include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CFPlugInCOM.h> #import "SpotlightTextContentRetriever.h" typedef struct PlugInInterfaceStruct { IUNKNOWN_C_GUTS; Boolean (*GetMetadataForFile)(void* myInstance, CFMutableDictionaryRef attributes, CFStringRef contentTypeUTI, CFStringRef pathToFile); } MDImporterInterfaceStruct; static MDImporterInterfaceStruct **mdimporterInterface = NULL; /* typedef Boolean (*getMetadataForFileFunction)(void* thisInterface, CFMutableDictionaryRef attributes, CFStringRef contentTypeUTI, CFStringRef pathToFile); */ @implementation SpotlightTextContentRetriever +(void)initialize { // Get and store MDImporter list [task setLaunchPath:@"/usr/bin/mdimport"]; [task setStandardError : messagePipe]; [task launch]; [task waitUntilExit]; NSString* message; encoding:NSUTF8StringEncoding] autorelease]; [task release]; // Cut unwanted string NSRange firstReturn = [message rangeOfString:@"Â¥n"]; // Convert string to array mdimporterArray = [[arrayStr propertyList] retain]; /* // Convert percentage encoded string int hoge; NSMutableArray* array = [NSMutableArray array]; for( hoge = 0; hoge < [tempArray count]; hoge++ ) { NSString* aPath = [tempArray objectAtIndex:hoge]; aPath = [aPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; [array addObject:aPath ]; } mdimporterArray = [[NSArray arrayWithArray: array ] retain]; */ //NSLog(@"mdimporterArray %@", [mdimporterArray description]); } { // Get UTI of the given file targetFilePath = [targetFilePath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; FSRef ref; CFURLGetFSRef(anUrl,&ref); CFTypeRef outValue; LSCopyItemAttribute ( &ref, kLSRolesAll, kLSItemContentType, &outValue ); if( outValue == nil ) return nil; CFRelease(outValue); //NSLog(@"uti %@",uti); //---------------- //Get handlers that can handle the file CFArrayRef ha = LSCopyAllRoleHandlersForContentType ( uti, kLSRolesAll ); CFRelease(ha); //NSLog(@"handlers %@",[handlerArray description]); //---------------- //Evaluate int hoge; for( hoge = 0; hoge < [mdimporterArray count]; hoge++ ) { if( bndl != nil ) { int piyo; for( piyo = 0; piyo < [handlerArray count]; piyo++ ) { //NSLog(@"Compareing %@, %@ ",aHandler, [bndl bundleIdentifier]); if( [aHandler isEqualToString:[bndl bundleIdentifier] ] ) { //NSLog(@"Executing mdimporterPath %@ for targetFilePath %@",mdimporterPath,targetFilePath); // found one mdimporter [SpotlightTextContentRetriever executeMDImporterAtPath:mdimporterPath forPath:targetFilePath uti:uti]; if( [attributes objectForKey:kMDItemTextContent] != nil ) return attributes; } } }else { //NSLog(@"bndl is null"); } } /* // use text mdimporter if appropriate importer cannot be found ///System/Library/Spotlight/RichText.mdimporter //NSLog(@"%@",uti); NSMutableDictionary* attributes = [SpotlightTextContentRetriever executeMDImporterAtPath:@"/System/Library/Spotlight/RichText.mdimporter" forPath:targetFilePath uti:uti]; return attributes; */ return nil; } { [SpotlightTextContentRetriever metaDataOfFileAtPath:targetFilePath]; id textContent = [attributes objectForKey:kMDItemTextContent]; { //NSLog(@"%@",textContent); return textContent; } return nil; } +(NSMutableDictionary*)executeMDImporterAtPath:(NSString*)mdimportPath forPath:(NSString*)path uti:(NSString*)uti { mdimportPath = [mdimportPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; CFBundleRef bundle; CFURLRef url = CFURLCreateWithString ( nil, mdimportPath, nil ); if( url == nil ) return nil; /* NSLog(@"start executeMDImporterAtPath"); NSLog(@"Execute GetMetadataForFile"); NSLog(@"uti %@",uti); NSLog(@"path %@",path); NSLog(@"mdimport %@",mdimportPath ); */ // Create CFPlugInRef CFPlugInRef plugin = CFPlugInCreate(NULL, url); CFRelease(url); if (!plugin) { //NSLog(@"Could not create CFPluginRef.Â¥n"); return nil; } // The plug-in was located. Now locate the interface. BOOL foundInterface = NO; CFArrayRef factories; // See if this plug-in implements the Test type. factories = CFPlugInFindFactoriesForPlugInTypeInPlugIn( kMDImporterTypeID, plugin ); // If there are factories for the Test type, attempt to get the IUnknown interface. if ( factories != NULL ) { CFIndex factoryCount; CFIndex index; factoryCount = CFArrayGetCount( factories ); if ( factoryCount > 0 ) { for ( index = 0 ; (index < factoryCount) && (foundInterface == false) ; index++ ) { CFUUIDRef factoryID; // Get the factory ID for the first location in the array of IDs. factoryID = (CFUUIDRef) CFArrayGetValueAtIndex( factories, index ); if ( factoryID ) { IUnknownVTbl **iunknown; // Use the factory ID to get an IUnknown interface. Here the plug-in code is loaded. iunknown = (IUnknownVTbl **) CFPlugInInstanceCreate( NULL, factoryID, kMDImporterTypeID ); if ( iunknown ) { // If this is an IUnknown interface, query for the test interface. (*iunknown)->QueryInterface( iunknown, CFUUIDGetUUIDBytes( kMDImporterInterfaceID ), (LPVOID *)( &mdimporterInterface ) ); // Now we are done with IUnknown (*iunknown)->Release( iunknown ); if ( mdimporterInterface ) { // We found the interface we need foundInterface = true; } } } } } } CFRelease( factories ); if ( foundInterface == false ) { } else { path = [path stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; //NSLog(@"start getMetadataForFile CFPlugin"); (*mdimporterInterface)->GetMetadataForFile( mdimporterInterface, attributes, uti, path); //NSLog(@"%@",[attributes description]); (*mdimporterInterface)->Release( mdimporterInterface ); } // Finished CFRelease( plugin ); plugin = NULL; return attributes; } @end
URL: http://homepage.mac.com/mnishikata/page2/page2.html