PageRenderTime 34ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/project/bridge/axiom/src/main/java/org/genxdm/bridge/axiom/enhanced/AxiomSAModel.java

http://genxdm.googlecode.com/
Java | 613 lines | 264 code | 65 blank | 284 comment | 0 complexity | 0ed0c496e3ee61a8aaa0b48f24cae90e MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright (c) 2009-2011 TIBCO Software Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.genxdm.bridge.axiom.enhanced;
  17. import java.net.URI;
  18. import java.util.List;
  19. import javax.xml.namespace.QName;
  20. import org.apache.axiom.om.OMAttribute;
  21. import org.apache.axiom.om.OMContainer;
  22. import org.apache.axiom.om.OMElement;
  23. import org.apache.axiom.om.OMNode;
  24. import org.genxdm.NodeKind;
  25. import org.genxdm.bridge.axiom.AxiomModel;
  26. import org.genxdm.bridgekit.atoms.XmlAtom;
  27. import org.genxdm.exceptions.GenXDMException;
  28. import org.genxdm.io.ContentHandler;
  29. import org.genxdm.names.NamespaceBinding;
  30. import org.genxdm.typed.TypedModel;
  31. import org.genxdm.typed.io.SequenceHandler;
  32. import org.genxdm.typed.types.AtomBridge;
  33. /**
  34. * Note that some implementations of Axiom implement {@link OMNode} on their
  35. * document node but don't support the OMNode interface methods. This means
  36. * that we have to check for a document first in order not to throw
  37. * unchecked exceptions.
  38. *
  39. */
  40. final class AxiomSAModel implements TypedModel<Object, XmlAtom>
  41. {
  42. public AxiomSAModel(final AxiomModel delegate, final AtomBridge<XmlAtom> atomBridge)
  43. {
  44. this.delegate = delegate;
  45. this.atomBridge = atomBridge;
  46. }
  47. public int compare(final Object one, final Object two)
  48. {
  49. return delegate.compare(one, two);
  50. }
  51. public Iterable<Object> getAncestorAxis(Object origin)
  52. {
  53. return delegate.getAncestorAxis(origin);
  54. }
  55. public Iterable<Object> getAncestorOrSelfAxis(Object origin)
  56. {
  57. return delegate.getAncestorOrSelfAxis(origin);
  58. }
  59. public OMAttribute getAttribute(final Object parent, final String namespaceURI, final String localName)
  60. {
  61. return delegate.getAttribute(parent, namespaceURI, localName);
  62. }
  63. public Iterable<Object> getAttributeAxis(final Object node, final boolean inherit)
  64. {
  65. return delegate.getAttributeAxis(node, inherit);
  66. }
  67. public Iterable<QName> getAttributeNames(final Object node, final boolean orderCanonical)
  68. {
  69. return delegate.getAttributeNames(node, orderCanonical);
  70. }
  71. public String getAttributeStringValue(final Object parent, final String namespaceURI, final String localName)
  72. {
  73. return delegate.getAttributeStringValue(parent, namespaceURI, localName);
  74. }
  75. public List<XmlAtom> getAttributeValue(final Object parent, final String namespaceURI, final String localName)
  76. {
  77. // TODO: implement
  78. throw new UnsupportedOperationException();
  79. }
  80. public QName getAttributeTypeName(final Object parent, final String namespaceURI, final String localName)
  81. {
  82. // TODO: implement
  83. throw new UnsupportedOperationException();
  84. }
  85. public URI getBaseURI(final Object node)
  86. {
  87. return delegate.getBaseURI(node);
  88. }
  89. public Iterable<Object> getChildAxis(final Object node)
  90. {
  91. return delegate.getChildAxis(node);
  92. }
  93. public Iterable<Object> getChildElements(final Object node)
  94. {
  95. return delegate.getChildElements(node);
  96. }
  97. public Iterable<Object> getChildElementsByName(final Object node, final String namespaceURI, final String localName)
  98. {
  99. return delegate.getChildElementsByName(node, namespaceURI, localName);
  100. }
  101. public Iterable<Object> getDescendantAxis(Object origin)
  102. {
  103. return delegate.getDescendantAxis(origin);
  104. }
  105. public Iterable<Object> getDescendantOrSelfAxis(Object origin)
  106. {
  107. return delegate.getDescendantOrSelfAxis(origin);
  108. }
  109. public URI getDocumentURI(final Object node)
  110. {
  111. return delegate.getDocumentURI(node);
  112. }
  113. public OMElement getElementById(final Object context, final String id)
  114. {
  115. return delegate.getElementById(context, id);
  116. }
  117. public OMNode getFirstChild(final Object origin)
  118. {
  119. return delegate.getFirstChild(origin);
  120. }
  121. public Object getFirstChildElement(final Object origin)
  122. {
  123. return delegate.getFirstChildElement(origin);
  124. }
  125. public Object getFirstChildElementByName(Object origin, String namespaceURI, String localName)
  126. {
  127. return delegate.getFirstChildElementByName(origin, namespaceURI, localName);
  128. }
  129. public Iterable<Object> getFollowingAxis(Object origin)
  130. {
  131. return delegate.getFollowingAxis(origin);
  132. }
  133. public Iterable<Object> getFollowingSiblingAxis(Object origin)
  134. {
  135. return delegate.getFollowingSiblingAxis(origin);
  136. }
  137. public Object getLastChild(final Object origin)
  138. {
  139. return delegate.getLastChild(origin);
  140. }
  141. public String getLocalName(final Object node)
  142. {
  143. return delegate.getLocalName(node);
  144. }
  145. public Iterable<Object> getNamespaceAxis(final Object node, final boolean inherit)
  146. {
  147. return delegate.getNamespaceAxis(node, inherit);
  148. }
  149. public Iterable<NamespaceBinding> getNamespaceBindings(final Object node)
  150. {
  151. return delegate.getNamespaceBindings(node);
  152. }
  153. public String getNamespaceForPrefix(final Object node, final String prefix)
  154. {
  155. return delegate.getNamespaceForPrefix(node, prefix);
  156. }
  157. public Iterable<String> getNamespaceNames(final Object node, final boolean orderCanonical)
  158. {
  159. return delegate.getNamespaceNames(node, orderCanonical);
  160. }
  161. public String getNamespaceURI(final Object node)
  162. {
  163. return delegate.getNamespaceURI(node);
  164. }
  165. public OMNode getNextSibling(final Object origin)
  166. {
  167. return delegate.getNextSibling(origin);
  168. }
  169. public Object getNextSiblingElement(Object node)
  170. {
  171. return delegate.getNextSiblingElement(node);
  172. }
  173. public Object getNextSiblingElementByName(Object node, String namespaceURI, String localName)
  174. {
  175. return delegate.getNextSiblingElementByName(node, namespaceURI, localName);
  176. }
  177. public Object getNodeId(final Object node)
  178. {
  179. return delegate.getNodeId(node);
  180. }
  181. public NodeKind getNodeKind(final Object origin)
  182. {
  183. return delegate.getNodeKind(origin);
  184. }
  185. public OMContainer getParent(final Object origin)
  186. {
  187. return delegate.getParent(origin);
  188. }
  189. public Iterable<Object> getPrecedingAxis(Object origin)
  190. {
  191. return delegate.getPrecedingAxis(origin);
  192. }
  193. public Iterable<Object> getPrecedingSiblingAxis(Object origin)
  194. {
  195. return delegate.getPrecedingSiblingAxis(origin);
  196. }
  197. public String getPrefix(final Object node)
  198. {
  199. return delegate.getPrefix(node);
  200. }
  201. public Object getPreviousSibling(final Object origin)
  202. {
  203. return delegate.getPreviousSibling(origin);
  204. }
  205. public Object getRoot(final Object origin)
  206. {
  207. return delegate.getRoot(origin);
  208. }
  209. public String getStringValue(final Object node)
  210. {
  211. return delegate.getStringValue(node);
  212. }
  213. public List<XmlAtom> getValue(Object node)
  214. {
  215. // TODO Auto-generated method stub
  216. throw new AssertionError("TODO");
  217. }
  218. public QName getTypeName(Object node)
  219. {
  220. // TODO: implement
  221. return null;
  222. }
  223. public boolean hasAttributes(final Object node)
  224. {
  225. return delegate.hasAttributes(node);
  226. }
  227. public boolean hasChildren(final Object node)
  228. {
  229. return delegate.hasChildren(node);
  230. }
  231. public boolean hasNamespaces(final Object node)
  232. {
  233. return delegate.hasNamespaces(node);
  234. }
  235. public boolean hasNextSibling(final Object node)
  236. {
  237. return delegate.hasNextSibling(node);
  238. }
  239. public boolean hasParent(final Object node)
  240. {
  241. return delegate.hasParent(node);
  242. }
  243. public boolean hasPreviousSibling(final Object node)
  244. {
  245. return delegate.hasPreviousSibling(node);
  246. }
  247. public boolean isAttribute(final Object node)
  248. {
  249. return delegate.isAttribute(node);
  250. }
  251. public boolean isElement(final Object node)
  252. {
  253. return delegate.isElement(node);
  254. }
  255. public boolean isId(final Object node)
  256. {
  257. return delegate.isId(node);
  258. }
  259. public boolean isIdRefs(final Object node)
  260. {
  261. return delegate.isIdRefs(node);
  262. }
  263. public boolean isNamespace(final Object node)
  264. {
  265. return delegate.isNamespace(node);
  266. }
  267. public boolean isText(final Object node)
  268. {
  269. return delegate.isText(node);
  270. }
  271. public boolean matches(final Object node, final NodeKind kind, String namespaceURI, String localName)
  272. {
  273. return delegate.matches(node, kind, namespaceURI, localName);
  274. }
  275. public boolean matches(final Object node, final String namespaceArg, final String localNameArg)
  276. {
  277. return delegate.matches(node, namespaceArg, localNameArg);
  278. }
  279. public void stream(Object node, boolean copyNamespaces, SequenceHandler<XmlAtom> handler) throws GenXDMException
  280. {
  281. // TODO Auto-generated method stub
  282. throw new AssertionError("TODO");
  283. }
  284. public void stream(Object node, boolean copyNamespaces, ContentHandler handler) throws GenXDMException
  285. {
  286. delegate.stream(node, copyNamespaces, handler);
  287. }
  288. // public Iterable<Object> getNamespaces(final Object node)
  289. // {
  290. // final OMElement element = AxiomSupport.dynamicDowncastElement(node);
  291. // if (null != element)
  292. // {
  293. // @SuppressWarnings("unchecked")
  294. // final Iterator<OMNamespace> it = element.getAllDeclaredNamespaces();
  295. // if (it.hasNext())
  296. // {
  297. // final ArrayList<Object> namespaces = new ArrayList<Object>();
  298. // while (it.hasNext())
  299. // {
  300. // final OMNamespace namespace = it.next();
  301. // final String prefix = namespace.getPrefix();
  302. // final String uri = namespace.getNamespaceURI();
  303. // if (uri.length() == 0 && prefix.length() == 0)
  304. // {
  305. // if (isNamespaceCancellationRequired(element))
  306. // {
  307. // namespaces.add(new FauxNamespace(prefix, uri, element));
  308. // }
  309. // }
  310. // else
  311. // {
  312. // namespaces.add(new FauxNamespace(prefix, uri, element));
  313. // }
  314. // }
  315. // return namespaces;
  316. // }
  317. // else
  318. // {
  319. // return Collections.emptyList();
  320. // }
  321. // }
  322. // else
  323. // {
  324. // return Collections.emptyList();
  325. // }
  326. // }
  327. //
  328. // public Iterable<Object> getNamespacesInScope(final Object node)
  329. // {
  330. // final OMElement element = AxiomSupport.dynamicDowncastElement(node);
  331. // if (null != element)
  332. // {
  333. // final LinkedList<OMElement> chain = new LinkedList<OMElement>();
  334. // OMElement ancestorOrSelf = element;
  335. // while (null != ancestorOrSelf)
  336. // {
  337. // chain.addFirst(ancestorOrSelf);
  338. // ancestorOrSelf = AxiomSupport.dynamicDowncastElement(ancestorOrSelf.getParent());
  339. // }
  340. // final Map<String, Object> namespaces = new HashMap<String, Object>();
  341. // for (final OMElement link : chain)
  342. // {
  343. // @SuppressWarnings("unchecked")
  344. // final Iterator<OMNamespace> it = link.getAllDeclaredNamespaces();
  345. // while (it.hasNext())
  346. // {
  347. // final OMNamespace namespace = it.next();
  348. // final String prefix = namespace.getPrefix();
  349. // final String uri = namespace.getNamespaceURI();
  350. // if (uri.length() == 0 && prefix.length() == 0)
  351. // {
  352. // if (namespaces.containsKey(prefix))
  353. // {
  354. // namespaces.remove(prefix);
  355. // }
  356. // }
  357. // else
  358. // {
  359. // namespaces.put(prefix, new FauxNamespace(prefix, uri, element));
  360. // }
  361. // }
  362. // }
  363. // namespaces.put(XMLConstants.XML_NS_PREFIX, new FauxNamespace(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI, element));
  364. // return namespaces.values();
  365. // }
  366. // else
  367. // {
  368. // return Collections.emptyList();
  369. // }
  370. // }
  371. //
  372. // public String getNamespaceURIInScope(final Object node, final String prefix)
  373. // {
  374. // if (XMLConstants.XML_NS_PREFIX.equals(prefix))
  375. // {
  376. // return XMLConstants.XML_NS_URI;
  377. // }
  378. // OMElement element = AxiomSupport.dynamicDowncastElement(node);
  379. // if (null != element)
  380. // {
  381. // while (null != element)
  382. // {
  383. // final OMNamespace namespace = element.findNamespaceURI(prefix);
  384. // if (null != namespace)
  385. // {
  386. // return namespace.getNamespaceURI();
  387. // }
  388. // element = AxiomSupport.dynamicDowncastElement(element.getParent());
  389. // }
  390. // return null;
  391. // }
  392. // final OMDocument document = AxiomSupport.dynamicDowncastDocument(node);
  393. // if (null != document)
  394. // {
  395. // if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix))
  396. // {
  397. // return XMLConstants.NULL_NS_URI;
  398. // }
  399. // }
  400. // return null;
  401. // }
  402. //
  403. // @SuppressWarnings("unused")
  404. // private Iterable<Object> getAttributes(final Object node)
  405. // {
  406. // final OMElement element = AxiomSupport.dynamicDowncastElement(node);
  407. // if (null != element)
  408. // {
  409. // final ArrayList<Object> attributes = new ArrayList<Object>();
  410. // @SuppressWarnings("unchecked")
  411. // final Iterator<OMAttribute> it = element.getAllAttributes();
  412. // while (it.hasNext())
  413. // {
  414. // attributes.add(it.next());
  415. // }
  416. // return attributes;
  417. // }
  418. // else
  419. // {
  420. // return Collections.emptyList();
  421. // }
  422. // }
  423. //
  424. // @SuppressWarnings("unused")
  425. // private Iterable<Object> getAttributesInScope(final Object node)
  426. // {
  427. // final OMElement element = AxiomSupport.dynamicDowncastElement(node);
  428. // if (null != element)
  429. // {
  430. // final LinkedList<OMElement> chain = new LinkedList<OMElement>();
  431. // OMElement ancestorElement = AxiomSupport.dynamicDowncastElement(element.getParent());
  432. // while (null != ancestorElement)
  433. // {
  434. // chain.addFirst(ancestorElement);
  435. // ancestorElement = AxiomSupport.dynamicDowncastElement(ancestorElement.getParent());
  436. // }
  437. // final Map<QName, Object> attributes = new HashMap<QName, Object>();
  438. // for (final OMElement link : chain)
  439. // {
  440. // @SuppressWarnings("unchecked")
  441. // final Iterator<OMAttribute> it = link.getAllAttributes();
  442. // while (it.hasNext())
  443. // {
  444. // final OMAttribute attribute = it.next();
  445. // if (attribute.getNamespace().getNamespaceURI().equals(XMLConstants.XML_NS_URI))
  446. // {
  447. // attributes.put(attribute.getQName(), new FauxAttribute(attribute, element));
  448. // }
  449. // }
  450. // }
  451. // @SuppressWarnings("unchecked")
  452. // final Iterator<OMAttribute> it = element.getAllAttributes();
  453. // while (it.hasNext())
  454. // {
  455. // final OMAttribute attribute = it.next();
  456. // attributes.put(attribute.getQName(), attribute);
  457. // }
  458. // return attributes.values();
  459. // }
  460. // else
  461. // {
  462. // return Collections.emptyList();
  463. // }
  464. // }
  465. //
  466. // /**
  467. // * Determines whether the cancellation, xmlns="", is required to ensure correct semantics.
  468. // *
  469. // * @param element
  470. // * The element that would be the parent of the cancellation.
  471. // * @return <code>true</code> if the cancellation is required.
  472. // */
  473. // private static boolean isNamespaceCancellationRequired(final OMElement element)
  474. // {
  475. // final OMContainer parent = element.getParent();
  476. // if (null != parent)
  477. // {
  478. // final OMElement scope = AxiomSupport.dynamicDowncastElement(parent);
  479. // if (null != scope)
  480. // {
  481. // final OMNamespace scopeDefaultNS = scope.findNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX);
  482. // if (null != scopeDefaultNS)
  483. // {
  484. // if (scopeDefaultNS.getNamespaceURI().length() > 0)
  485. // {
  486. // return true;
  487. // }
  488. // else
  489. // {
  490. // // The scope is the global namespace so the cancellation
  491. // // can be ignored.
  492. // }
  493. // }
  494. // else
  495. // {
  496. // // There does not seem to be any conflict so ignore the
  497. // // mapping.
  498. // }
  499. // }
  500. // else
  501. // {
  502. // // The parent must be a document, so the mapping must be
  503. // // spurious.
  504. // }
  505. // }
  506. // else
  507. // {
  508. // // If there is no parent then the mapping is ambiguous. Ignore it.
  509. // }
  510. // return false;
  511. // }
  512. //
  513. // private static boolean isNamespaceDeclarationRequired(final String prefix, final String uri, final OMElement element)
  514. // {
  515. // final OMContainer parent = element.getParent();
  516. // if (null != parent)
  517. // {
  518. // OMElement scope = AxiomSupport.dynamicDowncastElement(parent);
  519. // while (null != scope)
  520. // {
  521. // final OMNamespace namespace = scope.findNamespaceURI(prefix);
  522. // if (null != namespace)
  523. // {
  524. // // The prefix is mapped to something in a higher scope.
  525. // if (!namespace.getNamespaceURI().equals(uri))
  526. // {
  527. // // The mapping must be overridden.
  528. // return true;
  529. // }
  530. // else
  531. // {
  532. // // The mapping already exists.
  533. // return false;
  534. // }
  535. // }
  536. // scope = AxiomSupport.dynamicDowncastElement(scope.getParent());
  537. // }
  538. // }
  539. // else
  540. // {
  541. // // If there is no parent then the mapping is required.
  542. // }
  543. // return true;
  544. // }
  545. @SuppressWarnings("unused")
  546. private final AtomBridge<XmlAtom> atomBridge;
  547. private final AxiomModel delegate;
  548. }