1/*
2  Copyright (C) 2000-2005 SKYRIX Software AG
3
4  This file is part of SOPE.
5
6  SOPE is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License as published by the
8  Free Software Foundation; either version 2, or (at your option) any
9  later version.
10
11  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12  WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  License for more details.
15
16  You should have received a copy of the GNU Lesser General Public
17  License along with SOPE; see the file COPYING.  If not, write to the
18  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19  02111-1307, USA.
20*/
21
22#include "DOMAttribute.h"
23#include "DOMDocument.h"
24#include "common.h"
25
26@implementation NGDOMAttribute
27
28- (id)initWithName:(NSString *)_name namespaceURI:(NSString *)_uri {
29  if ((self = [super init])) {
30    self->name         = [_name copy];
31    self->namespaceURI = [_uri  copy];
32  }
33  return self;
34}
35
36- (id)initWithName:(NSString *)_name {
37  return [self initWithName:_name namespaceURI:nil];
38}
39
40- (void)dealloc {
41  [self->prefix release];
42  [self->name   release];
43  [self->namespaceURI release];
44  [super dealloc];
45}
46
47/* element tracking */
48
49- (void)_domNodeRegisterParentNode:(id)_element {
50  self->element = _element;
51}
52- (void)_domNodeForgetParentNode:(id)_element {
53  if (_element == self->element)
54    self->element = nil;
55}
56
57/* attributes */
58
59- (IDOMElement)ownerElement {
60  return self->element;
61}
62- (IDOMDocument)ownerDocument {
63  return [[self ownerElement] ownerDocument];
64}
65
66- (BOOL)specified {
67  return self->isSpecified;
68}
69
70- (NSString *)name {
71  return self->name;
72}
73
74- (NSString *)namespaceURI {
75  return self->namespaceURI;
76}
77
78- (void)setPrefix:(NSString *)_prefix {
79  id old = self->prefix;
80  self->prefix = [_prefix copy];
81  [old release];
82}
83- (NSString *)prefix {
84  return self->prefix;
85}
86
87- (void)setValue:(NSString *)_value {
88  id child;
89
90  self->isSpecified = YES;
91
92  /* remove all existing children */
93  while ((child = [self lastChild]))
94    [self removeChild:child];
95
96  child = [[self ownerDocument] createTextNode:_value];
97  NSAssert1(child, @"couldn't create text-node child for value '%@' !", _value);
98
99  [self appendChild:child];
100}
101
102- (NSString *)_stringValueOfChildNode:(id)_node {
103  return [_node nodeValue];
104}
105- (NSString *)value {
106  id       children;
107  unsigned count;
108
109  if (![self hasChildNodes])
110    return nil;
111
112  children = [self childNodes];
113  if ((count = [children count]) == 0)
114    return nil;
115
116  if (count == 1) {
117    return [self _stringValueOfChildNode:[children objectAtIndex:0]];
118  }
119  else {
120    unsigned i;
121    NSMutableString *s;
122
123    s = [NSMutableString stringWithCapacity:256];
124    for (i = 0; i < count; i++) {
125      [s appendString:
126	   [self _stringValueOfChildNode:[children objectAtIndex:i]]];
127    }
128    return [[s copy] autorelease];
129  }
130}
131
132/* node */
133
134- (BOOL)_isValidChildNode:(id)_node {
135  switch ([_node nodeType]) {
136    case DOM_TEXT_NODE:
137    case DOM_ENTITY_REFERENCE_NODE:
138      return YES;
139
140    default:
141      return NO;
142  }
143}
144
145- (DOMNodeType)nodeType {
146  return DOM_ATTRIBUTE_NODE;
147}
148
149- (id<NSObject,DOMNamedNodeMap>)attributes {
150  return nil;
151}
152
153- (id<NSObject,DOMNode>)parentNode {
154  return nil;
155}
156- (id<NSObject,DOMNode>)nextSibling {
157  return nil;
158}
159- (id<NSObject,DOMNode>)previousSibling {
160  return nil;
161}
162
163/* description */
164
165- (NSString *)description {
166  return [NSString stringWithFormat:@"<0x%p[%@]: {%@}%@%s '%@'>",
167                     self, NSStringFromClass([self class]),
168                     self->namespaceURI,
169                     [self name],
170                     [self specified] ? " specified" : "",
171                     [self stringValue]];
172}
173
174/* ObjCValues */
175
176- (NSString *)stringValue {
177  return [self value];
178}
179- (int)intValue {
180  return [[self stringValue] intValue];
181}
182- (double)doubleValue {
183  return [[self stringValue] doubleValue];
184}
185
186/* QPValues */
187
188- (NSException *)setQueryPathValue:(id)_value {
189  [self setValue:[_value stringValue]];
190  return nil;
191}
192- (id)queryPathValue {
193  return [self value];
194}
195
196@end /* NGDOMAttribute */
197