PageRenderTime 66ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/php/core/vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/Ntriples.php

https://github.com/smerrill/drupal-quickstart
PHP | 221 lines | 162 code | 7 blank | 52 comment | 10 complexity | 69ccc87daa58a30f57e6408f8c5779b3 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * EasyRdf
  4. *
  5. * LICENSE
  6. *
  7. * Copyright (c) 2009-2013 Nicholas J Humfrey. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
  17. * promote products derived from this software without specific prior
  18. * written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * @package EasyRdf
  33. * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
  34. * @license http://www.opensource.org/licenses/bsd-license.php
  35. */
  36. /**
  37. * Class to serialise an EasyRdf_Graph to N-Triples
  38. * with no external dependancies.
  39. *
  40. * @package EasyRdf
  41. * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
  42. * @license http://www.opensource.org/licenses/bsd-license.php
  43. */
  44. class EasyRdf_Serialiser_Ntriples extends EasyRdf_Serialiser
  45. {
  46. private $escChars = array(); // Character encoding cache
  47. /**
  48. * @ignore
  49. */
  50. protected function escapeString($str)
  51. {
  52. if (strpos(utf8_decode(str_replace('?', '', $str)), '?') === false) {
  53. $str = utf8_decode($str);
  54. }
  55. $result = '';
  56. $strLen = strlen($str);
  57. for ($i = 0; $i < $strLen; $i++) {
  58. $c = $str[$i];
  59. if (!isset($this->escChars[$c])) {
  60. $this->escChars[$c] = $this->escapedChar($c);
  61. }
  62. $result .= $this->escChars[$c];
  63. }
  64. return $result;
  65. }
  66. /**
  67. * @ignore
  68. */
  69. protected function unicodeCharNo($c)
  70. {
  71. $cUtf = utf8_encode($c);
  72. $bl = strlen($cUtf); /* binary length */
  73. $r = 0;
  74. switch ($bl) {
  75. case 1: /* 0####### (0-127) */
  76. $r = ord($cUtf);
  77. break;
  78. case 2: /* 110##### 10###### = 192+x 128+x */
  79. $r = ((ord($cUtf[0]) - 192) * 64) +
  80. (ord($cUtf[1]) - 128);
  81. break;
  82. case 3: /* 1110#### 10###### 10###### = 224+x 128+x 128+x */
  83. $r = ((ord($cUtf[0]) - 224) * 4096) +
  84. ((ord($cUtf[1]) - 128) * 64) +
  85. (ord($cUtf[2]) - 128);
  86. break;
  87. case 4: /* 1111#### 10###### 10###### 10###### = 240+x 128+x 128+x 128+x */
  88. $r = ((ord($cUtf[0]) - 240) * 262144) +
  89. ((ord($cUtf[1]) - 128) * 4096) +
  90. ((ord($cUtf[2]) - 128) * 64) +
  91. (ord($cUtf[3]) - 128);
  92. break;
  93. }
  94. return $r;
  95. }
  96. /**
  97. * @ignore
  98. */
  99. protected function escapedChar($c)
  100. {
  101. $no = $this->unicodeCharNo($c);
  102. /* see http://www.w3.org/TR/rdf-testcases/#ntrip_strings */
  103. if ($no < 9) {
  104. return "\\u" . sprintf('%04X', $no); /* #x0-#x8 (0-8) */
  105. } elseif ($no == 9) {
  106. return '\t'; /* #x9 (9) */
  107. } elseif ($no == 10) {
  108. return '\n'; /* #xA (10) */
  109. } elseif ($no < 13) {
  110. return "\\u" . sprintf('%04X', $no); /* #xB-#xC (11-12) */
  111. } elseif ($no == 13) {
  112. return '\r'; /* #xD (13) */
  113. } elseif ($no < 32) {
  114. return "\\u" . sprintf('%04X', $no); /* #xE-#x1F (14-31) */
  115. } elseif ($no < 34) {
  116. return $c; /* #x20-#x21 (32-33) */
  117. } elseif ($no == 34) {
  118. return '\"'; /* #x22 (34) */
  119. } elseif ($no < 92) {
  120. return $c; /* #x23-#x5B (35-91) */
  121. } elseif ($no == 92) {
  122. return '\\'; /* #x5C (92) */
  123. } elseif ($no < 127) {
  124. return $c; /* #x5D-#x7E (93-126) */
  125. } elseif ($no < 65536) {
  126. return "\\u" . sprintf('%04X', $no); /* #x7F-#xFFFF (128-65535) */
  127. } elseif ($no < 1114112) {
  128. return "\\U" . sprintf('%08X', $no); /* #x10000-#x10FFFF (65536-1114111) */
  129. } else {
  130. return ''; /* not defined => ignore */
  131. }
  132. }
  133. /**
  134. * @ignore
  135. */
  136. protected function serialiseResource($res)
  137. {
  138. $escaped = $this->escapeString($res);
  139. if (substr($res, 0, 2) == '_:') {
  140. return $escaped;
  141. } else {
  142. return "<$escaped>";
  143. }
  144. }
  145. /**
  146. * Serialise an RDF value into N-Triples
  147. *
  148. * The value can either be an array in RDF/PHP form, or
  149. * an EasyRdf_Literal or EasyRdf_Resource object.
  150. *
  151. * @param array|object $value An associative array or an object
  152. * @throws EasyRdf_Exception
  153. * @return string The RDF value serialised to N-Triples
  154. */
  155. public function serialiseValue($value)
  156. {
  157. if (is_object($value)) {
  158. $value = $value->toRdfPhp();
  159. }
  160. if ($value['type'] == 'uri' or $value['type'] == 'bnode') {
  161. return $this->serialiseResource($value['value']);
  162. } elseif ($value['type'] == 'literal') {
  163. $escaped = $this->escapeString($value['value']);
  164. if (isset($value['lang'])) {
  165. $lang = $this->escapeString($value['lang']);
  166. return '"' . $escaped . '"' . '@' . $lang;
  167. } elseif (isset($value['datatype'])) {
  168. $datatype = $this->escapeString($value['datatype']);
  169. return '"' . $escaped . '"' . "^^<$datatype>";
  170. } else {
  171. return '"' . $escaped . '"';
  172. }
  173. } else {
  174. throw new EasyRdf_Exception(
  175. "Unable to serialise object of type '".$value['type']."' to ntriples: "
  176. );
  177. }
  178. }
  179. /**
  180. * Serialise an EasyRdf_Graph into N-Triples
  181. *
  182. * @param EasyRdf_Graph $graph An EasyRdf_Graph object.
  183. * @param string $format The name of the format to convert to.
  184. * @param array $options
  185. * @throws EasyRdf_Exception
  186. * @return string The RDF in the new desired format.
  187. */
  188. public function serialise($graph, $format, array $options = array())
  189. {
  190. parent::checkSerialiseParams($graph, $format);
  191. if ($format == 'ntriples') {
  192. $nt = '';
  193. foreach ($graph->toRdfPhp() as $resource => $properties) {
  194. foreach ($properties as $property => $values) {
  195. foreach ($values as $value) {
  196. $nt .= $this->serialiseResource($resource)." ";
  197. $nt .= "<" . $this->escapeString($property) . "> ";
  198. $nt .= $this->serialiseValue($value)." .\n";
  199. }
  200. }
  201. }
  202. return $nt;
  203. } else {
  204. throw new EasyRdf_Exception(
  205. "EasyRdf_Serialiser_Ntriples does not support: $format"
  206. );
  207. }
  208. }
  209. }