fork download
  1. import javax.xml.parsers.DocumentBuilder;
  2. import javax.xml.parsers.DocumentBuilderFactory;
  3.  
  4. import org.w3c.dom.Attr;
  5. import org.w3c.dom.DOMException;
  6. import org.w3c.dom.Document;
  7. import org.w3c.dom.Element;
  8. import org.w3c.dom.NamedNodeMap;
  9. import org.w3c.dom.Node;
  10. import org.w3c.dom.NodeList;
  11. import org.w3c.dom.TypeInfo;
  12. import org.w3c.dom.UserDataHandler;
  13.  
  14. public class Main {
  15.  
  16. public static void main(String[] args) {
  17. try {
  18. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  19. DocumentBuilder builder = factory.newDocumentBuilder();
  20. Document document = builder.newDocument();
  21.  
  22. Node root = new Main().new MyElement(document.createElement("a"));
  23. Node e1 = document.createElement("a");
  24. Node e2 = document.createElement("a");
  25. Node e3 = new Main().new MyElement(document.createElement("a"));
  26.  
  27. root.appendChild(e1);
  28. System.out.println("Succeed: appendChild(e1)");
  29. root.appendChild(e2);
  30. System.out.println("Succeed: appendChild(e2)");
  31. root.appendChild(e3);
  32. System.out.println("Succeed: appendChild(e3)");
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. public class MyElement implements Element {
  39. private final Element element;
  40.  
  41. public MyElement(Element element) {
  42. super();
  43. this.element = element;
  44. }
  45.  
  46. @Override
  47. public String getNodeName() {
  48. return element.getNodeName();
  49. }
  50.  
  51. @Override
  52. public String getNodeValue() throws DOMException {
  53. return element.getNodeValue();
  54. }
  55.  
  56. @Override
  57. public void setNodeValue(String nodeValue) throws DOMException {
  58. element.setNodeValue(nodeValue);
  59. }
  60.  
  61. @Override
  62. public short getNodeType() {
  63. return element.getNodeType();
  64. }
  65.  
  66. @Override
  67. public Node getParentNode() {
  68. return element.getParentNode();
  69. }
  70.  
  71. @Override
  72. public NodeList getChildNodes() {
  73. return element.getChildNodes();
  74. }
  75.  
  76. @Override
  77. public Node getFirstChild() {
  78. return element.getFirstChild();
  79. }
  80.  
  81. @Override
  82. public Node getLastChild() {
  83. return element.getLastChild();
  84. }
  85.  
  86. @Override
  87. public Node getPreviousSibling() {
  88. return element.getPreviousSibling();
  89. }
  90.  
  91. @Override
  92. public Node getNextSibling() {
  93. return element.getNextSibling();
  94. }
  95.  
  96. @Override
  97. public NamedNodeMap getAttributes() {
  98. return element.getAttributes();
  99. }
  100.  
  101. @Override
  102. public Document getOwnerDocument() {
  103. return element.getOwnerDocument();
  104. }
  105.  
  106. @Override
  107. public Node insertBefore(Node newChild, Node refChild) throws DOMException {
  108. return element.insertBefore(newChild, refChild);
  109. }
  110.  
  111. @Override
  112. public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
  113. return element.replaceChild(newChild, oldChild);
  114. }
  115.  
  116. @Override
  117. public Node removeChild(Node oldChild) throws DOMException {
  118. return element.removeChild(oldChild);
  119. }
  120.  
  121. @Override
  122. public Node appendChild(Node newChild) throws DOMException {
  123. return element.appendChild(newChild);
  124. }
  125.  
  126. @Override
  127. public boolean hasChildNodes() {
  128. return element.hasChildNodes();
  129. }
  130.  
  131. @Override
  132. public Node cloneNode(boolean deep) {
  133. return element.cloneNode(deep);
  134. }
  135.  
  136. @Override
  137. public void normalize() {
  138. element.normalize();
  139. }
  140.  
  141. @Override
  142. public boolean isSupported(String feature, String version) {
  143. return element.isSupported(feature, version);
  144. }
  145.  
  146. @Override
  147. public String getNamespaceURI() {
  148. return element.getNamespaceURI();
  149. }
  150.  
  151. @Override
  152. public String getPrefix() {
  153. return element.getPrefix();
  154. }
  155.  
  156. @Override
  157. public void setPrefix(String prefix) throws DOMException {
  158. element.setPrefix(prefix);
  159. }
  160.  
  161. @Override
  162. public String getLocalName() {
  163. return element.getLocalName();
  164. }
  165.  
  166. @Override
  167. public boolean hasAttributes() {
  168. return element.hasAttributes();
  169. }
  170.  
  171. @Override
  172. public String getBaseURI() {
  173. return element.getBaseURI();
  174. }
  175.  
  176. @Override
  177. public short compareDocumentPosition(Node other) throws DOMException {
  178. return element.compareDocumentPosition(other);
  179. }
  180.  
  181. @Override
  182. public String getTextContent() throws DOMException {
  183. return element.getTextContent();
  184. }
  185.  
  186. @Override
  187. public void setTextContent(String textContent) throws DOMException {
  188. element.setTextContent(textContent);
  189. }
  190.  
  191. @Override
  192. public boolean isSameNode(Node other) {
  193. return element.isSameNode(other);
  194. }
  195.  
  196. @Override
  197. public String lookupPrefix(String namespaceURI) {
  198. return element.lookupPrefix(namespaceURI);
  199. }
  200.  
  201. @Override
  202. public boolean isDefaultNamespace(String namespaceURI) {
  203. return element.isDefaultNamespace(namespaceURI);
  204. }
  205.  
  206. @Override
  207. public String lookupNamespaceURI(String prefix) {
  208. return element.lookupNamespaceURI(prefix);
  209. }
  210.  
  211. @Override
  212. public boolean isEqualNode(Node arg) {
  213. return element.isEqualNode(arg);
  214. }
  215.  
  216. @Override
  217. public Object getFeature(String feature, String version) {
  218. return element.getFeature(feature, version);
  219. }
  220.  
  221. @Override
  222. public Object setUserData(String key, Object data, UserDataHandler handler) {
  223. return element.setUserData(key, data, handler);
  224. }
  225.  
  226. @Override
  227. public Object getUserData(String key) {
  228. return element.getUserData(key);
  229. }
  230.  
  231. @Override
  232. public String getTagName() {
  233. return element.getTagName();
  234. }
  235.  
  236. @Override
  237. public String getAttribute(String name) {
  238. return element.getAttribute(name);
  239. }
  240.  
  241. @Override
  242. public void setAttribute(String name, String value) throws DOMException {
  243. element.setAttribute(name, value);
  244. }
  245.  
  246. @Override
  247. public void removeAttribute(String name) throws DOMException {
  248. element.removeAttribute(name);
  249. }
  250.  
  251. @Override
  252. public Attr getAttributeNode(String name) {
  253. return element.getAttributeNode(name);
  254. }
  255.  
  256. @Override
  257. public Attr setAttributeNode(Attr newAttr) throws DOMException {
  258. return element.setAttributeNode(newAttr);
  259. }
  260.  
  261. @Override
  262. public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
  263. return element.removeAttributeNode(oldAttr);
  264. }
  265.  
  266. @Override
  267. public NodeList getElementsByTagName(String name) {
  268. return element.getElementsByTagName(name);
  269. }
  270.  
  271. @Override
  272. public String getAttributeNS(String namespaceURI, String localName) throws DOMException {
  273. return element.getAttributeNS(namespaceURI, localName);
  274. }
  275.  
  276. @Override
  277. public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException {
  278. element.setAttributeNS(namespaceURI, qualifiedName, value);
  279. }
  280.  
  281. @Override
  282. public void removeAttributeNS(String namespaceURI, String localName) throws DOMException {
  283. element.removeAttributeNS(namespaceURI, localName);
  284. }
  285.  
  286. @Override
  287. public Attr getAttributeNodeNS(String namespaceURI, String localName) throws DOMException {
  288. return element.getAttributeNodeNS(namespaceURI, localName);
  289. }
  290.  
  291. @Override
  292. public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
  293. return element.setAttributeNodeNS(newAttr);
  294. }
  295.  
  296. @Override
  297. public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException {
  298. return element.getElementsByTagNameNS(namespaceURI, localName);
  299. }
  300.  
  301. @Override
  302. public boolean hasAttribute(String name) {
  303. return element.hasAttribute(name);
  304. }
  305.  
  306. @Override
  307. public boolean hasAttributeNS(String namespaceURI, String localName) throws DOMException {
  308. return element.hasAttributeNS(namespaceURI, localName);
  309. }
  310.  
  311. @Override
  312. public TypeInfo getSchemaTypeInfo() {
  313. return element.getSchemaTypeInfo();
  314. }
  315.  
  316. @Override
  317. public void setIdAttribute(String name, boolean isId) throws DOMException {
  318. element.setIdAttribute(name, isId);
  319. }
  320.  
  321. @Override
  322. public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException {
  323. element.setIdAttributeNS(namespaceURI, localName, isId);
  324. }
  325.  
  326. @Override
  327. public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException {
  328. element.setIdAttributeNode(idAttr, isId);
  329. }
  330. }
  331. }
Success #stdin #stdout #stderr 0.11s 380800KB
stdin
Standard input is empty
stdout
Succeed: appendChild(e1)
Succeed: appendChild(e2)
stderr
java.lang.ClassCastException: Main$MyElement cannot be cast to com.sun.org.apache.xerces.internal.dom.ChildNode
	at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(ParentNode.java:389)
	at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(ParentNode.java:288)
	at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(NodeImpl.java:238)
	at Main$MyElement.appendChild(Main.java:123)
	at Main.main(Main.java:31)