PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/v2/mod_goodrelations/arc/ARC2_Class.php

https://code.google.com/p/goodrelations-for-joomla/
PHP | 256 lines | 199 code | 37 blank | 20 comment | 39 complexity | 76f7215e9a31be8a0699f99fdfe7cd52 MD5 | raw file
  1. <?php
  2. /*
  3. homepage: http://arc.semsol.org/
  4. license: http://arc.semsol.org/license
  5. class: ARC2 base class
  6. author: Benjamin Nowack
  7. version: 2009-02-16 (Tweak: support for "raw" parameter in toTurtle and toRDFXML methods)
  8. */
  9. class ARC2_Class {
  10. function __construct($a = '', &$caller) {
  11. $a = is_array($a) ? $a : array();
  12. $this->a = $a;
  13. $this->caller = &$caller;
  14. $this->__init();
  15. }
  16. function ARC2_Class($a = '', &$caller) {
  17. $this->__construct($a, $caller);
  18. }
  19. function __init() {/* base, time_limit */
  20. $this->inc_path = ARC2::getIncPath();
  21. $this->base = $this->v('base', ARC2::getScriptURI(), $this->a);
  22. $this->errors = array();
  23. $this->warnings = array();
  24. $this->adjust_utf8 = $this->v('adjust_utf8', 0, $this->a);
  25. }
  26. /* */
  27. function v($name, $default = false, $o = false) {/* value if set */
  28. $o = ($o !== false) ? $o : $this;
  29. if (is_array($o)) {
  30. return isset($o[$name]) ? $o[$name] : $default;
  31. }
  32. return isset($o->$name) ? $o->$name : $default;
  33. }
  34. function v1($name, $default = false, $o = false) {/* value if 1 (= not empty) */
  35. $o = ($o !== false) ? $o : $this;
  36. if (is_array($o)) {
  37. return (isset($o[$name]) && $o[$name]) ? $o[$name] : $default;
  38. }
  39. return (isset($o->$name) && $o->$name) ? $o->$name : $default;
  40. }
  41. function m($name, $a = false, $default = false, $o = false) {/* call method */
  42. $o = ($o !== false) ? $o : $this;
  43. return method_exists($o, $name) ? $o->$name($a) : $default;
  44. }
  45. /* */
  46. function camelCase($v) {
  47. $r = ucfirst($v);
  48. while (preg_match('/^(.*)[\-\_ ](.*)$/', $r, $m)) {
  49. $r = $m[1] . ucfirst($m[2]);
  50. }
  51. return $r;
  52. }
  53. /* */
  54. function addError($v) {
  55. if (!in_array($v, $this->errors)) {
  56. $this->errors[] = $v;
  57. }
  58. if ($this->caller && method_exists($this->caller, 'addError')) {
  59. $glue = strpos($v, ' in ') ? ' via ' : ' in ';
  60. $this->caller->addError($v . $glue . get_class($this));
  61. }
  62. return false;
  63. }
  64. function getErrors() {
  65. return $this->errors;
  66. }
  67. function getWarnings() {
  68. return $this->warnings;
  69. }
  70. /* */
  71. function splitURI($v) {
  72. return ARC2::splitURI($v);
  73. }
  74. /* */
  75. function expandPName($v) {
  76. if (!isset($this->ns) && isset($this->a['ns'])) $this->ns = $this->a['ns'];
  77. if (preg_match('/^([a-z0-9\_\-]+)\:([a-z0-9\_\-]+)$/i', $v, $m) && isset($this->ns[$m[1]])) {
  78. return $this->ns[$m[1]] . $m[2];
  79. }
  80. return $v;
  81. }
  82. function getPName($v, $connector = ':') {
  83. if (!isset($this->ns) && isset($this->a['ns'])) $this->ns = $this->a['ns'];
  84. if ($parts = $this->splitURI($v)) {
  85. foreach ($this->ns as $p => $ns) {
  86. if ($parts[0] == $ns) {
  87. return $p . $connector . $parts[1];
  88. }
  89. }
  90. }
  91. return $v;
  92. }
  93. /* */
  94. function calcURI($path, $base = "") {
  95. /* quick check */
  96. if (preg_match("/^[a-z0-9\_]+\:/i", $path)) {/* abs path or bnode */
  97. return $path;
  98. }
  99. if (preg_match("/^\/\//", $path)) {/* net path, assume http */
  100. return 'http:' . $path;
  101. }
  102. /* other URIs */
  103. $base = $base ? $base : $this->base;
  104. $base = preg_replace('/\#.*$/', '', $base);
  105. if ($path === true) {/* empty (but valid) URIref via turtle parser: <> */
  106. return $base;
  107. }
  108. $path = preg_replace("/^\.\//", '', $path);
  109. $root = preg_match('/(^[a-z0-9]+\:[\/]{1,2}[^\/]+)[\/|$]/i', $base, $m) ? $m[1] : $base; /* w/o trailing slash */
  110. $base .= ($base == $root) ? '/' : '';
  111. if (preg_match('/^\//', $path)) {/* leading slash */
  112. return $root . $path;
  113. }
  114. if (!$path) {
  115. return $base;
  116. }
  117. if (preg_match('/^([\#\?])/', $path, $m)) {
  118. return preg_replace('/\\' .$m[1]. '.*$/', '', $base) . $path;
  119. }
  120. if (preg_match('/^(\&)(.*)$/', $path, $m)) {/* not perfect yet */
  121. return preg_match('/\?/', $base) ? $base . $m[1] . $m[2] : $base . '?' . $m[2];
  122. }
  123. if (preg_match("/^[a-z0-9]+\:/i", $path)) {/* abs path */
  124. return $path;
  125. }
  126. /* rel path: remove stuff after last slash */
  127. $base = substr($base, 0, strrpos($base, '/')+1);
  128. /* resolve ../ */
  129. while (preg_match('/^(\.\.\/)(.*)$/', $path, $m)) {
  130. $path = $m[2];
  131. $base = ($base == $root.'/') ? $base : preg_replace('/^(.*\/)[^\/]+\/$/', '\\1', $base);
  132. }
  133. return $base . $path;
  134. }
  135. /* */
  136. function calcBase($path) {
  137. $r = $path;
  138. $r = preg_replace('/\#.*$/', '', $r);/* remove hash */
  139. $r = preg_replace('/^\/\//', 'http://', $r);/* net path (//), assume http */
  140. if (preg_match('/^[a-z0-9]+\:/', $r)) {/* scheme, abs path */
  141. while (preg_match('/^(.+\/)(\.\.\/.*)$/U', $r, $m)) {
  142. $r = $this->calcURI($m[1], $m[2]);
  143. }
  144. return $r;
  145. }
  146. return 'file://' . realpath($r);/* real path */
  147. }
  148. /* */
  149. function toNTriples($v, $ns = '', $raw = 0) {
  150. ARC2::inc('NTriplesSerializer');
  151. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  152. $ser = new ARC2_NTriplesSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  153. return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v, $raw) : $ser->getSerializedIndex($v, $raw);
  154. }
  155. function toTurtle($v, $ns = '', $raw = 0) {
  156. ARC2::inc('TurtleSerializer');
  157. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  158. $ser = new ARC2_TurtleSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  159. return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v, $raw) : $ser->getSerializedIndex($v, $raw);
  160. }
  161. function toRDFXML($v, $ns = '', $raw = 0) {
  162. ARC2::inc('RDFXMLSerializer');
  163. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  164. $ser = new ARC2_RDFXMLSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  165. return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v, $raw) : $ser->getSerializedIndex($v, $raw);
  166. }
  167. function toRDFJSON($v, $ns = '') {
  168. ARC2::inc('RDFJSONSerializer');
  169. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  170. $ser = new ARC2_RDFJSONSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  171. return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v) : $ser->getSerializedIndex($v);
  172. }
  173. function toLegacyXML($v, $ns = '') {
  174. ARC2::inc('LegacyXMLSerializer');
  175. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  176. $ser = new ARC2_LegacyXMLSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  177. return $ser->getSerializedArray($v);
  178. }
  179. function toLegacyJSON($v, $ns = '') {
  180. ARC2::inc('LegacyJSONSerializer');
  181. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  182. $ser = new ARC2_LegacyJSONSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  183. return $ser->getSerializedArray($v);
  184. }
  185. function toLegacyHTML($v, $ns = '') {
  186. ARC2::inc('LegacyHTMLSerializer');
  187. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  188. $ser = new ARC2_LegacyHTMLSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  189. return $ser->getSerializedArray($v);
  190. }
  191. function toHTML($v, $ns = '') {
  192. ARC2::inc('POSHRDFSerializer');
  193. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  194. $ser = new ARC2_POSHRDFSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  195. return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v) : $ser->getSerializedIndex($v);
  196. }
  197. /* */
  198. function getFilledTemplate($t, $vals, $g = '') {
  199. $parser = ARC2::getTurtleParser();
  200. $parser->parse($g, $this->getTurtleHead() . $t);
  201. return $parser->getSimpleIndex(0, $vals);
  202. }
  203. function getTurtleHead() {
  204. $r = '';
  205. $ns = $this->v('ns', array(), $this->a);
  206. foreach ($ns as $k => $v) {
  207. $r .= "@prefix " . $k . ": <" .$v. "> .\n";
  208. }
  209. return $r;
  210. }
  211. /* */
  212. function toUTF8($v) {
  213. return $this->adjust_utf8 ? ARC2::toUTF8($v) : $v;
  214. }
  215. /* */
  216. }