~ubuntu-branches/ubuntu/gutsy/soprano/gutsy

« back to all changes in this revision

Viewing changes to soprano/node.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-12 14:43:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071012144348-yajzi51v4k23ahxf
Tags: 1.95.0~beta2-1ubuntu1
* Sync with Debian
* Add versioned build-dep on raptor

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
 
30
30
 
31
 
class Soprano::Node::Private : public QSharedData
 
31
class Soprano::Node::NodeData : public QSharedData
32
32
{
33
33
public:
34
 
    Private()
35
 
        : type(EmptyNode) {}
 
34
    NodeData( Type type_ = EmptyNode )
 
35
        : type( type_ ) {}
 
36
 
 
37
    virtual ~NodeData() {}
36
38
 
37
39
    Type type;
 
40
 
 
41
    virtual QString toString() const {
 
42
        return QString();
 
43
    }
 
44
};
 
45
 
 
46
class Soprano::Node::ResourceNodeData : public NodeData
 
47
{
 
48
public:
 
49
    ResourceNodeData( const QUrl& uri_ = QUrl() )
 
50
        : NodeData( ResourceNode ),
 
51
          uri( uri_ ){
 
52
    }
 
53
 
38
54
    QUrl uri;
 
55
 
 
56
    QString toString() const {
 
57
        return uri.toString();
 
58
    }
 
59
};
 
60
 
 
61
class Soprano::Node::BNodeData : public NodeData
 
62
{
 
63
public:
 
64
    BNodeData( const QString& id = QString() )
 
65
        : NodeData( BlankNode ),
 
66
          identifier( id ) {
 
67
    }
 
68
 
 
69
    QString identifier;
 
70
 
 
71
    QString toString() const {
 
72
        return identifier;
 
73
    }
 
74
};
 
75
 
 
76
class Soprano::Node::LiteralNodeData : public NodeData
 
77
{
 
78
public:
 
79
    LiteralNodeData( const LiteralValue& val = LiteralValue(), const QString& lang = QString() )
 
80
        : NodeData( LiteralNode ),
 
81
          value( val ),
 
82
          language( lang ) {
 
83
    }
 
84
 
39
85
    LiteralValue value;
40
86
    QString language;
 
87
 
 
88
    QString toString() const {
 
89
        return value.toString();
 
90
    }
41
91
};
42
92
 
43
93
 
44
94
Soprano::Node::Node()
45
95
{
46
 
    d = new Private;
 
96
    d = new NodeData();
47
97
}
48
98
 
49
99
Soprano::Node::Node( const Node &other )
51
101
    d = other.d;
52
102
}
53
103
 
54
 
Soprano::Node::Node( const QUrl &uri, Type type )
55
 
