-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentHandler.cpp
585 lines (512 loc) · 15.4 KB
/
ContentHandler.cpp
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#include "ContentHandler.h"
#include "BaseURISAXResolver.h"
#include "BaseContentHandler.h"
#include "Builder.h"
#include "Constants.h"
#include "ContentHandler.h"
#include "Fragment.h"
#include "GenericDocumentRootIdentifier.h"
#include "Identifier.h"
#include "LanguageIdentifier.h"
#include "ReferencePartIdentifier.h"
#include "SchemaIdentifier.h"
#include "XBRLException.h"
#include "XBRLIdentifier.h"
#include "XBRLXLinkIdentifier.h"
#include "XBRLXLinkHandler.h"
#include "XLinkException.h"
#include "XLinkProcessor.h"
#include "XercesString.h"
#include <iostream>
#include <xercesc/sax/Locator.hpp>
#include <xercesc/sax/SAXException.hpp>
#include <xercesc/sax2/Attributes.hpp>
#include <xercesc/dom/DOMElement.hpp>
namespace xbrlcapi
{
struct ContentHandler::Impl
{
std::weak_ptr<ContentHandler> outer;
std::shared_ptr<Loader> loader;
std::shared_ptr<xercesc::Locator> locator;
std::string xml;
/**
* The list of fragment identifiers
*/
std::list<std::shared_ptr<Identifier>> identifiers;
Impl()
{
}
Impl(std::shared_ptr<Loader>& loader) :
outer(outer),
loader(loader)
{
// identifiers = getIdentifiers();
}
Impl(std::shared_ptr<Loader>& loader,
const std::string& xml) :
outer(outer),
loader(loader)
{
// identifiers = getIdentifiers();
setXML(xml);
}
void startDocument()
{
// Set up the base URI resolver for the content handler and the XLink handler.
if (outer.lock()->getURI().empty())
{
throw xercesc::SAXException("The document URI must not be null when setting up the base URI resolver.");
}
setBaseURISAXResolver(BaseURISAXResolver(outer.lock()->getURI()));
getXLinkHandler().setBaseURISAXResolver(baseURISAXResolver);
std::shared_ptr<XBRLXLinkIdentifier> xbrlLinkIdentifier(new XBRLXLinkIdentifier(outer.lock()));
std::shared_ptr<SchemaIdentifier> schemaIdentifier(new SchemaIdentifier(outer.lock()));
std::shared_ptr<XBRLIdentifier> xbrlIdentifier(new XBRLIdentifier(outer.lock()));
std::shared_ptr<LanguageIdentifier> languageIdentifier(new LanguageIdentifier(outer.lock()));
std::shared_ptr<ReferencePartIdentifier> referencePartIdentifier(new ReferencePartIdentifier(outer.lock()));
std::shared_ptr<GenericDocumentRootIdentifier> genericDocumentRootIdentifier(new GenericDocumentRootIdentifier(outer.lock()));
xbrlLinkIdentifier->initialize();
schemaIdentifier->initialize();
xbrlIdentifier->initialize();
languageIdentifier->initialize();
referencePartIdentifier->initialize();
genericDocumentRootIdentifier->initialize();
// Instantiate the fragment identifiers
try
{
addIdentifier(xbrlLinkIdentifier);
addIdentifier(schemaIdentifier);
addIdentifier(xbrlIdentifier);
addIdentifier(languageIdentifier);
addIdentifier(referencePartIdentifier);
addIdentifier(genericDocumentRootIdentifier);
}
catch (const XBRLException& e)
{
throw xercesc::SAXException(("One of the fragment identifiers could not be instantiated."
+ e.getMessage() ).c_str());
}
}
void startElement(
const XMLCh* namespaceURI,
const XMLCh* lName,
const XMLCh* qName,
const xercesc::Attributes& attrs)
{
// Update the information about the state of the current element (tracks ancestor attributes)
outer.lock()->setElementState( ElementState(outer.lock()->getElementState(), attrs ));
// Stash new URIs in xsi:schemaLocation attributes if desired
if (loader->useSchemaLocationAttributes())
{
std::string schemaLocations = to_string(attrs.getValue(XS(XMLConstants::XMLSchemaInstanceNamespace),XS("schemaLocation")));
if (!schemaLocations.empty())
{
//logger.debug("Processing schema locations: " + schemaLocations);
//std::string[] fields = schemaLocations.trim().split("\\s+");
//for (int i=1; i<fields.length; i=i+2)
{
//try
//{
// Poco::URI uri = getBaseURISAXResolver().getBaseURI().resolve(Poco::URI(fields[i]));
// logger.debug("Working on: " + uri);
// loader.stashURI(uri);
//}
//catch (URISyntaxException e)
//{
// logger.warn("Ignoring malformed XSI schemaLocation URI in: " + schemaLocations);
//}
//catch (XBRLException e)
//{
// logger.warn("A problem occurred when stashing the schemaLocation URI: " + fields[i]);
//}
//catch (XMLBaseException e)
//{
// logger.warn("A problem occurred when getting the base URI so schemaLocation URIs were not stashed from: " + schemaLocations);
//}
}
}
}
// Identify the fragments
for (const std::shared_ptr<Identifier>& identifier : identifiers)
{
try
{
identifier->startElement(namespaceURI, lName, qName, attrs);
if (loader->isBuildingAFragment())
{
if (loader->getFragment().isNewFragment())
{
break;
}
}
}
catch (XBRLException e)
{
// logger.error(this->getURI() + " : " + e.getMessage());
throw xercesc::SAXException(strcat("Fragment identification failed. ", + e.getMessage().c_str()));
}
}
if (! loader->isBuildingAFragment())
{
throw xercesc::SAXException("Some element has not been placed in a fragment.");
}
// Insert the current element into the fragment being built
try
{
Fragment fragment = loader->getFragment();
//if (fragment == NULL) throw xercesc::SAXException("A fragment should be being built.");
Builder builder = fragment.getBuilder();
//if (builder == null) throw xercesc::SAXException("A fragment that is being built needs a builder.");
builder.appendElement(namespaceURI, lName, qName, attrs);
// Hardwire XLink resource language code inheritance to
// improve query performance based on language selections.
if (attrs.getIndex(XS(XMLConstants::XLinkNamespace),XS("type")) > -1)
{
//try
//{
std::shared_ptr<DOMElement> element = fragment.getDataRootElement();
if (!element->hasAttributeNS(XS(XMLConstants::XMLNamespace),XS("lang")))
{
std::string code = outer.lock()->getElementState().getLanguageCode();
//if (code != null)
//{
element->setAttribute(XS("xml:lang"),XS(code));
//}
}
//}
//catch (Throwable t)
//{
// logger.info("bugger");
//}
}
}
catch (XBRLException e)
{
// logger.error(this->getURI() + " : " + e.getMessage());
throw xercesc::SAXException(strcat("The element could not be appended to the fragment.",e.getMessage().c_str()));
}
}
/**
* The end of an element triggers processing of an extended link
* if we have reached the end of an extended link.
* Otherwise, we step up to the parent element
* unless the element that is ending did not ever become the current element.
*/
void endElement(
const XMLCh* const namespaceURI,
const XMLCh* const lName,
const XMLCh* const qName)
{
// Get the attributes of the element being ended.
std::shared_ptr<const xercesc::Attributes> attrs = outer.lock()->getElementState().getAttributes();
// Handle the ending of an element in the fragment builder
//try
//{
// getLoader().getFragment().getBuilder().endElement(namespaceURI, lName, qName);
//}
//catch (XBRLException e)
//{
// throw xercesc::SAXException(("The XBRLAPI fragment endElement failed. " +
// e.getMessage()).c_str());
//}
//// Handle the ending of an element in the XLink processor
//try
//{
// getLoader().getXlinkProcessor().endElement(namespaceURI,
// lName,
// qName,
// *attrs);
//}
//catch (XLinkException e)
//{
// throw xercesc::SAXException(("The XLink processor endElement failed. " +
// e.getMessage()).c_str());
//}
//// Update the states of the fragment identifiers
//for (std::shared_ptr<Identifier>& id : getIdentifiers())
//{
// try
// {
// id->endElement(namespaceURI,lName,qName,*attrs);
// }
// catch (XBRLException e)
// {
// throw xercesc::SAXException(("Fragment identifier state update failed at the end of an element failed." +
// e.getMessage()).c_str());
// }
//}
//// Update the state of the loader.
//try
//{
// getLoader().updateState(getElementState());
//}
//catch (XBRLException e)
//{
// throw xercesc::SAXException(("The state of the loader could not be updated at the end of element " +
// namespaceURI +
// ":" +
// lName +
// "." +
// e.getMessage()).c_str());
//}
//// Update the information about the state of the current element
//setElementState(*getElementState().getParent());
}
/**
* Ignore ignorable whitespace
*/
void ignorableWhitespace(const XMLCh* const buf, const XMLSize_t offset)
{
/* try
{
std::string s(buf, offset, len);
if (!s.trim().equals(""))
getLoader().getFragment().getBuilder().appendText(s);
}
catch (XBRLException e)
{
throw xercesc::SAXException(("Failed to handle ignorable white space." +
getInputErrorInformation() +
e.getMessage()).c_str());
}*/
}
/**
* Copy across processing instructions to the DTSImpl
*/
void processingInstruction(const XMLCh* const target, const XMLCh* const data)
{
//try
//{
// Fragment fragment = getLoader().getFragment();
// fragment.getBuilder().appendProcessingInstruction(target,data);
//}
//catch (XBRLException e)
//{
// // Need to store processing instructions for incorporation into the fragment being created.
//}
}
/**
* Copy characters (trimming white space as required) to the DTSImpl.
*/
void characters(const XMLCh* const buf, const XMLSize_t offset)
{
//try
//{
// std::string s(buf, offset, len);
// getLoader().getFragment().getBuilder().appendText(s);
//}
//catch (XBRLException e)
//{
// throw xercesc::SAXException(("The characters could not be appended to the fragment." +
// getInputErrorInformation() +
// e.getMessage()).c_str());
//}
}
/**
* The locator for a document is stored to facilitate resolution
* of CacheURIImpl's relative to that location.
*/
void setDocumentLocator(const xercesc::Locator* locator)
{
//this->locator = locator;
}
/**
* @return the base URI resolver for SAX parsing.
*/
BaseURISAXResolver getBaseURISAXResolver()
{
return baseURISAXResolver;
}
/**
* @param identifier The identifier to add to the list of
* fragment identifiers used by the content handler.
*/
void addIdentifier(const std::shared_ptr<Identifier>& identifier)
{
identifiers.push_back(identifier);
}
/**
* @param index The index of the position at which
* the new identifier is to be inserted in the list of
* fragment identifiers.
* @param identifier The identifier to add to the list of
* fragment identifiers used by the content handler.
*/
//void addIdentifier(int index, const Identifier& identifier)
//{
// identifiers.push_back(std::shared_ptr<Identifier>(identifier));
//}
/**
* @param index The index of the identifier to remove from the list of
* fragment identifiers used by the content handler.
*/
void removeIdentifier(int index)
{
//if (index > identifiers.size()-1) throw XBRLException("The identifier index was too large.");
//if (index < 0) throw XBRLException("The identifier index was too low.");
//if (identifiers.size() == 0) throw XBRLException("There are no identifiers to remove.");
//identifiers.remove(index);
}
std::shared_ptr<xercesc::Locator> getLocator()
{
return this->locator;
}
std::string getPublicId()
{
return to_string(locator->getPublicId());
}
std::string getSystemId()
{
return to_string(locator->getSystemId());
}
long long getLineNumber()
{
return locator->getLineNumber();
}
long long getColumnNumber()
{
return locator->getColumnNumber();
}
std::string getInputErrorInformation()
{
// //StringBuffer s = new StringBuffer(" The problem occurred in ");
// //if (!(getSystemId() == null))
// // s.append(getSystemId() + ". ");
// //else
// // s.append("a document without a URI. All DTS documents must have a URI but one being parsed into the DTS does not.");
// //s.append("The problem seems to be on line" + getLineNumber() + " at column " + getColumnNumber() + ".");
// //return s.toString();
return std::string();
}
XLinkHandler getXLinkHandler()
{
return loader->getXlinkProcessor().getXLinkHandler();
}
BaseURISAXResolver baseURISAXResolver;
void setBaseURISAXResolver(const BaseURISAXResolver& resolver)
{
this->baseURISAXResolver = resolver;
}
void setXML(const std::string& xml)
{
// this->xml = xml;
}
std::shared_ptr<Loader> getLoader()
{
return loader;
}
void setOuter(const std::weak_ptr<ContentHandler>& contentHandler)
{
outer = std::weak_ptr<ContentHandler>(contentHandler);
}
};
ContentHandler::ContentHandler() {}
ContentHandler::~ContentHandler() {}
ContentHandler::ContentHandler(const ContentHandler& rhs)
{
pImpl = rhs.pImpl;
}
ContentHandler& ContentHandler::operator=(const ContentHandler& rhs)
{
if (pImpl != rhs.pImpl)
{
pImpl->~Impl();
pImpl = rhs.pImpl;
}
return *this;
}
ContentHandler::ContentHandler(std::shared_ptr<Loader>& loader, const Poco::URI& uri) :
BaseContentHandler(uri),
pImpl(loader)
{
}
ContentHandler::ContentHandler(ContentHandler&& rhs)
{
pImpl = std::move(rhs.pImpl);
}
ContentHandler& ContentHandler::operator=(ContentHandler&& rhs)
{
if (pImpl != rhs.pImpl)
pImpl = std::move(rhs.pImpl);
return *this;
}
bool ContentHandler::operator==(const ContentHandler& rhs)
{
return (pImpl == rhs.pImpl);
}
bool ContentHandler::operator!=(const ContentHandler& rhs)
{
return !this->operator==(rhs);
}
void ContentHandler::startDocument()
{
pImpl->startDocument();
}
void ContentHandler::startElement(
const XMLCh* const namespaceURI,
const XMLCh* const lName,
const XMLCh* const qName,
const xercesc::Attributes& attrs)
{
pImpl->startElement(
namespaceURI,
lName,
qName,
attrs);
}
void ContentHandler::endElement(
const XMLCh* const namespaceURI,
const XMLCh* const lName,
const XMLCh* const qName)
{
pImpl->endElement(
namespaceURI,
lName,
qName);
}
void ContentHandler::ignorableWhitespace(const XMLCh* const buf, const XMLSize_t offset)
{
pImpl->ignorableWhitespace(buf, offset);
}
void ContentHandler::processingInstruction(const XMLCh* const target, const XMLCh* const data)
{
pImpl->processingInstruction(target, data);
}
void ContentHandler::characters(const XMLCh* const buf, const XMLSize_t offset)
{
pImpl->characters(buf, offset);
}
void ContentHandler::setDocumentLocator(const xercesc::Locator* locator)
{
pImpl->setDocumentLocator(locator) ;
}
BaseURISAXResolver ContentHandler::getBaseURISAXResolver()
{
return pImpl->getBaseURISAXResolver();
}
void ContentHandler::addIdentifier(const std::shared_ptr<Identifier>& identifier)
{
pImpl->addIdentifier(identifier) ;
}
//void ContentHandler::addIdentifier(int index, const Identifier& identifier)
//{
// pImpl->addIdentifier(index, identifier);
//}
void ContentHandler::removeIdentifier(int index)
{
pImpl->removeIdentifier(index);
}
std::shared_ptr<Loader> ContentHandler::getLoader()
{
return pImpl->getLoader();
}
std::weak_ptr<ContentHandler> ContentHandler::getPtr()
{
return shared_from_this();
}
void ContentHandler::initialize()
{
pImpl->setOuter(getPtr());
}
}