MultXmlReaderPrivate::MultXmlReaderPrivate(const std::string& filename, std::ostream& output): m_parser(new QXmlSimpleReader()) { // Create the handler object and install it as the document and error // handler for the parser-> Then parse the file and catch any exceptions // that propogate out // try { MultXmlHandler handler(output); m_parser->setContentHandler(&handler); m_parser->setErrorHandler(&handler); m_parser->setFeature("http://qt-project.org/xml/features/report-start-end-entity",true); QFile file(filename.c_str()); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) throw XMLException(); if (!m_parser->parse( QXmlInputSource(&file))) { throw XMLException(); } } catch (const XMLException& e) { BOWLOGINIT; LERROR << "An XML exception occurred: " << e.what() ; throw; } }
void XMLWriter::endDocument() { if (_depth > 0) throw XMLException("Not well-formed (at least one tag has no matching end tag)"); if (_elementCount == 0) throw XMLException("No document element"); poco_assert_dbg (!_unclosedStartTag); _elementCount = 0; _depth = -1; }
/** * Function to validate that a given Node is not of types CDATANode and Document types. It also makes sure that if the given node * is of type TextNode, its contents are empty. This is used whenever a child is being added to the document object. Only ElementNode, * CommentNode or TextNodes with empty strings can be added as a child of a Document node. * * @param child The node object to be validated. */ void Document::validate(Node* child) { if (dynamic_cast<TextNode*>(child) != NULL) { if (!isEmptyText(static_cast<TextNode*>(child)->getText())) { throw XMLException ("Cannot add or remove a Text node directly to the XML Document"); } } if (dynamic_cast<CDATANode*>(child) != NULL || dynamic_cast<Document*>(child) != NULL) { throw XMLException ("Cannot add or remove a CDATA Node or an XML Document directly to the XML Document"); } }
void Document::_parse() { if (this->data == NULL) { if (this->fromResource) { hresource resource; resource.open(this->filename); this->_setup(resource, hrdir::normalize(this->filename)); } else { hfile file; file.open(this->filename); this->_setup(file, hdir::normalize(this->filename)); } } this->document = new rapidxml::xml_document<char>(); try { RAPIDXML_DOCUMENT->parse<rapidxml::parse_validate_closing_tags | rapidxml::parse_no_string_terminators | rapidxml::parse_no_data_nodes>(this->data); } catch (rapidxml::parse_error& e) { hstr desc = e.what() + hstr(" [") + e.where<char>() + "]"; delete RAPIDXML_DOCUMENT; this->document = NULL; throw XMLException(hsprintf("An error occcured parsing XML file '%s': %s", this->realFilename.cStr(), desc.cStr()), NULL); } }
//元素结束 </...> 要求有<element> 的配对 const char* CXML::parseEndElment(const char* xml) { char szBuf[80]; TAG *tag = 0; xml+=2;//xml = SkipTo(xml,"</"); xml = ReadWord(xml,szBuf,80,&tag); if(*xml!='>') throw XMLException("缺少>."); if(m_pcr && m_pcr->name.compare(szBuf)==0 ) m_pcr = m_pcr->parentElement; else throw XMLException("元素不匹配."); return xml+1; }
//跳过 const char* CXML::SkipTo(const char* xml, const char* tag) { if( !xml ) return 0; //子串搜索 const char* tempTag = 0; size_t tag_size = strlen(tag); if( tag_size==1 ) { while(*xml) { if(*tag==*xml) { tempTag = tag; break; } xml++; } } else { fge::CByteMatch m((const BYTE*)tag,tag_size); if(m_xmlEndTemp!=0) tempTag = (const char*)m.Find((const BYTE*)xml,m_xmlEndTemp-xml); else tempTag = (const char*)m.Find((const BYTE*)xml,(int)strlen(xml)); } if(!tempTag) throw XMLException("标签未正确关闭"); else return tempTag+tag_size; }
void XMLWriter::characters(const XMLChar ch[], int start, int length) { if (length == 0) return; if (_unclosedStartTag) closeStartTag(); _contentWritten = _contentWritten || length > 0; if (_inCDATA) { while (length-- > 0) writeXML(ch[start++]); } else { while (length-- > 0) { XMLChar c = ch[start++]; switch (c) { case '"': writeMarkup(MARKUP_QUOTENC); break; case '\'': writeMarkup(MARKUP_APOSENC); break; case '&': writeMarkup(MARKUP_AMPENC); break; case '<': writeMarkup(MARKUP_LTENC); break; case '>': writeMarkup(MARKUP_GTENC); break; default: if (c >= 0 && c < 32) { if (c == '\t' || c == '\r' || c == '\n') writeXML(c); else throw XMLException("Invalid character token."); } else writeXML(c); } } } }
void XMLWriter::endElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname) { if (_depth < 1) throw XMLException("No unclosed tag"); if (!_elementStack.back().equalsWeakly(qname, namespaceURI, localName)) throw XMLException("End tag does not match start tag", nameToString(localName, qname)); _elementStack.pop_back(); --_depth; if (!_unclosedStartTag) prettyPrint(); writeEndElement(namespaceURI, localName, qname); _contentWritten = false; if (_depth == 0) writeNewLine(); }
TinyXml_Document::TinyXml_Document(chstr filename) : Document(filename), rootNode(NULL) { hstr realFilename = hrdir::normalize(filename); hstr data = hresource::hread(this->filename); this->document = new TiXmlDocument(); this->document->Parse(data.c_str()); if (this->document->Error()) { hstr desc = this->document->ErrorDesc(); int row = this->document->ErrorRow(); int col = this->document->ErrorCol(); if (row != 0) { desc += hsprintf(" [row %d, column %d]", row, col); harray<hstr> lines = data.split("\n"); if (lines.size() >= row) // just in case! { desc += "\n----------------------------------------------------------\n"; desc += lines[row - 1].trim(); desc += "\n----------------------------------------------------------"; } } throw XMLException(hsprintf("An error occcured parsing XML file '%s': %s", realFilename.c_str(), desc.c_str()), NULL); } }
void XMLWriter::notationDecl(const XMLString& name, const XMLString* publicId, const XMLString* systemId) { if (!_inDTD) throw XMLException("Notation declaration not within DTD"); if (!_inInternalDTD) { writeMarkup(" ["); _inInternalDTD = true; } if (_options & PRETTY_PRINT) { writeNewLine(); writeMarkup(_indent); } writeMarkup("<!NOTATION "); writeXML(name); if (systemId && !systemId->empty()) { writeMarkup(" SYSTEM \""); writeXML(*systemId); writeMarkup("\""); } if (publicId && !publicId->empty()) { writeMarkup(" PUBLIC \""); writeXML(*publicId); writeMarkup("\""); } writeMarkup(">"); }
void XMLWriter::unparsedEntityDecl(const XMLString& name, const XMLString* publicId, const XMLString& systemId, const XMLString& notationName) { if (!_inDTD) throw XMLException("Entity declaration not within DTD"); if (!_inInternalDTD) { writeMarkup(" ["); _inInternalDTD = true; } if (_options & PRETTY_PRINT) { writeNewLine(); writeMarkup(_indent); } writeMarkup("<!ENTITY "); writeXML(name); if (!systemId.empty()) { writeMarkup(" SYSTEM \""); writeXML(systemId); writeMarkup("\""); } if (publicId && !publicId->empty()) { writeMarkup(" PUBLIC \""); writeXML(*publicId); writeMarkup("\""); } if (!notationName.empty()) { writeMarkup(" NDATA "); writeXML(notationName); } writeMarkup(">"); }
void XMLWriter::startCDATA() { if (_inCDATA) throw XMLException("Cannot nest CDATA sections"); if (_unclosedStartTag) closeStartTag(); _inCDATA = true; writeMarkup(MARKUP_BEGIN_CDATA); }
Node* TinyXml_Document::root(chstr type) { if (this->rootNode == NULL) { TiXmlNode* tinyXmlNode = this->document->FirstChildElement(); if (tinyXmlNode == NULL) { throw XMLException("No root node found in XML file '" + this->filename + "'!", NULL); } this->rootNode = this->node(tinyXmlNode); if (type != "" && *this->rootNode != type) { throw XMLException("Root node type is not '" + type + "' in XML file '" + this->filename + "'!", NULL); } } return this->rootNode; }
//元素结束 /> const char* CXML::parseEndEmptyElment(const char* xml) { if(m_pcr) m_pcr = m_pcr->parentElement; else throw XMLException("元素不匹配."); return SkipTo(xml,"/>"); }
LimaStatusCode SpecificEntitiesLoader:: process(AnalysisContent& analysis) const { // get analysis graph AnalysisGraph* graph=static_cast<AnalysisGraph*>(analysis.getData(m_graph)); if (graph==0) { LOGINIT("LP::SpecificEntities"); LERROR << "no graph '" << m_graph << "' available !"; return MISSING_DATA; } //create a RecognizerData (such as in ApplyRecognizer) to be able to use //CreateSpecificEntity actions RecognizerData* recoData=new RecognizerData; analysis.setData("RecognizerData",recoData); RecognizerResultData* resultData=new RecognizerResultData(m_graph); recoData->setResultData(resultData); try { SpecificEntitiesLoader::XMLHandler handler(m_language,analysis,graph); m_parser->setContentHandler(&handler); m_parser->setErrorHandler(&handler); QFile file(getInputFile(analysis).c_str()); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) throw XMLException(); if (!m_parser->parse( QXmlInputSource(&file))) { throw XMLException(); } } catch (const XMLException& ) { LOGINIT("LP::SpecificEntities"); LERROR << "Error: failed to parse XML input file"; } // remove recognizer data (used only internally to this process unit) recoData->deleteResultData(); resultData=0; analysis.removeData("RecognizerData"); return SUCCESS_ID; }
/** * Function to write the Document node as an XML node into a file at the given path. * * @param path The path to the file to which the Document object should be written as an XML node. */ void Document::write(const std::string& path) const { if(!this->isRootSet) throw XMLException("XML Document does not have a root"); std::ofstream file(path.c_str()); write(file); file.close(); }
void XMLWriter::endFragment() { if (_depth > 1) throw XMLException("Not well-formed (at least one tag has no matching end tag)"); _inFragment = false; _elementCount = 0; _depth = -1; }
/** * Function to add a child node to the Document object, at a given index. * * @param child The node to be added as a child of the Document object. * @param index The index at which 'child' should be added as a child of the Document object. */ void Document::addChildNode (Node* child, int index) { validate(child); setRootElement(child); if(index >= childNodes.size() || index < 0 ) throw XMLException("\nError! Trying to add a child node at an index that doesn't exist"); childNodes.insert(childNodes.begin() + index, child); }
/** * Function to write the Document node as an XML node into the given output stream. * * @param os The output stream to which the Document node should be written as an XML node. */ void Document::write(std::ostream& os) const { if(!this->isRootSet) throw XMLException("XML Document does not have a root"); for(int i=0; i < childNodes.size(); ++i){ childNodes[i]->write( os ); } }
void ParserEngine::addEncoding(const XMLString& name, TextEncoding* pEncoding) { poco_check_ptr (pEncoding); if (_encodings.find(name) == _encodings.end()) _encodings[name] = pEncoding; else throw XMLException("Encoding already defined"); }
//解析xml,返回文档结尾 const char* CXML::ParseEx(const char* xmlText,size_t size/*=0*/) { Clear( ); if(xmlText == 0) return xmlText; if(size) m_xmlEndTemp = xmlText+size; else m_xmlEndTemp = xmlText+strlen(xmlText); xmlText = Find(xmlText,"<"); if(xmlText == 0) return xmlText; const char* msg = xmlText; try { TAG *tag = 0; while(*msg && msg<m_xmlEndTemp) { tag = 0; msg = ReadTag(msg,&tag); if(tag == 0) break; msg = (this->*(tag->parseFun))(msg); if(m_pcr == &m_root) return msg; } if(m_pcr != &m_root) throw XMLException("标签未正确关闭"); if(m_pcr->subElement.size()!=1) throw XMLException("有且只能有一个根元素"); } catch( const XMLException& e ) { if(msg<m_xmlEndTemp){ m_xmlEndTemp = 0; throw e; } } m_xmlEndTemp = 0; return msg; }
void ParserEngine::parseExternal(XML_Parser extParser, InputSource* pInputSource) { pushContext(extParser, pInputSource); if (pInputSource->getCharacterStream()) parseExternalCharInputStream(extParser, *pInputSource->getCharacterStream()); else if (pInputSource->getByteStream()) parseExternalByteInputStream(extParser, *pInputSource->getByteStream()); else throw XMLException("Input source has no stream"); popContext(); }
Node* Document::root(chstr name) { if (this->document == NULL) { this->_parse(); } if (this->rootNode == NULL) { rapidxml::xml_node<char>* rapidXmlNode = RAPIDXML_DOCUMENT->first_node(); if (rapidXmlNode == NULL) { throw XMLException("No root node found in XML file '" + this->filename + "'!", NULL); } this->rootNode = new Node(this, rapidXmlNode); if (name != "" && this->rootNode->name != name) { throw XMLException("Root node type is not '" + name + "' in XML file '" + this->filename + "'!", NULL); } } return this->rootNode; }
void XMLWriter::startElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const Attributes& attributes) { if (_depth == 0 && !_inFragment && _elementCount > 1) throw XMLException("Not well-formed. Second root element found", nameToString(localName, qname)); if (_unclosedStartTag) closeStartTag(); prettyPrint(); writeStartElement(namespaceURI, localName, qname, attributes); _elementStack.push_back(Name(qname, namespaceURI, localName)); _contentWritten = false; ++_depth; }
void XMLWriter::emptyElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const Attributes& attributes) { if (_depth == 0 && _elementCount > 1) throw XMLException("Not well-formed. Second root element found."); if (_unclosedStartTag) closeStartTag(); prettyPrint(); writeStartElement(namespaceURI, localName, qname, attributes); _contentWritten = false; writeMarkup("/"); closeStartTag(); }
//读取文本内容 const char* CXML::ReadText(const char* xml,std::string &buf, char* end) { buf.clear(); //读值 const char* xmlNext = Find(xml,end); if(!xmlNext) throw XMLException("文本中有非法字符:<"); if(xmlNext > xml) { buf.assign(xml, xmlNext-xml); } return xmlNext; }
void ParserEngine::init() { if (_parser) XML_ParserFree(_parser); if (!_pBuffer) _pBuffer = new char[PARSE_BUFFER_SIZE]; if (dynamic_cast<NoNamespacePrefixesStrategy*>(_pNamespaceStrategy)) { _parser = XML_ParserCreateNS(_encodingSpecified ? _encoding.c_str() : 0, '\t'); if (_parser) { XML_SetNamespaceDeclHandler(_parser, handleStartNamespaceDecl, handleEndNamespaceDecl); } } else if (dynamic_cast<NamespacePrefixesStrategy*>(_pNamespaceStrategy)) { _parser = XML_ParserCreateNS(_encodingSpecified ? _encoding.c_str() : 0, '\t'); if (_parser) { XML_SetReturnNSTriplet(_parser, 1); XML_SetNamespaceDeclHandler(_parser, handleStartNamespaceDecl, handleEndNamespaceDecl); } } else { _parser = XML_ParserCreate(_encodingSpecified ? _encoding.c_str() : 0); } if (!_parser) throw XMLException("Cannot create Expat parser"); XML_SetUserData(_parser, this); XML_SetElementHandler(_parser, handleStartElement, handleEndElement); XML_SetCharacterDataHandler(_parser, handleCharacterData); XML_SetProcessingInstructionHandler(_parser, handleProcessingInstruction); if (_expandInternalEntities) XML_SetDefaultHandlerExpand(_parser, handleDefault); else XML_SetDefaultHandler(_parser, handleDefault); XML_SetUnparsedEntityDeclHandler(_parser, handleUnparsedEntityDecl); XML_SetNotationDeclHandler(_parser, handleNotationDecl); XML_SetExternalEntityRefHandler(_parser, handleExternalEntityRef); XML_SetCommentHandler(_parser, handleComment); XML_SetCdataSectionHandler(_parser, handleStartCdataSection, handleEndCdataSection); XML_SetDoctypeDeclHandler(_parser, handleStartDoctypeDecl, handleEndDoctypeDecl); XML_SetEntityDeclHandler(_parser, handleEntityDecl); XML_SetSkippedEntityHandler(_parser, handleSkippedEntity); XML_SetParamEntityParsing(_parser, _externalParameterEntities ? XML_PARAM_ENTITY_PARSING_ALWAYS : XML_PARAM_ENTITY_PARSING_NEVER); XML_SetUnknownEncodingHandler(_parser, handleUnknownEncoding, this); }
void CXML::Save(const char* filename,Element *root,const char* encoding) { if(!root) return; m_fileName = filename; std::ofstream file; file.open(filename); if(file.good() == false) throw XMLException("不能创建文件"); file<<"<?xml version=\"1.0\" encoding=\""<<encoding<<"\"?>"<<std::endl;; root->Save( file , 0 ); file.close(); }
void XMLWriter::startFragment() { if (_depth != -1) throw XMLException("Cannot start a fragment in another fragment or document"); _inFragment = true; _depth = 0; _elementCount = 0; _prefix = 0; _contentWritten = true; _namespaces.reset(); _namespaces.pushContext(); }
int ParserEngine::handleExternalEntityRef(XML_Parser parser, const XML_Char* context, const XML_Char* base, const XML_Char* systemId, const XML_Char* publicId) { ParserEngine* pThis = reinterpret_cast<ParserEngine*>(XML_GetUserData(parser)); if (!context && !pThis->_externalParameterEntities) return XML_STATUS_ERROR; if (context && !pThis->_externalGeneralEntities) return XML_STATUS_ERROR; InputSource* pInputSource = 0; EntityResolver* pEntityResolver = 0; EntityResolverImpl defaultResolver; XMLString sysId(systemId); XMLString pubId; if (publicId) pubId.assign(publicId); URI uri(fromXMLString(pThis->_context.back()->getSystemId())); uri.resolve(fromXMLString(sysId)); if (pThis->_pEntityResolver) { pEntityResolver = pThis->_pEntityResolver; pInputSource = pEntityResolver->resolveEntity(publicId ? &pubId : 0, toXMLString(uri.toString())); } if (!pInputSource && pThis->_externalGeneralEntities) { pEntityResolver = &defaultResolver; pInputSource = pEntityResolver->resolveEntity(publicId ? &pubId : 0, toXMLString(uri.toString())); } if (pInputSource) { XML_Parser extParser = XML_ExternalEntityParserCreate(pThis->_parser, context, 0); if (!extParser) throw XMLException("Cannot create external entity parser"); try { pThis->parseExternal(extParser, pInputSource); } catch (XMLException&) { pEntityResolver->releaseInputSource(pInputSource); XML_ParserFree(extParser); throw; } pEntityResolver->releaseInputSource(pInputSource); XML_ParserFree(extParser); return XML_STATUS_OK; } else return XML_STATUS_ERROR; }