xmlrole.c 28 KB

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