2
0

xmlrole.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. /*
  2. Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
  3. See the file copying.txt for copying permission.
  4. */
  5. #include "xmlrpc_config.h"
  6. #include "xmldef.h"
  7. #include "xmlrole.h"
  8. #include "ascii.h"
  9. /* Doesn't check:
  10. that ,| are not mixed in a model group
  11. content of literals
  12. */
  13. static const char KW_ANY[] = { ASCII_A, ASCII_N, ASCII_Y, '\0' };
  14. static const char KW_ATTLIST[] = { ASCII_A, ASCII_T, ASCII_T, ASCII_L, ASCII_I, ASCII_S, ASCII_T, '\0' };
  15. static const char KW_CDATA[] = { ASCII_C, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0' };
  16. static const char KW_DOCTYPE[] = { ASCII_D, ASCII_O, ASCII_C, ASCII_T, ASCII_Y, ASCII_P, ASCII_E, '\0' };
  17. static const char KW_ELEMENT[] = { ASCII_E, ASCII_L, ASCII_E, ASCII_M, ASCII_E, ASCII_N, ASCII_T, '\0' };
  18. static const char KW_EMPTY[] = { ASCII_E, ASCII_M, ASCII_P, ASCII_T, ASCII_Y, '\0' };
  19. static const char KW_ENTITIES[] = { ASCII_E, ASCII_N, ASCII_T, ASCII_I, ASCII_T, ASCII_I, ASCII_E, ASCII_S, '\0' };
  20. static const char KW_ENTITY[] = { ASCII_E, ASCII_N, ASCII_T, ASCII_I, ASCII_T, ASCII_Y, '\0' };
  21. static const char KW_FIXED[] = { ASCII_F, ASCII_I, ASCII_X, ASCII_E, ASCII_D, '\0' };
  22. static const char KW_ID[] = { ASCII_I, ASCII_D, '\0' };
  23. static const char KW_IDREF[] = { ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, '\0' };
  24. static const char KW_IDREFS[] = { ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, ASCII_S, '\0' };
  25. static const char KW_IGNORE[] = { ASCII_I, ASCII_G, ASCII_N, ASCII_O, ASCII_R, ASCII_E, '\0' };
  26. static const char KW_IMPLIED[] = { ASCII_I, ASCII_M, ASCII_P, ASCII_L, ASCII_I, ASCII_E, ASCII_D, '\0' };
  27. static const char KW_INCLUDE[] = { ASCII_I, ASCII_N, ASCII_C, ASCII_L, ASCII_U, ASCII_D, ASCII_E, '\0' };
  28. static const char KW_NDATA[] = { ASCII_N, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0' };
  29. static const char KW_NMTOKEN[] = { ASCII_N, ASCII_M, ASCII_T, ASCII_O, ASCII_K, ASCII_E, ASCII_N, '\0' };
  30. static const char KW_NMTOKENS[] = { ASCII_N, ASCII_M, ASCII_T, ASCII_O, ASCII_K, ASCII_E, ASCII_N, ASCII_S, '\0' };
  31. static const char KW_NOTATION[] = { ASCII_N, ASCII_O, ASCII_T, ASCII_A, ASCII_T, ASCII_I, ASCII_O, ASCII_N, '\0' };
  32. static const char KW_PCDATA[] = { ASCII_P, ASCII_C, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0' };
  33. static const char KW_PUBLIC[] = { ASCII_P, ASCII_U, ASCII_B, ASCII_L, ASCII_I, ASCII_C, '\0' };
  34. static const char KW_REQUIRED[] = { ASCII_R, ASCII_E, ASCII_Q, ASCII_U, ASCII_I, ASCII_R, ASCII_E, ASCII_D, '\0' };
  35. static const char KW_SYSTEM[] = { ASCII_S, ASCII_Y, ASCII_S, ASCII_T, ASCII_E, ASCII_M, '\0' };
  36. #ifndef MIN_BYTES_PER_CHAR
  37. #define MIN_BYTES_PER_CHAR(enc) ((enc)->minBytesPerChar)
  38. #endif
  39. #define setTopLevel(state) \
  40. ((state)->handler = ((state)->documentEntity \
  41. ? internalSubset \
  42. : externalSubset1))
  43. typedef int PROLOG_HANDLER(PROLOG_STATE *state,
  44. int tok,
  45. const char *ptr,
  46. const char *end,
  47. const ENCODING *enc);
  48. static PROLOG_HANDLER
  49. prolog0, prolog1, prolog2,
  50. doctype0, doctype1, doctype2, doctype3, doctype4, doctype5,
  51. internalSubset,
  52. entity0, entity1, entity2, entity3, entity4, entity5, entity6,
  53. entity7, entity8, entity9,
  54. notation0, notation1, notation2, notation3, notation4,
  55. attlist0, attlist1, attlist2, attlist3, attlist4, attlist5, attlist6,
  56. attlist7, attlist8, attlist9,
  57. element0, element1, element2, element3, element4, element5, element6,
  58. element7,
  59. externalSubset0, externalSubset1,
  60. condSect0, condSect1, condSect2,
  61. declClose,
  62. error;
  63. static
  64. int common(PROLOG_STATE *state, int tok);
  65. static
  66. int prolog0(PROLOG_STATE *state,
  67. int tok,
  68. const char *ptr,
  69. const char *end,
  70. const ENCODING *enc)
  71. {
  72. switch (tok) {
  73. case XML_TOK_PROLOG_S:
  74. state->handler = prolog1;
  75. return XML_ROLE_NONE;
  76. case XML_TOK_XML_DECL:
  77. state->handler = prolog1;
  78. return XML_ROLE_XML_DECL;
  79. case XML_TOK_PI:
  80. state->handler = prolog1;
  81. return XML_ROLE_NONE;
  82. case XML_TOK_COMMENT:
  83. state->handler = prolog1;
  84. case XML_TOK_BOM:
  85. return XML_ROLE_NONE;
  86. case XML_TOK_DECL_OPEN:
  87. if (!XmlNameMatchesAscii(enc,
  88. ptr + 2 * MIN_BYTES_PER_CHAR(enc),
  89. end,
  90. KW_DOCTYPE))
  91. break;
  92. state->handler = doctype0;
  93. return XML_ROLE_NONE;
  94. case XML_TOK_INSTANCE_START:
  95. state->handler = error;
  96. return XML_ROLE_INSTANCE_START;
  97. }
  98. return common(state, tok);
  99. }
  100. static
  101. int prolog1(PROLOG_STATE *state,
  102. int tok,
  103. const char *ptr,
  104. const char *end,
  105. const ENCODING *enc)
  106. {
  107. switch (tok) {
  108. case XML_TOK_PROLOG_S:
  109. return XML_ROLE_NONE;
  110. case XML_TOK_PI:
  111. case XML_TOK_COMMENT:
  112. case XML_TOK_BOM:
  113. return XML_ROLE_NONE;
  114. case XML_TOK_DECL_OPEN:
  115. if (!XmlNameMatchesAscii(enc,
  116. ptr + 2 * MIN_BYTES_PER_CHAR(enc),
  117. end,
  118. KW_DOCTYPE))
  119. break;
  120. state->handler = doctype0;
  121. return XML_ROLE_NONE;
  122. case XML_TOK_INSTANCE_START:
  123. state->handler = error;
  124. return XML_ROLE_INSTANCE_START;
  125. }
  126. return common(state, tok);
  127. }
  128. static
  129. int prolog2(PROLOG_STATE *state,
  130. int tok,
  131. const char *ptr ATTR_UNUSED,
  132. const char *end ATTR_UNUSED,
  133. const ENCODING *enc ATTR_UNUSED)
  134. {
  135. switch (tok) {
  136. case XML_TOK_PROLOG_S:
  137. return XML_ROLE_NONE;
  138. case XML_TOK_PI:
  139. case XML_TOK_COMMENT:
  140. return XML_ROLE_NONE;
  141. case XML_TOK_INSTANCE_START:
  142. state->handler = error;
  143. return XML_ROLE_INSTANCE_START;
  144. }
  145. return common(state, tok);
  146. }
  147. static
  148. int doctype0(PROLOG_STATE *state,
  149. int tok,
  150. const char *ptr ATTR_UNUSED,
  151. const char *end ATTR_UNUSED,
  152. const ENCODING *enc ATTR_UNUSED)
  153. {
  154. switch (tok) {
  155. case XML_TOK_PROLOG_S:
  156. return XML_ROLE_NONE;
  157. case XML_TOK_NAME:
  158. case XML_TOK_PREFIXED_NAME:
  159. state->handler = doctype1;
  160. return XML_ROLE_DOCTYPE_NAME;
  161. }
  162. return common(state, tok);
  163. }
  164. static
  165. int doctype1(PROLOG_STATE *state,
  166. int tok,
  167. const char *ptr,
  168. const char *end,
  169. const ENCODING *enc)
  170. {
  171. switch (tok) {
  172. case XML_TOK_PROLOG_S:
  173. return XML_ROLE_NONE;
  174. case XML_TOK_OPEN_BRACKET:
  175. state->handler = internalSubset;
  176. return XML_ROLE_NONE;
  177. case XML_TOK_DECL_CLOSE:
  178. state->handler = prolog2;
  179. return XML_ROLE_DOCTYPE_CLOSE;
  180. case XML_TOK_NAME:
  181. if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
  182. state->handler = doctype3;
  183. return XML_ROLE_NONE;
  184. }
  185. if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
  186. state->handler = doctype2;
  187. return XML_ROLE_NONE;
  188. }
  189. break;
  190. }
  191. return common(state, tok);
  192. }
  193. static
  194. int doctype2(PROLOG_STATE *state,
  195. int tok,
  196. const char *ptr ATTR_UNUSED,
  197. const char *end ATTR_UNUSED,
  198. const ENCODING *enc ATTR_UNUSED)
  199. {
  200. switch (tok) {
  201. case XML_TOK_PROLOG_S:
  202. return XML_ROLE_NONE;
  203. case XML_TOK_LITERAL:
  204. state->handler = doctype3;
  205. return XML_ROLE_DOCTYPE_PUBLIC_ID;
  206. }
  207. return common(state, tok);
  208. }
  209. static
  210. int doctype3(PROLOG_STATE *state,
  211. int tok,
  212. const char *ptr ATTR_UNUSED,
  213. const char *end ATTR_UNUSED,
  214. const ENCODING *enc ATTR_UNUSED)
  215. {
  216. switch (tok) {
  217. case XML_TOK_PROLOG_S:
  218. return XML_ROLE_NONE;
  219. case XML_TOK_LITERAL:
  220. state->handler = doctype4;
  221. return XML_ROLE_DOCTYPE_SYSTEM_ID;
  222. }
  223. return common(state, tok);
  224. }
  225. static
  226. int doctype4(PROLOG_STATE *state,
  227. int tok,
  228. const char *ptr ATTR_UNUSED,
  229. const char *end ATTR_UNUSED,
  230. const ENCODING *enc ATTR_UNUSED)
  231. {
  232. switch (tok) {
  233. case XML_TOK_PROLOG_S:
  234. return XML_ROLE_NONE;
  235. case XML_TOK_OPEN_BRACKET:
  236. state->handler = internalSubset;
  237. return XML_ROLE_NONE;
  238. case XML_TOK_DECL_CLOSE:
  239. state->handler = prolog2;
  240. return XML_ROLE_DOCTYPE_CLOSE;
  241. }
  242. return common(state, tok);
  243. }
  244. static
  245. int doctype5(PROLOG_STATE *state,
  246. int tok,
  247. const char *ptr ATTR_UNUSED,
  248. const char *end ATTR_UNUSED,
  249. const ENCODING *enc ATTR_UNUSED)
  250. {
  251. switch (tok) {
  252. case XML_TOK_PROLOG_S:
  253. return XML_ROLE_NONE;
  254. case XML_TOK_DECL_CLOSE:
  255. state->handler = prolog2;
  256. return XML_ROLE_DOCTYPE_CLOSE;
  257. }
  258. return common(state, tok);
  259. }
  260. static
  261. int internalSubset(PROLOG_STATE *state,
  262. int tok,
  263. const char *ptr,
  264. const char *end,
  265. const ENCODING *enc)
  266. {
  267. switch (tok) {
  268. case XML_TOK_PROLOG_S:
  269. return XML_ROLE_NONE;
  270. case XML_TOK_DECL_OPEN:
  271. if (XmlNameMatchesAscii(enc,
  272. ptr + 2 * MIN_BYTES_PER_CHAR(enc),
  273. end,
  274. KW_ENTITY)) {
  275. state->handler = entity0;
  276. return XML_ROLE_NONE;
  277. }
  278. if (XmlNameMatchesAscii(enc,
  279. ptr + 2 * MIN_BYTES_PER_CHAR(enc),
  280. end,
  281. KW_ATTLIST)) {
  282. state->handler = attlist0;
  283. return XML_ROLE_NONE;
  284. }
  285. if (XmlNameMatchesAscii(enc,
  286. ptr + 2 * MIN_BYTES_PER_CHAR(enc),
  287. end,
  288. KW_ELEMENT)) {
  289. state->handler = element0;
  290. return XML_ROLE_NONE;
  291. }
  292. if (XmlNameMatchesAscii(enc,
  293. ptr + 2 * MIN_BYTES_PER_CHAR(enc),
  294. end,
  295. KW_NOTATION)) {
  296. state->handler = notation0;
  297. return XML_ROLE_NONE;
  298. }
  299. break;
  300. case XML_TOK_PI:
  301. case XML_TOK_COMMENT:
  302. return XML_ROLE_NONE;
  303. case XML_TOK_PARAM_ENTITY_REF:
  304. return XML_ROLE_PARAM_ENTITY_REF;
  305. case XML_TOK_CLOSE_BRACKET:
  306. state->handler = doctype5;
  307. return XML_ROLE_NONE;
  308. }
  309. return common(state, tok);
  310. }
  311. static
  312. int externalSubset0(PROLOG_STATE *state,
  313. int tok,
  314. const char *ptr,
  315. const char *end,
  316. const ENCODING *enc)
  317. {
  318. state->handler = externalSubset1;
  319. if (tok == XML_TOK_XML_DECL)
  320. return XML_ROLE_TEXT_DECL;
  321. return externalSubset1(state, tok, ptr, end, enc);
  322. }
  323. static
  324. int externalSubset1(PROLOG_STATE *state,
  325. int tok,
  326. const char *ptr,
  327. const char *end,
  328. const ENCODING *enc)
  329. {
  330. switch (tok) {
  331. case XML_TOK_COND_SECT_OPEN:
  332. state->handler = condSect0;
  333. return XML_ROLE_NONE;
  334. case XML_TOK_COND_SECT_CLOSE:
  335. if (state->includeLevel == 0)
  336. break;
  337. state->includeLevel -= 1;
  338. return XML_ROLE_NONE;
  339. case XML_TOK_PROLOG_S:
  340. return XML_ROLE_NONE;
  341. case XML_TOK_CLOSE_BRACKET:
  342. break;
  343. case XML_TOK_NONE:
  344. if (state->includeLevel)
  345. break;
  346. return XML_ROLE_NONE;
  347. default:
  348. return internalSubset(state, tok, ptr, end, enc);
  349. }
  350. return common(state, tok);
  351. }
  352. static
  353. int entity0(PROLOG_STATE *state,
  354. int tok,
  355. const char *ptr ATTR_UNUSED,
  356. const char *end ATTR_UNUSED,
  357. const ENCODING *enc ATTR_UNUSED)
  358. {
  359. switch (tok) {
  360. case XML_TOK_PROLOG_S:
  361. return XML_ROLE_NONE;
  362. case XML_TOK_PERCENT:
  363. state->handler = entity1;
  364. return XML_ROLE_NONE;
  365. case XML_TOK_NAME:
  366. state->handler = entity2;
  367. return XML_ROLE_GENERAL_ENTITY_NAME;
  368. }
  369. return common(state, tok);
  370. }
  371. static
  372. int entity1(PROLOG_STATE *state,
  373. int tok,
  374. const char *ptr ATTR_UNUSED,
  375. const char *end ATTR_UNUSED,
  376. const ENCODING *enc ATTR_UNUSED)
  377. {
  378. switch (tok) {
  379. case XML_TOK_PROLOG_S:
  380. return XML_ROLE_NONE;
  381. case XML_TOK_NAME:
  382. state->handler = entity7;
  383. return XML_ROLE_PARAM_ENTITY_NAME;
  384. }
  385. return common(state, tok);
  386. }
  387. static
  388. int entity2(PROLOG_STATE *state,
  389. int tok,
  390. const char *ptr,
  391. const char *end,
  392. const ENCODING *enc)
  393. {
  394. switch (tok) {
  395. case XML_TOK_PROLOG_S:
  396. return XML_ROLE_NONE;
  397. case XML_TOK_NAME:
  398. if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
  399. state->handler = entity4;
  400. return XML_ROLE_NONE;
  401. }
  402. if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
  403. state->handler = entity3;
  404. return XML_ROLE_NONE;
  405. }
  406. break;
  407. case XML_TOK_LITERAL:
  408. state->handler = declClose;
  409. return XML_ROLE_ENTITY_VALUE;
  410. }
  411. return common(state, tok);
  412. }
  413. static
  414. int entity3(PROLOG_STATE *state,
  415. int tok,
  416. const char *ptr ATTR_UNUSED,
  417. const char *end ATTR_UNUSED,
  418. const ENCODING *enc ATTR_UNUSED)
  419. {
  420. switch (tok) {
  421. case XML_TOK_PROLOG_S:
  422. return XML_ROLE_NONE;
  423. case XML_TOK_LITERAL:
  424. state->handler = entity4;
  425. return XML_ROLE_ENTITY_PUBLIC_ID;
  426. }
  427. return common(state, tok);
  428. }
  429. static
  430. int entity4(PROLOG_STATE *state,
  431. int tok,
  432. const char *ptr ATTR_UNUSED,
  433. const char *end ATTR_UNUSED,
  434. const ENCODING *enc ATTR_UNUSED)
  435. {
  436. switch (tok) {
  437. case XML_TOK_PROLOG_S:
  438. return XML_ROLE_NONE;
  439. case XML_TOK_LITERAL:
  440. state->handler = entity5;
  441. return XML_ROLE_ENTITY_SYSTEM_ID;
  442. }
  443. return common(state, tok);
  444. }
  445. static
  446. int entity5(PROLOG_STATE *state,
  447. int tok,
  448. const char *ptr,
  449. const char *end,
  450. const ENCODING *enc)
  451. {
  452. switch (tok) {
  453. case XML_TOK_PROLOG_S:
  454. return XML_ROLE_NONE;
  455. case XML_TOK_DECL_CLOSE:
  456. setTopLevel(state);
  457. return XML_ROLE_EXTERNAL_GENERAL_ENTITY_NO_NOTATION;
  458. case XML_TOK_NAME:
  459. if (XmlNameMatchesAscii(enc, ptr, end, KW_NDATA)) {
  460. state->handler = entity6;
  461. return XML_ROLE_NONE;
  462. }
  463. break;
  464. }
  465. return common(state, tok);
  466. }
  467. static
  468. int entity6(PROLOG_STATE *state,
  469. int tok,
  470. const char *ptr ATTR_UNUSED,
  471. const char *end ATTR_UNUSED,
  472. const ENCODING *enc ATTR_UNUSED)
  473. {
  474. switch (tok) {
  475. case XML_TOK_PROLOG_S:
  476. return XML_ROLE_NONE;
  477. case XML_TOK_NAME:
  478. state->handler = declClose;
  479. return XML_ROLE_ENTITY_NOTATION_NAME;
  480. }
  481. return common(state, tok);
  482. }
  483. static
  484. int entity7(PROLOG_STATE *state,
  485. int tok,
  486. const char *ptr,
  487. const char *end,
  488. const ENCODING *enc)
  489. {
  490. switch (tok) {
  491. case XML_TOK_PROLOG_S:
  492. return XML_ROLE_NONE;
  493. case XML_TOK_NAME:
  494. if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
  495. state->handler = entity9;
  496. return XML_ROLE_NONE;
  497. }
  498. if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
  499. state->handler = entity8;
  500. return XML_ROLE_NONE;
  501. }
  502. break;
  503. case XML_TOK_LITERAL:
  504. state->handler = declClose;
  505. return XML_ROLE_ENTITY_VALUE;
  506. }
  507. return common(state, tok);
  508. }
  509. static
  510. int entity8(PROLOG_STATE *state,
  511. int tok,
  512. const char *ptr ATTR_UNUSED,
  513. const char *end ATTR_UNUSED,
  514. const ENCODING *enc ATTR_UNUSED)
  515. {
  516. switch (tok) {
  517. case XML_TOK_PROLOG_S:
  518. return XML_ROLE_NONE;
  519. case XML_TOK_LITERAL:
  520. state->handler = entity9;
  521. return XML_ROLE_ENTITY_PUBLIC_ID;
  522. }
  523. return common(state, tok);
  524. }
  525. static
  526. int entity9(PROLOG_STATE *state,
  527. int tok,
  528. const char *ptr ATTR_UNUSED,
  529. const char *end ATTR_UNUSED,
  530. const ENCODING *enc ATTR_UNUSED)
  531. {
  532. switch (tok) {
  533. case XML_TOK_PROLOG_S:
  534. return XML_ROLE_NONE;
  535. case XML_TOK_LITERAL:
  536. state->handler = declClose;
  537. return XML_ROLE_ENTITY_SYSTEM_ID;
  538. }
  539. return common(state, tok);
  540. }
  541. static
  542. int notation0(PROLOG_STATE *state,
  543. int tok,
  544. const char *ptr ATTR_UNUSED,
  545. const char *end ATTR_UNUSED,
  546. const ENCODING *enc ATTR_UNUSED)
  547. {
  548. switch (tok) {
  549. case XML_TOK_PROLOG_S:
  550. return XML_ROLE_NONE;
  551. case XML_TOK_NAME:
  552. state->handler = notation1;
  553. return XML_ROLE_NOTATION_NAME;
  554. }
  555. return common(state, tok);
  556. }
  557. static
  558. int notation1(PROLOG_STATE *state,
  559. int tok,
  560. const char *ptr,
  561. const char *end,
  562. const ENCODING *enc)
  563. {
  564. switch (tok) {
  565. case XML_TOK_PROLOG_S:
  566. return XML_ROLE_NONE;
  567. case XML_TOK_NAME:
  568. if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
  569. state->handler = notation3;
  570. return XML_ROLE_NONE;
  571. }
  572. if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
  573. state->handler = notation2;
  574. return XML_ROLE_NONE;
  575. }
  576. break;
  577. }
  578. return common(state, tok);
  579. }
  580. static
  581. int notation2(PROLOG_STATE *state,
  582. int tok,
  583. const char *ptr ATTR_UNUSED,
  584. const char *end ATTR_UNUSED,
  585. const ENCODING *enc ATTR_UNUSED)
  586. {
  587. switch (tok) {
  588. case XML_TOK_PROLOG_S:
  589. return XML_ROLE_NONE;
  590. case XML_TOK_LITERAL:
  591. state->handler = notation4;
  592. return XML_ROLE_NOTATION_PUBLIC_ID;
  593. }
  594. return common(state, tok);
  595. }
  596. static
  597. int notation3(PROLOG_STATE *state,
  598. int tok,
  599. const char *ptr ATTR_UNUSED,
  600. const char *end ATTR_UNUSED,
  601. const ENCODING *enc ATTR_UNUSED)
  602. {
  603. switch (tok) {
  604. case XML_TOK_PROLOG_S:
  605. return XML_ROLE_NONE;
  606. case XML_TOK_LITERAL:
  607. state->handler = declClose;
  608. return XML_ROLE_NOTATION_SYSTEM_ID;
  609. }
  610. return common(state, tok);
  611. }
  612. static
  613. int notation4(PROLOG_STATE *state,
  614. int tok,
  615. const char *ptr ATTR_UNUSED,
  616. const char *end ATTR_UNUSED,
  617. const ENCODING *enc ATTR_UNUSED)
  618. {
  619. switch (tok) {
  620. case XML_TOK_PROLOG_S:
  621. return XML_ROLE_NONE;
  622. case XML_TOK_LITERAL:
  623. state->handler = declClose;
  624. return XML_ROLE_NOTATION_SYSTEM_ID;
  625. case XML_TOK_DECL_CLOSE:
  626. setTopLevel(state);
  627. return XML_ROLE_NOTATION_NO_SYSTEM_ID;
  628. }
  629. return common(state, tok);
  630. }
  631. static
  632. int attlist0(PROLOG_STATE *state,
  633. int tok,
  634. const char *ptr ATTR_UNUSED,
  635. const char *end ATTR_UNUSED,
  636. const ENCODING *enc ATTR_UNUSED)
  637. {
  638. switch (tok) {
  639. case XML_TOK_PROLOG_S:
  640. return XML_ROLE_NONE;
  641. case XML_TOK_NAME:
  642. case XML_TOK_PREFIXED_NAME:
  643. state->handler = attlist1;
  644. return XML_ROLE_ATTLIST_ELEMENT_NAME;
  645. }
  646. return common(state, tok);
  647. }
  648. static
  649. int attlist1(PROLOG_STATE *state,
  650. int tok,
  651. const char *ptr ATTR_UNUSED,
  652. const char *end ATTR_UNUSED,
  653. const ENCODING *enc ATTR_UNUSED)
  654. {
  655. switch (tok) {
  656. case XML_TOK_PROLOG_S:
  657. return XML_ROLE_NONE;
  658. case XML_TOK_DECL_CLOSE:
  659. setTopLevel(state);
  660. return XML_ROLE_NONE;
  661. case XML_TOK_NAME:
  662. case XML_TOK_PREFIXED_NAME:
  663. state->handler = attlist2;
  664. return XML_ROLE_ATTRIBUTE_NAME;
  665. }
  666. return common(state, tok);
  667. }
  668. static
  669. int attlist2(PROLOG_STATE *state,
  670. int tok,
  671. const char *ptr,
  672. const char *end,
  673. const ENCODING *enc)
  674. {
  675. switch (tok) {
  676. case XML_TOK_PROLOG_S:
  677. return XML_ROLE_NONE;
  678. case XML_TOK_NAME:
  679. {
  680. static const char *types[] = {
  681. KW_CDATA,
  682. KW_ID,
  683. KW_IDREF,
  684. KW_IDREFS,
  685. KW_ENTITY,
  686. KW_ENTITIES,
  687. KW_NMTOKEN,
  688. KW_NMTOKENS,
  689. };
  690. int i;
  691. for (i = 0; i < (int)(sizeof(types)/sizeof(types[0])); i++)
  692. if (XmlNameMatchesAscii(enc, ptr, end, types[i])) {
  693. state->handler = attlist8;
  694. return XML_ROLE_ATTRIBUTE_TYPE_CDATA + i;
  695. }
  696. }
  697. if (XmlNameMatchesAscii(enc, ptr, end, KW_NOTATION)) {
  698. state->handler = attlist5;
  699. return XML_ROLE_NONE;
  700. }
  701. break;
  702. case XML_TOK_OPEN_PAREN:
  703. state->handler = attlist3;
  704. return XML_ROLE_NONE;
  705. }
  706. return common(state, tok);
  707. }
  708. static
  709. int attlist3(PROLOG_STATE *state,
  710. int tok,
  711. const char *ptr ATTR_UNUSED,
  712. const char *end ATTR_UNUSED,
  713. const ENCODING *enc ATTR_UNUSED)
  714. {
  715. switch (tok) {
  716. case XML_TOK_PROLOG_S:
  717. return XML_ROLE_NONE;
  718. case XML_TOK_NMTOKEN:
  719. case XML_TOK_NAME:
  720. case XML_TOK_PREFIXED_NAME:
  721. state->handler = attlist4;
  722. return XML_ROLE_ATTRIBUTE_ENUM_VALUE;
  723. }
  724. return common(state, tok);
  725. }
  726. static
  727. int attlist4(PROLOG_STATE *state,
  728. int tok,
  729. const char *ptr ATTR_UNUSED,
  730. const char *end ATTR_UNUSED,
  731. const ENCODING *enc ATTR_UNUSED)
  732. {
  733. switch (tok) {
  734. case XML_TOK_PROLOG_S:
  735. return XML_ROLE_NONE;
  736. case XML_TOK_CLOSE_PAREN:
  737. state->handler = attlist8;
  738. return XML_ROLE_NONE;
  739. case XML_TOK_OR:
  740. state->handler = attlist3;
  741. return XML_ROLE_NONE;
  742. }
  743. return common(state, tok);
  744. }
  745. static
  746. int attlist5(PROLOG_STATE *state,
  747. int tok,
  748. const char *ptr ATTR_UNUSED,
  749. const char *end ATTR_UNUSED,
  750. const ENCODING *enc ATTR_UNUSED)
  751. {
  752. switch (tok) {
  753. case XML_TOK_PROLOG_S:
  754. return XML_ROLE_NONE;
  755. case XML_TOK_OPEN_PAREN:
  756. state->handler = attlist6;
  757. return XML_ROLE_NONE;
  758. }
  759. return common(state, tok);
  760. }
  761. static
  762. int attlist6(PROLOG_STATE *state,
  763. int tok,
  764. const char *ptr ATTR_UNUSED,
  765. const char *end ATTR_UNUSED,
  766. const ENCODING *enc ATTR_UNUSED)
  767. {
  768. switch (tok) {
  769. case XML_TOK_PROLOG_S:
  770. return XML_ROLE_NONE;
  771. case XML_TOK_NAME:
  772. state->handler = attlist7;
  773. return XML_ROLE_ATTRIBUTE_NOTATION_VALUE;
  774. }
  775. return common(state, tok);
  776. }
  777. static
  778. int attlist7(PROLOG_STATE *state,
  779. int tok,
  780. const char *ptr ATTR_UNUSED,
  781. const char *end ATTR_UNUSED,
  782. const ENCODING *enc ATTR_UNUSED)
  783. {
  784. switch (tok) {
  785. case XML_TOK_PROLOG_S:
  786. return XML_ROLE_NONE;
  787. case XML_TOK_CLOSE_PAREN:
  788. state->handler = attlist8;
  789. return XML_ROLE_NONE;
  790. case XML_TOK_OR:
  791. state->handler = attlist6;
  792. return XML_ROLE_NONE;
  793. }
  794. return common(state, tok);
  795. }
  796. /* default value */
  797. static
  798. int attlist8(PROLOG_STATE *state,
  799. int tok,
  800. const char *ptr,
  801. const char *end,
  802. const ENCODING *enc)
  803. {
  804. switch (tok) {
  805. case XML_TOK_PROLOG_S:
  806. return XML_ROLE_NONE;
  807. case XML_TOK_POUND_NAME:
  808. if (XmlNameMatchesAscii(enc,
  809. ptr + MIN_BYTES_PER_CHAR(enc),
  810. end,
  811. KW_IMPLIED)) {
  812. state->handler = attlist1;
  813. return XML_ROLE_IMPLIED_ATTRIBUTE_VALUE;
  814. }
  815. if (XmlNameMatchesAscii(enc,
  816. ptr + MIN_BYTES_PER_CHAR(enc),
  817. end,
  818. KW_REQUIRED)) {
  819. state->handler = attlist1;
  820. return XML_ROLE_REQUIRED_ATTRIBUTE_VALUE;
  821. }
  822. if (XmlNameMatchesAscii(enc,
  823. ptr + MIN_BYTES_PER_CHAR(enc),
  824. end,
  825. KW_FIXED)) {
  826. state->handler = attlist9;
  827. return XML_ROLE_NONE;
  828. }
  829. break;
  830. case XML_TOK_LITERAL:
  831. state->handler = attlist1;
  832. return XML_ROLE_DEFAULT_ATTRIBUTE_VALUE;
  833. }
  834. return common(state, tok);
  835. }
  836. static
  837. int attlist9(PROLOG_STATE *state,
  838. int tok,
  839. const char *ptr ATTR_UNUSED,
  840. const char *end ATTR_UNUSED,
  841. const ENCODING *enc ATTR_UNUSED)
  842. {
  843. switch (tok) {
  844. case XML_TOK_PROLOG_S:
  845. return XML_ROLE_NONE;
  846. case XML_TOK_LITERAL:
  847. state->handler = attlist1;
  848. return XML_ROLE_FIXED_ATTRIBUTE_VALUE;
  849. }
  850. return common(state, tok);
  851. }
  852. static
  853. int element0(PROLOG_STATE *state,
  854. int tok,
  855. const char *ptr ATTR_UNUSED,
  856. const char *end ATTR_UNUSED,
  857. const ENCODING *enc ATTR_UNUSED)
  858. {
  859. switch (tok) {
  860. case XML_TOK_PROLOG_S:
  861. return XML_ROLE_NONE;
  862. case XML_TOK_NAME:
  863. case XML_TOK_PREFIXED_NAME:
  864. state->handler = element1;
  865. return XML_ROLE_ELEMENT_NAME;
  866. }
  867. return common(state, tok);
  868. }
  869. static
  870. int element1(PROLOG_STATE *state,
  871. int tok,
  872. const char *ptr,
  873. const char *end,
  874. const ENCODING *enc)
  875. {
  876. switch (tok) {
  877. case XML_TOK_PROLOG_S:
  878. return XML_ROLE_NONE;
  879. case XML_TOK_NAME:
  880. if (XmlNameMatchesAscii(enc, ptr, end, KW_EMPTY)) {
  881. state->handler = declClose;
  882. return XML_ROLE_CONTENT_EMPTY;
  883. }
  884. if (XmlNameMatchesAscii(enc, ptr, end, KW_ANY)) {
  885. state->handler = declClose;
  886. return XML_ROLE_CONTENT_ANY;
  887. }
  888. break;
  889. case XML_TOK_OPEN_PAREN:
  890. state->handler = element2;
  891. state->level = 1;
  892. return XML_ROLE_GROUP_OPEN;
  893. }
  894. return common(state, tok);
  895. }
  896. static
  897. int element2(PROLOG_STATE *state,
  898. int tok,
  899. const char *ptr,
  900. const char *end,
  901. const ENCODING *enc)
  902. {
  903. switch (tok) {
  904. case XML_TOK_PROLOG_S:
  905. return XML_ROLE_NONE;
  906. case XML_TOK_POUND_NAME:
  907. if (XmlNameMatchesAscii(enc,
  908. ptr + MIN_BYTES_PER_CHAR(enc),
  909. end,
  910. KW_PCDATA)) {
  911. state->handler = element3;
  912. return XML_ROLE_CONTENT_PCDATA;
  913. }
  914. break;
  915. case XML_TOK_OPEN_PAREN:
  916. state->level = 2;
  917. state->handler = element6;
  918. return XML_ROLE_GROUP_OPEN;
  919. case XML_TOK_NAME:
  920. case XML_TOK_PREFIXED_NAME:
  921. state->handler = element7;
  922. return XML_ROLE_CONTENT_ELEMENT;
  923. case XML_TOK_NAME_QUESTION:
  924. state->handler = element7;
  925. return XML_ROLE_CONTENT_ELEMENT_OPT;
  926. case XML_TOK_NAME_ASTERISK:
  927. state->handler = element7;
  928. return XML_ROLE_CONTENT_ELEMENT_REP;
  929. case XML_TOK_NAME_PLUS:
  930. state->handler = element7;
  931. return XML_ROLE_CONTENT_ELEMENT_PLUS;
  932. }
  933. return common(state, tok);
  934. }
  935. static
  936. int element3(PROLOG_STATE *state,
  937. int tok,
  938. const char *ptr ATTR_UNUSED,
  939. const char *end ATTR_UNUSED,
  940. const ENCODING *enc ATTR_UNUSED)
  941. {
  942. switch (tok) {
  943. case XML_TOK_PROLOG_S:
  944. return XML_ROLE_NONE;
  945. case XML_TOK_CLOSE_PAREN:
  946. case XML_TOK_CLOSE_PAREN_ASTERISK:
  947. state->handler = declClose;
  948. return XML_ROLE_GROUP_CLOSE_REP;
  949. case XML_TOK_OR:
  950. state->handler = element4;
  951. return XML_ROLE_NONE;
  952. }
  953. return common(state, tok);
  954. }
  955. static
  956. int element4(PROLOG_STATE *state,
  957. int tok,
  958. const char *ptr ATTR_UNUSED,
  959. const char *end ATTR_UNUSED,
  960. const ENCODING *enc ATTR_UNUSED)
  961. {
  962. switch (tok) {
  963. case XML_TOK_PROLOG_S:
  964. return XML_ROLE_NONE;
  965. case XML_TOK_NAME:
  966. case XML_TOK_PREFIXED_NAME:
  967. state->handler = element5;
  968. return XML_ROLE_CONTENT_ELEMENT;
  969. }
  970. return common(state, tok);
  971. }
  972. static
  973. int element5(PROLOG_STATE *state,
  974. int tok,
  975. const char *ptr ATTR_UNUSED,
  976. const char *end ATTR_UNUSED,
  977. const ENCODING *enc ATTR_UNUSED)
  978. {
  979. switch (tok) {
  980. case XML_TOK_PROLOG_S:
  981. return XML_ROLE_NONE;
  982. case XML_TOK_CLOSE_PAREN_ASTERISK:
  983. state->handler = declClose;
  984. return XML_ROLE_GROUP_CLOSE_REP;
  985. case XML_TOK_OR:
  986. state->handler = element4;
  987. return XML_ROLE_NONE;
  988. }
  989. return common(state, tok);
  990. }
  991. static
  992. int element6(PROLOG_STATE *state,
  993. int tok,
  994. const char *ptr ATTR_UNUSED,
  995. const char *end ATTR_UNUSED,
  996. const ENCODING *enc ATTR_UNUSED)
  997. {
  998. switch (tok) {
  999. case XML_TOK_PROLOG_S:
  1000. return XML_ROLE_NONE;
  1001. case XML_TOK_OPEN_PAREN:
  1002. state->level += 1;
  1003. return XML_ROLE_GROUP_OPEN;
  1004. case XML_TOK_NAME:
  1005. case XML_TOK_PREFIXED_NAME:
  1006. state->handler = element7;
  1007. return XML_ROLE_CONTENT_ELEMENT;
  1008. case XML_TOK_NAME_QUESTION:
  1009. state->handler = element7;
  1010. return XML_ROLE_CONTENT_ELEMENT_OPT;
  1011. case XML_TOK_NAME_ASTERISK:
  1012. state->handler = element7;
  1013. return XML_ROLE_CONTENT_ELEMENT_REP;
  1014. case XML_TOK_NAME_PLUS:
  1015. state->handler = element7;
  1016. return XML_ROLE_CONTENT_ELEMENT_PLUS;
  1017. }
  1018. return common(state, tok);
  1019. }
  1020. static
  1021. int element7(PROLOG_STATE *state,
  1022. int tok,
  1023. const char *ptr ATTR_UNUSED,
  1024. const char *end ATTR_UNUSED,
  1025. const ENCODING *enc ATTR_UNUSED)
  1026. {
  1027. switch (tok) {
  1028. case XML_TOK_PROLOG_S:
  1029. return XML_ROLE_NONE;
  1030. case XML_TOK_CLOSE_PAREN:
  1031. state->level -= 1;
  1032. if (state->level == 0)
  1033. state->handler = declClose;
  1034. return XML_ROLE_GROUP_CLOSE;
  1035. case XML_TOK_CLOSE_PAREN_ASTERISK:
  1036. state->level -= 1;
  1037. if (state->level == 0)
  1038. state->handler = declClose;
  1039. return XML_ROLE_GROUP_CLOSE_REP;
  1040. case XML_TOK_CLOSE_PAREN_QUESTION:
  1041. state->level -= 1;
  1042. if (state->level == 0)
  1043. state->handler = declClose;
  1044. return XML_ROLE_GROUP_CLOSE_OPT;
  1045. case XML_TOK_CLOSE_PAREN_PLUS:
  1046. state->level -= 1;
  1047. if (state->level == 0)
  1048. state->handler = declClose;
  1049. return XML_ROLE_GROUP_CLOSE_PLUS;
  1050. case XML_TOK_COMMA:
  1051. state->handler = element6;
  1052. return XML_ROLE_GROUP_SEQUENCE;
  1053. case XML_TOK_OR:
  1054. state->handler = element6;
  1055. return XML_ROLE_GROUP_CHOICE;
  1056. }
  1057. return common(state, tok);
  1058. }
  1059. static
  1060. int condSect0(PROLOG_STATE *state,
  1061. int tok,
  1062. const char *ptr,
  1063. const char *end,
  1064. const ENCODING *enc)
  1065. {
  1066. switch (tok) {
  1067. case XML_TOK_PROLOG_S:
  1068. return XML_ROLE_NONE;
  1069. case XML_TOK_NAME:
  1070. if (XmlNameMatchesAscii(enc, ptr, end, KW_INCLUDE)) {
  1071. state->handler = condSect1;
  1072. return XML_ROLE_NONE;
  1073. }
  1074. if (XmlNameMatchesAscii(enc, ptr, end, KW_IGNORE)) {
  1075. state->handler = condSect2;
  1076. return XML_ROLE_NONE;
  1077. }
  1078. break;
  1079. }
  1080. return common(state, tok);
  1081. }
  1082. static int
  1083. condSect1(PROLOG_STATE * state,
  1084. int tok,
  1085. const char * ptr ATTR_UNUSED,
  1086. const char * end ATTR_UNUSED,
  1087. const ENCODING * enc ATTR_UNUSED) {
  1088. switch (tok) {
  1089. case XML_TOK_PROLOG_S:
  1090. return XML_ROLE_NONE;
  1091. case XML_TOK_OPEN_BRACKET:
  1092. state->handler = externalSubset1;
  1093. state->includeLevel += 1;
  1094. return XML_ROLE_NONE;
  1095. }
  1096. return common(state, tok);
  1097. }
  1098. static int
  1099. condSect2(PROLOG_STATE * state,
  1100. int tok,
  1101. const char * ptr ATTR_UNUSED,
  1102. const char * end ATTR_UNUSED,
  1103. const ENCODING * enc ATTR_UNUSED) {
  1104. switch (tok) {
  1105. case XML_TOK_PROLOG_S:
  1106. return XML_ROLE_NONE;
  1107. case XML_TOK_OPEN_BRACKET:
  1108. state->handler = externalSubset1;
  1109. return XML_ROLE_IGNORE_SECT;
  1110. }
  1111. return common(state, tok);
  1112. }
  1113. static
  1114. int declClose(PROLOG_STATE *state,
  1115. int tok,
  1116. const char *ptr ATTR_UNUSED,
  1117. const char *end ATTR_UNUSED,
  1118. const ENCODING *enc ATTR_UNUSED)
  1119. {
  1120. switch (tok) {
  1121. case XML_TOK_PROLOG_S:
  1122. return XML_ROLE_NONE;
  1123. case XML_TOK_DECL_CLOSE:
  1124. setTopLevel(state);
  1125. return XML_ROLE_NONE;
  1126. }
  1127. return common(state, tok);
  1128. }
  1129. #if 0
  1130. static
  1131. int ignore(PROLOG_STATE *state,
  1132. int tok,
  1133. const char *ptr,
  1134. const char *end,
  1135. const ENCODING *enc)
  1136. {
  1137. switch (tok) {
  1138. case XML_TOK_DECL_CLOSE:
  1139. state->handler = internalSubset;
  1140. return 0;
  1141. default:
  1142. return XML_ROLE_NONE;
  1143. }
  1144. return common(state, tok);
  1145. }
  1146. #endif
  1147. static
  1148. int error(PROLOG_STATE *state ATTR_UNUSED,
  1149. int tok ATTR_UNUSED,
  1150. const char *ptr ATTR_UNUSED,
  1151. const char *end ATTR_UNUSED,
  1152. const ENCODING *enc ATTR_UNUSED)
  1153. {
  1154. return XML_ROLE_NONE;
  1155. }
  1156. static
  1157. int common(PROLOG_STATE *state, int tok ATTR_UNUSED)
  1158. {
  1159. if (!state->documentEntity && tok == XML_TOK_PARAM_ENTITY_REF)
  1160. return XML_ROLE_INNER_PARAM_ENTITY_REF;
  1161. state->handler = error;
  1162. return XML_ROLE_ERROR;
  1163. }
  1164. void
  1165. xmlrpc_XmlPrologStateInit(PROLOG_STATE * const state) {
  1166. state->handler = prolog0;
  1167. state->documentEntity = 1;
  1168. state->includeLevel = 0;
  1169. }
  1170. void
  1171. xmlrpc_XmlPrologStateInitExternalEntity(PROLOG_STATE * const state) {
  1172. state->handler = externalSubset0;
  1173. state->documentEntity = 0;
  1174. state->includeLevel = 0;
  1175. }