2009-02-09 16:16:11 +00:00
|
|
|
/* Inmplementation for NSXMLDTD for GNUStep
|
|
|
|
Copyright (C) 2008 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
Created: September 2008
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 3 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
2009-02-09 16:16:11 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
2024-11-07 13:37:59 +00:00
|
|
|
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
|
2009-02-09 16:16:11 +00:00
|
|
|
*/
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
|
|
|
|
2012-03-21 08:00:33 +00:00
|
|
|
#if defined(HAVE_LIBXML)
|
|
|
|
|
2012-03-12 13:27:32 +00:00
|
|
|
#define GSInternal NSXMLDTDInternal
|
|
|
|
|
2012-01-05 20:40:12 +00:00
|
|
|
#import "NSXMLPrivate.h"
|
|
|
|
#import "GSInternal.h"
|
2012-01-04 12:41:45 +00:00
|
|
|
GS_PRIVATE_INTERNAL(NSXMLDTD)
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
@implementation NSXMLDTD
|
|
|
|
|
|
|
|
+ (NSXMLDTDNode*) predefinedEntityDeclarationForName: (NSString*)name
|
|
|
|
{
|
2012-03-18 12:20:29 +00:00
|
|
|
xmlEntityPtr node = xmlGetPredefinedEntity(XMLSTRING(name));
|
|
|
|
return (NSXMLDTDNode*)[self _objectForNode: (xmlNodePtr)node];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
if (GS_EXISTS_INTERNAL)
|
|
|
|
{
|
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addChild: (NSXMLNode*)child
|
|
|
|
{
|
2012-03-17 13:57:58 +00:00
|
|
|
[self insertChild: child atIndex: [self childCount]];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLDTDNode*) attributeDeclarationForName: (NSString*)name
|
2012-03-17 13:57:58 +00:00
|
|
|
elementName: (NSString*)elementName
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDtdPtr theNode = internal->node.dtd;
|
2012-03-17 13:57:58 +00:00
|
|
|
xmlNodePtr children = NULL;
|
|
|
|
const xmlChar *xmlName = XMLSTRING(name);
|
|
|
|
const xmlChar *xmlElementName = XMLSTRING(elementName);
|
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
if ((theNode == NULL) ||
|
|
|
|
(theNode->children == NULL))
|
2012-03-17 13:57:58 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
for (children = theNode->children; children; children = children->next)
|
2012-03-17 13:57:58 +00:00
|
|
|
{
|
|
|
|
if (children->type == XML_ATTRIBUTE_DECL)
|
|
|
|
{
|
|
|
|
xmlAttributePtr attr = (xmlAttributePtr)children;
|
|
|
|
|
|
|
|
if ((xmlStrcmp(attr->name, xmlName) == 0) &&
|
|
|
|
(xmlStrcmp(attr->elem, xmlElementName) == 0))
|
|
|
|
{
|
|
|
|
return (NSXMLDTDNode*)[NSXMLNode _objectForNode: children];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLDTDNode*) elementDeclarationForName: (NSString*)name
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDtdPtr theNode = internal->node.dtd;
|
2012-03-17 13:57:58 +00:00
|
|
|
xmlNodePtr children = NULL;
|
|
|
|
const xmlChar *xmlName = XMLSTRING(name);
|
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
if ((theNode == NULL) ||
|
|
|
|
(theNode->children == NULL))
|
2012-03-17 13:57:58 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
for (children = theNode->children; children; children = children->next)
|
2012-03-17 13:57:58 +00:00
|
|
|
{
|
|
|
|
if (children->type == XML_ELEMENT_DECL)
|
|
|
|
{
|
|
|
|
xmlElementPtr elem = (xmlElementPtr)children;
|
|
|
|
|
|
|
|
if (xmlStrcmp(elem->name, xmlName) == 0)
|
|
|
|
{
|
|
|
|
return (NSXMLDTDNode*)[NSXMLNode _objectForNode: children];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLDTDNode*) entityDeclarationForName: (NSString*)name
|
|
|
|
{
|
2012-03-18 12:20:29 +00:00
|
|
|
//xmlGetEntityFromDtd
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDtdPtr theNode = internal->node.dtd;
|
2012-03-17 13:57:58 +00:00
|
|
|
xmlNodePtr children = NULL;
|
|
|
|
const xmlChar *xmlName = XMLSTRING(name);
|
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
if ((theNode == NULL) ||
|
|
|
|
(theNode->children == NULL))
|
2012-03-17 13:57:58 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
for (children = theNode->children; children; children = children->next)
|
2012-03-17 13:57:58 +00:00
|
|
|
{
|
|
|
|
if (children->type == XML_ENTITY_DECL)
|
|
|
|
{
|
|
|
|
xmlEntityPtr entity = (xmlEntityPtr)children;
|
|
|
|
|
|
|
|
if (xmlStrcmp(entity->name, xmlName) == 0)
|
|
|
|
{
|
|
|
|
return (NSXMLDTDNode*)[NSXMLNode _objectForNode: children];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
- (void) _createInternal
|
|
|
|
{
|
|
|
|
GS_CREATE_INTERNAL(NSXMLDTD);
|
|
|
|
}
|
|
|
|
|
2012-01-05 18:55:17 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
return [self initWithKind: NSXMLDTDKind options: 0];
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (id) initWithContentsOfURL: (NSURL*)url
|
|
|
|
options: (NSUInteger)mask
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
NSData *data;
|
|
|
|
NSXMLDTD *doc;
|
|
|
|
|
|
|
|
data = [NSData dataWithContentsOfURL: url];
|
2012-03-08 19:59:12 +00:00
|
|
|
doc = [self initWithData: data options: mask error: error];
|
2012-03-04 21:40:39 +00:00
|
|
|
[doc setURI: [url absoluteString]];
|
2009-02-09 16:16:11 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithData: (NSData*)data
|
|
|
|
options: (NSUInteger)mask
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
2012-03-17 13:57:58 +00:00
|
|
|
NSXMLDocument *tempDoc =
|
|
|
|
[[NSXMLDocument alloc] initWithData: data
|
|
|
|
options: mask
|
|
|
|
error: error];
|
|
|
|
if (tempDoc != nil)
|
|
|
|
{
|
|
|
|
NSArray *children = [tempDoc children];
|
|
|
|
NSEnumerator *enumerator = [children objectEnumerator];
|
|
|
|
NSXMLNode *child;
|
|
|
|
|
|
|
|
self = [self initWithKind: NSXMLDTDKind options: mask];
|
|
|
|
|
|
|
|
while ((child = [enumerator nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[child detach]; // detach from document.
|
|
|
|
[self addChild: child];
|
|
|
|
}
|
|
|
|
[tempDoc release];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
- (id) initWithKind: (NSXMLNodeKind)theKind options: (NSUInteger)theOptions
|
2012-01-04 12:41:45 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
if (NSXMLDTDKind == theKind)
|
2012-01-04 12:41:45 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
return [super initWithKind: theKind options: theOptions];
|
2012-03-04 21:40:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self release];
|
2012-04-06 22:27:55 +00:00
|
|
|
// This cast is here to keep clang quite that expects an init* method to
|
|
|
|
// return an object of the same class, which is not true here.
|
|
|
|
return (NSXMLDTD*)[[NSXMLNode alloc] initWithKind: theKind
|
|
|
|
options: theOptions];
|
2012-01-04 12:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index
|
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
NSXMLNodeKind theKind = [child kind];
|
2012-03-17 13:57:58 +00:00
|
|
|
NSUInteger childCount = [self childCount];
|
|
|
|
|
|
|
|
// Check to make sure this is a valid addition...
|
|
|
|
NSAssert(nil != child, NSInvalidArgumentException);
|
|
|
|
NSAssert(index <= childCount, NSInvalidArgumentException);
|
|
|
|
NSAssert(nil == [child parent], NSInvalidArgumentException);
|
2012-04-06 22:27:55 +00:00
|
|
|
NSAssert(NSXMLAttributeKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLDTDKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLDocumentKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLElementKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLInvalidKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLNamespaceKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLTextKind != theKind, NSInvalidArgumentException);
|
2012-03-17 13:57:58 +00:00
|
|
|
|
|
|
|
[self _insertChild: child atIndex: index];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertChildren: (NSArray*)children atIndex: (NSUInteger)index
|
|
|
|
{
|
|
|
|
NSEnumerator *enumerator = [children objectEnumerator];
|
|
|
|
NSXMLNode *child;
|
|
|
|
|
|
|
|
while ((child = [enumerator nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[self insertChild: child atIndex: index++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLDTDNode*) notationDeclarationForName: (NSString*)name
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDtdPtr theNode = internal->node.dtd;
|
2012-03-17 13:57:58 +00:00
|
|
|
xmlNodePtr children = NULL;
|
|
|
|
const xmlChar *xmlName = XMLSTRING(name);
|
2009-02-09 16:16:11 +00:00
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
if ((theNode == NULL) ||
|
|
|
|
(theNode->children == NULL))
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-03-17 13:57:58 +00:00
|
|
|
return nil;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
2012-03-17 13:57:58 +00:00
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
for (children = theNode->children; children; children = children->next)
|
2012-03-17 13:57:58 +00:00
|
|
|
{
|
|
|
|
if (children->type == XML_NOTATION_NODE)
|
|
|
|
{
|
|
|
|
if (xmlStrcmp(children->name, xmlName) == 0)
|
|
|
|
{
|
|
|
|
return (NSXMLDTDNode*)[NSXMLNode _objectForNode: children];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) publicID
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDtd *theNode = internal->node.dtd;
|
2012-03-08 19:59:12 +00:00
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
return StringFromXMLStringPtr(theNode->ExternalID);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeChildAtIndex: (NSUInteger)index
|
|
|
|
{
|
2012-03-17 13:57:58 +00:00
|
|
|
NSXMLNode *child;
|
|
|
|
|
|
|
|
if (index >= [self childCount])
|
|
|
|
{
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
format: @"index too large"];
|
|
|
|
}
|
|
|
|
|
|
|
|
child = [self childAtIndex: index];
|
|
|
|
[child detach];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
- (void) replaceChildAtIndex: (NSUInteger)index withNode: (NSXMLNode*)theNode
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
[self insertChild: theNode atIndex: index];
|
2012-03-17 13:57:58 +00:00
|
|
|
[self removeChildAtIndex: index + 1];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setChildren: (NSArray*)children
|
|
|
|
{
|
2012-03-17 13:57:58 +00:00
|
|
|
NSUInteger count = [self childCount];
|
|
|
|
|
|
|
|
while (count-- > 0)
|
|
|
|
{
|
|
|
|
[self removeChildAtIndex: count];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self insertChildren: children atIndex: 0];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setPublicID: (NSString*)publicID
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDtd *theNode = internal->node.dtd;
|
2012-03-08 19:59:12 +00:00
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
theNode->ExternalID = XMLStringCopy(publicID);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setSystemID: (NSString*)systemID
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDtd *theNode = internal->node.dtd;
|
2012-03-08 19:59:12 +00:00
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
theNode->SystemID = XMLStringCopy(systemID);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) systemID
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDtd *theNode = internal->node.dtd;
|
2012-03-08 19:59:12 +00:00
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
return StringFromXMLStringPtr(theNode->SystemID);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2012-03-21 08:00:33 +00:00
|
|
|
#endif /* HAVE_LIBXML */
|