{
56
 
    d = new Private;
57
 
    if( !uri.isEmpty() &&
58
 
        type != LiteralNode &&
59
 
        type != EmptyNode ) {
60
 
        d->uri = uri;
61
 
        d->type = type;
 
104
Soprano::Node::Node( const QUrl &uri )
 
105
{
 
106
    if( !uri.isEmpty() ) {
 
107
        d = new ResourceNodeData( uri );
 
108
    }
 
109
    else {
 
110
        d = new NodeData();
 
111
    }
 
112
}
 
113
 
 
114
Soprano::Node::Node( const QString &id )
 
115
{
 
116
    if( !id.isEmpty() ) {
 
117
        d = new BNodeData( id );
 
118
    }
 
119
    else {
 
120
        d = new NodeData();
62
121
    }
63
122
}
64
123
 
65
124
Soprano::Node::Node( const LiteralValue& value, const QString& lang )
66
125
{
67
 
    d = new Private;
68
126
    if ( value.isValid() ) {
69
 
        d->type = LiteralNode;
70
 
        d->value = value;
71
 
        d->language = lang;
 
127
        d = new LiteralNodeData( value, lang );
 
128
    }
 
129
    else {
 
130
        d = new NodeData();
72
131
    }
73
132
}
74
133
 
108
167
 
109
168
QUrl Soprano::Node::uri() const
110
169
{
111
 
    // d->uri is only defined for Resource and blank Nodes
112
 
    return d->uri;
 
170
    if ( const ResourceNodeData* rnd = dynamic_cast<const ResourceNodeData*>( d.constData() ) ) {
 
171
        return rnd->uri;
 
172
    }
 
173
    else {
 
174
        return QUrl();
 
175
    }
 
176
}
 
177
 
 
178
 
 
179
QString Soprano::Node::identifier() const
 
180
{
 
181
    if ( const BNodeData* bnd = dynamic_cast<const BNodeData*>( d.constData() ) ) {
 
182
        return bnd->identifier;
 
183
    }
 
184
    else {
 
185
        return QString();
 
186
    }
113
187
}
114
188
 
115
189
 
116
190
Soprano::LiteralValue Soprano::Node::literal() const
117
191
{
118
 
    if ( isLiteral() ) {
119
 
        return d->value;
 
192
    if ( const LiteralNodeData* lnd = dynamic_cast<const LiteralNodeData*>( d.constData() ) ) {
 
193
        return lnd->value;
120
194
    }
121
195
    else {
122
196
        return LiteralValue();
125
199
 
126
200
QUrl Soprano::Node::dataType() const
127
201
{
128
 
    if ( isLiteral() ) {
129
 
        return d->value.dataTypeUri();
 
202
    if ( const LiteralNodeData* lnd = dynamic_cast<const LiteralNodeData*>( d.constData() ) ) {
 
203
        return lnd->value.dataTypeUri();
130
204
    }
131
205
    else {
132
206
        return QUrl();
135
209
 
136
210
QString Soprano::Node::language() const
137
211
{
138
 
    return d->language;
 
212
    if ( const LiteralNodeData* lnd = dynamic_cast<const LiteralNodeData*>( d.constData() ) ) {
 
213
        return lnd->language;
 
214
    }
 
215
    else {
 
216
        return QString();
 
217
    }
139
218
}
140
219
 
141
220
QString Soprano::Node::toString() const
142
221
{
143
 
    if ( isLiteral() ) {
144
 
        return d->value.toString();
145
 
    }
146
 
    else if ( isResource() || isBlank() ) {
147
 
        return d->uri.toString();
148
 
    }
149
 
    else {
150
 
        return QString();
151
 
    }
 
222
    return d->toString();
152
223
}
153
224
 
154
225
Soprano::Node& Soprano::Node::operator=( const Node& other )
159
230
 
160
231
Soprano::Node& Soprano::Node::operator=( const QUrl& resource )
161
232
{
162
 
    d->uri = resource;
163
 
    d->type = resource.isEmpty() ? EmptyNode : ResourceNode;
164
 
    d->value = LiteralValue();
165
 
    d->language.truncate( 0 );
 
233
    if ( !resource.isEmpty() ) {
 
234
        d = new ResourceNodeData( resource );
 
235
    }
 
236
    else {
 
237
        d = new NodeData();
 
238
    }
166
239
    return *this;
167
240
}
168
241
 
169
242
Soprano::Node& Soprano::Node::operator=( const LiteralValue& literal )
170
243
{
171
 
    d->value = literal;
172
 
    d->uri = QUrl();
173
 
    d->type = literal.isValid() ? LiteralNode : EmptyNode;
 
244
    if ( literal.isValid() ) {
 
245
        d = new LiteralNodeData( literal );
 
246
    }
 
247
    else {
 
248
        d = new NodeData();
 
249
    }
174
250
    return *this;
175
251
}
176
252
 
177
253
bool Soprano::Node::operator==( const Node& other ) const
178
254
{
179
 
    if (d->type != other.d->type) {
 
255
    if ( d->type != other.d->type ) {
180
256
        return false;
181
257
    }
182
 
    else if (d->type == ResourceNode) {
183
 
        return d->uri == other.d->uri;
 
258
    else if ( d->type == ResourceNode ) {
 
259
        return(  dynamic_cast<const ResourceNodeData*>( d.constData() )->uri ==
 
260
                 dynamic_cast<const ResourceNodeData*>( other.d.constData() )->uri );
 
261
    }
 
262
    else if ( d->type == BlankNode ) {
 
263
        return( dynamic_cast<const BNodeData*>( d.constData() )->identifier ==
 
264
                dynamic_cast<const BNodeData*>( other.d.constData() )->identifier );
 
265
    }
 
266
    else if ( d->type == LiteralNode ) {
 
267
        return ( dynamic_cast<const LiteralNodeData*>( d.constData() )->value ==
 
268
                 dynamic_cast<const LiteralNodeData*>( other.d.constData() )->value &&
 
269
                 dynamic_cast<const LiteralNodeData*>( d.constData() )->language ==
 
270
                 dynamic_cast<const LiteralNodeData*>( other.d.constData() )->language );
184
271
    }
185
272
    else {
186
 
        return ( d->value == other.d->value &&
187
 
                 d->language == other.d->language );
 
273
        // emppty nodes are always equal
 
274
        return true;
188
275
    }
189
276
}
190
277
 
191
278
bool Soprano::Node::operator!=( const Node& other ) const
192
279
{
193
 
    if (d->type != other.d->type) {
 
280
    if ( d->type != other.d->type ) {
194
281
        return true;
195
282
    }
196
283
 
197
 
    else if (d->type == ResourceNode) {
198
 
        return d->uri != other.d->uri;
 
284
    else if ( d->type == ResourceNode ) {
 
285
        return(  dynamic_cast<const ResourceNodeData*>( d.constData() )->uri
 
286
                 != dynamic_cast<const ResourceNodeData*>( other.d.constData() )->uri );
 
287
    }
 
288
    else if ( d->type == BlankNode ) {
 
289
        return( dynamic_cast<const BNodeData*>( d.constData() )->identifier !=
 
290
                dynamic_cast<const BNodeData*>( other.d.constData() )->identifier );
 
291
    }
 
292
    else if ( d->type == LiteralNode ) {
 
293
        return ( dynamic_cast<const LiteralNodeData*>( d.constData() )->value !=
 
294
                 dynamic_cast<const LiteralNodeData*>( other.d.constData() )->value ||
 
295
                 dynamic_cast<const LiteralNodeData*>( d.constData() )->language !=
 
296
                 dynamic_cast<const LiteralNodeData*>( other.d.constData() )->language );
199
297
    }
200
298
    else {
201
 
        return ( d->value != other.d->value ||
202
 
                 d->language != other.d->language );
 
299
        // emppty nodes are always equal
 
300
        return false;
203
301
    }
204
302
}
205
303
 
214
312
}
215
313
 
216
314
 
 
315
Soprano::Node Soprano::Node::createEmptyNode()
 
316
{
 
317
    return Node();
 
318
}
 
319
 
 
320
 
 
321
Soprano::Node Soprano::Node::createResourceNode( const QUrl& uri )
 
322
{
 
323
    return Node( uri );
 
324
}
 
325
 
 
326
 
 
327
Soprano::Node Soprano::Node::createBlankNode( const QString& id )
 
328
{
 
329
    return Node( id );
 
330
}
 
331
 
 
332
 
 
333
Soprano::Node Soprano::Node::createLiteralNode( const LiteralValue& val, const QString& language )
 
334
{
 
335
    return Node( val, language );
 
336
}
 
337
 
 
338
 
217
339
QDebug operator<<( QDebug s, const Soprano::Node& n )
218
340
{
219
 
    switch(n.type()) {
 
341
    switch( n.type() ) {
220
342
    case Soprano::Node::EmptyNode:
221
343
        s.nospace() << "(empty)";
222
344
        break;
224
346
//         s.nospace() << "(blank)";
225
347
//         break;
226
348
    case Soprano::Node::LiteralNode:
227
 
        s.nospace() << n.literal().toString();
228
 
        if( !n.language().isEmpty() )
229
 
            s.nospace() << " (" << n.language() << ")";
 
349
        s.nospace() << '\"' << n.literal().toString() << "\"";
 
350
        if ( n.literal().isString() && !n.language().isEmpty() ) {
 
351
            s.nospace() << "@" << n.language();
 
352
        }
 
353
        else {
 
354
            s.nospace() << "^^<" << n.literal().dataTypeUri().toString() << '>';
 
355
        }
 
356
        break;
 
357
    case Soprano::Node::BlankNode:
 
358
        s.nospace() << "_:" << n.identifier();
230
359
        break;
231
360
    default:
232
 
        s.nospace() << n.uri();
 
361
        s.nospace() << n.uri().toString();
233
362
        break;
234
363
    }
235
 
    return s.space();
 
364
    return s;
236
365
}
237
366
 
238
367
 
246
375
//         s.nospace() << "(blank)";
247
376
//         break;
248
377
    case Soprano::Node::LiteralNode:
249
 
        s << '\"' << n.literal().toString() << "\"^^<" << n.literal().dataTypeUri().toString() << '>';
250
 
        if( !n.language().isEmpty() )
251
 
            s << " (" << n.language() << ")";
 
378
        s << '\"' << n.literal().toString() << "\"";
 
379
        if ( n.literal().isString() && !n.language().isEmpty() ) {
 
380
            s << "@" << n.language();
 
381
        }
 
382
        else {
 
383
            s << "^^<" << n.literal().dataTypeUri().toString() << '>';
 
384
        }
 
385
        break;
 
386
    case Soprano::Node::BlankNode:
 
387
        s << "_:" << n.identifier();
252
388
        break;
253
389
    default:
254
390
        s << '<' << n.uri().toString() << '>';