PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/org.mwc.asset.comms/docs/restlet_src/org.restlet.ext.rdf/org/restlet/ext/rdf/Link.java

https://bitbucket.org/haris_peco/debrief
Java | 424 lines | 107 code | 38 blank | 279 comment | 2 complexity | 1d0b0b3a80bff97468bc55903ef24561 MD5 | raw file
  1. /**
  2. * Copyright 2005-2010 Noelios Technologies.
  3. *
  4. * The contents of this file are subject to the terms of one of the following
  5. * open source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL 1.0 (the
  6. * "Licenses"). You can select the license that you prefer but you may not use
  7. * this file except in compliance with one of these Licenses.
  8. *
  9. * You can obtain a copy of the LGPL 3.0 license at
  10. * http://www.opensource.org/licenses/lgpl-3.0.html
  11. *
  12. * You can obtain a copy of the LGPL 2.1 license at
  13. * http://www.opensource.org/licenses/lgpl-2.1.php
  14. *
  15. * You can obtain a copy of the CDDL 1.0 license at
  16. * http://www.opensource.org/licenses/cddl1.php
  17. *
  18. * You can obtain a copy of the EPL 1.0 license at
  19. * http://www.opensource.org/licenses/eclipse-1.0.php
  20. *
  21. * See the Licenses for the specific language governing permissions and
  22. * limitations under the Licenses.
  23. *
  24. * Alternatively, you can obtain a royalty free commercial license with less
  25. * limitations, transferable or non-transferable, directly at
  26. * http://www.noelios.com/products/restlet-engine
  27. *
  28. * Restlet is a registered trademark of Noelios Technologies.
  29. */
  30. package org.restlet.ext.rdf;
  31. import org.restlet.data.Reference;
  32. import org.restlet.util.Triple;
  33. /**
  34. * Link between a source resource and a target resource or literal. This exactly
  35. * maps with the concepts of statement, triple or relationship defined by RDF,
  36. * the core specification of the Semantic Web. A link is composed of a source
  37. * node (or subject in RDF terminology), a type URI reference (or predicate in
  38. * RDF terminology) and a target node (or object in RDF terminology).
  39. *
  40. * We use this class in Restlet to enhance resources and make them part of the
  41. * Web of data (also know as Linked Data and Hyperdata).
  42. *
  43. * @author Jerome Louvel
  44. * @see <a href="http://www.w3.org/TR/rdf-concepts/">RDF concepts</a>
  45. */
  46. public class Link extends Triple<Object, Reference, Object> {
  47. /**
  48. * Creates a reference to a blank node. In this API, we support RDF blank
  49. * nodes using the "_" namespace and local identifiers, in a way similar to
  50. * the RDF n3 serialization format.
  51. *
  52. * @param identifier
  53. * The blank node identifier.
  54. * @return A reference to a blank node.
  55. */
  56. public static Reference createBlankRef(String identifier) {
  57. return new Reference("_:" + identifier);
  58. }
  59. /**
  60. * Indicates if a reference is identifying a blank node.
  61. *
  62. * @param reference
  63. * The reference to test.
  64. * @return True if a reference is identifying a blank node.
  65. * @see #createBlankRef(String)
  66. */
  67. public static boolean isBlankRef(Reference reference) {
  68. return ((reference != null) && ("_".equals(reference.getScheme())));
  69. }
  70. /**
  71. * Constructor. Leverages n3 reification feature where a graph itself can be
  72. * the source node of a link.
  73. *
  74. * @param sourceGraph
  75. * The source graph or subject in RDF terminology.
  76. * @param typeRef
  77. * The type reference or predicate in RDF terminology.
  78. * @param targetLit
  79. * The target literal or object in RDF terminology.
  80. */
  81. public Link(Graph sourceGraph, Reference typeRef, Literal targetLit) {
  82. this((Object) sourceGraph, typeRef, (Object) targetLit);
  83. }
  84. /**
  85. * Constructor. Leverages n3 reification feature where a graph itself can be
  86. * the source node of a link.
  87. *
  88. * @param sourceGraph
  89. * The source graph or subject in RDF terminology.
  90. * @param typeRef
  91. * The type reference or predicate in RDF terminology.
  92. * @param target
  93. * The target node or object in RDF terminology.
  94. */
  95. public Link(Graph sourceGraph, Reference typeRef, Object target) {
  96. this((Object) sourceGraph, typeRef, target);
  97. }
  98. /**
  99. * Constructor. Leverages n3 reification feature where a graph itself can be
  100. * the source node of a link.
  101. *
  102. * @param sourceGraph
  103. * The source graph or subject in RDF terminology.
  104. * @param typeRef
  105. * The type reference or predicate in RDF terminology.
  106. * @param targetRef
  107. * The target reference or object in RDF terminology.
  108. */
  109. public Link(Graph sourceGraph, Reference typeRef, Reference targetRef) {
  110. this(sourceGraph, typeRef, (Object) targetRef);
  111. }
  112. /**
  113. * Constructor by copy.
  114. *
  115. * @param from
  116. * The link to copy from.
  117. */
  118. public Link(Link from) {
  119. this(from.getSource(), from.getTypeRef(), from.getTarget());
  120. }
  121. /**
  122. * Constructor.
  123. *
  124. * @param source
  125. * The source node or subject in RDF terminology.
  126. * @param typeRef
  127. * The type reference or predicate in RDF terminology.
  128. * @param target
  129. * The target node or object in RDF terminology.
  130. */
  131. private Link(Object source, Reference typeRef, Object target) {
  132. super(source, typeRef, target);
  133. }
  134. /**
  135. * Constructor.
  136. *
  137. * @param sourceRef
  138. * The source resource reference or subject in RDF terminology.
  139. * @param typeRef
  140. * The type reference or predicate in RDF terminology.
  141. * @param targetLit
  142. * The target literal node or object in RDF terminology.
  143. */
  144. public Link(Reference sourceRef, Reference typeRef, Literal targetLit) {
  145. this(sourceRef, typeRef, (Object) targetLit);
  146. }
  147. /**
  148. * Constructor.
  149. *
  150. * @param sourceRef
  151. * The source resource reference or subject in RDF terminology.
  152. * @param typeRef
  153. * The type reference or predicate in RDF terminology.
  154. * @param targetRef
  155. * The target resource reference or object in RDF terminology.
  156. */
  157. public Link(Reference sourceRef, Reference typeRef, Reference targetRef) {
  158. this(sourceRef, typeRef, (Object) targetRef);
  159. }
  160. /**
  161. * Returns the source which can be either a reference or a link or a graph
  162. * or null. This maps with the concept of subject in RDF terminology.
  163. *
  164. * @return The source.
  165. */
  166. public Object getSource() {
  167. return getFirst();
  168. }
  169. /**
  170. * Returns the source graph. Supports RDF reification or N3 formulae.
  171. *
  172. * @return The source graph.
  173. * @see #getSource()
  174. */
  175. public Graph getSourceAsGraph() {
  176. return hasGraphSource() ? (Graph) getSource() : null;
  177. }
  178. /**
  179. * Returns the source link. Supports RDF reification.
  180. *
  181. * @return The source link.
  182. * @see #getSource()
  183. */
  184. public Link getSourceAsLink() {
  185. return hasLinkSource() ? (Link) getSource() : null;
  186. }
  187. /**
  188. * Returns the source resource reference.
  189. *
  190. * @return The source resource reference.
  191. * @see #getSource()
  192. */
  193. public Reference getSourceAsReference() {
  194. return hasReferenceSource() ? (Reference) getSource() : null;
  195. }
  196. /**
  197. * Returns the target which can be either a literal or a reference or is
  198. * null. This maps with the concept of object in RDF terminology.
  199. *
  200. * @return The target.
  201. */
  202. public Object getTarget() {
  203. return getThird();
  204. }
  205. /**
  206. * Returns the target graph.
  207. *
  208. * @return The target graph.
  209. * @see #getTarget()
  210. */
  211. public Graph getTargetAsGraph() {
  212. return hasGraphTarget() ? (Graph) getTarget() : null;
  213. }
  214. /**
  215. * Returns the target link.
  216. *
  217. * @return The target link.
  218. * @see #getTarget()
  219. */
  220. public Link getTargetAsLink() {
  221. return hasLinkTarget() ? (Link) getTarget() : null;
  222. }
  223. /**
  224. * Returns the target literal.
  225. *
  226. * @return The target literal.
  227. * @see #getTarget()
  228. */
  229. public Literal getTargetAsLiteral() {
  230. return hasLiteralTarget() ? (Literal) getTarget() : null;
  231. }
  232. /**
  233. * Returns the target resource reference.
  234. *
  235. * @return The target resource reference.
  236. * @see #getTarget()
  237. */
  238. public Reference getTargetAsReference() {
  239. return hasReferenceTarget() ? (Reference) getTarget() : null;
  240. }
  241. /**
  242. * Returns the type reference. This maps with the concept of predicate in
  243. * RDF terminology.
  244. *
  245. * @return The type reference.
  246. */
  247. public Reference getTypeRef() {
  248. return getSecond();
  249. }
  250. /**
  251. * Indicates if the source is a graph.
  252. *
  253. * @return True if the source is a graph.
  254. */
  255. public boolean hasGraphSource() {
  256. return getSource() instanceof Graph;
  257. }
  258. /**
  259. * Indicates if the target is a graph.
  260. *
  261. * @return True if the target is a graph.
  262. */
  263. public boolean hasGraphTarget() {
  264. return getTarget() instanceof Graph;
  265. }
  266. /**
  267. * Indicates if the source is a link.
  268. *
  269. * @return True if the source is a link.
  270. */
  271. public boolean hasLinkSource() {
  272. return getSource() instanceof Link;
  273. }
  274. /**
  275. * Indicates if the target is a link.
  276. *
  277. * @return True if the target is a link.
  278. */
  279. public boolean hasLinkTarget() {
  280. return getTarget() instanceof Link;
  281. }
  282. /**
  283. * Indicates if the target is a literal.
  284. *
  285. * @return True if the target is a literal.
  286. */
  287. public boolean hasLiteralTarget() {
  288. return getTarget() instanceof Literal;
  289. }
  290. /**
  291. * Indicates if the source is a reference.
  292. *
  293. * @return True if the source is a reference.
  294. */
  295. public boolean hasReferenceSource() {
  296. return getSource() instanceof Reference;
  297. }
  298. /**
  299. * Indicates if the target is a reference.
  300. *
  301. * @return True if the target is a reference.
  302. */
  303. public boolean hasReferenceTarget() {
  304. return getTarget() instanceof Reference;
  305. }
  306. /**
  307. * Sets the source as a graph. This maps with the concept of subject in RDF
  308. * terminology.
  309. *
  310. * @param sourceGraph
  311. * The source graph.
  312. */
  313. public void setSource(Graph sourceGraph) {
  314. setFirst(sourceGraph);
  315. }
  316. /**
  317. * Sets the source as a link. This maps with the concept of subject in RDF
  318. * terminology.
  319. *
  320. * @param sourceLink
  321. * The source link.
  322. */
  323. public void setSource(Link sourceLink) {
  324. setFirst(sourceLink);
  325. }
  326. /**
  327. * Sets the source resource reference. This maps with the concept of subject
  328. * in RDF terminology.
  329. *
  330. * @param sourceRef
  331. * The source resource reference.
  332. */
  333. public void setSource(Reference sourceRef) {
  334. setFirst(sourceRef);
  335. }
  336. /**
  337. * Sets the target as a graph. This maps with the concept of object in RDF
  338. * terminology.
  339. *
  340. * @param targetGraph
  341. * The target graph.
  342. */
  343. public void setTarget(Graph targetGraph) {
  344. setThird(targetGraph);
  345. }
  346. /**
  347. * Sets the target as a link. This maps with the concept of object in RDF
  348. * terminology.
  349. *
  350. * @param targetLink
  351. * The target link.
  352. */
  353. public void setTarget(Link targetLink) {
  354. setThird(targetLink);
  355. }
  356. /**
  357. * Sets the target literal. This maps with the concept of object in RDF
  358. * terminology.
  359. *
  360. * @param targetLit
  361. * The target literal.
  362. */
  363. public void setTarget(Literal targetLit) {
  364. setThird(targetLit);
  365. }
  366. /**
  367. * Sets the target as a resource reference. This maps with the concept of
  368. * object in RDF terminology.
  369. *
  370. * @param targetRef
  371. * The target resource reference.
  372. */
  373. public void setTarget(Reference targetRef) {
  374. setThird(targetRef);
  375. }
  376. /**
  377. * Sets the type reference. This maps with the concept of predicate in RDF
  378. * terminology.
  379. *
  380. * @param typeRef
  381. * The type reference.
  382. */
  383. public void setTypeRef(Reference typeRef) {
  384. setSecond(typeRef);
  385. }
  386. }