2 using System
.Collections
;
6 namespace SemWeb
.Query
{
7 public abstract class ValueFilterFactory
{
8 public abstract ValueFilter
GetValueFilter(string predicate
, Resource obj
);
11 public abstract class ValueFilter
{
12 public static Entity qFilterStringContains
= "http://purl.oclc.org/NET/rsquary/string-contains";
14 public static Entity qFilterLT
= "http://purl.oclc.org/NET/rsquary/lt";
15 public static Entity qFilterLE
= "http://purl.oclc.org/NET/rsquary/le";
16 public static Entity qFilterNE
= "http://purl.oclc.org/NET/rsquary/ne";
17 public static Entity qFilterEQ
= "http://purl.oclc.org/NET/rsquary/eq";
18 public static Entity qFilterGT
= "http://purl.oclc.org/NET/rsquary/gt";
19 public static Entity qFilterGE
= "http://purl.oclc.org/NET/rsquary/ge";
21 public abstract bool Filter(Resource resource
, Store targetModel
);
23 public static ValueFilter
GetValueFilter(Entity predicate
, Resource obj
) {
24 if (predicate
== qFilterStringContains
&& obj
is Literal
)
25 return new StringContainsFilter((Literal
)obj
);
26 if (obj
is Literal
&& (predicate
== qFilterLT
|| predicate
== qFilterLE
|| predicate
== qFilterNE
|| predicate
== qFilterEQ
|| predicate
== qFilterGT
|| predicate
== qFilterGE
)) {
27 Literal lit
= (Literal
)obj
;
28 int c
= 0; bool e
= false;
29 if (predicate
== qFilterLT
|| predicate
== qFilterLE
) c
= -1;
30 if (predicate
== qFilterGT
|| predicate
== qFilterGE
) c
= 1;
31 if (predicate
== qFilterLE
|| predicate
== qFilterGE
) e
= true;
32 if (predicate
== qFilterEQ
) e
= true;
34 if (lit
.DataType
== null || lit
.DataType
== "" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#string" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#normalizedString")
35 return new StringCompareFilter(lit
, c
, e
);
37 if (lit
.DataType
== "http://www.w3.org/2001/XMLSchema#float" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#double" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#decimal" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#integer" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#nonPositiveInteger" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#negativeInteger" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#long" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#int" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#short" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#byte" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#nonNegativeInteger" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#unsignedLong" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#unsignedInt" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#unsignedShort" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#unsignedByte" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#positiveInteger")
38 return new NumericCompareFilter(lit
, c
, e
);
40 if (lit
.DataType
== "http://www.w3.org/2001/XMLSchema#dateTime" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#date" || lit
.DataType
== "http://www.w3.org/2001/XMLSchema#time")
41 return new DateTimeCompareFilter(lit
, c
, e
);
43 if (lit
.DataType
== "http://www.w3.org/2001/XMLSchema#duration")
44 return new TimeSpanCompareFilter(lit
, c
, e
);
51 public abstract class LiteralValueFilter
: ValueFilter
{
54 internal abstract class StringFilter
: LiteralValueFilter
{
55 protected readonly string pattern
;
56 public StringFilter(Literal res
) : this(res
.Value
) {
58 public StringFilter(string pattern
) {
59 this.pattern
= pattern
;
63 internal class StringCompareFilter
: StringFilter
{
68 // compareResult orEqual Meaning
70 // -1 true Less Than Or Equal
73 // 1 false Greater Than
74 // 1 true Greater Than Or Equal
76 public StringCompareFilter(Literal res
, int compareResult
, bool orEqual
) : base(res
) { compare = compareResult; eq = orEqual; }
77 public StringCompareFilter(string pattern
, int compareResult
, bool orEqual
) : base(pattern
) { compare = compareResult; eq = orEqual; }
79 public override bool Filter(Resource resource
, Store targetModel
) {
80 string v
= ((Literal
)resource
).Value
;
81 int c
= v
.CompareTo(pattern
);
82 if (compare
== 0) return (c
== 0) ^
!eq
;
83 return c
== compare
|| (c
== 0 && eq
);
87 internal class StringContainsFilter
: StringFilter
{
88 public StringContainsFilter(Literal res
) : base(res
) { }
89 public StringContainsFilter(string pattern
) : base(pattern
) { }
91 public override bool Filter(Resource resource
, Store targetModel
) {
92 string v
= ((Literal
)resource
).Value
;
93 return v
.IndexOf(pattern
) != -1;
97 internal abstract class NumericFilter
: LiteralValueFilter
{
98 protected readonly Decimal number
;
99 public NumericFilter(Literal res
) : this(int.Parse(res
.Value
)) { }
100 public NumericFilter(Decimal number
) { this.number = number; }
104 internal class NumericCompareFilter
: NumericFilter
{
108 public NumericCompareFilter(Literal res
, int compareResult
, bool orEqual
) : base(res
) { compare = compareResult; eq = orEqual; }
109 public NumericCompareFilter(Decimal number
, int compareResult
, bool orEqual
) : base(number
) { compare = compareResult; eq = orEqual; }
111 public override bool Filter(Resource resource
, Store targetModel
) {
112 string v
= ((Literal
)resource
).Value
;
114 Decimal i
= Decimal
.Parse(v
);
115 int c
= i
.CompareTo(number
);
116 if (compare
== 0) return (c
== 0) ^
!eq
;
117 return c
== compare
|| (c
== 0 && eq
);
118 } catch (Exception e
) {
124 internal abstract class DateTimeFilter
: LiteralValueFilter
{
125 protected readonly DateTime datetime
;
126 public DateTimeFilter(Literal res
) : this(DateTime
.Parse(res
.Value
)) { }
127 public DateTimeFilter(DateTime datetime
) { this.datetime = datetime; }
131 internal class DateTimeCompareFilter
: DateTimeFilter
{
135 public DateTimeCompareFilter(Literal res
, int compareResult
, bool orEqual
) : base(res
) { compare = compareResult; eq = orEqual; }
136 public DateTimeCompareFilter(DateTime datetime
, int compareResult
, bool orEqual
) : base(datetime
) { compare = compareResult; eq = orEqual; }
138 public override bool Filter(Resource resource
, Store targetModel
) {
139 string v
= ((Literal
)resource
).Value
;
141 DateTime i
= DateTime
.Parse(v
);
142 int c
= i
.CompareTo(datetime
);
143 if (compare
== 0) return (c
== 0) ^
!eq
;
144 return c
== compare
|| (c
== 0 && eq
);
145 } catch (Exception e
) {
151 internal abstract class TimeSpanFilter
: LiteralValueFilter
{
152 protected readonly TimeSpan timespan
;
153 public TimeSpanFilter(Literal res
) : this(TimeSpan
.Parse(res
.Value
)) { }
154 public TimeSpanFilter(TimeSpan timespan
) { this.timespan = timespan; }
158 internal class TimeSpanCompareFilter
: TimeSpanFilter
{
162 public TimeSpanCompareFilter(Literal res
, int compareResult
, bool orEqual
) : base(res
) { compare = compareResult; eq = orEqual; }
163 public TimeSpanCompareFilter(TimeSpan timespan
, int compareResult
, bool orEqual
) : base(timespan
) { compare = compareResult; eq = orEqual; }
165 public override bool Filter(Resource resource
, Store targetModel
) {
166 string v
= ((Literal
)resource
).Value
;
168 TimeSpan i
= TimeSpan
.Parse(v
);
169 int c
= i
.CompareTo(timespan
);
170 if (compare
== 0) return (c
== 0) ^
!eq
;
171 return c
== compare
|| (c
== 0 && eq
);
172 } catch (Exception e
) {