Back to index

lightning-sunbird  0.9+nobinonly
xmlrole.c
Go to the documentation of this file.
00001 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
00002    See the file COPYING for copying permission.
00003 */
00004 
00005 #ifdef COMPILED_FROM_DSP
00006 #include "winconfig.h"
00007 #elif defined(MACOS_CLASSIC)
00008 #include "macconfig.h"
00009 #else
00010 #ifdef HAVE_EXPAT_CONFIG_H
00011 #include <expat_config.h>
00012 #endif
00013 #endif /* ndef COMPILED_FROM_DSP */
00014 
00015 #include "internal.h"
00016 #include "xmlrole.h"
00017 #include "ascii.h"
00018 
00019 /* Doesn't check:
00020 
00021  that ,| are not mixed in a model group
00022  content of literals
00023 
00024 */
00025 
00026 static const char KW_ANY[] = {
00027     ASCII_A, ASCII_N, ASCII_Y, '\0' };
00028 static const char KW_ATTLIST[] = {
00029     ASCII_A, ASCII_T, ASCII_T, ASCII_L, ASCII_I, ASCII_S, ASCII_T, '\0' };
00030 static const char KW_CDATA[] = {
00031     ASCII_C, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0' };
00032 static const char KW_DOCTYPE[] = {
00033     ASCII_D, ASCII_O, ASCII_C, ASCII_T, ASCII_Y, ASCII_P, ASCII_E, '\0' };
00034 static const char KW_ELEMENT[] = {
00035     ASCII_E, ASCII_L, ASCII_E, ASCII_M, ASCII_E, ASCII_N, ASCII_T, '\0' };
00036 static const char KW_EMPTY[] = {
00037     ASCII_E, ASCII_M, ASCII_P, ASCII_T, ASCII_Y, '\0' };
00038 static const char KW_ENTITIES[] = {
00039     ASCII_E, ASCII_N, ASCII_T, ASCII_I, ASCII_T, ASCII_I, ASCII_E, ASCII_S,
00040     '\0' };
00041 static const char KW_ENTITY[] = {
00042     ASCII_E, ASCII_N, ASCII_T, ASCII_I, ASCII_T, ASCII_Y, '\0' };
00043 static const char KW_FIXED[] = {
00044     ASCII_F, ASCII_I, ASCII_X, ASCII_E, ASCII_D, '\0' };
00045 static const char KW_ID[] = {
00046     ASCII_I, ASCII_D, '\0' };
00047 static const char KW_IDREF[] = {
00048     ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, '\0' };
00049 static const char KW_IDREFS[] = {
00050     ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, ASCII_S, '\0' };
00051 static const char KW_IGNORE[] = {
00052     ASCII_I, ASCII_G, ASCII_N, ASCII_O, ASCII_R, ASCII_E, '\0' };
00053 static const char KW_IMPLIED[] = {
00054     ASCII_I, ASCII_M, ASCII_P, ASCII_L, ASCII_I, ASCII_E, ASCII_D, '\0' };
00055 static const char KW_INCLUDE[] = {
00056     ASCII_I, ASCII_N, ASCII_C, ASCII_L, ASCII_U, ASCII_D, ASCII_E, '\0' };
00057 static const char KW_NDATA[] = {
00058     ASCII_N, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0' };
00059 static const char KW_NMTOKEN[] = {
00060     ASCII_N, ASCII_M, ASCII_T, ASCII_O, ASCII_K, ASCII_E, ASCII_N, '\0' };
00061 static const char KW_NMTOKENS[] = {
00062     ASCII_N, ASCII_M, ASCII_T, ASCII_O, ASCII_K, ASCII_E, ASCII_N, ASCII_S,
00063     '\0' };
00064 static const char KW_NOTATION[] =
00065     { ASCII_N, ASCII_O, ASCII_T, ASCII_A, ASCII_T, ASCII_I, ASCII_O, ASCII_N,
00066       '\0' };
00067 static const char KW_PCDATA[] = {
00068     ASCII_P, ASCII_C, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0' };
00069 static const char KW_PUBLIC[] = {
00070     ASCII_P, ASCII_U, ASCII_B, ASCII_L, ASCII_I, ASCII_C, '\0' };
00071 static const char KW_REQUIRED[] = {
00072     ASCII_R, ASCII_E, ASCII_Q, ASCII_U, ASCII_I, ASCII_R, ASCII_E, ASCII_D,
00073     '\0' };
00074 static const char KW_SYSTEM[] = {
00075     ASCII_S, ASCII_Y, ASCII_S, ASCII_T, ASCII_E, ASCII_M, '\0' };
00076 
00077 #ifndef MIN_BYTES_PER_CHAR
00078 #define MIN_BYTES_PER_CHAR(enc) ((enc)->minBytesPerChar)
00079 #endif
00080 
00081 #ifdef XML_DTD
00082 #define setTopLevel(state) \
00083   ((state)->handler = ((state)->documentEntity \
00084                        ? internalSubset \
00085                        : externalSubset1))
00086 #else /* not XML_DTD */
00087 #define setTopLevel(state) ((state)->handler = internalSubset)
00088 #endif /* not XML_DTD */
00089 
00090 typedef int PTRCALL PROLOG_HANDLER(PROLOG_STATE *state,
00091                                    int tok,
00092                                    const char *ptr,
00093                                    const char *end,
00094                                    const ENCODING *enc);
00095 
00096 static PROLOG_HANDLER
00097   prolog0, prolog1, prolog2,
00098   doctype0, doctype1, doctype2, doctype3, doctype4, doctype5,
00099   internalSubset,
00100   entity0, entity1, entity2, entity3, entity4, entity5, entity6,
00101   entity7, entity8, entity9, entity10,
00102   notation0, notation1, notation2, notation3, notation4,
00103   attlist0, attlist1, attlist2, attlist3, attlist4, attlist5, attlist6,
00104   attlist7, attlist8, attlist9,
00105   element0, element1, element2, element3, element4, element5, element6,
00106   element7,
00107 #ifdef XML_DTD
00108   externalSubset0, externalSubset1,
00109   condSect0, condSect1, condSect2,
00110 #endif /* XML_DTD */
00111   declClose,
00112   error;
00113 
00114 static int FASTCALL common(PROLOG_STATE *state, int tok);
00115 
00116 static int PTRCALL
00117 prolog0(PROLOG_STATE *state,
00118         int tok,
00119         const char *ptr,
00120         const char *end,
00121         const ENCODING *enc)
00122 {
00123   switch (tok) {
00124   case XML_TOK_PROLOG_S:
00125     state->handler = prolog1;
00126     return XML_ROLE_NONE;
00127   case XML_TOK_XML_DECL:
00128     state->handler = prolog1;
00129     return XML_ROLE_XML_DECL;
00130   case XML_TOK_PI:
00131     state->handler = prolog1;
00132     return XML_ROLE_PI;
00133   case XML_TOK_COMMENT:
00134     state->handler = prolog1;
00135     return XML_ROLE_COMMENT;
00136   case XML_TOK_BOM:
00137     return XML_ROLE_NONE;
00138   case XML_TOK_DECL_OPEN:
00139     if (!XmlNameMatchesAscii(enc,
00140                              ptr + 2 * MIN_BYTES_PER_CHAR(enc),
00141                              end,
00142                              KW_DOCTYPE))
00143       break;
00144     state->handler = doctype0;
00145     return XML_ROLE_DOCTYPE_NONE;
00146   case XML_TOK_INSTANCE_START:
00147     state->handler = error;
00148     return XML_ROLE_INSTANCE_START;
00149   }
00150   return common(state, tok);
00151 }
00152 
00153 static int PTRCALL
00154 prolog1(PROLOG_STATE *state,
00155         int tok,
00156         const char *ptr,
00157         const char *end,
00158         const ENCODING *enc)
00159 {
00160   switch (tok) {
00161   case XML_TOK_PROLOG_S:
00162     return XML_ROLE_NONE;
00163   case XML_TOK_PI:
00164     return XML_ROLE_PI;
00165   case XML_TOK_COMMENT:
00166     return XML_ROLE_COMMENT;
00167   case XML_TOK_BOM:
00168     return XML_ROLE_NONE;
00169   case XML_TOK_DECL_OPEN:
00170     if (!XmlNameMatchesAscii(enc,
00171                              ptr + 2 * MIN_BYTES_PER_CHAR(enc),
00172                              end,
00173                              KW_DOCTYPE))
00174       break;
00175     state->handler = doctype0;
00176     return XML_ROLE_DOCTYPE_NONE;
00177   case XML_TOK_INSTANCE_START:
00178     state->handler = error;
00179     return XML_ROLE_INSTANCE_START;
00180   }
00181   return common(state, tok);
00182 }
00183 
00184 static int PTRCALL
00185 prolog2(PROLOG_STATE *state,
00186         int tok,
00187         const char *ptr,
00188         const char *end,
00189         const ENCODING *enc)
00190 {
00191   switch (tok) {
00192   case XML_TOK_PROLOG_S:
00193     return XML_ROLE_NONE;
00194   case XML_TOK_PI:
00195     return XML_ROLE_PI;
00196   case XML_TOK_COMMENT:
00197     return XML_ROLE_COMMENT;
00198   case XML_TOK_INSTANCE_START:
00199     state->handler = error;
00200     return XML_ROLE_INSTANCE_START;
00201   }
00202   return common(state, tok);
00203 }
00204 
00205 static int PTRCALL
00206 doctype0(PROLOG_STATE *state,
00207          int tok,
00208          const char *ptr,
00209          const char *end,
00210          const ENCODING *enc)
00211 {
00212   switch (tok) {
00213   case XML_TOK_PROLOG_S:
00214     return XML_ROLE_DOCTYPE_NONE;
00215   case XML_TOK_NAME:
00216   case XML_TOK_PREFIXED_NAME:
00217     state->handler = doctype1;
00218     return XML_ROLE_DOCTYPE_NAME;
00219   }
00220   return common(state, tok);
00221 }
00222 
00223 static int PTRCALL
00224 doctype1(PROLOG_STATE *state,
00225          int tok,
00226          const char *ptr,
00227          const char *end,
00228          const ENCODING *enc)
00229 {
00230   switch (tok) {
00231   case XML_TOK_PROLOG_S:
00232     return XML_ROLE_DOCTYPE_NONE;
00233   case XML_TOK_OPEN_BRACKET:
00234     state->handler = internalSubset;
00235     return XML_ROLE_DOCTYPE_INTERNAL_SUBSET;
00236   case XML_TOK_DECL_CLOSE:
00237     state->handler = prolog2;
00238     return XML_ROLE_DOCTYPE_CLOSE;
00239   case XML_TOK_NAME:
00240     if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
00241       state->handler = doctype3;
00242       return XML_ROLE_DOCTYPE_NONE;
00243     }
00244     if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
00245       state->handler = doctype2;
00246       return XML_ROLE_DOCTYPE_NONE;
00247     }
00248     break;
00249   }
00250   return common(state, tok);
00251 }
00252 
00253 static int PTRCALL
00254 doctype2(PROLOG_STATE *state,
00255          int tok,
00256          const char *ptr,
00257          const char *end,
00258          const ENCODING *enc)
00259 {
00260   switch (tok) {
00261   case XML_TOK_PROLOG_S:
00262     return XML_ROLE_DOCTYPE_NONE;
00263   case XML_TOK_LITERAL:
00264     state->handler = doctype3;
00265     return XML_ROLE_DOCTYPE_PUBLIC_ID;
00266   }
00267   return common(state, tok);
00268 }
00269 
00270 static int PTRCALL
00271 doctype3(PROLOG_STATE *state,
00272          int tok,
00273          const char *ptr,
00274          const char *end,
00275          const ENCODING *enc)
00276 {
00277   switch (tok) {
00278   case XML_TOK_PROLOG_S:
00279     return XML_ROLE_DOCTYPE_NONE;
00280   case XML_TOK_LITERAL:
00281     state->handler = doctype4;
00282     return XML_ROLE_DOCTYPE_SYSTEM_ID;
00283   }
00284   return common(state, tok);
00285 }
00286 
00287 static int PTRCALL
00288 doctype4(PROLOG_STATE *state,
00289          int tok,
00290          const char *ptr,
00291          const char *end,
00292          const ENCODING *enc)
00293 {
00294   switch (tok) {
00295   case XML_TOK_PROLOG_S:
00296     return XML_ROLE_DOCTYPE_NONE;
00297   case XML_TOK_OPEN_BRACKET:
00298     state->handler = internalSubset;
00299     return XML_ROLE_DOCTYPE_INTERNAL_SUBSET;
00300   case XML_TOK_DECL_CLOSE:
00301     state->handler = prolog2;
00302     return XML_ROLE_DOCTYPE_CLOSE;
00303   }
00304   return common(state, tok);
00305 }
00306 
00307 static int PTRCALL
00308 doctype5(PROLOG_STATE *state,
00309          int tok,
00310          const char *ptr,
00311          const char *end,
00312          const ENCODING *enc)
00313 {
00314   switch (tok) {
00315   case XML_TOK_PROLOG_S:
00316     return XML_ROLE_DOCTYPE_NONE;
00317   case XML_TOK_DECL_CLOSE:
00318     state->handler = prolog2;
00319     return XML_ROLE_DOCTYPE_CLOSE;
00320   }
00321   return common(state, tok);
00322 }
00323 
00324 static int PTRCALL
00325 internalSubset(PROLOG_STATE *state,
00326                int tok,
00327                const char *ptr,
00328                const char *end,
00329                const ENCODING *enc)
00330 {
00331   switch (tok) {
00332   case XML_TOK_PROLOG_S:
00333     return XML_ROLE_NONE;
00334   case XML_TOK_DECL_OPEN:
00335     if (XmlNameMatchesAscii(enc,
00336                             ptr + 2 * MIN_BYTES_PER_CHAR(enc),
00337                             end,
00338                             KW_ENTITY)) {
00339       state->handler = entity0;
00340       return XML_ROLE_ENTITY_NONE;
00341     }
00342     if (XmlNameMatchesAscii(enc,
00343                             ptr + 2 * MIN_BYTES_PER_CHAR(enc),
00344                             end,
00345                             KW_ATTLIST)) {
00346       state->handler = attlist0;
00347       return XML_ROLE_ATTLIST_NONE;
00348     }
00349     if (XmlNameMatchesAscii(enc,
00350                             ptr + 2 * MIN_BYTES_PER_CHAR(enc),
00351                             end,
00352                             KW_ELEMENT)) {
00353       state->handler = element0;
00354       return XML_ROLE_ELEMENT_NONE;
00355     }
00356     if (XmlNameMatchesAscii(enc,
00357                             ptr + 2 * MIN_BYTES_PER_CHAR(enc),
00358                             end,
00359                             KW_NOTATION)) {
00360       state->handler = notation0;
00361       return XML_ROLE_NOTATION_NONE;
00362     }
00363     break;
00364   case XML_TOK_PI:
00365     return XML_ROLE_PI;
00366   case XML_TOK_COMMENT:
00367     return XML_ROLE_COMMENT;
00368   case XML_TOK_PARAM_ENTITY_REF:
00369     return XML_ROLE_PARAM_ENTITY_REF;
00370   case XML_TOK_CLOSE_BRACKET:
00371     state->handler = doctype5;
00372     return XML_ROLE_DOCTYPE_NONE;
00373   }
00374   return common(state, tok);
00375 }
00376 
00377 #ifdef XML_DTD
00378 
00379 static int PTRCALL
00380 externalSubset0(PROLOG_STATE *state,
00381                 int tok,
00382                 const char *ptr,
00383                 const char *end,
00384                 const ENCODING *enc)
00385 {
00386   state->handler = externalSubset1;
00387   if (tok == XML_TOK_XML_DECL)
00388     return XML_ROLE_TEXT_DECL;
00389   return externalSubset1(state, tok, ptr, end, enc);
00390 }
00391 
00392 static int PTRCALL
00393 externalSubset1(PROLOG_STATE *state,
00394                 int tok,
00395                 const char *ptr,
00396                 const char *end,
00397                 const ENCODING *enc)
00398 {
00399   switch (tok) {
00400   case XML_TOK_COND_SECT_OPEN:
00401     state->handler = condSect0;
00402     return XML_ROLE_NONE;
00403   case XML_TOK_COND_SECT_CLOSE:
00404     if (state->includeLevel == 0)
00405       break;
00406     state->includeLevel -= 1;
00407     return XML_ROLE_NONE;
00408   case XML_TOK_PROLOG_S:
00409     return XML_ROLE_NONE;
00410   case XML_TOK_CLOSE_BRACKET:
00411     break;
00412   case XML_TOK_NONE:
00413     if (state->includeLevel)
00414       break;
00415     return XML_ROLE_NONE;
00416   default:
00417     return internalSubset(state, tok, ptr, end, enc);
00418   }
00419   return common(state, tok);
00420 }
00421 
00422 #endif /* XML_DTD */
00423 
00424 static int PTRCALL
00425 entity0(PROLOG_STATE *state,
00426         int tok,
00427         const char *ptr,
00428         const char *end,
00429         const ENCODING *enc)
00430 {
00431   switch (tok) {
00432   case XML_TOK_PROLOG_S:
00433     return XML_ROLE_ENTITY_NONE;
00434   case XML_TOK_PERCENT:
00435     state->handler = entity1;
00436     return XML_ROLE_ENTITY_NONE;
00437   case XML_TOK_NAME:
00438     state->handler = entity2;
00439     return XML_ROLE_GENERAL_ENTITY_NAME;
00440   }
00441   return common(state, tok);
00442 }
00443 
00444 static int PTRCALL
00445 entity1(PROLOG_STATE *state,
00446         int tok,
00447         const char *ptr,
00448         const char *end,
00449         const ENCODING *enc)
00450 {
00451   switch (tok) {
00452   case XML_TOK_PROLOG_S:
00453     return XML_ROLE_ENTITY_NONE;
00454   case XML_TOK_NAME:
00455     state->handler = entity7;
00456     return XML_ROLE_PARAM_ENTITY_NAME;
00457   }
00458   return common(state, tok);
00459 }
00460 
00461 static int PTRCALL
00462 entity2(PROLOG_STATE *state,
00463         int tok,
00464         const char *ptr,
00465         const char *end,
00466         const ENCODING *enc)
00467 {
00468   switch (tok) {
00469   case XML_TOK_PROLOG_S:
00470     return XML_ROLE_ENTITY_NONE;
00471   case XML_TOK_NAME:
00472     if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
00473       state->handler = entity4;
00474       return XML_ROLE_ENTITY_NONE;
00475     }
00476     if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
00477       state->handler = entity3;
00478       return XML_ROLE_ENTITY_NONE;
00479     }
00480     break;
00481   case XML_TOK_LITERAL:
00482     state->handler = declClose;
00483     state->role_none = XML_ROLE_ENTITY_NONE;
00484     return XML_ROLE_ENTITY_VALUE;
00485   }
00486   return common(state, tok);
00487 }
00488 
00489 static int PTRCALL
00490 entity3(PROLOG_STATE *state,
00491         int tok,
00492         const char *ptr,
00493         const char *end,
00494         const ENCODING *enc)
00495 {
00496   switch (tok) {
00497   case XML_TOK_PROLOG_S:
00498     return XML_ROLE_ENTITY_NONE;
00499   case XML_TOK_LITERAL:
00500     state->handler = entity4;
00501     return XML_ROLE_ENTITY_PUBLIC_ID;
00502   }
00503   return common(state, tok);
00504 }
00505 
00506 static int PTRCALL
00507 entity4(PROLOG_STATE *state,
00508         int tok,
00509         const char *ptr,
00510         const char *end,
00511         const ENCODING *enc)
00512 {
00513   switch (tok) {
00514   case XML_TOK_PROLOG_S:
00515     return XML_ROLE_ENTITY_NONE;
00516   case XML_TOK_LITERAL:
00517     state->handler = entity5;
00518     return XML_ROLE_ENTITY_SYSTEM_ID;
00519   }
00520   return common(state, tok);
00521 }
00522 
00523 static int PTRCALL
00524 entity5(PROLOG_STATE *state,
00525         int tok,
00526         const char *ptr,
00527         const char *end,
00528         const ENCODING *enc)
00529 {
00530   switch (tok) {
00531   case XML_TOK_PROLOG_S:
00532     return XML_ROLE_ENTITY_NONE;
00533   case XML_TOK_DECL_CLOSE:
00534     setTopLevel(state);
00535     return XML_ROLE_ENTITY_COMPLETE;
00536   case XML_TOK_NAME:
00537     if (XmlNameMatchesAscii(enc, ptr, end, KW_NDATA)) {
00538       state->handler = entity6;
00539       return XML_ROLE_ENTITY_NONE;
00540     }
00541     break;
00542   }
00543   return common(state, tok);
00544 }
00545 
00546 static int PTRCALL
00547 entity6(PROLOG_STATE *state,
00548         int tok,
00549         const char *ptr,
00550         const char *end,
00551         const ENCODING *enc)
00552 {
00553   switch (tok) {
00554   case XML_TOK_PROLOG_S:
00555     return XML_ROLE_ENTITY_NONE;
00556   case XML_TOK_NAME:
00557     state->handler = declClose;
00558     state->role_none = XML_ROLE_ENTITY_NONE;
00559     return XML_ROLE_ENTITY_NOTATION_NAME;
00560   }
00561   return common(state, tok);
00562 }
00563 
00564 static int PTRCALL
00565 entity7(PROLOG_STATE *state,
00566         int tok,
00567         const char *ptr,
00568         const char *end,
00569         const ENCODING *enc)
00570 {
00571   switch (tok) {
00572   case XML_TOK_PROLOG_S:
00573     return XML_ROLE_ENTITY_NONE;
00574   case XML_TOK_NAME:
00575     if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
00576       state->handler = entity9;
00577       return XML_ROLE_ENTITY_NONE;
00578     }
00579     if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
00580       state->handler = entity8;
00581       return XML_ROLE_ENTITY_NONE;
00582     }
00583     break;
00584   case XML_TOK_LITERAL:
00585     state->handler = declClose;
00586     state->role_none = XML_ROLE_ENTITY_NONE;
00587     return XML_ROLE_ENTITY_VALUE;
00588   }
00589   return common(state, tok);
00590 }
00591 
00592 static int PTRCALL
00593 entity8(PROLOG_STATE *state,
00594         int tok,
00595         const char *ptr,
00596         const char *end,
00597         const ENCODING *enc)
00598 {
00599   switch (tok) {
00600   case XML_TOK_PROLOG_S:
00601     return XML_ROLE_ENTITY_NONE;
00602   case XML_TOK_LITERAL:
00603     state->handler = entity9;
00604     return XML_ROLE_ENTITY_PUBLIC_ID;
00605   }
00606   return common(state, tok);
00607 }
00608 
00609 static int PTRCALL
00610 entity9(PROLOG_STATE *state,
00611         int tok,
00612         const char *ptr,
00613         const char *end,
00614         const ENCODING *enc)
00615 {
00616   switch (tok) {
00617   case XML_TOK_PROLOG_S:
00618     return XML_ROLE_ENTITY_NONE;
00619   case XML_TOK_LITERAL:
00620     state->handler = entity10;
00621     return XML_ROLE_ENTITY_SYSTEM_ID;
00622   }
00623   return common(state, tok);
00624 }
00625 
00626 static int PTRCALL
00627 entity10(PROLOG_STATE *state,
00628          int tok,
00629          const char *ptr,
00630          const char *end,
00631          const ENCODING *enc)
00632 {
00633   switch (tok) {
00634   case XML_TOK_PROLOG_S:
00635     return XML_ROLE_ENTITY_NONE;
00636   case XML_TOK_DECL_CLOSE:
00637     setTopLevel(state);
00638     return XML_ROLE_ENTITY_COMPLETE;
00639   }
00640   return common(state, tok);
00641 }
00642 
00643 static int PTRCALL
00644 notation0(PROLOG_STATE *state,
00645           int tok,
00646           const char *ptr,
00647           const char *end,
00648           const ENCODING *enc)
00649 {
00650   switch (tok) {
00651   case XML_TOK_PROLOG_S:
00652     return XML_ROLE_NOTATION_NONE;
00653   case XML_TOK_NAME:
00654     state->handler = notation1;
00655     return XML_ROLE_NOTATION_NAME;
00656   }
00657   return common(state, tok);
00658 }
00659 
00660 static int PTRCALL
00661 notation1(PROLOG_STATE *state,
00662           int tok,
00663           const char *ptr,
00664           const char *end,
00665           const ENCODING *enc)
00666 {
00667   switch (tok) {
00668   case XML_TOK_PROLOG_S:
00669     return XML_ROLE_NOTATION_NONE;
00670   case XML_TOK_NAME:
00671     if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
00672       state->handler = notation3;
00673       return XML_ROLE_NOTATION_NONE;
00674     }
00675     if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
00676       state->handler = notation2;
00677       return XML_ROLE_NOTATION_NONE;
00678     }
00679     break;
00680   }
00681   return common(state, tok);
00682 }
00683 
00684 static int PTRCALL
00685 notation2(PROLOG_STATE *state,
00686           int tok,
00687           const char *ptr,
00688           const char *end,
00689           const ENCODING *enc)
00690 {
00691   switch (tok) {
00692   case XML_TOK_PROLOG_S:
00693     return XML_ROLE_NOTATION_NONE;
00694   case XML_TOK_LITERAL:
00695     state->handler = notation4;
00696     return XML_ROLE_NOTATION_PUBLIC_ID;
00697   }
00698   return common(state, tok);
00699 }
00700 
00701 static int PTRCALL
00702 notation3(PROLOG_STATE *state,
00703           int tok,
00704           const char *ptr,
00705           const char *end,
00706           const ENCODING *enc)
00707 {
00708   switch (tok) {
00709   case XML_TOK_PROLOG_S:
00710     return XML_ROLE_NOTATION_NONE;
00711   case XML_TOK_LITERAL:
00712     state->handler = declClose;
00713     state->role_none = XML_ROLE_NOTATION_NONE;
00714     return XML_ROLE_NOTATION_SYSTEM_ID;
00715   }
00716   return common(state, tok);
00717 }
00718 
00719 static int PTRCALL
00720 notation4(PROLOG_STATE *state,
00721           int tok,
00722           const char *ptr,
00723           const char *end,
00724           const ENCODING *enc)
00725 {
00726   switch (tok) {
00727   case XML_TOK_PROLOG_S:
00728     return XML_ROLE_NOTATION_NONE;
00729   case XML_TOK_LITERAL:
00730     state->handler = declClose;
00731     state->role_none = XML_ROLE_NOTATION_NONE;
00732     return XML_ROLE_NOTATION_SYSTEM_ID;
00733   case XML_TOK_DECL_CLOSE:
00734     setTopLevel(state);
00735     return XML_ROLE_NOTATION_NO_SYSTEM_ID;
00736   }
00737   return common(state, tok);
00738 }
00739 
00740 static int PTRCALL
00741 attlist0(PROLOG_STATE *state,
00742          int tok,
00743          const char *ptr,
00744          const char *end,
00745          const ENCODING *enc)
00746 {
00747   switch (tok) {
00748   case XML_TOK_PROLOG_S:
00749     return XML_ROLE_ATTLIST_NONE;
00750   case XML_TOK_NAME:
00751   case XML_TOK_PREFIXED_NAME:
00752     state->handler = attlist1;
00753     return XML_ROLE_ATTLIST_ELEMENT_NAME;
00754   }
00755   return common(state, tok);
00756 }
00757 
00758 static int PTRCALL
00759 attlist1(PROLOG_STATE *state,
00760          int tok,
00761          const char *ptr,
00762          const char *end,
00763          const ENCODING *enc)
00764 {
00765   switch (tok) {
00766   case XML_TOK_PROLOG_S:
00767     return XML_ROLE_ATTLIST_NONE;
00768   case XML_TOK_DECL_CLOSE:
00769     setTopLevel(state);
00770     return XML_ROLE_ATTLIST_NONE;
00771   case XML_TOK_NAME:
00772   case XML_TOK_PREFIXED_NAME:
00773     state->handler = attlist2;
00774     return XML_ROLE_ATTRIBUTE_NAME;
00775   }
00776   return common(state, tok);
00777 }
00778 
00779 static int PTRCALL
00780 attlist2(PROLOG_STATE *state,
00781          int tok,
00782          const char *ptr,
00783          const char *end,
00784          const ENCODING *enc)
00785 {
00786   switch (tok) {
00787   case XML_TOK_PROLOG_S:
00788     return XML_ROLE_ATTLIST_NONE;
00789   case XML_TOK_NAME:
00790     {
00791       static const char *types[] = {
00792         KW_CDATA,
00793         KW_ID,
00794         KW_IDREF,
00795         KW_IDREFS,
00796         KW_ENTITY,
00797         KW_ENTITIES,
00798         KW_NMTOKEN,
00799         KW_NMTOKENS,
00800       };
00801       int i;
00802       for (i = 0; i < (int)(sizeof(types)/sizeof(types[0])); i++)
00803         if (XmlNameMatchesAscii(enc, ptr, end, types[i])) {
00804           state->handler = attlist8;
00805           return XML_ROLE_ATTRIBUTE_TYPE_CDATA + i;
00806         }
00807     }
00808     if (XmlNameMatchesAscii(enc, ptr, end, KW_NOTATION)) {
00809       state->handler = attlist5;
00810       return XML_ROLE_ATTLIST_NONE;
00811     }
00812     break;
00813   case XML_TOK_OPEN_PAREN:
00814     state->handler = attlist3;
00815     return XML_ROLE_ATTLIST_NONE;
00816   }
00817   return common(state, tok);
00818 }
00819 
00820 static int PTRCALL
00821 attlist3(PROLOG_STATE *state,
00822          int tok,
00823          const char *ptr,
00824          const char *end,
00825          const ENCODING *enc)
00826 {
00827   switch (tok) {
00828   case XML_TOK_PROLOG_S:
00829     return XML_ROLE_ATTLIST_NONE;
00830   case XML_TOK_NMTOKEN:
00831   case XML_TOK_NAME:
00832   case XML_TOK_PREFIXED_NAME:
00833     state->handler = attlist4;
00834     return XML_ROLE_ATTRIBUTE_ENUM_VALUE;
00835   }
00836   return common(state, tok);
00837 }
00838 
00839 static int PTRCALL
00840 attlist4(PROLOG_STATE *state,
00841          int tok,
00842          const char *ptr,
00843          const char *end,
00844          const ENCODING *enc)
00845 {
00846   switch (tok) {
00847   case XML_TOK_PROLOG_S:
00848     return XML_ROLE_ATTLIST_NONE;
00849   case XML_TOK_CLOSE_PAREN:
00850     state->handler = attlist8;
00851     return XML_ROLE_ATTLIST_NONE;
00852   case XML_TOK_OR:
00853     state->handler = attlist3;
00854     return XML_ROLE_ATTLIST_NONE;
00855   }
00856   return common(state, tok);
00857 }
00858 
00859 static int PTRCALL
00860 attlist5(PROLOG_STATE *state,
00861          int tok,
00862          const char *ptr,
00863          const char *end,
00864          const ENCODING *enc)
00865 {
00866   switch (tok) {
00867   case XML_TOK_PROLOG_S:
00868     return XML_ROLE_ATTLIST_NONE;
00869   case XML_TOK_OPEN_PAREN:
00870     state->handler = attlist6;
00871     return XML_ROLE_ATTLIST_NONE;
00872   }
00873   return common(state, tok);
00874 }
00875 
00876 static int PTRCALL
00877 attlist6(PROLOG_STATE *state,
00878          int tok,
00879          const char *ptr,
00880          const char *end,
00881          const ENCODING *enc)
00882 {
00883   switch (tok) {
00884   case XML_TOK_PROLOG_S:
00885     return XML_ROLE_ATTLIST_NONE;
00886   case XML_TOK_NAME:
00887     state->handler = attlist7;
00888     return XML_ROLE_ATTRIBUTE_NOTATION_VALUE;
00889   }
00890   return common(state, tok);
00891 }
00892 
00893 static int PTRCALL
00894 attlist7(PROLOG_STATE *state,
00895          int tok,
00896          const char *ptr,
00897          const char *end,
00898          const ENCODING *enc)
00899 {
00900   switch (tok) {
00901   case XML_TOK_PROLOG_S:
00902     return XML_ROLE_ATTLIST_NONE;
00903   case XML_TOK_CLOSE_PAREN:
00904     state->handler = attlist8;
00905     return XML_ROLE_ATTLIST_NONE;
00906   case XML_TOK_OR:
00907     state->handler = attlist6;
00908     return XML_ROLE_ATTLIST_NONE;
00909   }
00910   return common(state, tok);
00911 }
00912 
00913 /* default value */
00914 static int PTRCALL
00915 attlist8(PROLOG_STATE *state,
00916          int tok,
00917          const char *ptr,
00918          const char *end,
00919          const ENCODING *enc)
00920 {
00921   switch (tok) {
00922   case XML_TOK_PROLOG_S:
00923     return XML_ROLE_ATTLIST_NONE;
00924   case XML_TOK_POUND_NAME:
00925     if (XmlNameMatchesAscii(enc,
00926                             ptr + MIN_BYTES_PER_CHAR(enc),
00927                             end,
00928                             KW_IMPLIED)) {
00929       state->handler = attlist1;
00930       return XML_ROLE_IMPLIED_ATTRIBUTE_VALUE;
00931     }
00932     if (XmlNameMatchesAscii(enc,
00933                             ptr + MIN_BYTES_PER_CHAR(enc),
00934                             end,
00935                             KW_REQUIRED)) {
00936       state->handler = attlist1;
00937       return XML_ROLE_REQUIRED_ATTRIBUTE_VALUE;
00938     }
00939     if (XmlNameMatchesAscii(enc,
00940                             ptr + MIN_BYTES_PER_CHAR(enc),
00941                             end,
00942                             KW_FIXED)) {
00943       state->handler = attlist9;
00944       return XML_ROLE_ATTLIST_NONE;
00945     }
00946     break;
00947   case XML_TOK_LITERAL:
00948     state->handler = attlist1;
00949     return XML_ROLE_DEFAULT_ATTRIBUTE_VALUE;
00950   }
00951   return common(state, tok);
00952 }
00953 
00954 static int PTRCALL
00955 attlist9(PROLOG_STATE *state,
00956          int tok,
00957          const char *ptr,
00958          const char *end,
00959          const ENCODING *enc)
00960 {
00961   switch (tok) {
00962   case XML_TOK_PROLOG_S:
00963     return XML_ROLE_ATTLIST_NONE;
00964   case XML_TOK_LITERAL:
00965     state->handler = attlist1;
00966     return XML_ROLE_FIXED_ATTRIBUTE_VALUE;
00967   }
00968   return common(state, tok);
00969 }
00970 
00971 static int PTRCALL
00972 element0(PROLOG_STATE *state,
00973          int tok,
00974          const char *ptr,
00975          const char *end,
00976          const ENCODING *enc)
00977 {
00978   switch (tok) {
00979   case XML_TOK_PROLOG_S:
00980     return XML_ROLE_ELEMENT_NONE;
00981   case XML_TOK_NAME:
00982   case XML_TOK_PREFIXED_NAME:
00983     state->handler = element1;
00984     return XML_ROLE_ELEMENT_NAME;
00985   }
00986   return common(state, tok);
00987 }
00988 
00989 static int PTRCALL
00990 element1(PROLOG_STATE *state,
00991          int tok,
00992          const char *ptr,
00993          const char *end,
00994          const ENCODING *enc)
00995 {
00996   switch (tok) {
00997   case XML_TOK_PROLOG_S:
00998     return XML_ROLE_ELEMENT_NONE;
00999   case XML_TOK_NAME:
01000     if (XmlNameMatchesAscii(enc, ptr, end, KW_EMPTY)) {
01001       state->handler = declClose;
01002       state->role_none = XML_ROLE_ELEMENT_NONE;
01003       return XML_ROLE_CONTENT_EMPTY;
01004     }
01005     if (XmlNameMatchesAscii(enc, ptr, end, KW_ANY)) {
01006       state->handler = declClose;
01007       state->role_none = XML_ROLE_ELEMENT_NONE;
01008       return XML_ROLE_CONTENT_ANY;
01009     }
01010     break;
01011   case XML_TOK_OPEN_PAREN:
01012     state->handler = element2;
01013     state->level = 1;
01014     return XML_ROLE_GROUP_OPEN;
01015   }
01016   return common(state, tok);
01017 }
01018 
01019 static int PTRCALL
01020 element2(PROLOG_STATE *state,
01021          int tok,
01022          const char *ptr,
01023          const char *end,
01024          const ENCODING *enc)
01025 {
01026   switch (tok) {
01027   case XML_TOK_PROLOG_S:
01028     return XML_ROLE_ELEMENT_NONE;
01029   case XML_TOK_POUND_NAME:
01030     if (XmlNameMatchesAscii(enc,
01031                             ptr + MIN_BYTES_PER_CHAR(enc),
01032                             end,
01033                             KW_PCDATA)) {
01034       state->handler = element3;
01035       return XML_ROLE_CONTENT_PCDATA;
01036     }
01037     break;
01038   case XML_TOK_OPEN_PAREN:
01039     state->level = 2;
01040     state->handler = element6;
01041     return XML_ROLE_GROUP_OPEN;
01042   case XML_TOK_NAME:
01043   case XML_TOK_PREFIXED_NAME:
01044     state->handler = element7;
01045     return XML_ROLE_CONTENT_ELEMENT;
01046   case XML_TOK_NAME_QUESTION:
01047     state->handler = element7;
01048     return XML_ROLE_CONTENT_ELEMENT_OPT;
01049   case XML_TOK_NAME_ASTERISK:
01050     state->handler = element7;
01051     return XML_ROLE_CONTENT_ELEMENT_REP;
01052   case XML_TOK_NAME_PLUS:
01053     state->handler = element7;
01054     return XML_ROLE_CONTENT_ELEMENT_PLUS;
01055   }
01056   return common(state, tok);
01057 }
01058 
01059 static int PTRCALL
01060 element3(PROLOG_STATE *state,
01061          int tok,
01062          const char *ptr,
01063          const char *end,
01064          const ENCODING *enc)
01065 {
01066   switch (tok) {
01067   case XML_TOK_PROLOG_S:
01068     return XML_ROLE_ELEMENT_NONE;
01069   case XML_TOK_CLOSE_PAREN:
01070     state->handler = declClose;
01071     state->role_none = XML_ROLE_ELEMENT_NONE;
01072     return XML_ROLE_GROUP_CLOSE;
01073   case XML_TOK_CLOSE_PAREN_ASTERISK:
01074     state->handler = declClose;
01075     state->role_none = XML_ROLE_ELEMENT_NONE;
01076     return XML_ROLE_GROUP_CLOSE_REP;
01077   case XML_TOK_OR:
01078     state->handler = element4;
01079     return XML_ROLE_ELEMENT_NONE;
01080   }
01081   return common(state, tok);
01082 }
01083 
01084 static int PTRCALL
01085 element4(PROLOG_STATE *state,
01086          int tok,
01087          const char *ptr,
01088          const char *end,
01089          const ENCODING *enc)
01090 {
01091   switch (tok) {
01092   case XML_TOK_PROLOG_S:
01093     return XML_ROLE_ELEMENT_NONE;
01094   case XML_TOK_NAME:
01095   case XML_TOK_PREFIXED_NAME:
01096     state->handler = element5;
01097     return XML_ROLE_CONTENT_ELEMENT;
01098   }
01099   return common(state, tok);
01100 }
01101 
01102 static int PTRCALL
01103 element5(PROLOG_STATE *state,
01104          int tok,
01105          const char *ptr,
01106          const char *end,
01107          const ENCODING *enc)
01108 {
01109   switch (tok) {
01110   case XML_TOK_PROLOG_S:
01111     return XML_ROLE_ELEMENT_NONE;
01112   case XML_TOK_CLOSE_PAREN_ASTERISK:
01113     state->handler = declClose;
01114     state->role_none = XML_ROLE_ELEMENT_NONE;
01115     return XML_ROLE_GROUP_CLOSE_REP;
01116   case XML_TOK_OR:
01117     state->handler = element4;
01118     return XML_ROLE_ELEMENT_NONE;
01119   }
01120   return common(state, tok);
01121 }
01122 
01123 static int PTRCALL
01124 element6(PROLOG_STATE *state,
01125          int tok,
01126          const char *ptr,
01127          const char *end,
01128          const ENCODING *enc)
01129 {
01130   switch (tok) {
01131   case XML_TOK_PROLOG_S:
01132     return XML_ROLE_ELEMENT_NONE;
01133   case XML_TOK_OPEN_PAREN:
01134     state->level += 1;
01135     return XML_ROLE_GROUP_OPEN;
01136   case XML_TOK_NAME:
01137   case XML_TOK_PREFIXED_NAME:
01138     state->handler = element7;
01139     return XML_ROLE_CONTENT_ELEMENT;
01140   case XML_TOK_NAME_QUESTION:
01141     state->handler = element7;
01142     return XML_ROLE_CONTENT_ELEMENT_OPT;
01143   case XML_TOK_NAME_ASTERISK:
01144     state->handler = element7;
01145     return XML_ROLE_CONTENT_ELEMENT_REP;
01146   case XML_TOK_NAME_PLUS:
01147     state->handler = element7;
01148     return XML_ROLE_CONTENT_ELEMENT_PLUS;
01149   }
01150   return common(state, tok);
01151 }
01152 
01153 static int PTRCALL
01154 element7(PROLOG_STATE *state,
01155          int tok,
01156          const char *ptr,
01157          const char *end,
01158          const ENCODING *enc)
01159 {
01160   switch (tok) {
01161   case XML_TOK_PROLOG_S:
01162     return XML_ROLE_ELEMENT_NONE;
01163   case XML_TOK_CLOSE_PAREN:
01164     state->level -= 1;
01165     if (state->level == 0) {
01166       state->handler = declClose;
01167       state->role_none = XML_ROLE_ELEMENT_NONE;
01168     }
01169     return XML_ROLE_GROUP_CLOSE;
01170   case XML_TOK_CLOSE_PAREN_ASTERISK:
01171     state->level -= 1;
01172     if (state->level == 0) {
01173       state->handler = declClose;
01174       state->role_none = XML_ROLE_ELEMENT_NONE;
01175     }
01176     return XML_ROLE_GROUP_CLOSE_REP;
01177   case XML_TOK_CLOSE_PAREN_QUESTION:
01178     state->level -= 1;
01179     if (state->level == 0) {
01180       state->handler = declClose;
01181       state->role_none = XML_ROLE_ELEMENT_NONE;
01182     }
01183     return XML_ROLE_GROUP_CLOSE_OPT;
01184   case XML_TOK_CLOSE_PAREN_PLUS:
01185     state->level -= 1;
01186     if (state->level == 0) {
01187       state->handler = declClose;
01188       state->role_none = XML_ROLE_ELEMENT_NONE;
01189     }
01190     return XML_ROLE_GROUP_CLOSE_PLUS;
01191   case XML_TOK_COMMA:
01192     state->handler = element6;
01193     return XML_ROLE_GROUP_SEQUENCE;
01194   case XML_TOK_OR:
01195     state->handler = element6;
01196     return XML_ROLE_GROUP_CHOICE;
01197   }
01198   return common(state, tok);
01199 }
01200 
01201 #ifdef XML_DTD
01202 
01203 static int PTRCALL
01204 condSect0(PROLOG_STATE *state,
01205           int tok,
01206           const char *ptr,
01207           const char *end,
01208           const ENCODING *enc)
01209 {
01210   switch (tok) {
01211   case XML_TOK_PROLOG_S:
01212     return XML_ROLE_NONE;
01213   case XML_TOK_NAME:
01214     if (XmlNameMatchesAscii(enc, ptr, end, KW_INCLUDE)) {
01215       state->handler = condSect1;
01216       return XML_ROLE_NONE;
01217     }
01218     if (XmlNameMatchesAscii(enc, ptr, end, KW_IGNORE)) {
01219       state->handler = condSect2;
01220       return XML_ROLE_NONE;
01221     }
01222     break;
01223   }
01224   return common(state, tok);
01225 }
01226 
01227 static int PTRCALL
01228 condSect1(PROLOG_STATE *state,
01229           int tok,
01230           const char *ptr,
01231           const char *end,
01232           const ENCODING *enc)
01233 {
01234   switch (tok) {
01235   case XML_TOK_PROLOG_S:
01236     return XML_ROLE_NONE;
01237   case XML_TOK_OPEN_BRACKET:
01238     state->handler = externalSubset1;
01239     state->includeLevel += 1;
01240     return XML_ROLE_NONE;
01241   }
01242   return common(state, tok);
01243 }
01244 
01245 static int PTRCALL
01246 condSect2(PROLOG_STATE *state,
01247           int tok,
01248           const char *ptr,
01249           const char *end,
01250           const ENCODING *enc)
01251 {
01252   switch (tok) {
01253   case XML_TOK_PROLOG_S:
01254     return XML_ROLE_NONE;
01255   case XML_TOK_OPEN_BRACKET:
01256     state->handler = externalSubset1;
01257     return XML_ROLE_IGNORE_SECT;
01258   }
01259   return common(state, tok);
01260 }
01261 
01262 #endif /* XML_DTD */
01263 
01264 static int PTRCALL
01265 declClose(PROLOG_STATE *state,
01266           int tok,
01267           const char *ptr,
01268           const char *end,
01269           const ENCODING *enc)
01270 {
01271   switch (tok) {
01272   case XML_TOK_PROLOG_S:
01273     return state->role_none;
01274   case XML_TOK_DECL_CLOSE:
01275     setTopLevel(state);
01276     return state->role_none;
01277   }
01278   return common(state, tok);
01279 }
01280 
01281 static int PTRCALL
01282 error(PROLOG_STATE *state,
01283       int tok,
01284       const char *ptr,
01285       const char *end,
01286       const ENCODING *enc)
01287 {
01288   return XML_ROLE_NONE;
01289 }
01290 
01291 static int FASTCALL
01292 common(PROLOG_STATE *state, int tok)
01293 {
01294 #ifdef XML_DTD
01295   if (!state->documentEntity && tok == XML_TOK_PARAM_ENTITY_REF)
01296     return XML_ROLE_INNER_PARAM_ENTITY_REF;
01297 #endif
01298   state->handler = error;
01299   return XML_ROLE_ERROR;
01300 }
01301 
01302 void
01303 XmlPrologStateInit(PROLOG_STATE *state)
01304 {
01305   state->handler = prolog0;
01306 #ifdef XML_DTD
01307   state->documentEntity = 1;
01308   state->includeLevel = 0;
01309   state->inEntityValue = 0;
01310 #endif /* XML_DTD */
01311 }
01312 
01313 #ifdef XML_DTD
01314 
01315 void
01316 XmlPrologStateInitExternalEntity(PROLOG_STATE *state)
01317 {
01318   state->handler = externalSubset0;
01319   state->documentEntity = 0;
01320   state->includeLevel = 0;
01321 }
01322 
01323 #endif /* XML_DTD */