ExternalId.C 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: ExternalId.C /main/1 1996/07/29 16:51:51 cde-hp $ */
  24. // Copyright (c) 1994 James Clark
  25. // See the file COPYING for copying permission.
  26. #ifdef __GNUG__
  27. #pragma implementation
  28. #endif
  29. #include "splib.h"
  30. #include "ExternalId.h"
  31. #include "CharsetInfo.h"
  32. #include "macros.h"
  33. #include "ParserMessages.h"
  34. #ifdef SP_NAMESPACE
  35. namespace SP_NAMESPACE {
  36. #endif
  37. ExternalId::ExternalId()
  38. : haveSystem_(0), havePublic_(0)
  39. {
  40. }
  41. void ExternalId::setSystem(Text &text)
  42. {
  43. text.swap(system_);
  44. haveSystem_ = 1;
  45. }
  46. Boolean ExternalId::setPublic(Text &text, const CharsetInfo &charset,
  47. Char space, const MessageType1 *&error)
  48. {
  49. havePublic_ = 1;
  50. return public_.init(text, charset, space, error);
  51. }
  52. void ExternalId::setLocation(const Location &loc)
  53. {
  54. loc_ = loc;
  55. }
  56. PublicId::PublicId()
  57. : formal_(0), ownerType_(ISO), textClass_(CAPACITY), unavailable_(false), haveDisplayVersion_(false)
  58. {
  59. }
  60. Boolean PublicId::init(Text &text, const CharsetInfo &charset,
  61. Char space, const MessageType1 *&error)
  62. {
  63. text.swap(text_);
  64. const StringC &str = text_.string();
  65. formal_ = 0;
  66. const Char *next = str.data();
  67. const Char *lim = str.data() + str.size();
  68. Char solidus = charset.execToDesc('/');
  69. Char minus = charset.execToDesc('-');
  70. Char plus = charset.execToDesc('+');
  71. const Char *fieldStart;
  72. size_t fieldLength;
  73. if (!nextField(solidus, next, lim, fieldStart, fieldLength)) {
  74. error = &ParserMessages::fpiMissingField;
  75. return 0;
  76. }
  77. if (fieldLength == 1 && (*fieldStart == minus || *fieldStart == plus)) {
  78. ownerType_ = (*fieldStart == plus ? registered : unregistered);
  79. if (!nextField(solidus, next, lim, fieldStart, fieldLength)) {
  80. error = &ParserMessages::fpiMissingField;
  81. return 0;
  82. }
  83. }
  84. else
  85. ownerType_ = ISO;
  86. owner_.assign(fieldStart, fieldLength);
  87. if (!nextField(solidus, next, lim, fieldStart, fieldLength)) {
  88. error = &ParserMessages::fpiMissingField;
  89. return 0;
  90. }
  91. size_t i;
  92. for (i = 0; i < fieldLength; i++)
  93. if (fieldStart[i] == space)
  94. break;
  95. if (i >= fieldLength) {
  96. error = &ParserMessages::fpiMissingTextClassSpace;
  97. return 0;
  98. }
  99. StringC textClassString(fieldStart, i);
  100. if (!lookupTextClass(textClassString, charset, textClass_)) {
  101. error = &ParserMessages::fpiInvalidTextClass;
  102. return 0;
  103. }
  104. i++; // skip the space
  105. fieldStart += i;
  106. fieldLength -= i;
  107. if (fieldLength == 1 && *fieldStart == minus) {
  108. unavailable_ = 1;
  109. if (!nextField(solidus, next, lim, fieldStart, fieldLength)) {
  110. error = &ParserMessages::fpiMissingField;
  111. return 0;
  112. }
  113. }
  114. else
  115. unavailable_ = 0;
  116. description_.assign(fieldStart, fieldLength);
  117. if (!nextField(solidus, next, lim, fieldStart, fieldLength)) {
  118. error = &ParserMessages::fpiMissingField;
  119. return 0;
  120. }
  121. if (textClass_ != CHARSET) {
  122. for (i = 0; i < fieldLength; i++) {
  123. UnivChar c;
  124. if (!charset.descToUniv(fieldStart[i], c)
  125. || c < UnivCharsetDesc::A || c >= UnivCharsetDesc::A + 26) {
  126. error = &ParserMessages::fpiInvalidLanguage;
  127. return 0;
  128. }
  129. }
  130. // The public text language must be a name.
  131. // Names cannot be empty.
  132. if (fieldLength == 0) {
  133. error = &ParserMessages::fpiInvalidLanguage;
  134. return 0;
  135. }
  136. }
  137. languageOrDesignatingSequence_.assign(fieldStart, fieldLength);
  138. if (nextField(solidus, next, lim, fieldStart, fieldLength)) {
  139. switch (textClass_) {
  140. case CAPACITY:
  141. case CHARSET:
  142. case NOTATION:
  143. case SYNTAX:
  144. error = &ParserMessages::fpiIllegalDisplayVersion;
  145. return 0;
  146. default:
  147. break;
  148. }
  149. haveDisplayVersion_ = 1;
  150. displayVersion_.assign(fieldStart, fieldLength);
  151. }
  152. else
  153. haveDisplayVersion_ = 0;
  154. if (next != 0) {
  155. error = &ParserMessages::fpiExtraField;
  156. return 0;
  157. }
  158. formal_ = 1;
  159. return 1;
  160. }
  161. Boolean PublicId::nextField(Char solidus,
  162. const Char *&next,
  163. const Char *lim,
  164. const Char *&fieldStart,
  165. size_t &fieldLength)
  166. {
  167. if (next == 0)
  168. return 0;
  169. fieldStart = next;
  170. for (; next < lim; next++) {
  171. if (next[0] == solidus && next + 1 < lim && next[1] == solidus) {
  172. fieldLength = next - fieldStart;
  173. next += 2;
  174. return 1;
  175. }
  176. }
  177. fieldLength = lim - fieldStart;
  178. next = 0;
  179. return 1;
  180. }
  181. const char *const PublicId::textClasses[] = {
  182. "CAPACITY",
  183. "CHARSET",
  184. "DOCUMENT",
  185. "DTD",
  186. "ELEMENTS",
  187. "ENTITIES",
  188. "LPD",
  189. "NONSGML",
  190. "NOTATION",
  191. "SHORTREF",
  192. "SUBDOC",
  193. "SYNTAX",
  194. "TEXT",
  195. };
  196. Boolean PublicId::lookupTextClass(const StringC &str,
  197. const CharsetInfo &charset,
  198. TextClass &textClass)
  199. {
  200. for (size_t i = 0; i < SIZEOF(textClasses); i++)
  201. if (str == charset.execToDesc(textClasses[i])) {
  202. textClass = TextClass(i);
  203. return 1;
  204. }
  205. return 0;
  206. }
  207. Boolean PublicId::getOwnerType(OwnerType &result) const
  208. {
  209. if (!formal_)
  210. return 0;
  211. result = ownerType_;
  212. return 1;
  213. }
  214. Boolean PublicId::getOwner(StringC &result) const
  215. {
  216. if (!formal_)
  217. return 0;
  218. result = owner_;
  219. return 1;
  220. }
  221. Boolean PublicId::getTextClass(TextClass &result) const
  222. {
  223. if (!formal_)
  224. return 0;
  225. result = textClass_;
  226. return 1;
  227. }
  228. Boolean PublicId::getUnavailable(Boolean &result) const
  229. {
  230. if (!formal_)
  231. return 0;
  232. result = unavailable_;
  233. return 1;
  234. }
  235. Boolean PublicId::getDescription(StringC &result) const
  236. {
  237. if (!formal_)
  238. return 0;
  239. result = description_;
  240. return 1;
  241. }
  242. Boolean PublicId::getLanguage(StringC &result) const
  243. {
  244. if (!formal_ || textClass_ == CHARSET)
  245. return 0;
  246. result = languageOrDesignatingSequence_;
  247. return 1;
  248. }
  249. Boolean PublicId::getDesignatingSequence(StringC &result) const
  250. {
  251. if (!formal_ || textClass_ != CHARSET)
  252. return 0;
  253. result = languageOrDesignatingSequence_;
  254. return 1;
  255. }
  256. Boolean PublicId::getDisplayVersion(StringC &result) const
  257. {
  258. if (!formal_)
  259. return 0;
  260. if (haveDisplayVersion_)
  261. result = displayVersion_;
  262. return 1;
  263. }
  264. #ifdef SP_NAMESPACE
  265. }
  266. #endif