Revision: 18464
http://bibdesk.svn.sourceforge.net/bibdesk/?rev=18464&view=rev
Author: hofman
Date: 2012-02-02 12:59:05 +0000 (Thu, 02 Feb 2012)
Log Message:
-----------
move parsing of string for items to string parser class
Modified Paths:
--------------
trunk/bibdesk/BDSKEditor.m
trunk/bibdesk/BDSKImportCommand.m
trunk/bibdesk/BDSKStringParser.h
trunk/bibdesk/BDSKStringParser.m
trunk/bibdesk/BDSKTextImportController.m
trunk/bibdesk/BibDocument.m
Modified: trunk/bibdesk/BDSKEditor.m
===================================================================
--- trunk/bibdesk/BDSKEditor.m 2012-02-02 12:01:42 UTC (rev 18463)
+++ trunk/bibdesk/BDSKEditor.m 2012-02-02 12:59:05 UTC (rev 18464)
@@ -2689,11 +2689,14 @@
NSString *pbString = [pboard stringForType:NSStringPboardType];
NSError *error = nil;
// this returns nil when there was a parser error and the user didn't
decide to proceed anyway
- draggedPubs = [[self document] publicationsForString:pbString
type:[pbString contentStringType] verbose:NO error:&error];
+ draggedPubs = [BDSKStringParser itemsFromString:pbString
ofType:[pbString contentStringType] owner:[self document] error:&error];
// we ignore warnings for parsing with temporary keys, but we want to
ignore the cite key in that case
if([error isLocalError] && [error code] == kBDSKHadMissingCiteKeys) {
hasTemporaryCiteKey = YES;
error = nil;
+ } else if ([error isLocalError] && [error code] ==
kBDSKBibTeXParserFailed) {
+ // should we accept partially parsed bibtex?
+ draggedPubs = nil;
}
}else if([pboardType isEqualToString:BDSKBibItemPboardType]){
NSData *pbData = [pboard dataForType:BDSKBibItemPboardType];
Modified: trunk/bibdesk/BDSKImportCommand.m
===================================================================
--- trunk/bibdesk/BDSKImportCommand.m 2012-02-02 12:01:42 UTC (rev 18463)
+++ trunk/bibdesk/BDSKImportCommand.m 2012-02-02 12:59:05 UTC (rev 18464)
@@ -118,7 +118,7 @@
[self setScriptErrorNumber:NSArgumentsWrongScriptError];
return nil;
}
- pubs = [document publicationsForString:string
type:BDSKUnknownStringType verbose:NO error:NULL];
+ pubs = [BDSKStringParser itemsFromString:string
ofType:BDSKUnknownStringType owner:document error:NULL];
} else if (searchTerm) {
pubs = [NSArray arrayWithObjects:[BibItem
itemWithPubMedSearchTerm:searchTerm], nil];
} else {
Modified: trunk/bibdesk/BDSKStringParser.h
===================================================================
--- trunk/bibdesk/BDSKStringParser.h 2012-02-02 12:01:42 UTC (rev 18463)
+++ trunk/bibdesk/BDSKStringParser.h 2012-02-02 12:59:05 UTC (rev 18464)
@@ -56,10 +56,13 @@
};
typedef NSInteger BDSKStringType;
+@protocol BDSKOwner;
+
@interface BDSKStringParser : NSObject
// passing BDSKUnknownStringType will ise the appropriate -contentStringType
+ (BOOL)canParseString:(NSString *)string ofType:(BDSKStringType)stringType;
+ (NSArray *)itemsFromString:(NSString *)string
ofType:(BDSKStringType)stringType error:(NSError **)outError;
++ (NSArray *)itemsFromString:(NSString *)string ofType:(BDSKStringType)type
owner:(id <BDSKOwner>)owner error:(NSError **)outError;
@end
Modified: trunk/bibdesk/BDSKStringParser.m
===================================================================
--- trunk/bibdesk/BDSKStringParser.m 2012-02-02 12:01:42 UTC (rev 18463)
+++ trunk/bibdesk/BDSKStringParser.m 2012-02-02 12:59:05 UTC (rev 18464)
@@ -94,6 +94,34 @@
return [parserClass itemsFromString:string error:outError];
}
++ (NSArray *)itemsFromString:(NSString *)string ofType:(BDSKStringType)type
owner:(id <BDSKOwner>)owner error:(NSError **)outError {
+ NSArray *newPubs = nil;
+ NSError *parseError = nil;
+ BOOL isPartialData = NO;
+
+ // @@ BDSKStringParser doesn't handle any BibTeX types, so it's not really
useful as a funnel point for any string type, since each usage requires special
casing for BibTeX.
+ if(BDSKUnknownStringType == type)
+ type = [string contentStringType];
+
+ if(type == BDSKBibTeXStringType)
+ newPubs = [BDSKBibTeXParser itemsFromString:string owner:owner
isPartialData:&isPartialData error:&parseError];
+ else if(type == BDSKNoKeyBibTeXStringType)
+ newPubs = [BDSKBibTeXParser itemsFromString:[string
stringWithPhoneyCiteKeys:@"FixMe"] owner:owner isPartialData:&isPartialData
error:&parseError];
+ else
+ // this will create the NSError if the type is unrecognized
+ newPubs = [self itemsFromString:string ofType:type error:&parseError];
+
+ if(type == BDSKNoKeyBibTeXStringType && parseError == nil){
+ // return an error when we inserted temporary keys, let the caller
decide what to do with it
+ // don't override a parseError though, as that is probably more
relevant
+ parseError = [NSError
mutableLocalErrorWithCode:kBDSKHadMissingCiteKeys
localizedDescription:NSLocalizedString(@"Temporary Cite Keys", @"Error
description")];
+ [parseError setValue:@"FixMe" forKey:@"temporaryCiteKey"];
+ }
+
+ if(outError) *outError = parseError;
+ return newPubs;
+}
+
@end
Modified: trunk/bibdesk/BDSKTextImportController.m
===================================================================
--- trunk/bibdesk/BDSKTextImportController.m 2012-02-02 12:01:42 UTC (rev
18463)
+++ trunk/bibdesk/BDSKTextImportController.m 2012-02-02 12:59:05 UTC (rev
18464)
@@ -46,8 +46,6 @@
#import "BDSKCiteKeyFormatter.h"
#import "BDSKFieldNameFormatter.h"
#import "BDSKEdgeView.h"
-#import "BibDocument.h"
-#import "BibDocument_Groups.h"
#import "NSFileManager_BDSKExtensions.h"
#import "BDSKAppController.h"
#import "BDSKFieldEditor.h"
@@ -1503,7 +1501,7 @@
return;
NSError *error = nil;
- NSArray *pubs = [(BibDocument *)owner publicationsForString:string
type:type verbose:NO error:&error];
+ NSArray *pubs = [BDSKStringParser itemsFromString:string ofType:type
owner:owner error:&error];
// ignore warnings for parsing with temporary citekeys, as we're not
interested in the cite key
if ([error isLocalError] && [error code] == kBDSKHadMissingCiteKeys)
Modified: trunk/bibdesk/BibDocument.m
===================================================================
--- trunk/bibdesk/BibDocument.m 2012-02-02 12:01:42 UTC (rev 18463)
+++ trunk/bibdesk/BibDocument.m 2012-02-02 12:59:05 UTC (rev 18464)
@@ -2214,9 +2214,11 @@
newPubs = newPubs ? [newPubs
arrayByAddingObjectsFromArray:newFilePubs]: newFilePubs;
}
- if([error isLocalError] && [error code] == kBDSKHadMissingCiteKeys) {
- temporaryCiteKey = [[error userInfo] objectForKey:@"temporaryCiteKey"];
- error = nil; // accept temporary cite keys, but show a warning later
+ if([error isLocalError]){
+ if([error code] == kBDSKHadMissingCiteKeys) {
+ temporaryCiteKey = [[error userInfo]
objectForKey:@"temporaryCiteKey"];
+ error = nil; // accept temporary cite keys, but show a warning
later
+ }
}
if ([newPubs count] > 0)
@@ -2247,67 +2249,47 @@
// pass BDSKUnkownStringType to allow BDSKStringParser to sniff the text and
determine the format
- (NSArray *)publicationsForString:(NSString *)string
type:(BDSKStringType)type verbose:(BOOL)verbose error:(NSError **)outError {
NSArray *newPubs = nil;
- NSError *parseError = nil;
- BOOL isPartialData = NO;
+ NSError *error = nil;
- // @@ BDSKStringParser doesn't handle any BibTeX types, so it's not really
useful as a funnel point for any string type, since each usage requires special
casing for BibTeX.
- if(BDSKUnknownStringType == type)
- type = [string contentStringType];
+ newPubs = [BDSKStringParser itemsFromString:string ofType:type owner:self
error:&error];
- if(type == BDSKBibTeXStringType)
- newPubs = [BDSKBibTeXParser itemsFromString:string owner:self
isPartialData:&isPartialData error:&parseError];
- else if(type == BDSKNoKeyBibTeXStringType)
- newPubs = [BDSKBibTeXParser itemsFromString:[string
stringWithPhoneyCiteKeys:@"FixMe"] owner:self isPartialData:&isPartialData
error:&parseError];
- else
- // this will create the NSError if the type is unrecognized
- newPubs = [BDSKStringParser itemsFromString:string ofType:type
error:&parseError];
-
- if(nil == newPubs || isPartialData) {
+ if([error isLocalError]) {
- if(verbose == NO){
+ if (verbose == NO) {
// if not BibTeX, it's an unknown type or failed due to parser
error; in either case, we must have a valid NSError since the parser returned
nil
// no partial data here since that only applies to BibTeX parsing;
all we can do is just return nil and propagate the error back up, although I
suppose we could display the error editor...
- newPubs = nil;
- } else if(type == BDSKBibTeXStringType || type ==
BDSKNoKeyBibTeXStringType){
-
- // run a modal dialog asking if we want to use partial data or
give up
- if ([parseError code] == kBDSKParserIgnoredFrontMatter) {
+ if ([error code] == kBDSKBibTeXParserFailed)
+ newPubs = nil;
+ else if ([error code] == kBDSKParserIgnoredFrontMatter)
+ error = nil;
+ } else if ([error code] == kBDSKParserIgnoredFrontMatter) {
// the partial data alert only applies to BibTeX; we could
show the editor window for non-BibTeX data (I think...), but we also have to
deal with alerts being shown twice if NSError is involved
// here we want to display an alert, but don't propagate a
nil/error back up, since it's not a failure
- [NSApp presentError:parseError];
+ [self presentError:error];
// @@ fixme: NSError
- parseError = nil;
- } else {
- // this was BibTeX, but the user may want to try going with
partial data
- NSAlert *alert = [NSAlert
alertWithMessageText:NSLocalizedString(@"Error Reading String", @"Message in
alert dialog when failing to parse dropped or copied string")
-
defaultButton:NSLocalizedString(@"Cancel", @"Button title")
-
alternateButton:NSLocalizedString(@"Edit data", @"Button title")
-
otherButton:NSLocalizedString(@"Keep going", @"Button title")
-
informativeTextWithFormat:NSLocalizedString(@"There was a problem inserting the
data. Do you want to ignore this data, open a window containing the data to
edit it and remove the errors, or keep going and use everything that BibDesk
could parse?\n(It's likely that choosing \"Keep Going\" will lose some data.)",
@"Informative text in alert dialog")];
- NSInteger rv = [alert runModal];
-
- if(rv == NSAlertDefaultReturn){
- // the user said to give up
- newPubs = nil;
- }else if(rv == NSAlertAlternateReturn){
- // they said to edit the file.
- [[BDSKErrorObjectController sharedErrorObjectController]
showEditorForLastPasteDragError];
- newPubs = nil;
- }
+ error = nil;
+ } else if ([error code] == kBDSKBibTeXParserFailed) {
+ // this was BibTeX, but the user may want to try going with
partial data
+ // run a modal dialog asking if we want to use partial data or
give up
+ NSAlert *alert = [NSAlert
alertWithMessageText:NSLocalizedString(@"Error Reading String", @"Message in
alert dialog when failing to parse dropped or copied string")
+
defaultButton:NSLocalizedString(@"Cancel", @"Button title")
+
alternateButton:NSLocalizedString(@"Edit data", @"Button title")
+
otherButton:NSLocalizedString(@"Keep going", @"Button title")
+
informativeTextWithFormat:NSLocalizedString(@"There was a problem inserting the
data. Do you want to ignore this data, open a window containing the data to
edit it and remove the errors, or keep going and use everything that BibDesk
could parse?\n(It's likely that choosing \"Keep Going\" will lose some data.)",
@"Informative text in alert dialog")];
+ NSInteger rv = [alert runModal];
+
+ if(rv == NSAlertDefaultReturn){
+ // the user said to give up
+ newPubs = nil;
+ }else if(rv == NSAlertAlternateReturn){
+ // they said to edit the file.
+ [[BDSKErrorObjectController sharedErrorObjectController]
showEditorForLastPasteDragError];
+ newPubs = nil;
}
}
-
- }else if(type == BDSKNoKeyBibTeXStringType){
-
- BDSKASSERT(parseError == nil);
-
- // return an error when we inserted temporary keys, let the caller
decide what to do with it
- // don't override a parseError though, as that is probably more
relevant
- parseError = [NSError
mutableLocalErrorWithCode:kBDSKHadMissingCiteKeys
localizedDescription:NSLocalizedString(@"Temporary Cite Keys", @"Error
description")];
- [parseError setValue:@"FixMe" forKey:@"temporaryCiteKey"];
}
- if(outError) *outError = parseError;
+ if(outError) *outError = error;
return newPubs;
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit