1005
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
Object subclass:#URI
|
|
4 |
instanceVariableNames:'scheme'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Resources'
|
|
8 |
!
|
|
9 |
|
|
10 |
|
|
11 |
!URI class methodsFor:'instance creation'!
|
|
12 |
|
|
13 |
fromString:aString
|
|
14 |
"create an URI from a given String"
|
|
15 |
|
|
16 |
|i scheme rest|
|
|
17 |
|
|
18 |
i := aString indexOf:$:.
|
|
19 |
i == 0 ifTrue:[
|
|
20 |
self error:'missing scheme in URI'
|
|
21 |
].
|
|
22 |
scheme := aString copyFrom:1 to:i-1.
|
|
23 |
rest := aString copyFrom:i+1.
|
|
24 |
^ (self classForScheme:scheme) scheme:scheme fromString:rest
|
|
25 |
! !
|
|
26 |
|
|
27 |
!URI class methodsFor:'accessing'!
|
|
28 |
|
|
29 |
classForScheme:aString
|
|
30 |
"find a class for a given scheme name aString"
|
|
31 |
|
|
32 |
|s|
|
|
33 |
|
|
34 |
s := aString asLowercase.
|
|
35 |
|
|
36 |
^ self allSubclasses detect:[:cls| |schemes|
|
|
37 |
schemes := cls schemes.
|
|
38 |
schemes size ~~ 0 and:[schemes includes:s]
|
|
39 |
] ifNone:[HierarchicalURI]
|
|
40 |
!
|
|
41 |
|
|
42 |
schemes
|
|
43 |
"answer the schemes supported by an URI-class.
|
|
44 |
Concrete subclasses redefine this to answer an array of scheme names"
|
|
45 |
|
|
46 |
^ nil
|
|
47 |
! !
|
|
48 |
|
|
49 |
!URI class methodsFor:'escape'!
|
|
50 |
|
|
51 |
escape:aString allow:additionalCharacters on:aStream
|
|
52 |
|
|
53 |
|val|
|
|
54 |
|
|
55 |
aString do:[:c|
|
|
56 |
((c isLetterOrDigit)
|
|
57 |
or:[('-_.!!~*''()' includes:c)
|
|
58 |
or:[additionalCharacters notNil
|
|
59 |
and:[additionalCharacters includes:c]]]
|
|
60 |
) ifTrue:[
|
|
61 |
aStream nextPut:c
|
|
62 |
] ifFalse:[
|
|
63 |
val := c asciiValue.
|
|
64 |
aStream nextPut:$%;
|
|
65 |
nextPut:(Character digitValue:val//16);
|
|
66 |
nextPut:(Character digitValue:val\\16).
|
|
67 |
].
|
|
68 |
].
|
|
69 |
|
|
70 |
|
|
71 |
"
|
|
72 |
self escape:'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
73 |
allow:nil on:TextCollector open
|
|
74 |
|
|
75 |
self escape:'Ein text mit blanks und @ & #'
|
|
76 |
allow:nil on:TextCollector open
|
|
77 |
"
|
|
78 |
!
|
|
79 |
|
|
80 |
unEscape:aStream
|
|
81 |
"convert escape sequences to the original characters"
|
|
82 |
|
|
83 |
|s c1 c2|
|
|
84 |
|
|
85 |
s := WriteStream on:''.
|
|
86 |
|
|
87 |
[aStream atEnd] whileFalse:[
|
|
88 |
c1 := aStream next.
|
|
89 |
c1 == $% ifTrue:[
|
|
90 |
c1 := aStream next.
|
|
91 |
c1 isNil ifTrue:[
|
|
92 |
self error:'escape sequence incomplete'
|
|
93 |
].
|
|
94 |
c2 := aStream next.
|
|
95 |
c2 isNil ifTrue:[
|
|
96 |
self error:'escape sequence incomplete'
|
|
97 |
].
|
|
98 |
c1 := c1 digitValue.
|
|
99 |
c2 := c2 digitValue.
|
|
100 |
(c1 > 15 or:[c2 > 15]) ifTrue:[
|
|
101 |
self error:'escape sequence: expect hex digit'
|
|
102 |
].
|
|
103 |
c1 := Character value:c1*16 + c2.
|
|
104 |
].
|
|
105 |
s nextPut:c1.
|
|
106 |
].
|
|
107 |
|
|
108 |
^ s contents.
|
|
109 |
|
|
110 |
"
|
|
111 |
self unEscape:(self escape:' &%@ ' allow:nil) readStream
|
|
112 |
"
|
|
113 |
! !
|
|
114 |
|
|
115 |
!URI methodsFor:'accessing'!
|
|
116 |
|
|
117 |
scheme
|
|
118 |
"return primary scheme of the class.
|
|
119 |
Concrete subclasses may redefine this"
|
|
120 |
|
|
121 |
|schemes|
|
|
122 |
|
|
123 |
schemes := self class schemes.
|
|
124 |
^ schemes size ~~ 0 ifTrue:[
|
|
125 |
schemes at:1
|
|
126 |
] ifFalse:[
|
|
127 |
nil
|
|
128 |
]
|
|
129 |
!
|
|
130 |
|
|
131 |
scheme:aString
|
|
132 |
"set the scheme. This is a noop here and may be
|
|
133 |
defined by subclasses"
|
|
134 |
|
|
135 |
^ self
|
|
136 |
! !
|
|
137 |
|
|
138 |
!URI methodsFor:'comparing'!
|
|
139 |
|
|
140 |
= anURI
|
|
141 |
^ self class == anURI class and:[self asString == anURI asString]
|
|
142 |
!
|
|
143 |
|
|
144 |
hash
|
|
145 |
^ self asString hash
|
|
146 |
! !
|
|
147 |
|
|
148 |
!URI methodsFor:'converting'!
|
|
149 |
|
|
150 |
asURI
|
|
151 |
|
|
152 |
^ self
|
|
153 |
! !
|
|
154 |
|
|
155 |
!URI methodsFor:'printing & storing'!
|
|
156 |
|
|
157 |
printOn:aStream
|
|
158 |
|
|
159 |
|scheme|
|
|
160 |
|
|
161 |
scheme := self scheme.
|
|
162 |
scheme size ~~ 0 ifTrue:[
|
|
163 |
aStream nextPutAll:scheme; nextPut:$:
|
|
164 |
].
|
|
165 |
! !
|
|
166 |
|
|
167 |
!URI methodsFor:'subclass responsibility'!
|
|
168 |
|
|
169 |
readStreamDo:aBlock
|
|
170 |
|
|
171 |
"evaluate a block with the read stream as first argument
|
|
172 |
and a dictionary containing attributes as second argument.
|
|
173 |
The stream is closed after aBlock has been evaluated.
|
|
174 |
Attributes may be the mime type (key #MIME)"
|
|
175 |
|
|
176 |
^ self subclassResponsibility
|
|
177 |
! !
|
|
178 |
|
|
179 |
!URI class methodsFor:'documentation'!
|
|
180 |
|
|
181 |
version
|
|
182 |
^ '$Header: /cvs/stx/stx/libbasic2/URI.st,v 1.1 2002-01-17 14:25:48 stefan Exp $'
|
|
183 |
! !
|