dedeatt.class.php
6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* 属性的数据描述
*
* @version $Id: dedeatt.class.php 1 13:50 2010年7月6日Z tianya $
* @package DedeCMS.Libraries
* @copyright Copyright (c) 2007 - 2010, DesDev, Inc.
* @license http://help.dedecms.com/usersguide/license.html
* @link http://www.dedecms.com
*/
// ------------------------------------------------------------------------
/**
* 属性的数据描述
* function c____DedeAtt();
*
* @package DedeAtt
* @subpackage DedeCMS.Libraries
* @link http://www.dedecms.com
*/
class DedeAtt
{
var $Count = -1;
var $Items = ""; //属性元素的集合
/**
* //获得某个属性
*
* @access public
* @param string $str 名称
* @return string
*/
function GetAtt($str)
{
if($str=="")
{
return "";
}
if(isset($this->Items[$str]))
{
return $this->Items[$str];
}
else
{
return "";
}
}
//同上
function GetAttribute($str)
{
return $this->GetAtt($str);
}
/**
* 判断属性是否存在
*
* @access public
* @param string $str 属性名称
* @return string
*/
function IsAttribute($str)
{
return isset($this->Items[$str]) ? TRUE : FALSE;
}
/**
* 获得标记名称
*
* @access public
* @return string
*/
function GetTagName()
{
return $this->GetAtt("tagname");
}
/**
* 获得属性个数
*
* @access public
* @return int
*/
function GetCount()
{
return $this->Count+1;
}
}//End DedeAtt
/**
* 属性解析器
* function c____DedeAttParse();
*
* @package DedeAtt
* @subpackage DedeCMS.Libraries
* @link http://www.dedecms.com
*/
class DedeAttParse
{
var $SourceString = "";
var $SourceMaxSize = 1024;
var $CAtt = ""; //属性的数据描述类
var $CharToLow = TRUE;
/**
* 设置属性解析器源字符串
*
* @access public
* @param string $str 需要解析的字符串
* @return string
*/
function SetSource($str="")
{
$this->CAtt = new DedeAtt();
$strLen = 0;
$this->SourceString = trim(preg_replace("/[ \t\r\n]{1,}/"," ",$str));
$strLen = strlen($this->SourceString);
if($strLen>0&&$strLen<=$this->SourceMaxSize)
{
$this->ParseAtt();
}
}
/**
* 解析属性(私有成员,仅给SetSource调用)
*
* @access private
* @return void
*/
function ParseAtt()
{
$d = "";
$tmpatt="";
$tmpvalue="";
$startdd=-1;
$ddtag="";
$notAttribute=TRUE;
$strLen = strlen($this->SourceString);
// 这里是获得Tag的名称,可视情况是否需要
// 如果不在这个里解析,则在解析整个Tag时解析
// 属性中不应该存在tagname这个名称
for($i=0;$i<$strLen;$i++)
{
$d = substr($this->SourceString,$i,1);
if($d==' ')
{
$this->CAtt->Count++;
if($this->CharToLow)
{
$this->CAtt->Items["tagname"]=strtolower(trim($tmpvalue));
}
else
{
$this->CAtt->Items["tagname"]=trim($tmpvalue);
}
$tmpvalue = "";
$notAttribute = FALSE;
break;
}
else
{
$tmpvalue .= $d;
}
}
//不存在属性列表的情况
if($notAttribute)
{
$this->CAtt->Count++;
$this->CAtt->Items["tagname"]= ($this->CharToLow ? strtolower(trim($tmpvalue)) : trim($tmpvalue));
}
//如果字符串含有属性值,遍历源字符串,并获得各属性
if(!$notAttribute)
{
for($i;$i<$strLen;$i++)
{
$d = substr($this->SourceString,$i,1);
if($startdd==-1)
{
if($d!="=")
{
$tmpatt .= $d;
}
else
{
if($this->CharToLow)
{
$tmpatt = strtolower(trim($tmpatt));
}
else
{
$tmpatt = trim($tmpatt);
}
$startdd=0;
}
}
else if($startdd==0)
{
switch($d)
{
case ' ':
continue;
break;
case '\'':
$ddtag='\'';
$startdd=1;
break;
case '"':
$ddtag='"';
$startdd=1;
break;
default:
$tmpvalue.=$d;
$ddtag=' ';
$startdd=1;
break;
}
}
else if($startdd==1)
{
if($d==$ddtag)
{
$this->CAtt->Count++;
$this->CAtt->Items[$tmpatt] = trim($tmpvalue);//strtolower(trim($tmpvalue));
$tmpatt = "";
$tmpvalue = "";
$startdd=-1;
}
else
{
$tmpvalue.=$d;
}
}
}
if($tmpatt!="")
{
$this->CAtt->Count++;
$this->CAtt->Items[$tmpatt]=trim($tmpvalue);//strtolower(trim($tmpvalue));
}//完成属性解析
}//for
}//has Attribute
}//End DedeAttParse