DomDocument.java 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. /* DomDocument.java --
  2. Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.dom;
  32. import java.util.Iterator;
  33. import javax.xml.XMLConstants;
  34. import org.w3c.dom.Attr;
  35. import org.w3c.dom.CDATASection;
  36. import org.w3c.dom.Comment;
  37. import org.w3c.dom.Document;
  38. import org.w3c.dom.DocumentFragment;
  39. import org.w3c.dom.DocumentType;
  40. import org.w3c.dom.DOMConfiguration;
  41. import org.w3c.dom.DOMImplementation;
  42. import org.w3c.dom.DOMException;
  43. import org.w3c.dom.Element;
  44. import org.w3c.dom.Entity;
  45. import org.w3c.dom.EntityReference;
  46. import org.w3c.dom.NamedNodeMap;
  47. import org.w3c.dom.Node;
  48. import org.w3c.dom.Notation;
  49. import org.w3c.dom.ProcessingInstruction;
  50. import org.w3c.dom.Text;
  51. import org.w3c.dom.UserDataHandler;
  52. import org.w3c.dom.traversal.DocumentTraversal;
  53. import org.w3c.dom.traversal.NodeFilter;
  54. import org.w3c.dom.traversal.NodeIterator;
  55. import org.w3c.dom.traversal.TreeWalker;
  56. import org.w3c.dom.xpath.XPathEvaluator;
  57. import org.w3c.dom.xpath.XPathException;
  58. import org.w3c.dom.xpath.XPathExpression;
  59. import org.w3c.dom.xpath.XPathNSResolver;
  60. /**
  61. * <p> "Document" and "DocumentTraversal" implementation.
  62. *
  63. * <p> Note that when this checks names for legality, it uses an
  64. * approximation of the XML rules, not the real ones. Specifically,
  65. * it uses Unicode rules, with sufficient tweaks to pass a majority
  66. * of basic XML conformance tests. (The huge XML character tables are
  67. * hairy to implement.)
  68. *
  69. * @author David Brownell
  70. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  71. */
  72. public class DomDocument
  73. extends DomNode
  74. implements Document, DocumentTraversal, XPathEvaluator
  75. {
  76. private final DOMImplementation implementation;
  77. private boolean checkingCharacters = true;
  78. boolean checkingWellformedness = true;
  79. private boolean defaultAttributes = true;
  80. boolean building; // if true, skip mutation events in the tree
  81. DomDocumentConfiguration config;
  82. String inputEncoding;
  83. String encoding;
  84. String version = "1.0";
  85. boolean standalone;
  86. String systemId;
  87. /**
  88. * Constructs a Document node, associating it with an instance
  89. * of the DomImpl class.
  90. *
  91. * <p> Note that this constructor disables character checking.
  92. * It is normally used when connecting a DOM to an XML parser,
  93. * and duplicating such checks is undesirable. When used for
  94. * purposes other than connecting to a parser, you should
  95. * re-enable that checking.
  96. *
  97. * @see #setCheckingCharacters
  98. */
  99. public DomDocument()
  100. {
  101. this(new DomImpl());
  102. }
  103. /**
  104. * Constructs a Document node, associating it with the specified
  105. * implementation. This should only be used in conjunction with
  106. * a specialized implementation; it will normally be called by
  107. * that implementation.
  108. *
  109. * @see DomImpl
  110. * @see #setCheckingCharacters
  111. */
  112. protected DomDocument(DOMImplementation impl)
  113. {
  114. super(DOCUMENT_NODE, null);
  115. implementation = impl;
  116. }
  117. /**
  118. * Sets the <code>building</code> flag.
  119. * Mutation events in the document are not reported.
  120. */
  121. public void setBuilding(boolean flag)
  122. {
  123. building = flag;
  124. }
  125. /**
  126. * Sets whether to check for document well-formedness.
  127. * If true, an exception will be raised if a second doctype or root
  128. * element node is added to the document.
  129. */
  130. public void setCheckWellformedness(boolean flag)
  131. {
  132. checkingWellformedness = flag;
  133. }
  134. /**
  135. * Sets whether to check for document characters.
  136. */
  137. public void setCheckingCharacters(boolean flag)
  138. {
  139. checkingCharacters = flag;
  140. }
  141. /**
  142. * Sets whether to default attributes for new elements.
  143. */
  144. public void setDefaultAttributes(boolean flag)
  145. {
  146. defaultAttributes = flag;
  147. }
  148. /**
  149. * <b>DOM L1</b>
  150. * Returns the constant "#document".
  151. */
  152. final public String getNodeName()
  153. {
  154. return "#document";
  155. }
  156. /**
  157. * <b>DOM L1</b>
  158. * Returns the document's root element, or null.
  159. */
  160. final public Element getDocumentElement()
  161. {
  162. for (DomNode ctx = first; ctx != null; ctx = ctx.next)
  163. {
  164. if (ctx.nodeType == ELEMENT_NODE)
  165. {
  166. return (Element) ctx;
  167. }
  168. }
  169. return null;
  170. }
  171. /**
  172. * <b>DOM L1</b>
  173. * Returns the document's DocumentType, or null.
  174. */
  175. final public DocumentType getDoctype()
  176. {
  177. for (DomNode ctx = first; ctx != null; ctx = ctx.next)
  178. {
  179. if (ctx.nodeType == DOCUMENT_TYPE_NODE)
  180. {
  181. return (DocumentType) ctx;
  182. }
  183. }
  184. return null;
  185. }
  186. /**
  187. * <b>DOM L1</b>
  188. * Returns the document's DOMImplementation.
  189. */
  190. final public DOMImplementation getImplementation()
  191. {
  192. return implementation;
  193. }
  194. /**
  195. * <b>DOM L1 (relocated in DOM L2)</b>
  196. * Returns the element with the specified "ID" attribute, or null.
  197. *
  198. * <p>Returns null unless {@link Consumer} was used to populate internal
  199. * DTD declaration information, using package-private APIs. If that
  200. * internal DTD information is available, the document may be searched for
  201. * the element with that ID.
  202. */
  203. public Element getElementById(String id)
  204. {
  205. if (id == null || id.length() == 0)
  206. {
  207. return null;
  208. }
  209. DomDoctype doctype = (DomDoctype) getDoctype();
  210. if (doctype != null && !doctype.hasIds())
  211. {
  212. doctype = null;
  213. }
  214. // yes, this is linear in size of document.
  215. // it'd be easy enough to maintain a hashtable.
  216. Node current = getDocumentElement();
  217. Node temp;
  218. if (current == null)
  219. {
  220. return null;
  221. }
  222. while (current != this)
  223. {
  224. // done?
  225. if (current.getNodeType() == ELEMENT_NODE)
  226. {
  227. DomElement element = (DomElement) current;
  228. if (element.userIdAttrs != null)
  229. {
  230. for (Iterator i = element.userIdAttrs.iterator();
  231. i.hasNext(); )
  232. {
  233. Node idAttr = (Node) i.next();
  234. if (id.equals(idAttr.getNodeValue()))
  235. {
  236. return element;
  237. }
  238. }
  239. }
  240. if (doctype != null)
  241. {
  242. DTDElementTypeInfo info =
  243. doctype.getElementTypeInfo(current.getNodeName());
  244. if (info != null &&
  245. id.equals(element.getAttribute(info.idAttrName)))
  246. {
  247. return element;
  248. }
  249. }
  250. // xml:id
  251. String xmlId = element.getAttribute("xml:id");
  252. if (xmlId == null)
  253. {
  254. xmlId = element.getAttributeNS(XMLConstants.XML_NS_URI,
  255. "id");
  256. }
  257. if (id.equals(xmlId))
  258. {
  259. return element;
  260. }
  261. }
  262. // descend?
  263. if (current.hasChildNodes())
  264. {
  265. current = current.getFirstChild();
  266. continue;
  267. }
  268. // lateral?
  269. temp = current.getNextSibling();
  270. if (temp != null)
  271. {
  272. current = temp;
  273. continue;
  274. }
  275. // back up ...
  276. do
  277. {
  278. temp = current.getParentNode();
  279. if (temp == null)
  280. {
  281. return null;
  282. }
  283. current = temp;
  284. temp = current.getNextSibling();
  285. }
  286. while (temp == null);
  287. current = temp;
  288. }
  289. return null;
  290. }
  291. private void checkNewChild(Node newChild)
  292. {
  293. if (newChild.getNodeType() == ELEMENT_NODE
  294. && getDocumentElement() != null)
  295. {
  296. throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR,
  297. "document element already present: " +
  298. getDocumentElement(), newChild, 0);
  299. }
  300. if (newChild.getNodeType() == DOCUMENT_TYPE_NODE
  301. && getDoctype() != null)
  302. {
  303. throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR,
  304. "document type already present: " +
  305. getDoctype(), newChild, 0);
  306. }
  307. }
  308. /**
  309. * <b>DOM L1</b>
  310. * Appends the specified node to this node's list of children,
  311. * enforcing the constraints that there be only one root element
  312. * and one document type child.
  313. */
  314. public Node appendChild(Node newChild)
  315. {
  316. if (checkingWellformedness)
  317. {
  318. checkNewChild(newChild);
  319. }
  320. return super.appendChild(newChild);
  321. }
  322. /**
  323. * <b>DOM L1</b>
  324. * Inserts the specified node in this node's list of children,
  325. * enforcing the constraints that there be only one root element
  326. * and one document type child.
  327. */
  328. public Node insertBefore(Node newChild, Node refChild)
  329. {
  330. if (checkingWellformedness)
  331. {
  332. checkNewChild(newChild);
  333. }
  334. return super.insertBefore(newChild, refChild);
  335. }
  336. /**
  337. * <b>DOM L1</b>
  338. * Replaces the specified node in this node's list of children,
  339. * enforcing the constraints that there be only one root element
  340. * and one document type child.
  341. */
  342. public Node replaceChild(Node newChild, Node refChild)
  343. {
  344. if (checkingWellformedness &&
  345. ((newChild.getNodeType() == ELEMENT_NODE &&
  346. refChild.getNodeType() != ELEMENT_NODE) ||
  347. (newChild.getNodeType() == DOCUMENT_TYPE_NODE &&
  348. refChild.getNodeType() != DOCUMENT_TYPE_NODE)))
  349. {
  350. checkNewChild(newChild);
  351. }
  352. return super.replaceChild(newChild, refChild);
  353. }
  354. // NOTE: DOM can't really tell when the name of an entity,
  355. // notation, or PI must follow the namespace rules (excluding
  356. // colons) instead of the XML rules (which allow them without
  357. // much restriction). That's an API issue. verifyXmlName
  358. // aims to enforce the XML rules, not the namespace rules.
  359. /**
  360. * Throws a DOM exception if the specified name is not a legal XML 1.0
  361. * Name.
  362. * @deprecated This method is deprecated and may be removed in future
  363. * versions of GNU JAXP
  364. */
  365. public static void verifyXmlName(String name)
  366. {
  367. // XXX why is this public?
  368. checkName(name, false);
  369. }
  370. static void checkName(String name, boolean xml11)
  371. {
  372. if (name == null)
  373. {
  374. throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0);
  375. }
  376. int len = name.length();
  377. if (len == 0)
  378. {
  379. throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0);
  380. }
  381. // dog: rewritten to use the rules for XML 1.0 and 1.1
  382. // Name start character
  383. char c = name.charAt(0);
  384. if (xml11)
  385. {
  386. // XML 1.1
  387. if ((c < 0x0041 || c > 0x005a) &&
  388. (c < 0x0061 || c > 0x007a) &&
  389. c != ':' && c != '_' &&
  390. (c < 0x00c0 || c > 0x00d6) &&
  391. (c < 0x00d8 || c > 0x00f6) &&
  392. (c < 0x00f8 || c > 0x02ff) &&
  393. (c < 0x0370 || c > 0x037d) &&
  394. (c < 0x037f || c > 0x1fff) &&
  395. (c < 0x200c || c > 0x200d) &&
  396. (c < 0x2070 || c > 0x218f) &&
  397. (c < 0x2c00 || c > 0x2fef) &&
  398. (c < 0x3001 || c > 0xd7ff) &&
  399. (c < 0xf900 || c > 0xfdcf) &&
  400. (c < 0xfdf0 || c > 0xfffd) &&
  401. (c < 0x10000 || c > 0xeffff))
  402. {
  403. throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
  404. name, null, c);
  405. }
  406. }
  407. else
  408. {
  409. // XML 1.0
  410. int type = Character.getType(c);
  411. switch (type)
  412. {
  413. case Character.LOWERCASE_LETTER: // Ll
  414. case Character.UPPERCASE_LETTER: // Lu
  415. case Character.OTHER_LETTER: // Lo
  416. case Character.TITLECASE_LETTER: // Lt
  417. case Character.LETTER_NUMBER: // Nl
  418. if ((c > 0xf900 && c < 0xfffe) ||
  419. (c >= 0x20dd && c <= 0x20e0))
  420. {
  421. // Compatibility area and Unicode 2.0 exclusions
  422. throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
  423. name, null, c);
  424. }
  425. break;
  426. default:
  427. if (c != ':' && c != '_' && (c < 0x02bb || c > 0x02c1) &&
  428. c != 0x0559 && c != 0x06e5 && c != 0x06e6)
  429. {
  430. throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
  431. name, null, c);
  432. }
  433. }
  434. }
  435. // Subsequent characters
  436. for (int i = 1; i < len; i++)
  437. {
  438. c = name.charAt(i);
  439. if (xml11)
  440. {
  441. // XML 1.1
  442. if ((c < 0x0041 || c > 0x005a) &&
  443. (c < 0x0061 || c > 0x007a) &&
  444. (c < 0x0030 || c > 0x0039) &&
  445. c != ':' && c != '_' && c != '-' && c != '.' &&
  446. (c < 0x00c0 || c > 0x00d6) &&
  447. (c < 0x00d8 || c > 0x00f6) &&
  448. (c < 0x00f8 || c > 0x02ff) &&
  449. (c < 0x0370 || c > 0x037d) &&
  450. (c < 0x037f || c > 0x1fff) &&
  451. (c < 0x200c || c > 0x200d) &&
  452. (c < 0x2070 || c > 0x218f) &&
  453. (c < 0x2c00 || c > 0x2fef) &&
  454. (c < 0x3001 || c > 0xd7ff) &&
  455. (c < 0xf900 || c > 0xfdcf) &&
  456. (c < 0xfdf0 || c > 0xfffd) &&
  457. (c < 0x10000 || c > 0xeffff) &&
  458. c != 0x00b7 &&
  459. (c < 0x0300 || c > 0x036f) &&
  460. (c < 0x203f || c > 0x2040))
  461. {
  462. throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, name,
  463. null, c);
  464. }
  465. }
  466. else
  467. {
  468. // XML 1.0
  469. int type = Character.getType(c);
  470. switch (type)
  471. {
  472. case Character.LOWERCASE_LETTER: // Ll
  473. case Character.UPPERCASE_LETTER: // Lu
  474. case Character.DECIMAL_DIGIT_NUMBER: // Nd
  475. case Character.OTHER_LETTER: // Lo
  476. case Character.TITLECASE_LETTER: // Lt
  477. case Character.LETTER_NUMBER: // Nl
  478. case Character.COMBINING_SPACING_MARK: // Mc
  479. case Character.ENCLOSING_MARK: // Me
  480. case Character.NON_SPACING_MARK: // Mn
  481. case Character.MODIFIER_LETTER: // Lm
  482. if ((c > 0xf900 && c < 0xfffe) ||
  483. (c >= 0x20dd && c <= 0x20e0))
  484. {
  485. // Compatibility area and Unicode 2.0 exclusions
  486. throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
  487. name, null, c);
  488. }
  489. break;
  490. default:
  491. if (c != '-' && c != '.' && c != ':' && c != '_' &&
  492. c != 0x0387 && (c < 0x02bb || c > 0x02c1) &&
  493. c != 0x0559 && c != 0x06e5 && c != 0x06e6 && c != 0x00b7)
  494. {
  495. throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
  496. name, null, c);
  497. }
  498. }
  499. }
  500. }
  501. // FIXME characters with a font or compatibility decomposition (i.e.
  502. // those with a "compatibility formatting tag" in field 5 of the
  503. // database -- marked by field 5 beginning with a "<") are not allowed.
  504. }
  505. // package private
  506. static void checkNCName(String name, boolean xml11)
  507. {
  508. checkName(name, xml11);
  509. int len = name.length();
  510. int index = name.indexOf(':');
  511. if (index != -1)
  512. {
  513. if (index == 0 || index == (len - 1) || name.lastIndexOf(':') != index)
  514. {
  515. throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0);
  516. }
  517. }
  518. }
  519. // package private
  520. static void checkChar(String value, boolean xml11)
  521. {
  522. char[] chars = value.toCharArray();
  523. checkChar(chars, 0, chars.length, xml11);
  524. }
  525. static void checkChar(char[] buf, int off, int len, boolean xml11)
  526. {
  527. for (int i = 0; i < len; i++)
  528. {
  529. char c = buf[i];
  530. // assume surrogate pairing checks out OK, for simplicity
  531. if ((c >= 0x0020 && c <= 0xd7ff) ||
  532. (c == 0x000a || c == 0x000d || c == 0x0009) ||
  533. (c >= 0xe000 && c <= 0xfffd) ||
  534. (c >= 0x10000 && c <= 0x10ffff))
  535. {
  536. continue;
  537. }
  538. if (xml11)
  539. {
  540. if ((c >= 0x0001 && c <= 0x001f) ||
  541. (c >= 0x007f && c <= 0x0084) ||
  542. (c >= 0x0086 && c <= 0x009f))
  543. {
  544. continue;
  545. }
  546. }
  547. throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR,
  548. new String(buf, off, len), null, c);
  549. }
  550. }
  551. /**
  552. * <b>DOM L1</b>
  553. * Returns a newly created element with the specified name.
  554. * The node name of the created element will be equal to {@code name}.
  555. * The namespace, prefix and local name will all be {@code null}.
  556. */
  557. public Element createElement(String name)
  558. {
  559. Element element;
  560. if (checkingCharacters)
  561. {
  562. checkName(name, "1.1".equals(version));
  563. }
  564. if (name.startsWith("xml:"))
  565. {
  566. element = createElementNS(null, name);
  567. }
  568. else
  569. {
  570. DomElement domElement = new DomElement(this, null, name, null, null);
  571. element = domElement;
  572. }
  573. if (defaultAttributes)
  574. setDefaultAttributes(element, name);
  575. return element;
  576. }
  577. /**
  578. * <b>DOM L2</b>
  579. * Returns a newly created element with the specified name
  580. * and namespace information.
  581. */
  582. public Element createElementNS(String namespaceURI, String name)
  583. {
  584. if (checkingCharacters)
  585. {
  586. checkNCName(name, "1.1".equals(version));
  587. }
  588. if ("".equals(namespaceURI))
  589. {
  590. namespaceURI = null;
  591. }
  592. if (name.startsWith("xml:"))
  593. {
  594. if (namespaceURI != null
  595. && !XMLConstants.XML_NS_URI.equals(namespaceURI))
  596. {
  597. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  598. "xml namespace is always " +
  599. XMLConstants.XML_NS_URI, this, 0);
  600. }
  601. namespaceURI = XMLConstants.XML_NS_URI;
  602. }
  603. else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) ||
  604. name.startsWith("xmlns:"))
  605. {
  606. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  607. "xmlns is reserved", this, 0);
  608. }
  609. else if (namespaceURI == null && name.indexOf(':') != -1)
  610. {
  611. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  612. "prefixed name '" + name +
  613. "' needs a URI", this, 0);
  614. }
  615. Element element = new DomElement(this, namespaceURI, name);
  616. if (defaultAttributes)
  617. setDefaultAttributes(element, name);
  618. return element;
  619. }
  620. private void setDefaultAttributes(Element element, String name)
  621. {
  622. DomDoctype doctype = (DomDoctype) getDoctype();
  623. if (doctype == null)
  624. {
  625. return;
  626. }
  627. // default any attributes that need it
  628. DTDElementTypeInfo info = doctype.getElementTypeInfo(name);
  629. if (info != null)
  630. {
  631. for (Iterator i = info.attributes(); i != null && i.hasNext(); )
  632. {
  633. DTDAttributeTypeInfo attr = (DTDAttributeTypeInfo) i.next();
  634. String value = attr.value;
  635. if ("#IMPLIED".equals(attr.mode) && value == null)
  636. continue;
  637. DomAttr node = (DomAttr) createAttribute(attr.name);
  638. if (value == null)
  639. {
  640. value = "";
  641. }
  642. node.setValue(value);
  643. node.setSpecified(false);
  644. element.setAttributeNode(node);
  645. }
  646. }
  647. }
  648. /**
  649. * <b>DOM L1</b>
  650. * Returns a newly created document fragment.
  651. */
  652. public DocumentFragment createDocumentFragment()
  653. {
  654. return new DomDocumentFragment(this);
  655. }
  656. /**
  657. * <b>DOM L1</b>
  658. * Returns a newly created text node with the specified value.
  659. */
  660. public Text createTextNode(String value)
  661. {
  662. if (checkingCharacters)
  663. {
  664. checkChar(value, "1.1".equals(version));
  665. }
  666. return new DomText(this, value);
  667. }
  668. /**
  669. * Returns a newly created text node with the specified value.
  670. */
  671. public Text createTextNode(char[] buf, int off, int len)
  672. {
  673. if (checkingCharacters)
  674. {
  675. checkChar(buf, off, len, "1.1".equals(version));
  676. }
  677. return new DomText(this, buf, off, len);
  678. }
  679. /**
  680. * <b>DOM L1</b>
  681. * Returns a newly created comment node with the specified value.
  682. */
  683. public Comment createComment(String value)
  684. {
  685. if (checkingCharacters)
  686. {
  687. checkChar(value, "1.1".equals(version));
  688. }
  689. return new DomComment(this, value);
  690. }
  691. /**
  692. * <b>DOM L1</b>
  693. * Returns a newly created CDATA section node with the specified value.
  694. */
  695. public CDATASection createCDATASection(String value)
  696. {
  697. if (checkingCharacters)
  698. {
  699. checkChar(value, "1.1".equals(version));
  700. }
  701. return new DomCDATASection(this, value);
  702. }
  703. /**
  704. * Returns a newly created CDATA section node with the specified value.
  705. */
  706. public CDATASection createCDATASection(char[] buf, int off, int len)
  707. {
  708. if (checkingCharacters)
  709. {
  710. checkChar(buf, off, len, "1.1".equals(version));
  711. }
  712. return new DomCDATASection(this, buf, off, len);
  713. }
  714. /**
  715. * <b>DOM L1</b>
  716. * Returns a newly created processing instruction.
  717. */
  718. public ProcessingInstruction createProcessingInstruction(String target,
  719. String data)
  720. {
  721. if (checkingCharacters)
  722. {
  723. boolean xml11 = "1.1".equals(version);
  724. checkName(target, xml11);
  725. if ("xml".equalsIgnoreCase(target))
  726. {
  727. throw new DomDOMException(DOMException.SYNTAX_ERR,
  728. "illegal PI target name",
  729. this, 0);
  730. }
  731. checkChar(data, xml11);
  732. }
  733. return new DomProcessingInstruction(this, target, data);
  734. }
  735. /**
  736. * <b>DOM L1</b>
  737. * Returns a newly created attribute with the specified name.
  738. */
  739. public Attr createAttribute(String name)
  740. {
  741. if (checkingCharacters)
  742. {
  743. checkName(name, "1.1".equals(version));
  744. }
  745. if (name.startsWith("xml:"))
  746. {
  747. return createAttributeNS(XMLConstants.XML_NS_URI, name);
  748. }
  749. else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) ||
  750. name.startsWith("xmlns:"))
  751. {
  752. return createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, name);
  753. }
  754. else
  755. {
  756. DomAttr ret = new DomAttr(this, null, name, null, null);
  757. return ret;
  758. }
  759. }
  760. /**
  761. * <b>DOM L2</b>
  762. * Returns a newly created attribute with the specified name
  763. * and namespace information.
  764. */
  765. public Attr createAttributeNS(String namespaceURI, String name)
  766. {
  767. if (checkingCharacters)
  768. {
  769. checkNCName(name, "1.1".equals(version));
  770. }
  771. if ("".equals(namespaceURI))
  772. {
  773. namespaceURI = null;
  774. }
  775. if (name.startsWith ("xml:"))
  776. {
  777. if (namespaceURI == null)
  778. {
  779. namespaceURI = XMLConstants.XML_NS_URI;
  780. }
  781. else if (!XMLConstants.XML_NS_URI.equals(namespaceURI))
  782. {
  783. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  784. "xml namespace is always " +
  785. XMLConstants.XML_NS_URI,
  786. this, 0);
  787. }
  788. }
  789. else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) ||
  790. name.startsWith("xmlns:"))
  791. {
  792. if (namespaceURI == null)
  793. {
  794. namespaceURI = XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
  795. }
  796. else if (!XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI))
  797. {
  798. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  799. "xmlns namespace must be " +
  800. XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
  801. this, 0);
  802. }
  803. }
  804. else if (namespaceURI == null && name.indexOf(':') != -1)
  805. {
  806. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  807. "prefixed name needs a URI: " + name, this, 0);
  808. }
  809. return new DomAttr(this, namespaceURI, name);
  810. }
  811. /**
  812. * <b>DOM L1</b>
  813. * Returns a newly created reference to the specified entity.
  814. * The caller should populate this with the appropriate children
  815. * and then mark it as readonly.
  816. *
  817. * @see DomNode#makeReadonly
  818. */
  819. public EntityReference createEntityReference(String name)
  820. {
  821. DomEntityReference ret = new DomEntityReference(this, name);
  822. DocumentType doctype = getDoctype();
  823. if (doctype != null)
  824. {
  825. DomEntity ent = (DomEntity) doctype.getEntities().getNamedItem(name);
  826. if (ent != null)
  827. {
  828. for (DomNode ctx = ent.first; ctx != null; ctx = ctx.next)
  829. {
  830. ret.appendChild(ctx.cloneNode(true));
  831. }
  832. }
  833. }
  834. ret.makeReadonly();
  835. return ret;
  836. }
  837. /**
  838. * <b>DOM L2</b>
  839. * Makes a copy of the specified node, with all nodes "owned" by
  840. * this document and with children optionally copied. This type
  841. * of standard utility has become, well, a standard utility.
  842. *
  843. * <p> Note that EntityReference nodes created through this method (either
  844. * directly, or recursively) never have children, and that there is no
  845. * portable way to associate them with such children.
  846. *
  847. * <p> Note also that there is no requirement that the specified node
  848. * be associated with a different document. This differs from the
  849. * <em>cloneNode</em> operation in that the node itself is not given
  850. * an opportunity to participate, so that any information managed
  851. * by node subclasses will be lost.
  852. */
  853. public Node importNode(Node src, boolean deep)
  854. {
  855. Node dst = null;
  856. switch (src.getNodeType())
  857. {
  858. case TEXT_NODE:
  859. dst = createTextNode(src.getNodeValue());
  860. break;
  861. case CDATA_SECTION_NODE:
  862. dst = createCDATASection(src.getNodeValue());
  863. break;
  864. case COMMENT_NODE:
  865. dst = createComment(src.getNodeValue());
  866. break;
  867. case PROCESSING_INSTRUCTION_NODE:
  868. dst = createProcessingInstruction(src.getNodeName(),
  869. src.getNodeValue());
  870. break;
  871. case NOTATION_NODE:
  872. // NOTE: There's no standard way to create
  873. // these, or add them to a doctype. Useless.
  874. Notation notation = (Notation) src;
  875. dst = new DomNotation(this, notation.getNodeName(),
  876. notation.getPublicId(),
  877. notation.getSystemId());
  878. break;
  879. case ENTITY_NODE:
  880. // NOTE: There's no standard way to create
  881. // these, or add them to a doctype. Useless.
  882. Entity entity = (Entity) src;
  883. dst = new DomEntity(this, entity.getNodeName(),
  884. entity.getPublicId(),
  885. entity.getSystemId(),
  886. entity.getNotationName());
  887. if (deep)
  888. {
  889. for (Node ctx = src.getFirstChild(); ctx != null;
  890. ctx = ctx.getNextSibling())
  891. {
  892. dst.appendChild(importNode(ctx, deep));
  893. }
  894. }
  895. break;
  896. case ENTITY_REFERENCE_NODE:
  897. dst = createEntityReference(src.getNodeName());
  898. break;
  899. case DOCUMENT_FRAGMENT_NODE:
  900. dst = new DomDocumentFragment(this);
  901. if (deep)
  902. {
  903. for (Node ctx = src.getFirstChild(); ctx != null;
  904. ctx = ctx.getNextSibling())
  905. {
  906. dst.appendChild(importNode(ctx, deep));
  907. }
  908. }
  909. break;
  910. case ATTRIBUTE_NODE:
  911. String attr_nsuri = src.getNamespaceURI();
  912. if (attr_nsuri != null)
  913. {
  914. dst = createAttributeNS(attr_nsuri, src.getNodeName());
  915. }
  916. else
  917. {
  918. dst = createAttribute(src.getNodeName());
  919. }
  920. // this is _always_ done regardless of "deep" setting
  921. for (Node ctx = src.getFirstChild(); ctx != null;
  922. ctx = ctx.getNextSibling())
  923. {
  924. dst.appendChild(importNode(ctx, false));
  925. }
  926. break;
  927. case ELEMENT_NODE:
  928. String elem_nsuri = src.getNamespaceURI();
  929. if (elem_nsuri != null)
  930. {
  931. dst = createElementNS(elem_nsuri, src.getNodeName());
  932. }
  933. else
  934. {
  935. dst = createElement(src.getNodeName());
  936. }
  937. NamedNodeMap srcAttrs = src.getAttributes();
  938. NamedNodeMap dstAttrs = dst.getAttributes();
  939. int len = srcAttrs.getLength();
  940. for (int i = 0; i < len; i++)
  941. {
  942. Attr a = (Attr) srcAttrs.item(i);
  943. Attr dflt;
  944. // maybe update defaulted attributes
  945. dflt = (Attr) dstAttrs.getNamedItem(a.getNodeName());
  946. if (dflt != null)
  947. {
  948. String newval = a.getNodeValue();
  949. if (!dflt.getNodeValue().equals(newval)
  950. || a.getSpecified () == true)
  951. {
  952. dflt.setNodeValue (newval);
  953. }
  954. continue;
  955. }
  956. dstAttrs.setNamedItem((Attr) importNode(a, false));
  957. }
  958. if (deep)
  959. {
  960. for (Node ctx = src.getFirstChild(); ctx != null;
  961. ctx = ctx.getNextSibling())
  962. {
  963. dst.appendChild(importNode(ctx, true));
  964. }
  965. }
  966. break;
  967. // can't import document or doctype nodes
  968. case DOCUMENT_NODE:
  969. case DOCUMENT_TYPE_NODE:
  970. // FALLTHROUGH
  971. // can't import unrecognized or nonstandard nodes
  972. default:
  973. throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, null, src, 0);
  974. }
  975. // FIXME cleanup a bit -- for deep copies, copy those
  976. // children in one place, here (code sharing is healthy)
  977. if (src instanceof DomNode)
  978. {
  979. ((DomNode) src).notifyUserDataHandlers(UserDataHandler.NODE_IMPORTED,
  980. src, dst);
  981. }
  982. return dst;
  983. }
  984. /**
  985. * <b>DOM L2 (Traversal)</b>
  986. * Returns a newly created node iterator. Don't forget to detach
  987. * this iterator when you're done using it!
  988. *
  989. * @see DomIterator
  990. */
  991. public NodeIterator createNodeIterator(Node root,
  992. int whatToShow,
  993. NodeFilter filter,
  994. boolean expandEntities)
  995. {
  996. return new DomNodeIterator(root, whatToShow, filter, expandEntities,
  997. false);
  998. }
  999. public TreeWalker createTreeWalker(Node root,
  1000. int whatToShow,
  1001. NodeFilter filter,
  1002. boolean expandEntities)
  1003. {
  1004. return new DomNodeIterator(root, whatToShow, filter, expandEntities,
  1005. true);
  1006. }
  1007. // DOM Level 3 methods
  1008. /**
  1009. * DOM L3
  1010. */
  1011. public String getInputEncoding()
  1012. {
  1013. return inputEncoding;
  1014. }
  1015. public void setInputEncoding(String inputEncoding)
  1016. {
  1017. this.inputEncoding = inputEncoding;
  1018. }
  1019. /**
  1020. * DOM L3
  1021. */
  1022. public String getXmlEncoding()
  1023. {
  1024. return encoding;
  1025. }
  1026. public void setXmlEncoding(String encoding)
  1027. {
  1028. this.encoding = encoding;
  1029. }
  1030. public boolean getXmlStandalone()
  1031. {
  1032. return standalone;
  1033. }
  1034. public void setXmlStandalone(boolean xmlStandalone)
  1035. {
  1036. standalone = xmlStandalone;
  1037. }
  1038. public String getXmlVersion()
  1039. {
  1040. return version;
  1041. }
  1042. public void setXmlVersion(String xmlVersion)
  1043. {
  1044. if (xmlVersion == null)
  1045. {
  1046. xmlVersion = "1.0";
  1047. }
  1048. if ("1.0".equals(xmlVersion) ||
  1049. "1.1".equals(xmlVersion))
  1050. {
  1051. version = xmlVersion;
  1052. }
  1053. else
  1054. {
  1055. throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
  1056. }
  1057. }
  1058. public boolean getStrictErrorChecking()
  1059. {
  1060. return checkingCharacters;
  1061. }
  1062. public void setStrictErrorChecking(boolean strictErrorChecking)
  1063. {
  1064. checkingCharacters = strictErrorChecking;
  1065. }
  1066. public String lookupPrefix(String namespaceURI)
  1067. {
  1068. Node root = getDocumentElement();
  1069. return (root == null) ? null : root.lookupPrefix(namespaceURI);
  1070. }
  1071. public boolean isDefaultNamespace(String namespaceURI)
  1072. {
  1073. Node root = getDocumentElement();
  1074. return (root == null) ? false : root.isDefaultNamespace(namespaceURI);
  1075. }
  1076. public String lookupNamespaceURI(String prefix)
  1077. {
  1078. Node root = getDocumentElement();
  1079. return (root == null) ? null : root.lookupNamespaceURI(prefix);
  1080. }
  1081. public String getBaseURI()
  1082. {
  1083. return getDocumentURI();
  1084. /*
  1085. Node root = getDocumentElement();
  1086. if (root != null)
  1087. {
  1088. NamedNodeMap attrs = root.getAttributes();
  1089. Node xmlBase = attrs.getNamedItemNS(XMLConstants.XML_NS_URI, "base");
  1090. if (xmlBase != null)
  1091. {
  1092. return xmlBase.getNodeValue();
  1093. }
  1094. }
  1095. return systemId;
  1096. */
  1097. }
  1098. public String getDocumentURI()
  1099. {
  1100. return systemId;
  1101. }
  1102. public void setDocumentURI(String documentURI)
  1103. {
  1104. systemId = documentURI;
  1105. }
  1106. public Node adoptNode(Node source)
  1107. {
  1108. int sourceNodeType = source.getNodeType();
  1109. switch (sourceNodeType)
  1110. {
  1111. case DOCUMENT_NODE:
  1112. case DOCUMENT_TYPE_NODE:
  1113. throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
  1114. case ENTITY_NODE:
  1115. case NOTATION_NODE:
  1116. throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
  1117. }
  1118. if (source instanceof DomNode)
  1119. {
  1120. // GNU native
  1121. DomNode src = (DomNode) source;
  1122. DomNode dst = src;
  1123. if (dst.parent != null)
  1124. {
  1125. dst = (DomNode) dst.cloneNode(true);
  1126. }
  1127. dst.setOwner(this);
  1128. src.notifyUserDataHandlers(UserDataHandler.NODE_ADOPTED, src, dst);
  1129. return dst;
  1130. }
  1131. else
  1132. {
  1133. // Some other implementation
  1134. Node dst = null;
  1135. switch (sourceNodeType)
  1136. {
  1137. case Node.ATTRIBUTE_NODE:
  1138. {
  1139. Attr src = (Attr) source;
  1140. String nodeName = src.getNodeName();
  1141. String localName = src.getLocalName();
  1142. String namespaceUri = src.getNamespaceURI();
  1143. dst = (localName == null) ?
  1144. createAttribute(nodeName) :
  1145. createAttributeNS(namespaceUri, nodeName);
  1146. adoptChildren(src, dst);
  1147. break;
  1148. }
  1149. case Node.CDATA_SECTION_NODE:
  1150. {
  1151. CDATASection src = (CDATASection) source;
  1152. dst = createCDATASection(src.getData());
  1153. break;
  1154. }
  1155. case Node.COMMENT_NODE:
  1156. {
  1157. Comment src = (Comment) source;
  1158. dst = createComment(src.getData());
  1159. break;
  1160. }
  1161. case Node.DOCUMENT_FRAGMENT_NODE:
  1162. {
  1163. DocumentFragment src = (DocumentFragment) source;
  1164. dst = createDocumentFragment();
  1165. adoptChildren(src, dst);
  1166. break;
  1167. }
  1168. case Node.ELEMENT_NODE:
  1169. {
  1170. Element src = (Element) source;
  1171. String nodeName = src.getNodeName();
  1172. String localName = src.getLocalName();
  1173. String namespaceUri = src.getNamespaceURI();
  1174. dst = (localName == null) ?
  1175. createElement(nodeName) :
  1176. createElementNS(namespaceUri, nodeName);
  1177. adoptAttributes(src, dst);
  1178. adoptChildren(src, dst);
  1179. break;
  1180. }
  1181. case Node.ENTITY_REFERENCE_NODE:
  1182. {
  1183. EntityReference src = (EntityReference) source;
  1184. dst = createEntityReference(src.getNodeName());
  1185. adoptChildren(src, dst);
  1186. break;
  1187. }
  1188. case Node.PROCESSING_INSTRUCTION_NODE:
  1189. {
  1190. ProcessingInstruction src = (ProcessingInstruction) source;
  1191. dst = createProcessingInstruction(src.getTarget(),
  1192. src.getData());
  1193. break;
  1194. }
  1195. case Node.TEXT_NODE:
  1196. {
  1197. Text src = (Text) source;
  1198. dst = createTextNode(src.getData());
  1199. break;
  1200. }
  1201. }
  1202. return dst;
  1203. }
  1204. }
  1205. void adoptChildren(Node src, Node dst)
  1206. {
  1207. Node node = src.getFirstChild();
  1208. while (node != null)
  1209. {
  1210. Node next = node.getNextSibling();
  1211. dst.appendChild(adoptNode(node));
  1212. node = next;
  1213. }
  1214. }
  1215. void adoptAttributes(Node src, Node dst)
  1216. {
  1217. NamedNodeMap srcAttrs = src.getAttributes();
  1218. NamedNodeMap dstAttrs = dst.getAttributes();
  1219. int len = srcAttrs.getLength();
  1220. for (int i = 0; i < len; i++)
  1221. {
  1222. Node node = srcAttrs.item(i);
  1223. String localName = node.getLocalName();
  1224. if (localName == null)
  1225. {
  1226. dstAttrs.setNamedItem(adoptNode(node));
  1227. }
  1228. else
  1229. {
  1230. dstAttrs.setNamedItemNS(adoptNode(node));
  1231. }
  1232. }
  1233. }
  1234. public DOMConfiguration getDomConfig()
  1235. {
  1236. if (config == null)
  1237. {
  1238. config = new DomDocumentConfiguration();
  1239. }
  1240. return config;
  1241. }
  1242. public boolean isEqualNode(Node arg)
  1243. {
  1244. if (!super.isEqualNode(arg))
  1245. return false;
  1246. Document d = (Document) arg;
  1247. String dversion = d.getXmlVersion();
  1248. if (dversion == null || !dversion.equals(version))
  1249. return false;
  1250. boolean dstandalone = d.getXmlStandalone();
  1251. if (dstandalone != standalone)
  1252. return false;
  1253. String dencoding = d.getXmlEncoding();
  1254. if (dencoding == null || dencoding.equalsIgnoreCase("UTF-8"))
  1255. {
  1256. if (encoding != null && !encoding.equalsIgnoreCase("UTF-8"))
  1257. return false;
  1258. }
  1259. else
  1260. {
  1261. if (!dencoding.equals(encoding))
  1262. return false;
  1263. }
  1264. return true;
  1265. }
  1266. public void normalizeDocument()
  1267. {
  1268. boolean save = building;
  1269. building = true;
  1270. normalizeNode(this);
  1271. building = save;
  1272. }
  1273. void normalizeNode(DomNode node)
  1274. {
  1275. node.normalize();
  1276. if (config != null)
  1277. {
  1278. switch (node.nodeType)
  1279. {
  1280. case CDATA_SECTION_NODE:
  1281. if (!config.cdataSections)
  1282. {
  1283. // replace CDATA section with text node
  1284. Text text = createTextNode(node.getNodeValue());
  1285. node.parent.insertBefore(text, node);
  1286. node.parent.removeChild(node);
  1287. // merge adjacent text nodes
  1288. String data = text.getWholeText();
  1289. node = (DomNode) text.replaceWholeText(data);
  1290. }
  1291. else if (config.splitCdataSections)
  1292. {
  1293. String value = node.getNodeValue();
  1294. int i = value.indexOf("]]>");
  1295. while (i != -1)
  1296. {
  1297. Node node2 = createCDATASection(value.substring(0, i));
  1298. node.parent.insertBefore(node2, node);
  1299. value = value.substring(i + 3);
  1300. node.setNodeValue(value);
  1301. i = value.indexOf("]]>");
  1302. }
  1303. }
  1304. break;
  1305. case COMMENT_NODE:
  1306. if (!config.comments)
  1307. {
  1308. node.parent.removeChild(node);
  1309. }
  1310. break;
  1311. case TEXT_NODE:
  1312. if (!config.elementContentWhitespace &&
  1313. ((Text) node).isElementContentWhitespace())
  1314. {
  1315. node.parent.removeChild(node);
  1316. }
  1317. break;
  1318. case ENTITY_REFERENCE_NODE:
  1319. if (!config.entities)
  1320. {
  1321. for (DomNode ctx = node.first; ctx != null; )
  1322. {
  1323. DomNode ctxNext = ctx.next;
  1324. node.parent.insertBefore(ctx, node);
  1325. ctx = ctxNext;
  1326. }
  1327. node.parent.removeChild(node);
  1328. }
  1329. break;
  1330. case ELEMENT_NODE:
  1331. if (!config.namespaceDeclarations)
  1332. {
  1333. DomNamedNodeMap attrs =
  1334. (DomNamedNodeMap) node.getAttributes();
  1335. boolean aro = attrs.readonly;
  1336. attrs.readonly = false; // Ensure we can delete if necessary
  1337. int len = attrs.getLength();
  1338. for (int i = 0; i < len; i++)
  1339. {
  1340. Node attr = attrs.item(i);
  1341. String namespace = attr.getNamespaceURI();
  1342. if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespace))
  1343. {
  1344. attrs.removeNamedItemNS(namespace,
  1345. attr.getLocalName());
  1346. i--;
  1347. len--;
  1348. }
  1349. }
  1350. attrs.readonly = aro;
  1351. }
  1352. break;
  1353. }
  1354. }
  1355. for (DomNode ctx = node.first; ctx != null; )
  1356. {
  1357. DomNode ctxNext = ctx.next;
  1358. normalizeNode(ctx);
  1359. ctx = ctxNext;
  1360. }
  1361. }
  1362. public Node renameNode(Node n, String namespaceURI, String qualifiedName)
  1363. throws DOMException
  1364. {
  1365. if (n instanceof DomNsNode)
  1366. {
  1367. DomNsNode src = (DomNsNode) n;
  1368. if (src == null)
  1369. {
  1370. throw new DomDOMException(DOMException.NOT_FOUND_ERR);
  1371. }
  1372. if (src.owner != this)
  1373. {
  1374. throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR,
  1375. null, src, 0);
  1376. }
  1377. boolean xml11 = "1.1".equals(version);
  1378. checkName(qualifiedName, xml11);
  1379. int ci = qualifiedName.indexOf(':');
  1380. if ("".equals(namespaceURI))
  1381. {
  1382. namespaceURI = null;
  1383. }
  1384. if (namespaceURI != null)
  1385. {
  1386. checkNCName(qualifiedName, xml11);
  1387. String prefix = (ci == -1) ? "" :
  1388. qualifiedName.substring(0, ci);
  1389. if (XMLConstants.XML_NS_PREFIX.equals(prefix) &&
  1390. !XMLConstants.XML_NS_URI.equals(namespaceURI))
  1391. {
  1392. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  1393. "xml namespace must be " +
  1394. XMLConstants.XML_NS_URI, src, 0);
  1395. }
  1396. else if (src.nodeType == ATTRIBUTE_NODE &&
  1397. (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) ||
  1398. XMLConstants.XMLNS_ATTRIBUTE.equals(qualifiedName)) &&
  1399. !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI))
  1400. {
  1401. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  1402. "xmlns namespace must be " +
  1403. XMLConstants.XMLNS_ATTRIBUTE_NS_URI, src, 0);
  1404. }
  1405. if (XMLConstants.XML_NS_URI.equals(namespaceURI) &&
  1406. !XMLConstants.XML_NS_PREFIX.equals(prefix))
  1407. {
  1408. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  1409. "xml namespace must be " +
  1410. XMLConstants.XML_NS_URI, src, 0);
  1411. }
  1412. else if (src.nodeType == ATTRIBUTE_NODE &&
  1413. XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI) &&
  1414. !(XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) ||
  1415. XMLConstants.XMLNS_ATTRIBUTE.equals(qualifiedName)))
  1416. {
  1417. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  1418. "xmlns namespace must be " +
  1419. XMLConstants.XMLNS_ATTRIBUTE_NS_URI, src, 0);
  1420. }
  1421. }
  1422. src.setNodeName(qualifiedName);
  1423. src.setNamespaceURI(namespaceURI);
  1424. src.notifyUserDataHandlers(UserDataHandler.NODE_RENAMED, src, src);
  1425. // TODO MutationNameEvents
  1426. // DOMElementNameChanged or DOMAttributeNameChanged
  1427. return src;
  1428. }
  1429. throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, null, n, 0);
  1430. }
  1431. // -- XPathEvaluator --
  1432. public XPathExpression createExpression(String expression,
  1433. XPathNSResolver resolver)
  1434. throws XPathException, DOMException
  1435. {
  1436. return new DomXPathExpression(this, expression, resolver);
  1437. }
  1438. public XPathNSResolver createNSResolver(Node nodeResolver)
  1439. {
  1440. return new DomXPathNSResolver(nodeResolver);
  1441. }
  1442. public Object evaluate(String expression,
  1443. Node contextNode,
  1444. XPathNSResolver resolver,
  1445. short type,
  1446. Object result)
  1447. throws XPathException, DOMException
  1448. {
  1449. XPathExpression xpe =
  1450. new DomXPathExpression(this, expression, resolver);
  1451. return xpe.evaluate(contextNode, type, result);
  1452. }
  1453. }