📄 documentparser.cpp
字号:
} } inet->Close(inet, &stream); result = buffer; log.LogDiagnostic(2, L"DocumentParser::FetchBuffer - success"); return 0;}void DocumentParser::ReleaseBuffer(const VXIbyte * & buffer){ if (buffer != PLATFORM_DEFAULTS_DOC) delete[] const_cast<VXIbyte *>(buffer); buffer = NULL;}// -2: Internal errorint DocumentParser::FetchDocument(const VXIchar * url, const VXIMapHolder & properties, VXIinetInterface * inet, SimpleLogger & log, VXMLDocument & document, VXIMapHolder & docProperties, bool isDefaults){ int result; if (log.IsLogging(2)) { log.StartDiagnostic(2) << L"DocumentParser::FetchDocument(" << url << L")"; log.EndDiagnostic(); } // (1) Load the VXML DTD for validation. This will override an externally // specified DTD if the user provides a link. try { if (isDefaults && lastParse != DocumentParser::DEFAULTS) { parser->parse(MemBufInputSource(DUMMY_VXML_DEFAULTS_DOC, DUMMY_VXML_DEFAULTS_DOC_SIZE, "vxml 1.0 defaults"), false); lastParse = DocumentParser::DEFAULTS; } else if (!isDefaults && lastParse != DocumentParser::DOCUMENT) { parser->parse(MemBufInputSource(DUMMY_VXML_DOC, DUMMY_VXML_DOC_SIZE, "vxml 1.0 dtd"), false); lastParse = DocumentParser::DOCUMENT; } } catch (const XMLException & exception) { log.StartDiagnostic(0) << L"DocumentParser::FetchDocument - XML parsing " L"error from DOM: " << XMLChToVXIchar(exception.getMessage()); log.EndDiagnostic(); log.LogError(999, SimpleLogger::MESSAGE, L"unable to load VXML DTD"); return 4; } catch (const SAXParseException & exception) { log.StartDiagnostic(0) << L"DocumentParser::FetchDocument - Parse error " << L"in file \"" << XMLChToVXIchar(exception.getSystemId()) << L"\", line " << exception.getLineNumber() << L", column " << exception.getColumnNumber() << L" - " << XMLChToVXIchar(exception.getMessage()); log.EndDiagnostic(); log.LogError(999, SimpleLogger::MESSAGE, L"unable to load VXML DTD"); return 4; } // (2) Load the url into memory. const VXIbyte * buffer; VXIulong bufSize; vxistring docURL; if (isDefaults && wcslen(url) == 0) { buffer = PLATFORM_DEFAULTS_DOC; bufSize = PLATFORM_DEFAULTS_DOC_SIZE; docURL = L"builtin defaults"; result = 0; } else { result = DocumentParser::FetchBuffer(url, properties, inet, log, buffer, bufSize, docURL); } if (result != 0) { if (log.IsLogging(0)) { log.StartDiagnostic(0) << L"DocumentParser::FetchDocument - exiting " L"with error result " << result; log.EndDiagnostic(); } return result; // may return { -1, 1, 2, 3 } } // (3) Create memory buffer for Xerces. VXIcharToXMLCh membufURL(url); MemBufInputSource *inputSource = new MemBufInputSource(buffer, bufSize, membufURL.c_str()); if (inputSource == NULL) { DocumentParser::ReleaseBuffer(buffer); return -1; } // (4) Convert URL into DOM document. try { parser->parse(*inputSource, true); } catch (const XMLException & exception) { delete inputSource; DocumentParser::ReleaseBuffer(buffer); if (log.IsLogging(0)) { log.StartDiagnostic(0) << L"DocumentParser::FetchDocument - XML parsing " L"error from DOM: " << XMLChToVXIchar(exception.getMessage()); log.EndDiagnostic(); } return 4; } catch (const SAXParseException & exception) { delete inputSource; DocumentParser::ReleaseBuffer(buffer); if (log.IsLogging(0)) { log.StartDiagnostic(0) << L"DocumentParser::FetchDocument - Parse error " << L"in file \"" << XMLChToVXIchar(exception.getSystemId()) << L"\", line " << exception.getLineNumber() << L", column " << exception.getColumnNumber() << L" - " << XMLChToVXIchar(exception.getMessage()); log.EndDiagnostic(); } return 4; } delete inputSource; DocumentParser::ReleaseBuffer(buffer); // (5) Parse was successful, process document. We want only the top level // <vxml> node. const VXMLDocument doc(converter->GetDocument()); const VXMLElement root = doc.GetRoot(); VXMLElementType nodeName = root.GetName(); // If we're looking for the defaults, we can exit early. if (isDefaults && nodeName == DEFAULTS_ROOT) { log.LogDiagnostic(2, L"DocumentParser::FetchDocument - success"); document = doc; return 0; } else if (nodeName != NODE_VXML) { document = VXMLDocument(); if (log.IsLogging(0)) { log.StartDiagnostic(0) << L"DocumentParser::FetchDocument - unable to " L"find " << NODE_VXML << L" in document."; log.EndDiagnostic(); } return 4; } vxistring temp; // If the properties map is NULL, don't bother with the remaining settings if (docProperties.GetValue() != NULL) { // Retrieve the properties and put them into the map. VXIString * str = NULL; temp = docURL; str = VXIStringCreate(temp.c_str()); if (str == NULL) throw VXIException::OutOfMemory(); VXIMapSetProperty(docProperties.GetValue(), PropertyList::AbsoluteURI, reinterpret_cast<VXIValue *>(str)); if (!root.GetAttribute(ATTRIBUTE_BASE, temp)) temp = docURL; str = VXIStringCreate(temp.c_str()); if (str == NULL) throw VXIException::OutOfMemory(); VXIMapSetProperty(docProperties.GetValue(), PropertyList::BaseURI, reinterpret_cast<VXIValue *>(str)); if (root.GetAttribute(ATTRIBUTE_XMLLANG, temp)) { str = VXIStringCreate(temp.c_str()); if (str == NULL) throw VXIException::OutOfMemory(); VXIMapSetProperty(docProperties.GetValue(), PropertyList::Language, reinterpret_cast<VXIValue *>(str)); } } log.LogDiagnostic(2, L"DocumentParser::FetchDocument - success"); document = doc; return 0;}int DocumentParser::FetchContent(const VXIchar * uri, const VXIMapHolder & properties, VXIinetInterface * inet, SimpleLogger & log, const vxistring & encoding, vxistring & content){ const VXIbyte * buffer; VXIulong bufSize; vxistring docURL; // (1) Retrieve the URI. switch (DocumentParser::FetchBuffer(uri, properties, inet, log, buffer, bufSize, docURL)) { case -1: // Out of memory? return -1; case 0: // Success break; case 2: // Unable to open URI return 2; case 3: // Unable to read from URI return 3; case 1: // Invalid parameter default: return 1; } // (2) Create a transcoder for the requested type. VXIcharToXMLCh encName(encoding.c_str()); XMLTransService::Codes failReason; XMLTranscoder* transcoder = XMLPlatformUtils::fgTransService->makeNewTranscoderFor(encName.c_str(), failReason, 8*1064); if (transcoder == NULL) return 4; // (3) Allocate memory for the conversion. XMLCh * convertedString = new XMLCh[bufSize+1]; unsigned char* charSizes = new unsigned char[bufSize]; if (convertedString == NULL || charSizes == NULL) { delete[] convertedString; delete[] charSizes; return -1; } // (4) Transcode the values into our string. unsigned int bytesEaten; unsigned int charsDone = transcoder->transcodeFrom(buffer, bufSize, convertedString, bufSize, bytesEaten, charSizes); // (5) Finally convert from XMLCh to VXIchar. convertedString[charsDone] = '\0'; // Add terminator. XMLChToVXIchar result(convertedString); // (6) Done. Release memory. content = result.c_str(); delete[] convertedString; delete[] charSizes; ReleaseBuffer(buffer); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -