-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
428 lines (395 loc) · 11.5 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
/**
* QName class and factory functions. This is ported from Arelle.
*
* _ _ _ _ _
* | | _ _ __ _ _ _(_) __| (_) |_ _ _
* | | | | | |/ _` | | | | |/ _` | | __| | | |
* | |__| |_| | (_| | |_| | | (_| | | |_| |_| |
* |_____\__, |\__, |\__,_|_|\__,_|_|\__|\__, |
* |___/ |_| |___/
*
* @author Bill Seddon
* @version 0.9
* @Copyright (C) 2018 Lyquidity Solutions Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace lyquidity\xml;
use lyquidity\xml\schema\SchemaTypes;
/**
* Definition of the main schema prefix
*/
// BMS 2018-04-09 Fixing the kluge
define( 'SCHEMA_PREFIX', "xs" );
define( 'SCHEMA_PREFIX_ALTERNATIVE', "xsd" );
define( 'SCHEMA_INSTANCE_PREFIX', "xsi" );
define( 'XML_PREFIX', "xml" );
/**
* Definition of the main schema namespace
*/
define( 'SCHEMA_NAMESPACE', "http://www.w3.org/2001/XMLSchema" );
define( 'SCHEMA_INSTANCE_NAMESPACE', "http://www.w3.org/2001/XMLSchema-instance" );
define( 'XML_NAMESPACE', "http://www.w3.org/XML/1998/namespace" );
/**
* Generate a QName instance.
*
* @param array|string $value can be a QName, an array or a string. If a string it will be the
* namespace of the value of $name or a Clark notation.
* If an array then it will be an array representation of the QName.
* @param array|string $name Can be an array of prefix/namespace key/value pairs or it can be
* the prefixed local name
* @param bool $noPrefixIsNoNamespace If no prefix is found then there will be no namespace if this is true
* @param \Exception $castException In case there is a cast exception
* @param \Exception $prefixException In case there is a prefix exception
* @throws \Exception
* @return QName
*/
function qname( $value, $name = null, $noPrefixIsNoNamespace = false, $castException = null, $prefixException = null )
{
if ( $value instanceof \SimpleXMLElement )
{
if ( $name ) // name is prefixed name
{
$element = $value; // may be an attribute
$value = $name;
$name = null;
}
else
{
return new QName( $value['prefix'], $value['namespaceURI'], $value['localName'] );
}
}
else if ( $name instanceof \SimpleXMLElement )
{
$element = $name;
$name = null;
$element = null;
$value = $name;
}
else
{
$element = null;
}
if ( $value instanceof QName )
{
return $value;
}
else if ( is_array( $value ) )
{
if ( ! isset( $value['localname'] ) )
{
return null;
}
return new QName(
isset( $value['prefix'] ) ? $value['prefix'] : null,
isset( $value['namespace'] ) ? $value['namespace'] : null,
$value['localname']
);
}
else if ( ! is_string( $value ) )
{
if ( $castException ) throw $castException;
return null;
}
$namespaceDict = null;
if ( $value && $value[0] == '{' ) // clark notation (with optional prefix)
{
// namespaceURI,sep,prefixedLocalName = value[1:].rpartition('}')
$matches = null;
if ( ! preg_match( "/({(?<namespaceURI>.*)})?(?<prefixedLocalName>.*)/", $value, $matches ) )
{
return null;
}
$namespaceURI = $matches['namespaceURI'];
$prefixedLocalName = $matches['prefixedLocalName'];
// prefix,sep,localName = $prefixedLocalName.rpartition(':')
$matches = null;
if ( ! preg_match( "/((?<prefix>.*):)?(?<localName>.*)/", $prefixedLocalName, $matches ) )
{
return null;
}
$prefix = $matches['prefix'];
$localName = $matches['localName'];
if ( ! $prefix )
{
$prefix = null;
if ( is_array( $name ) )
{
if ( isset( $name[ $namespaceURI ] ) )
{
$prefix = $name[ $namespaceURI ];
}
else // reverse lookup
{
foreach ( $name as $_prefix => $_namespaceURI )
{
if ( $_namespaceURI == $namespaceURI )
{
$prefix = $_prefix;
break;
}
}
}
}
}
}
else
{
if ( is_array( $name ) )
{
$namespaceURI = null;
$namespaceDict = $name; // note that functional prefix must be null, not '', in dict
$namespaceDict['xml'] = XML_NAMESPACE;
}
else if ( $name != null )
{
$namespaceURI = $name // len > 0
? $value
: null;
$namespaceDict = null;
$value = $name;
}
else
{
$namespaceURI = null;
$namespaceDict = null;
}
// prefix,sep,localName = value.strip().partition(":") # must be whitespace collapsed
$matches = null;
if ( ! preg_match( "/((?<prefix>.*):)?(?<localName>.*)/", $value, $matches ) )
{
return null;
}
$prefix = $matches['prefix'];
$localName = $matches['localName'];
if ( ! $prefix )
{
$prefix = null; # don't want '' but instead null if no prefix
if ( $noPrefixIsNoNamespace )
{
return new QName( null, null, $localName );
}
}
}
if ( $namespaceURI )
{
return new QName( $prefix, $namespaceURI, $localName );
}
else if ( $namespaceDict && isset( $namespaceDict[ $prefix ] ) )
{
return new QName( $prefix, $namespaceDict[ $prefix ], $localName );
}
else if ( isset( $element ) )
{
// same as XmlUtil.xmlns but local for efficiency
// namespaceURI = element.nsmap.get(prefix)
$names = $element->getDocNamespaces();
$namespaceURI = $names[ $prefix ] ? $names[ $prefix ] : null;
if ( ! $namespaceURI && $prefix == 'xml' )
{
$namespaceURI = "http://www.w3.org/XML/1998/namespace";
}
}
if ( ! $namespaceURI )
{
if ( $prefix )
{
if ( $prefixException ) throw $prefixException;
return null; // error, prefix not found
}
$namespaceURI = null; # cancel namespace if it is a zero length string
}
return new QName( $prefix, $namespaceURI, $localName );
}
/**
* Convert a namespace/local name pair into a QName instance
* Does not handle localNames with prefix
*
* @param string $namespaceURI
* @param string $localName
* @return null|QName
*/
function qnameNsLocalName( $namespaceURI, $localName )
{
return new QName( null, $namespaceURI ? $namespaceURI : null, $localName );
}
/**
* Converts a string in the clark notation format to a QName instance
* Does not handle clark names with prefix
*
* @param string $clarkname
* @return null|QName
*/
function qnameClarkName( $clarkname )
{
// clark notation (with optional prefix)
if ( $clarkname && $clarkname[0] == '{' )
{
// namespaceURI,sep,prefixedLocalName = value[1:].rpartition('}')
$matches = null;
if ( ! preg_match( "/({(?<namespaceURI>.*)})?(?<prefixedLocalName>.*)/", $clarkname, $matches ) )
{
return null;
}
$namespaceURI = $matches['namespaceURI'] ? $matches['namespaceURI'] : null;
$prefixedLocalName = $matches['prefixedLocalName'];
// prefix,sep,localName = $prefixedLocalName.rpartition(':')
$matches = null;
if ( ! preg_match( "/((?<prefix>.*):)?(?<localName>.*)/", $prefixedLocalName, $matches ) )
{
return null;
}
$prefix = $matches['prefix'] ? $matches['prefix'] : null;
$localName = $matches['localName'] ? $matches['localName'] : null;
return new QName( $prefix, $namespaceURI, $localName );
}
else
{
return new QName( null, null, $clarkname );
}
}
/**
* Create a QName from a prefix:name pair. Use the namespace associated
* with $element to resolve the prefix (if there is one)
*
* @param \SimpleXMLElement $element
* @param string $prefixedName
* @param \Exception $prefixException
* @throws \Exception
* @return NULL|QName
*/
function qnameEltPfxName( $element, $prefixedName, $prefixException = null )
{
$matches = null;
if ( ! preg_match( "/(?<prefix>.*)?:(?<localName>.*)/", $prefixedName, $matches ) )
{
return null;
}
$prefix = $matches['prefix'] ?? null; // don't want '' but instead null if no prefix
$localName = $matches['loocalName'] ?? null; // don't want '' but instead null if no prefix
$names = $element->getDocNamespaces();
$namespaceURI = $names[ $prefix ] ? $names[ $prefix ] : null;
if ( ! $namespaceURI )
{
if ( $prefix )
{
if ( $prefix == 'xml' )
{
$namespaceURI = "http://www.w3.org/XML/1998/namespace";
}
else
{
if ( $prefixException ) throw $prefixException;
return null;
}
}
else
{
$namespaceURI = null; // cancel namespace if it is a zero length string
}
}
return new QName( $prefix, $namespaceURI, $localName );
}
/**
* Call this function to initialize a default logger.
* This function does not need to be called if the Lyquidity XPath 2.0 or
* Lyquidity XBRL library bootstrap functions have been called as both
* these initialize a suitable logger.
*/
function initializeLog()
{
/**
* Load the Log class if not already loaded
*/
if ( ! class_exists( "\\Log", true ) )
{
$logPath = isset( $_ENV['LOG_LIBRARY_PATH'] )
? $_ENV['LOG_LIBRARY_PATH']
: ( defined( 'LOG_LIBRARY_PATH' ) ? LOG_LIBRARY_PATH : __DIR__ . "/../log/" );
require_once $logPath . "Log.php";
/**
* Load the event_log handler implementation
*/
require_once "$logPath/Log/error_log.php";
}
$log = \Log::singleton( 'error_log', PEAR_LOG_TYPE_SYSTEM, 'xpath2_log',
array(
'lineFormat' => '[%{priority}] %{message}',
)
);
}
/**
* Call to load types for an XML instance document
* The current SchemaTypes instance will be returned but the same instance
* can be retrieve any time by calling SchemaTypes::getInstance()
* @param \SimpleXMLElement $doc A document
* @return SchemaTypes
*/
function importTypesForDocument( $doc )
{
if ( ! $doc )
{
return false;
}
$owner = dom_import_simplexml( $doc )->ownerDocument;
$attributes = $doc->attributes( SCHEMA_INSTANCE_NAMESPACE );
if ( ! count( $attributes ) || ! property_exists( $attributes, 'schemaLocation' ) )
{
return false;
}
$parts = array_filter( preg_split( "/\s/s", (string)$attributes['schemaLocation'] ) );
if ( count( $parts ) == 1 ) array_unshift( $parts, '-' );
// Assume the schema location is relative to this file
$types = new SchemaTypes();
$key = "";
foreach ( $parts as $part )
{
if ( empty( $key ) )
{
$key = $part;
}
else
{
$basename = basename( $part );
$path = SchemaTypes::resolve_path( null, $basename );
$types->processSchema( $path, true );
$key = "";
}
}
return $types;
}
/**
* Examines the content to determine if the content represent an XML document
* @param string $content
* @param bool $throwException
* @return bool
* @throws \Exception
*/
function isXml( $content, $throwException = true )
{
// Use strpos because the xml document might start with comments
if ( empty( $content ) || strpos( $content, "<?xml" ) === false )
{
$previous = libxml_use_internal_errors(true);
$xml = \simplexml_load_string( $content );
libxml_use_internal_errors( $previous );
if ( $xml !== false ) return $xml;
if ( ! $throwException ) return false;
throw new \Exception( __( "The file does not contain a valid XML document", 'xbrl_validate' ) );
}
return true;
}
?>