KXmlParser.java 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. /* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE. */
  20. package org.kxml2.io;
  21. import java.io.*;
  22. import java.util.*;
  23. import org.xmlpull.v1.*;
  24. /** A simple, pull based XML parser. This classe replaces the
  25. XmlParser class and the corresponding event classes. */
  26. public class KXmlParser implements IXmlPullParser {
  27. private Object location;
  28. static final private String UNEXPECTED_EOF = "Unexpected EOF";
  29. static final private String ILLEGAL_TYPE = "Wrong event type";
  30. static final private int LEGACY = 999;
  31. static final private int XML_DECL = 998;
  32. // general
  33. private String version;
  34. private Boolean standalone;
  35. private boolean processNsp;
  36. private boolean relaxed;
  37. private Hashtable entityMap;
  38. private int depth;
  39. private String[] elementStack = new String[16];
  40. private String[] nspStack = new String[8];
  41. private int[] nspCounts = new int[4];
  42. // source
  43. private Reader reader;
  44. private String encoding;
  45. private char[] srcBuf;
  46. private int srcPos;
  47. private int srcCount;
  48. private int line;
  49. private int column;
  50. // txtbuffer
  51. private char[] txtBuf = new char[128];
  52. private int txtPos;
  53. // Event-related
  54. private int type;
  55. //private String text;
  56. private boolean isWhitespace;
  57. private String namespace;
  58. private String prefix;
  59. private String name;
  60. private boolean degenerated;
  61. private int attributeCount;
  62. private String[] attributes = new String[16];
  63. private int stackMismatch = 0;
  64. private String error;
  65. /**
  66. * A separate peek buffer seems simpler than managing
  67. * wrap around in the first level read buffer */
  68. private int[] peek = new int[2];
  69. private int peekCount;
  70. private boolean wasCR;
  71. private boolean unresolved;
  72. private boolean token;
  73. public KXmlParser() {
  74. srcBuf =
  75. new char[Runtime.getRuntime().freeMemory() >= 1048576 ? 8192 : 128];
  76. }
  77. private final boolean isProp(String n1, boolean prop, String n2) {
  78. if (!n1.startsWith("http://xmlpull.org/v1/doc/"))
  79. return false;
  80. if (prop)
  81. return n1.substring(42).equals(n2);
  82. else
  83. return n1.substring(40).equals(n2);
  84. }
  85. private final boolean adjustNsp() throws XmlPullParserException {
  86. boolean any = false;
  87. for (int i = 0; i < attributeCount << 2; i += 4) {
  88. // * 4 - 4; i >= 0; i -= 4) {
  89. String attrName = attributes[i + 2];
  90. int cut = attrName.indexOf(':');
  91. String prefix;
  92. if (cut != -1) {
  93. prefix = attrName.substring(0, cut);
  94. attrName = attrName.substring(cut + 1);
  95. }
  96. else if (attrName.equals("xmlns")) {
  97. prefix = attrName;
  98. attrName = null;
  99. }
  100. else
  101. continue;
  102. if (!prefix.equals("xmlns")) {
  103. any = true;
  104. }
  105. else {
  106. int j = (nspCounts[depth]++) << 1;
  107. nspStack = ensureCapacity(nspStack, j + 2);
  108. nspStack[j] = attrName;
  109. nspStack[j + 1] = attributes[i + 3];
  110. if (attrName != null && attributes[i + 3].equals(""))
  111. error("illegal empty namespace");
  112. // prefixMap = new PrefixMap (prefixMap, attrName, attr.getValue ());
  113. //System.out.println (prefixMap);
  114. System.arraycopy(
  115. attributes,
  116. i + 4,
  117. attributes,
  118. i,
  119. ((--attributeCount) << 2) - i);
  120. i -= 4;
  121. }
  122. }
  123. if (any) {
  124. for (int i = (attributeCount << 2) - 4; i >= 0; i -= 4) {
  125. String attrName = attributes[i + 2];
  126. int cut = attrName.indexOf(':');
  127. if (cut == 0 && !relaxed)
  128. throw new RuntimeException(
  129. "illegal attribute name: " + attrName + " at " + this);
  130. else if (cut != -1) {
  131. String attrPrefix = attrName.substring(0, cut);
  132. attrName = attrName.substring(cut + 1);
  133. String attrNs = getNamespace(attrPrefix);
  134. if (attrNs == null && !relaxed)
  135. throw new RuntimeException(
  136. "Undefined Prefix: " + attrPrefix + " in " + this);
  137. attributes[i] = attrNs;
  138. attributes[i + 1] = attrPrefix;
  139. attributes[i + 2] = attrName;
  140. /*
  141. if (!relaxed) {
  142. for (int j = (attributeCount << 2) - 4; j > i; j -= 4)
  143. if (attrName.equals(attributes[j + 2])
  144. && attrNs.equals(attributes[j]))
  145. exception(
  146. "Duplicate Attribute: {"
  147. + attrNs
  148. + "}"
  149. + attrName);
  150. }
  151. */
  152. }
  153. }
  154. }
  155. int cut = name.indexOf(':');
  156. if (cut == 0)
  157. error("illegal tag name: " + name);
  158. if (cut != -1) {
  159. prefix = name.substring(0, cut);
  160. name = name.substring(cut + 1);
  161. }
  162. this.namespace = getNamespace(prefix);
  163. if (this.namespace == null) {
  164. if (prefix != null)
  165. error("undefined prefix: " + prefix);
  166. this.namespace = NO_NAMESPACE;
  167. }
  168. return any;
  169. }
  170. private final String[] ensureCapacity(String[] arr, int required) {
  171. if (arr.length >= required)
  172. return arr;
  173. String[] bigger = new String[required + 16];
  174. System.arraycopy(arr, 0, bigger, 0, arr.length);
  175. return bigger;
  176. }
  177. private final void error(String desc) throws XmlPullParserException {
  178. if (relaxed) {
  179. if (error == null)
  180. error = "ERR: " + desc;
  181. }
  182. else
  183. exception(desc);
  184. }
  185. private final void exception(String desc) throws XmlPullParserException {
  186. throw new XmlPullParserException(
  187. desc.length() < 100 ? desc : desc.substring(0, 100) + "\n",
  188. this,
  189. null);
  190. }
  191. /**
  192. * common base for next and nextToken. Clears the state, except from
  193. * txtPos and whitespace. Does not set the type variable */
  194. private final void nextImpl() throws IOException, XmlPullParserException {
  195. if (reader == null)
  196. exception("No Input specified");
  197. if (type == END_TAG)
  198. depth--;
  199. while (true) {
  200. attributeCount = -1;
  201. // degenerated needs to be handled before error because of possible
  202. // processor expectations(!)
  203. if (degenerated) {
  204. degenerated = false;
  205. type = END_TAG;
  206. return;
  207. }
  208. if (error != null) {
  209. for (int i = 0; i < error.length(); i++)
  210. push(error.charAt(i));
  211. // text = error;
  212. error = null;
  213. type = COMMENT;
  214. return;
  215. }
  216. if (relaxed
  217. && (stackMismatch > 0 || (peek(0) == -1 && depth > 0))) {
  218. int sp = (depth - 1) << 2;
  219. type = END_TAG;
  220. namespace = elementStack[sp];
  221. prefix = elementStack[sp + 1];
  222. name = elementStack[sp + 2];
  223. if (stackMismatch != 1)
  224. error = "missing end tag /" + name + " inserted";
  225. if (stackMismatch > 0)
  226. stackMismatch--;
  227. return;
  228. }
  229. prefix = null;
  230. name = null;
  231. namespace = null;
  232. // text = null;
  233. type = peekType();
  234. switch (type) {
  235. case ENTITY_REF :
  236. pushEntity();
  237. return;
  238. case START_TAG :
  239. parseStartTag(false);
  240. return;
  241. case END_TAG :
  242. parseEndTag();
  243. return;
  244. case END_DOCUMENT :
  245. return;
  246. case TEXT :
  247. pushText('<', !token);
  248. if (depth == 0) {
  249. if (isWhitespace)
  250. type = IGNORABLE_WHITESPACE;
  251. // make exception switchable for instances.chg... !!!!
  252. // else
  253. // exception ("text '"+getText ()+"' not allowed outside root element");
  254. }
  255. return;
  256. default :
  257. type = parseLegacy(token);
  258. if (type != XML_DECL)
  259. return;
  260. }
  261. }
  262. }
  263. private final int parseLegacy(boolean push)
  264. throws IOException, XmlPullParserException {
  265. String req = "";
  266. int term;
  267. int result;
  268. int prev = 0;
  269. read(); // <
  270. int c = read();
  271. if (c == '?') {
  272. if ((peek(0) == 'x' || peek(0) == 'X')
  273. && (peek(1) == 'm' || peek(1) == 'M')) {
  274. if (push) {
  275. push(peek(0));
  276. push(peek(1));
  277. }
  278. read();
  279. read();
  280. if ((peek(0) == 'l' || peek(0) == 'L') && peek(1) <= ' ') {
  281. if (line != 1 || column > 4)
  282. error("PI must not start with xml");
  283. parseStartTag(true);
  284. if (attributeCount < 1 || !"version".equals(attributes[2]))
  285. error("version expected");
  286. version = attributes[3];
  287. int pos = 1;
  288. if (pos < attributeCount
  289. && "encoding".equals(attributes[2 + 4])) {
  290. encoding = attributes[3 + 4];
  291. pos++;
  292. }
  293. if (pos < attributeCount
  294. && "standalone".equals(attributes[4 * pos + 2])) {
  295. String st = attributes[3 + 4 * pos];
  296. if ("yes".equals(st))
  297. standalone = new Boolean(true);
  298. else if ("no".equals(st))
  299. standalone = new Boolean(false);
  300. else
  301. error("illegal standalone value: " + st);
  302. pos++;
  303. }
  304. if (pos != attributeCount)
  305. error("illegal xmldecl");
  306. isWhitespace = true;
  307. txtPos = 0;
  308. return XML_DECL;
  309. }
  310. }
  311. /* int c0 = read ();
  312. int c1 = read ();
  313. int */
  314. term = '?';
  315. result = PROCESSING_INSTRUCTION;
  316. }
  317. else if (c == '!') {
  318. if (peek(0) == '-') {
  319. result = COMMENT;
  320. req = "--";
  321. term = '-';
  322. }
  323. else if (peek(0) == '[') {
  324. result = CDSECT;
  325. req = "[CDATA[";
  326. term = ']';
  327. push = true;
  328. }
  329. else {
  330. result = DOCDECL;
  331. req = "DOCTYPE";
  332. term = -1;
  333. }
  334. }
  335. else {
  336. error("illegal: <" + c);
  337. return COMMENT;
  338. }
  339. for (int i = 0; i < req.length(); i++)
  340. read(req.charAt(i));
  341. if (result == DOCDECL)
  342. parseDoctype(push);
  343. else {
  344. while (true) {
  345. c = read();
  346. if (c == -1){
  347. error(UNEXPECTED_EOF);
  348. return COMMENT;
  349. }
  350. if (push)
  351. push(c);
  352. if ((term == '?' || c == term)
  353. && peek(0) == term
  354. && peek(1) == '>')
  355. break;
  356. prev = c;
  357. }
  358. if (term == '-' && prev == '-')
  359. error("illegal comment delimiter: --->");
  360. read();
  361. read();
  362. if (push && term != '?')
  363. txtPos--;
  364. }
  365. return result;
  366. }
  367. /** precondition: &lt! consumed */
  368. private final void parseDoctype(boolean push)
  369. throws IOException, XmlPullParserException {
  370. int nesting = 1;
  371. boolean quoted = false;
  372. // read();
  373. while (true) {
  374. int i = read();
  375. switch (i) {
  376. case -1 :
  377. error(UNEXPECTED_EOF);
  378. return;
  379. case '\'' :
  380. quoted = !quoted;
  381. break;
  382. case '<' :
  383. if (!quoted)
  384. nesting++;
  385. break;
  386. case '>' :
  387. if (!quoted) {
  388. if ((--nesting) == 0)
  389. return;
  390. }
  391. break;
  392. }
  393. if (push)
  394. push(i);
  395. }
  396. }
  397. /* precondition: &lt;/ consumed */
  398. private final void parseEndTag()
  399. throws IOException, XmlPullParserException {
  400. read(); // '<'
  401. read(); // '/'
  402. name = readName();
  403. skip();
  404. read('>');
  405. int sp = (depth - 1) << 2;
  406. if (depth == 0) {
  407. error("element stack empty");
  408. type = COMMENT;
  409. return;
  410. }
  411. if (!name.equals(elementStack[sp + 3])) {
  412. error("expected: /" + elementStack[sp + 3] + " read: " + name);
  413. // become case insensitive in relaxed mode
  414. int probe = sp;
  415. while (probe >= 0 && !name.toLowerCase().equals(elementStack[probe + 3].toLowerCase())) {
  416. stackMismatch++;
  417. probe -= 4;
  418. }
  419. if (probe < 0) {
  420. stackMismatch = 0;
  421. // text = "unexpected end tag ignored";
  422. type = COMMENT;
  423. return;
  424. }
  425. }
  426. namespace = elementStack[sp];
  427. prefix = elementStack[sp + 1];
  428. name = elementStack[sp + 2];
  429. }
  430. private final int peekType() throws IOException {
  431. switch (peek(0)) {
  432. case -1 :
  433. return END_DOCUMENT;
  434. case '&' :
  435. return ENTITY_REF;
  436. case '<' :
  437. switch (peek(1)) {
  438. case '/' :
  439. return END_TAG;
  440. case '?' :
  441. case '!' :
  442. return LEGACY;
  443. default :
  444. return START_TAG;
  445. }
  446. default :
  447. return TEXT;
  448. }
  449. }
  450. private final String get(int pos) {
  451. return new String(txtBuf, pos, txtPos - pos);
  452. }
  453. /*
  454. private final String pop (int pos) {
  455. String result = new String (txtBuf, pos, txtPos - pos);
  456. txtPos = pos;
  457. return result;
  458. }
  459. */
  460. private final void push(int c) {
  461. isWhitespace &= c <= ' ';
  462. if (txtPos == txtBuf.length) {
  463. char[] bigger = new char[txtPos * 4 / 3 + 4];
  464. System.arraycopy(txtBuf, 0, bigger, 0, txtPos);
  465. txtBuf = bigger;
  466. }
  467. txtBuf[txtPos++] = (char) c;
  468. }
  469. /** Sets name and attributes */
  470. private final void parseStartTag(boolean xmldecl)
  471. throws IOException, XmlPullParserException {
  472. if (!xmldecl)
  473. read();
  474. name = readName();
  475. attributeCount = 0;
  476. while (true) {
  477. skip();
  478. int c = peek(0);
  479. if (xmldecl) {
  480. if (c == '?') {
  481. read();
  482. read('>');
  483. return;
  484. }
  485. }
  486. else {
  487. if (c == '/') {
  488. degenerated = true;
  489. read();
  490. skip();
  491. read('>');
  492. break;
  493. }
  494. if (c == '>' && !xmldecl) {
  495. read();
  496. break;
  497. }
  498. }
  499. if (c == -1) {
  500. error(UNEXPECTED_EOF);
  501. //type = COMMENT;
  502. return;
  503. }
  504. String attrName = readName();
  505. if (attrName.length() == 0) {
  506. error("attr name expected");
  507. //type = COMMENT;
  508. break;
  509. }
  510. int i = (attributeCount++) << 2;
  511. attributes = ensureCapacity(attributes, i + 4);
  512. attributes[i++] = "";
  513. attributes[i++] = null;
  514. attributes[i++] = attrName;
  515. skip();
  516. if (peek(0) != '=') {
  517. error("Attr.value missing f. "+attrName);
  518. attributes[i] = "1";
  519. }
  520. else {
  521. read('=');
  522. skip();
  523. int delimiter = peek(0);
  524. if (delimiter != '\'' && delimiter != '"') {
  525. error("attr value delimiter missing!");
  526. delimiter = ' ';
  527. }
  528. else
  529. read();
  530. int p = txtPos;
  531. pushText(delimiter, true);
  532. attributes[i] = get(p);
  533. txtPos = p;
  534. if (delimiter != ' ')
  535. read(); // skip endquote
  536. }
  537. }
  538. int sp = depth++ << 2;
  539. elementStack = ensureCapacity(elementStack, sp + 4);
  540. elementStack[sp + 3] = name;
  541. if (depth >= nspCounts.length) {
  542. int[] bigger = new int[depth + 4];
  543. System.arraycopy(nspCounts, 0, bigger, 0, nspCounts.length);
  544. nspCounts = bigger;
  545. }
  546. nspCounts[depth] = nspCounts[depth - 1];
  547. /*
  548. if(!relaxed){
  549. for (int i = attributeCount - 1; i > 0; i--) {
  550. for (int j = 0; j < i; j++) {
  551. if (getAttributeName(i).equals(getAttributeName(j)))
  552. exception("Duplicate Attribute: " + getAttributeName(i));
  553. }
  554. }
  555. }
  556. */
  557. if (processNsp)
  558. adjustNsp();
  559. else
  560. namespace = "";
  561. elementStack[sp] = namespace;
  562. elementStack[sp + 1] = prefix;
  563. elementStack[sp + 2] = name;
  564. }
  565. /** result: isWhitespace; if the setName parameter is set,
  566. the name of the entity is stored in "name" */
  567. private final void pushEntity()
  568. throws IOException, XmlPullParserException {
  569. read(); // &
  570. int pos = txtPos;
  571. while (true) {
  572. int c = read();
  573. if (c == ';')
  574. break;
  575. if (c < 128
  576. && (c < '0' || c > '9')
  577. && (c < 'a' || c > 'z')
  578. && (c < 'A' || c > 'Z')
  579. && c != '_'
  580. && c != '-'
  581. && c != '#') {
  582. error("unterminated entity ref");
  583. //; ends with:"+(char)c);
  584. if (c != -1)
  585. push(c);
  586. return;
  587. }
  588. push(c);
  589. }
  590. String code = get(pos);
  591. txtPos = pos;
  592. if (token && type == ENTITY_REF)
  593. name = code;
  594. if (code.charAt(0) == '#') {
  595. int c =
  596. (code.charAt(1) == 'x'
  597. ? Integer.parseInt(code.substring(2), 16)
  598. : Integer.parseInt(code.substring(1)));
  599. push(c);
  600. return;
  601. }
  602. String result = (String) entityMap.get(code);
  603. unresolved = result == null;
  604. if (unresolved) {
  605. if (!token)
  606. error("unresolved: &" + code + ";");
  607. }
  608. else {
  609. for (int i = 0; i < result.length(); i++)
  610. push(result.charAt(i));
  611. }
  612. }
  613. /** types:
  614. '<': parse to any token (for nextToken ())
  615. '"': parse to quote
  616. ' ': parse to whitespace or '>'
  617. */
  618. private final void pushText(int delimiter, boolean resolveEntities)
  619. throws IOException, XmlPullParserException {
  620. int next = peek(0);
  621. int cbrCount = 0;
  622. while (next != -1 && next != delimiter) { // covers eof, '<', '"'
  623. if (delimiter == ' ')
  624. if (next <= ' ' || next == '>')
  625. break;
  626. if (next == '&') {
  627. if (!resolveEntities)
  628. break;
  629. pushEntity();
  630. }
  631. else if (next == '\n' && type == START_TAG) {
  632. read();
  633. push(' ');
  634. }
  635. else
  636. push(read());
  637. if (next == '>' && cbrCount >= 2 && delimiter != ']')
  638. error("Illegal: ]]>");
  639. if (next == ']')
  640. cbrCount++;
  641. else
  642. cbrCount = 0;
  643. next = peek(0);
  644. }
  645. }
  646. private final void read(char c)
  647. throws IOException, XmlPullParserException {
  648. int a = read();
  649. if (a != c)
  650. error("expected: '" + c + "' actual: '" + ((char) a) + "'");
  651. }
  652. private final int read() throws IOException {
  653. int result;
  654. if (peekCount == 0)
  655. result = peek(0);
  656. else {
  657. result = peek[0];
  658. peek[0] = peek[1];
  659. }
  660. // else {
  661. // result = peek[0];
  662. // System.arraycopy (peek, 1, peek, 0, peekCount-1);
  663. // }
  664. peekCount--;
  665. column++;
  666. if (result == '\n') {
  667. line++;
  668. column = 1;
  669. }
  670. return result;
  671. }
  672. /** Does never read more than needed */
  673. private final int peek(int pos) throws IOException {
  674. while (pos >= peekCount) {
  675. int nw;
  676. if (srcBuf.length <= 1)
  677. nw = reader.read();
  678. else if (srcPos < srcCount)
  679. nw = srcBuf[srcPos++];
  680. else {
  681. srcCount = reader.read(srcBuf, 0, srcBuf.length);
  682. if (srcCount <= 0)
  683. nw = -1;
  684. else
  685. nw = srcBuf[0];
  686. srcPos = 1;
  687. }
  688. if (nw == '\r') {
  689. wasCR = true;
  690. peek[peekCount++] = '\n';
  691. }
  692. else {
  693. if (nw == '\n') {
  694. if (!wasCR)
  695. peek[peekCount++] = '\n';
  696. }
  697. else
  698. peek[peekCount++] = nw;
  699. wasCR = false;
  700. }
  701. }
  702. return peek[pos];
  703. }
  704. private final String readName()
  705. throws IOException, XmlPullParserException {
  706. int pos = txtPos;
  707. int c = peek(0);
  708. if ((c < 'a' || c > 'z')
  709. && (c < 'A' || c > 'Z')
  710. && c != '_'
  711. && c != ':'
  712. && c < 0x0c0
  713. && !relaxed)
  714. error("name expected");
  715. do {
  716. push(read());
  717. c = peek(0);
  718. }
  719. while ((c >= 'a' && c <= 'z')
  720. || (c >= 'A' && c <= 'Z')
  721. || (c >= '0' && c <= '9')
  722. || c == '_'
  723. || c == '-'
  724. || c == ':'
  725. || c == '.'
  726. || c >= 0x0b7);
  727. String result = get(pos);
  728. txtPos = pos;
  729. return result;
  730. }
  731. private final void skip() throws IOException {
  732. while (true) {
  733. int c = peek(0);
  734. if (c > ' ' || c == -1)
  735. break;
  736. read();
  737. }
  738. }
  739. //--------------- public part starts here... ---------------
  740. public void setInput(Reader reader) throws XmlPullParserException {
  741. this.reader = reader;
  742. line = 1;
  743. column = 0;
  744. type = START_DOCUMENT;
  745. name = null;
  746. namespace = null;
  747. degenerated = false;
  748. attributeCount = -1;
  749. encoding = null;
  750. version = null;
  751. standalone = null;
  752. if (reader == null)
  753. return;
  754. srcPos = 0;
  755. srcCount = 0;
  756. peekCount = 0;
  757. depth = 0;
  758. entityMap = new Hashtable();
  759. entityMap.put("amp", "&");
  760. entityMap.put("apos", "'");
  761. entityMap.put("gt", ">");
  762. entityMap.put("lt", "<");
  763. entityMap.put("quot", "\"");
  764. }
  765. public void setInput(InputStream is, String _enc)
  766. throws XmlPullParserException {
  767. srcPos = 0;
  768. srcCount = 0;
  769. String enc = _enc;
  770. if (is == null)
  771. throw new IllegalArgumentException("kxml: is=null");
  772. try {
  773. if (enc == null) {
  774. // read four bytes
  775. int chk = 0;
  776. while (srcCount < 4) {
  777. int i = is.read();
  778. if (i == -1)
  779. break;
  780. chk = (chk << 8) | i;
  781. srcBuf[srcCount++] = (char) i;
  782. }
  783. if (srcCount == 4) {
  784. switch (chk) {
  785. case 0x00000FEFF :
  786. enc = "UTF-32BE";
  787. srcCount = 0;
  788. break;
  789. case 0x0FFFE0000 :
  790. enc = "UTF-32LE";
  791. srcCount = 0;
  792. break;
  793. case 0x03c :
  794. enc = "UTF-32BE";
  795. srcBuf[0] = '<';
  796. srcCount = 1;
  797. break;
  798. case 0x03c000000 :
  799. enc = "UTF-32LE";
  800. srcBuf[0] = '<';
  801. srcCount = 1;
  802. break;
  803. case 0x0003c003f :
  804. enc = "UTF-16BE";
  805. srcBuf[0] = '<';
  806. srcBuf[1] = '?';
  807. srcCount = 2;
  808. break;
  809. case 0x03c003f00 :
  810. enc = "UTF-16LE";
  811. srcBuf[0] = '<';
  812. srcBuf[1] = '?';
  813. srcCount = 2;
  814. break;
  815. case 0x03c3f786d :
  816. while (true) {
  817. int i = is.read();
  818. if (i == -1)
  819. break;
  820. srcBuf[srcCount++] = (char) i;
  821. if (i == '>') {
  822. String s = new String(srcBuf, 0, srcCount);
  823. int i0 = s.indexOf("encoding");
  824. if (i0 != -1) {
  825. while (s.charAt(i0) != '"'
  826. && s.charAt(i0) != '\'')
  827. i0++;
  828. char deli = s.charAt(i0++);
  829. int i1 = s.indexOf(deli, i0);
  830. enc = s.substring(i0, i1);
  831. }
  832. break;
  833. }
  834. }
  835. default :
  836. if ((chk & 0x0ffff0000) == 0x0FEFF0000) {
  837. enc = "UTF-16BE";
  838. srcBuf[0] =
  839. (char) ((srcBuf[2] << 8) | srcBuf[3]);
  840. srcCount = 1;
  841. }
  842. else if ((chk & 0x0ffff0000) == 0x0fffe0000) {
  843. enc = "UTF-16LE";
  844. srcBuf[0] =
  845. (char) ((srcBuf[3] << 8) | srcBuf[2]);
  846. srcCount = 1;
  847. }
  848. else if ((chk & 0x0ffffff00) == 0x0EFBBBF) {
  849. enc = "UTF-8";
  850. srcBuf[0] = srcBuf[3];
  851. srcCount = 1;
  852. }
  853. }
  854. }
  855. }
  856. if (enc == null)
  857. enc = "UTF-8";
  858. int sc = srcCount;
  859. setInput(new InputStreamReader(is, enc));
  860. encoding = _enc;
  861. srcCount = sc;
  862. }
  863. catch (Exception e) {
  864. throw new XmlPullParserException(
  865. "Invalid stream or encoding: " + e.toString(),
  866. this,
  867. e);
  868. }
  869. }
  870. public boolean getFeature(String feature) {
  871. if (IXmlPullParser.FEATURE_PROCESS_NAMESPACES.equals(feature))
  872. return processNsp;
  873. else if (isProp(feature, false, "relaxed"))
  874. return relaxed;
  875. else
  876. return false;
  877. }
  878. public String getInputEncoding() {
  879. return encoding;
  880. }
  881. public void defineEntityReplacementText(String entity, String value)
  882. throws XmlPullParserException {
  883. if (entityMap == null)
  884. throw new RuntimeException("entity replacement text must be defined after setInput!");
  885. entityMap.put(entity, value);
  886. }
  887. public Object getProperty(String property) {
  888. if (isProp(property, true, "xmldecl-version"))
  889. return version;
  890. if (isProp(property, true, "xmldecl-standalone"))
  891. return standalone;
  892. if (isProp(property, true, "location"))
  893. return location != null ? location : reader.toString();
  894. return null;
  895. }
  896. public int getNamespaceCount(int depth) {
  897. if (depth > this.depth)
  898. throw new IndexOutOfBoundsException();
  899. return nspCounts[depth];
  900. }
  901. public String getNamespacePrefix(int pos) {
  902. return nspStack[pos << 1];
  903. }
  904. public String getNamespaceUri(int pos) {
  905. return nspStack[(pos << 1) + 1];
  906. }
  907. public String getNamespace(String prefix) {
  908. if ("xml".equals(prefix))
  909. return "http://www.w3.org/XML/1998/namespace";
  910. if ("xmlns".equals(prefix))
  911. return "http://www.w3.org/2000/xmlns/";
  912. for (int i = (getNamespaceCount(depth) << 1) - 2; i >= 0; i -= 2) {
  913. if (prefix == null) {
  914. if (nspStack[i] == null)
  915. return nspStack[i + 1];
  916. }
  917. else if (prefix.equals(nspStack[i]))
  918. return nspStack[i + 1];
  919. }
  920. return null;
  921. }
  922. public int getDepth() {
  923. return depth;
  924. }
  925. public String getPositionDescription() {
  926. StringBuffer buf =
  927. new StringBuffer(type < TYPES.length ? TYPES[type] : "unknown");
  928. buf.append(' ');
  929. if (type == START_TAG || type == END_TAG) {
  930. if (degenerated)
  931. buf.append("(empty) ");
  932. buf.append('<');
  933. if (type == END_TAG)
  934. buf.append('/');
  935. if (prefix != null)
  936. buf.append("{" + namespace + "}" + prefix + ":");
  937. buf.append(name);
  938. int cnt = attributeCount << 2;
  939. for (int i = 0; i < cnt; i += 4) {
  940. buf.append(' ');
  941. if (attributes[i + 1] != null)
  942. buf.append(
  943. "{" + attributes[i] + "}" + attributes[i + 1] + ":");
  944. buf.append(attributes[i + 2] + "='" + attributes[i + 3] + "'");
  945. }
  946. buf.append('>');
  947. }
  948. else if (type == IGNORABLE_WHITESPACE);
  949. else if (type != TEXT)
  950. buf.append(getText());
  951. else if (isWhitespace)
  952. buf.append("(whitespace)");
  953. else {
  954. String text = getText();
  955. if (text.length() > 16)
  956. text = text.substring(0, 16) + "...";
  957. buf.append(text);
  958. }
  959. buf.append("@"+line + ":" + column);
  960. buf.append(" in ");
  961. buf.append(location == null ? reader.toString() : location);
  962. return buf.toString();
  963. }
  964. public int getLineNumber() {
  965. return line;
  966. }
  967. public int getColumnNumber() {
  968. return column;
  969. }
  970. public boolean isWhitespace() throws XmlPullParserException {
  971. if (type != TEXT && type != IGNORABLE_WHITESPACE && type != CDSECT)
  972. exception(ILLEGAL_TYPE);
  973. return isWhitespace;
  974. }
  975. public String getText() {
  976. return type < TEXT
  977. || (type == ENTITY_REF && unresolved) ? null : get(0);
  978. }
  979. public char[] getTextCharacters(int[] poslen) {
  980. if (type >= TEXT) {
  981. if (type == ENTITY_REF) {
  982. poslen[0] = 0;
  983. poslen[1] = name.length();
  984. return name.toCharArray();
  985. }
  986. poslen[0] = 0;
  987. poslen[1] = txtPos;
  988. return txtBuf;
  989. }
  990. poslen[0] = -1;
  991. poslen[1] = -1;
  992. return null;
  993. }
  994. public String getNamespace() {
  995. return namespace;
  996. }
  997. public String getName() {
  998. return name;
  999. }
  1000. public String getPrefix() {
  1001. return prefix;
  1002. }
  1003. public boolean isEmptyElementTag() throws XmlPullParserException {
  1004. if (type != START_TAG)
  1005. exception(ILLEGAL_TYPE);
  1006. return degenerated;
  1007. }
  1008. public int getAttributeCount() {
  1009. return attributeCount;
  1010. }
  1011. public String getAttributeType(int index) {
  1012. return "CDATA";
  1013. }
  1014. public boolean isAttributeDefault(int index) {
  1015. return false;
  1016. }
  1017. public String getAttributeNamespace(int index) {
  1018. if (index >= attributeCount)
  1019. throw new IndexOutOfBoundsException();
  1020. return attributes[index << 2];
  1021. }
  1022. public String getAttributeName(int index) {
  1023. if (index >= attributeCount)
  1024. throw new IndexOutOfBoundsException();
  1025. return attributes[(index << 2) + 2];
  1026. }
  1027. public String getAttributePrefix(int index) {
  1028. if (index >= attributeCount)
  1029. throw new IndexOutOfBoundsException();
  1030. return attributes[(index << 2) + 1];
  1031. }
  1032. public String getAttributeValue(int index) {
  1033. if (index >= attributeCount)
  1034. throw new IndexOutOfBoundsException();
  1035. return attributes[(index << 2) + 3];
  1036. }
  1037. public String getAttributeValue(String namespace, String name) {
  1038. for (int i = (attributeCount << 2) - 4; i >= 0; i -= 4) {
  1039. if (attributes[i + 2].equals(name)
  1040. && (namespace == null || attributes[i].equals(namespace)))
  1041. return attributes[i + 3];
  1042. }
  1043. return null;
  1044. }
  1045. public int getEventType() throws XmlPullParserException {
  1046. return type;
  1047. }
  1048. public int next() throws XmlPullParserException, IOException {
  1049. txtPos = 0;
  1050. isWhitespace = true;
  1051. int minType = 9999;
  1052. token = false;
  1053. do {
  1054. nextImpl();
  1055. if (type < minType)
  1056. minType = type;
  1057. // if (curr <= TEXT) type = curr;
  1058. }
  1059. while (minType > ENTITY_REF // ignorable
  1060. || (minType >= TEXT && peekType() >= TEXT));
  1061. type = minType;
  1062. if (type > TEXT)
  1063. type = TEXT;
  1064. return type;
  1065. }
  1066. public int nextToken() throws XmlPullParserException, IOException {
  1067. isWhitespace = true;
  1068. txtPos = 0;
  1069. token = true;
  1070. nextImpl();
  1071. return type;
  1072. }
  1073. //----------------------------------------------------------------------
  1074. // utility methods to make XML parsing easier ...
  1075. public int nextTag() throws XmlPullParserException, IOException {
  1076. next();
  1077. if (type == TEXT && isWhitespace)
  1078. next();
  1079. if (type != END_TAG && type != START_TAG)
  1080. exception("unexpected type");
  1081. return type;
  1082. }
  1083. public void require(int type, String namespace, String name)
  1084. throws XmlPullParserException, IOException {
  1085. if (type != this.type
  1086. || (namespace != null && !namespace.equals(getNamespace()))
  1087. || (name != null && !name.equals(getName())))
  1088. exception(
  1089. "expected: " + TYPES[type] + " {" + namespace + "}" + name);
  1090. }
  1091. public String nextText() throws XmlPullParserException, IOException {
  1092. if (type != START_TAG)
  1093. exception("precondition: START_TAG");
  1094. next();
  1095. String result;
  1096. if (type == TEXT) {
  1097. result = getText();
  1098. next();
  1099. }
  1100. else
  1101. result = "";
  1102. if (type != END_TAG)
  1103. exception("END_TAG expected");
  1104. return result;
  1105. }
  1106. public void setFeature(String feature, boolean value)
  1107. throws XmlPullParserException {
  1108. if (IXmlPullParser.FEATURE_PROCESS_NAMESPACES.equals(feature))
  1109. processNsp = value;
  1110. else if (isProp(feature, false, "relaxed"))
  1111. relaxed = value;
  1112. else
  1113. exception("unsupported feature: " + feature);
  1114. }
  1115. public void setProperty(String property, Object value)
  1116. throws XmlPullParserException {
  1117. if(isProp(property, true, "location"))
  1118. location = value;
  1119. else
  1120. throw new XmlPullParserException("unsupported property: " + property);
  1121. }
  1122. /**
  1123. * Skip sub tree that is currently porser positioned on.
  1124. * <br>NOTE: parser must be on START_TAG and when funtion returns
  1125. * parser will be positioned on corresponding END_TAG.
  1126. */
  1127. // Implementation copied from Alek's mail...
  1128. public void skipSubTree() throws XmlPullParserException, IOException {
  1129. require(START_TAG, null, null);
  1130. int level = 1;
  1131. while (level > 0) {
  1132. int eventType = next();
  1133. if (eventType == END_TAG) {
  1134. --level;
  1135. }
  1136. else if (eventType == START_TAG) {
  1137. ++level;
  1138. }
  1139. }
  1140. }
  1141. }