ReaderXmlElement.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // Copyright (c) 2010, 2013 Per M.A. Bothner
  2. // This is free software; for terms and warranty disclaimer see ../../../COPYING.
  3. package gnu.kawa.lispexpr;
  4. import gnu.mapping.*;
  5. import gnu.text.*;
  6. import gnu.lists.*;
  7. import gnu.xml.*;
  8. import gnu.kawa.xml.*;
  9. import gnu.expr.*;
  10. public class ReaderXmlElement extends ReaderExtendedLiteral
  11. {
  12. static final Symbol xmlTextSymbol = Symbol.valueOf("$xml-text$");
  13. static final Symbol xmlCommentSymbol = Symbol.valueOf("$xml-comment$");
  14. static final Symbol xmlElementSymbol = Symbol.valueOf("$xml-element$");
  15. static final Symbol xmlCDATASymbol = Symbol.valueOf("$xml-CDATA$");
  16. static final Symbol xmlProcInstSymbol = Symbol.valueOf("$xml-processing-instruction$");
  17. static final Symbol xmlAttributeSymbol = Symbol.valueOf("$xml-attribute$");
  18. static final Symbol resolveQNameSymbol = Symbol.valueOf("$resolve-qname$");
  19. public Object read (Lexer in, int ch, int count)
  20. throws java.io.IOException, SyntaxException
  21. {
  22. LispReader reader = (LispReader) in;
  23. return readXMLConstructor(reader, reader.readUnicodeChar(), false);
  24. }
  25. public static Pair quote (Object obj)
  26. {
  27. Symbol q = Namespace.EmptyNamespace.getSymbol(LispLanguage.quote_str);
  28. return LList.list2(q, obj);
  29. }
  30. static final String DEFAULT_ELEMENT_NAMESPACE = "$default-element-namespace$";
  31. public static final Symbol defaultElementNamespaceSymbol
  32. = Symbol.valueOf(DEFAULT_ELEMENT_NAMESPACE);
  33. /** Read either a QName literal or an enclosed QName-producing form.
  34. * If literal, returns a quoted symbol, and the source literal
  35. * in the non-empty token-buffer.
  36. * If non-literal, tokenBufferLength is set to 0.
  37. */
  38. public Object readQNameExpression (LispReader reader, int ch, boolean forElement)
  39. throws java.io.IOException, SyntaxException
  40. {
  41. String file = reader.getName();
  42. int line = reader.getLineNumber() + 1;
  43. int column = reader.getColumnNumber();
  44. reader.tokenBufferLength = 0;
  45. if (XName.isNameStart(ch))
  46. {
  47. int colon = -1;
  48. for (;;)
  49. {
  50. reader.tokenBufferAppend(ch);
  51. ch = reader.readUnicodeChar();
  52. if (ch == ':' && colon < 0)
  53. colon = reader.tokenBufferLength;
  54. else if (! XName.isNamePart(ch))
  55. {
  56. reader.unread(ch);
  57. break;
  58. }
  59. }
  60. if (colon >= 0 || forElement)
  61. {
  62. int llen = reader.tokenBufferLength - colon - 1;
  63. String local
  64. = new String(reader.tokenBuffer, colon+1, llen).intern();
  65. Object ns = LList.Empty;
  66. if (colon >= 0) {
  67. String prefix = new String(reader.tokenBuffer, 0, colon).intern();
  68. ns = new Pair(Symbol.valueOf(prefix), ns);
  69. }
  70. return new Pair(resolveQNameSymbol,
  71. PairWithPosition.make(Symbol.valueOf(local), ns,
  72. reader.getName(), line, column));
  73. }
  74. else
  75. {
  76. Symbol symbol = Namespace.getDefaultSymbol(reader.tokenBufferString().intern());
  77. return quote(symbol);
  78. }
  79. }
  80. else if (enclosedExprDelim(ch, reader) >= 0 || ch == '(')
  81. {
  82. return readEnclosedSingleExpression(reader, ReadTable.getCurrent(), ch);
  83. }
  84. else
  85. {
  86. reader.error("missing element name");
  87. return null;
  88. }
  89. }
  90. /** Parse an Element or other constructs starting with '<'.
  91. * Assume initial '<' has been processed.
  92. * @param next next character (after '<').
  93. */
  94. Object readXMLConstructor (LispReader reader, int next, boolean inElementContent)
  95. throws java.io.IOException, SyntaxException
  96. {
  97. Object exp;
  98. int startLine = reader.getLineNumber() + 1;
  99. int startColumn = reader.getColumnNumber() - 2;
  100. if (next == '!')
  101. {
  102. next = reader.read();
  103. if (next == '-' && (next = reader.peek()) == '-')
  104. {
  105. reader.skip();
  106. if (! reader.readDelimited("-->"))
  107. reader.error('f', reader.getName(), startLine, startColumn, "unexpected end-of-file in XML comment starting here - expected \"-->\"");
  108. String str = reader.tokenBufferString();
  109. exp = LList.list2(xmlCommentSymbol, str);
  110. }
  111. else if (next == '['
  112. && (next = reader.read()) == 'C'
  113. && (next = reader.read()) == 'D'
  114. && (next = reader.read()) == 'A'
  115. && (next = reader.read()) == 'T'
  116. && (next = reader.read()) == 'A'
  117. && (next = reader.read()) == '[')
  118. {
  119. if (! reader.readDelimited("]]>"))
  120. reader.error('f', reader.getName(), startLine, startColumn,
  121. "unexpected end-of-file in CDATA starting here - expected \"]]>\"");
  122. String str = reader.tokenBufferString();
  123. exp = LList.list2(xmlCDATASymbol, str);
  124. }
  125. else
  126. {
  127. reader.error('f', reader.getName(), startLine, startColumn,
  128. "'<!' must be followed by '--' or '[CDATA['");
  129. while (next >= 0 && next != '>'
  130. && next != '\n' && next != '\r')
  131. {
  132. next = reader.read();
  133. }
  134. exp = null;
  135. }
  136. }
  137. else if (next == '?')
  138. {
  139. next = reader.readUnicodeChar();
  140. if (next < 0 || ! XName.isNameStart(next))
  141. reader.error("missing target after '<?'");
  142. for (;;)
  143. {
  144. reader.tokenBufferAppend(next);
  145. next = reader.readUnicodeChar();
  146. if (! XName.isNamePart(next))
  147. break;
  148. }
  149. String target = reader.tokenBufferString();
  150. int nspaces = 0;
  151. while (next >= 0 && Character.isWhitespace(next))
  152. {
  153. nspaces++;
  154. next = reader.read();
  155. }
  156. reader.unread(next);
  157. char saveReadState = reader.pushNesting('?');
  158. try
  159. {
  160. if (! reader.readDelimited("?>"))
  161. reader.error('f', reader.getName(), startLine, startColumn,
  162. "unexpected end-of-file looking for \"?>\"");
  163. }
  164. finally
  165. {
  166. reader.popNesting(saveReadState);
  167. }
  168. if (nspaces == 0 && reader.tokenBufferLength > 0)
  169. reader.error("target must be followed by space or '?>'");
  170. String content = reader.tokenBufferString();
  171. exp = LList.list3(xmlProcInstSymbol, target, content);
  172. }
  173. else {
  174. exp = readElementConstructor(reader, next);
  175. }
  176. return exp;
  177. }
  178. /** Parse ElementConstructor.
  179. * Assume initial {@code '<'} has been processed,
  180. * and we're read the next character.
  181. * Reads through end of the end tag.
  182. */
  183. public Object readElementConstructor(LispReader reader, int ch)
  184. throws java.io.IOException, SyntaxException {
  185. int startLine = reader.getLineNumber() + 1;
  186. int startColumn = reader.getColumnNumber() - 2;
  187. Object tag = readQNameExpression(reader, ch, true);
  188. // Note that we cannot do namespace resolution at reader time,
  189. // because of constructs like this: <a x="&{x:v}" xmlns:x="xx"/>
  190. // Instead we defer namespace lookup until rewrite-time.
  191. String startTag = reader.tokenBufferLength == 0 ? null
  192. : reader.tokenBufferString();
  193. Pair tagPair = PairWithPosition.make(tag, LList.Empty,
  194. reader.getName(),
  195. startLine, startColumn);
  196. Pair resultTail = tagPair;
  197. LList namespaceList = LList.Empty;
  198. NamespaceBinding nsBindings = null; // ???
  199. boolean sawSpace = false;
  200. for (;;) {
  201. ch = reader.readUnicodeChar();
  202. while (ch >= 0 && Character.isWhitespace(ch)) {
  203. ch = reader.readUnicodeChar();
  204. sawSpace = true;
  205. }
  206. if (ch < 0 || ch == '>' || ch == '/')
  207. break;
  208. if (! sawSpace)
  209. reader.error("missing space before attribute");
  210. Object attrName = readQNameExpression(reader, ch, false);
  211. int line = reader.getLineNumber() + 1;
  212. int column = reader.getColumnNumber() + 1 - reader.tokenBufferLength;
  213. String definingNamespace = null;
  214. if (reader.tokenBufferLength >= 5
  215. && reader.tokenBuffer[0] == 'x'
  216. && reader.tokenBuffer[1] == 'm'
  217. && reader.tokenBuffer[2] == 'l'
  218. && reader.tokenBuffer[3] == 'n'
  219. && reader.tokenBuffer[4] == 's') {
  220. if (reader.tokenBufferLength == 5)
  221. definingNamespace = "";
  222. else if (reader.tokenBuffer[5] == ':')
  223. definingNamespace =
  224. new String(reader.tokenBuffer, 6, reader.tokenBufferLength-6);
  225. }
  226. sawSpace = false;
  227. for (;;) {
  228. ch = reader.readUnicodeChar();
  229. if (ch < 0)
  230. break;
  231. if (Character.isWhitespace(ch))
  232. sawSpace = true;
  233. else
  234. break;
  235. }
  236. Object attrExpr;
  237. if (ch != '=' && reader.tokenBufferLength == 0
  238. && (sawSpace || ch == '/' || ch == '>')) {
  239. // Attribute is an enclosed (attribute-valued) expression.
  240. reader.unread(ch);
  241. attrExpr = attrName;
  242. }
  243. else {
  244. if (ch != '=')
  245. reader.error("missing '=' after attribute");
  246. ch = skipSpace(reader, ' ');
  247. PairWithPosition attrList
  248. = PairWithPosition.make(xmlAttributeSymbol, LList.Empty,
  249. reader.getName(), startLine, startColumn);
  250. PairWithPosition attrPair
  251. = PairWithPosition.make(attrName, LList.Empty,
  252. reader.getName(), startLine, startColumn);
  253. reader.setCdr(attrList, attrPair);
  254. Pair attrTail = attrPair;
  255. if (ch == '[' || ch == '(') {
  256. ReadTable rtable = ReadTable.getCurrent();
  257. attrTail = readEnclosed(reader, rtable, attrTail, ch,
  258. ch == '[' ? ']' : ')');
  259. } else if (ch == '"' || ch == '\'')
  260. attrTail = readContent(reader, (char) ch, attrTail);
  261. else {
  262. reader.unread(ch);
  263. reader.error("missing attribute value (literal values must be quoted)");
  264. for (;;) {
  265. ch = reader.readUnicodeChar();
  266. if (ch < 0)
  267. break;
  268. if (ch == '\'' || ch == '\"' || ch == '/' || ch == '>'
  269. || Character.isWhitespace(ch)) {
  270. reader.unread(ch);
  271. break;
  272. }
  273. }
  274. }
  275. attrExpr = attrList;
  276. if (definingNamespace != null) {
  277. namespaceList = new PairWithPosition(attrPair,
  278. Pair.make(Symbol.valueOf(definingNamespace), attrPair.getCdr()),
  279. namespaceList);
  280. }
  281. sawSpace = false;
  282. }
  283. if (definingNamespace == null) {
  284. Pair pair = PairWithPosition.make(attrExpr, reader.makeNil(),
  285. null, -1, -1); // FIXME
  286. resultTail.setCdrBackdoor(pair);
  287. resultTail = pair;
  288. }
  289. }
  290. boolean empty = false;
  291. if (ch == '/') {
  292. ch = reader.read();
  293. if (ch == '>')
  294. empty = true;
  295. else
  296. reader.unread(ch);
  297. }
  298. if (! empty) {
  299. if (ch != '>') {
  300. reader.error("missing '>' after start element");
  301. return Boolean.FALSE;
  302. }
  303. resultTail = readContent(reader, '<', resultTail);
  304. ch = reader.readUnicodeChar();
  305. if (XName.isNameStart(ch)) {
  306. reader.tokenBufferLength = 0;
  307. for (;;) {
  308. reader.tokenBufferAppend(ch);
  309. ch = reader.readUnicodeChar();
  310. if (! XName.isNamePart(ch) && ch != ':')
  311. break;
  312. }
  313. String endTag = reader.tokenBufferString();
  314. if (startTag == null || ! endTag.equals(startTag))
  315. reader.error('e', reader.getName(),
  316. reader.getLineNumber() + 1,
  317. reader.getColumnNumber() - reader.tokenBufferLength,
  318. startTag == null
  319. ? "computed start tag closed by '</"+endTag+">'"
  320. : "'<"+startTag+">' closed by '</"+endTag+">'");
  321. reader.tokenBufferLength = 0;
  322. }
  323. ch = skipSpace(reader, ch);
  324. if (ch != '>')
  325. reader.error("missing '>' after end element");
  326. }
  327. namespaceList = LList.reverseInPlace(namespaceList);
  328. return PairWithPosition.make(xmlElementSymbol,
  329. Pair.make(namespaceList, tagPair),
  330. reader.getName(),
  331. startLine, startColumn);
  332. }
  333. protected Object checkDelim(LispReader reader, int next, int delimiter)
  334. throws java.io.IOException, SyntaxException {
  335. if (delimiter == '<' && next == '<') {
  336. next = reader.read();
  337. if (next == '/')
  338. return Special.eof;
  339. else
  340. return readXMLConstructor(reader, next, true);
  341. }
  342. else if (next < 0 || (delimiter != '<' && next == delimiter))
  343. return Special.eof;
  344. else
  345. return null;
  346. }
  347. protected boolean isNestableStartDelim(int next) {
  348. return false;
  349. }
  350. protected boolean isNestableEndDelim(int next) {
  351. return false;
  352. }
  353. public static int skipSpace (LispReader reader, int ch)
  354. throws java.io.IOException, SyntaxException
  355. {
  356. for (;;)
  357. {
  358. if (ch < 0 || ! Character.isWhitespace(ch))
  359. return ch;
  360. ch = reader.readUnicodeChar();
  361. }
  362. }
  363. protected int enclosedExprDelim(int ch, LispReader reader) {
  364. if (ch == '[')
  365. return ']';
  366. if (ch == '{') {
  367. if (! reader.deprecatedXmlEnlosedReported) {
  368. reader.error('w', "use '[' instead of deprecated '{' for enclosed expression");
  369. reader.deprecatedXmlEnlosedReported = true;
  370. }
  371. return '}';
  372. }
  373. return -1;
  374. }
  375. }