Move XIB type detection into GSXibLoader.m

This commit is contained in:
fredkiefer 2020-01-03 18:21:28 +01:00
parent 97891ed2bb
commit d45ce451f9
2 changed files with 52 additions and 63 deletions

View file

@ -764,67 +764,34 @@ static NSArray *XmlBoolDefaultYes = nil;
DESTROY(self);
return nil;
}
#if GNUSTEP_BASE_HAVE_LIBXML
else
{
// Ensure we have a XIB 5 version...first see if we can parse the XML...
NSXMLDocument *document = [[NSXMLDocument alloc] initWithData: data
options: 0
error: NULL];
if (document == nil)
NSXMLParser *theParser = nil;
// Initialize...
[self _initCommon];
// Create the parser and parse the data...
theParser = [[NSXMLParser alloc] initWithData: data];
[theParser setDelegate: self];
NS_DURING
{
NSLog(@"%s:DOCUMENT IS NIL: %@\n", __PRETTY_FUNCTION__, document);
// Parse the XML data
[theParser parse];
// Decode optional resources
_resources = RETAIN([self decodeObjectForKey: @"resources"]);
}
NS_HANDLER
{
NSLog(@"Exception occurred while parsing Xib: %@", [localException reason]);
DESTROY(self);
return nil;
}
else
{
// Test to see if this is an Xcode 5 XIB...
NSArray *documentNodes = [document nodesForXPath: @"/document" error: NULL];
NS_ENDHANDLER
// Need at LEAST ONE document node...we should find something a bit more
// specific to check here...
if ([documentNodes count] == 0)
{
DESTROY(self);
return nil;
}
else
{
NSXMLParser *theParser = nil;
// Initialize...
[self _initCommon];
// Create the parser and parse the data...
theParser = [[NSXMLParser alloc] initWithData: data];
[theParser setDelegate: self];
NS_DURING
{
// Parse the XML data
[theParser parse];
}
NS_HANDLER
{
NSLog(@"Exception occurred while parsing Xib: %@", [localException reason]);
DESTROY(self);
}
NS_ENDHANDLER
DESTROY(theParser);
// Decode optional resources
_resources = RETAIN([self decodeObjectForKey: @"resources"]);
}
}
DESTROY(theParser);
}
#else
else
{
DESTROY(self);
}
#endif
return self;
}

View file

@ -35,9 +35,7 @@
#import <Foundation/NSKeyValueCoding.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSXMLParser.h>
#import <Foundation/NSXMLDocument.h>
#import <Foundation/NSXMLElement.h>
#import "AppKit/NSApplication.h"
#import "AppKit/NSNib.h"
@ -45,8 +43,6 @@
#import "GNUstepGUI/GSModelLoaderFactory.h"
#import "GNUstepGUI/GSNibLoading.h"
#import "GNUstepGUI/GSXibLoading.h"
#import "GNUstepGUI/GSXibObjectContainer.h"
#import "GNUstepGUI/GSXibElement.h"
#import "GNUstepGUI/GSXibKeyedUnarchiver.h"
#import "GSXib5KeyedUnarchiver.h"
@ -1015,6 +1011,32 @@
}
}
- (BOOL) checkXib5: (NSData *)data
{
#if GNUSTEP_BASE_HAVE_LIBXML
// Ensure we have a XIB 5 version...first see if we can parse the XML...
NSXMLDocument *document = [[NSXMLDocument alloc] initWithData: data
options: 0
error: NULL];
if (document == nil)
{
return NO;
}
else
{
// Test to see if this is an Xcode 5 XIB...
NSArray *documentNodes = [document nodesForXPath: @"/document" error: NULL];
// Need at LEAST ONE document node...we should find something a bit more
// specific to check here...
return [documentNodes count] != 0;
}
#else
// We now default to checking XIB 5 versions
return YES;
#endif
}
- (BOOL) loadModelData: (NSData *)data
externalNameTable: (NSDictionary *)context
withZone: (NSZone *)zone;
@ -1026,11 +1048,11 @@
{
if (data != nil)
{
// We now default to checking XIB 5 versions first...
unarchiver = [[GSXib5KeyedUnarchiver alloc] initForReadingWithData: data];
// If that doesn't work try the XIB 5 loader...
if (unarchiver == nil)
if ([self checkXib5: data])
{
unarchiver = [[GSXib5KeyedUnarchiver alloc] initForReadingWithData: data];
}
else
{
unarchiver = [[GSXibKeyedUnarchiver alloc] initForReadingWithData: data];
}