ValueMap dictionaryWithContentsOfFile(const std::string& fileName) { _resultType = SAX_RESULT_DICT; SAXParser parser; CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8"); parser.setDelegator(this); parser.parse(fileName); return _rootDict; }
ValueVector arrayWithContentsOfFile(const std::string& fileName) { _resultType = SAX_RESULT_ARRAY; SAXParser parser; CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8"); parser.setDelegator(this); parser.parse(fileName); return _rootArray; }
ValueMap dictionaryWithDataOfFile(const char* filedata, int filesize) { _resultType = SAX_RESULT_DICT; SAXParser parser; CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8"); parser.setDelegator(this); parser.parse(filedata, filesize); return _rootDict; }
Vector<SpriteFrame*> getFrames(const std::string& fileName) { SAXParser parser; parser.init("UTF-8"); parser.setDelegator(this); parser.parse(fileName); return _frames; }
TMXFile::TMXFile(const char* pPath) { TMXFileParser tParser(this, pPath); SAXParser tSAXParser; tSAXParser.setDelegator(&tParser); tSAXParser.parse(pPath); if (tParser.Failed()) { CHU_LOGERROR("failed to load tmx from '%s'.", pPath); throw std::logic_error("LoadFailed"); } }
bool TMXMapInfo::parseXMLFile(const std::string& xmlFilename) { SAXParser parser; if (false == parser.init("UTF-8") ) { return false; } parser.setDelegator(this); return parser.parse(FileUtils::getInstance()->fullPathForFilename(xmlFilename)); }
void SAXParserTest::testUndeclaredNamespaceNoPrefixes() { SAXParser parser; parser.setFeature(XMLReader::FEATURE_NAMESPACES, true); parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, false); try { std::string xml = parse(parser, XMLWriter::CANONICAL, UNDECLARED_NAMESPACE); fail("undeclared namespace - must throw exception"); } catch (SAXParseException&) { } }
VXIlogResult SBlogErrorMapper::ParseErrorMappingFiles(const VXIVector *errorMapFiles) { VXIlogResult rc = VXIlog_RESULT_SUCCESS; if ( errorMapFiles == NULL ) return rc; InitializeXMLParser(); char narrowFileToParse[4096]; VXIunsigned len = VXIVectorLength(errorMapFiles); const VXIValue *xmlFilePtr = NULL; const VXIchar *xmlFile = NULL; for(VXIunsigned i = 0; i < len; ++i) { xmlFilePtr = VXIVectorGetElement(errorMapFiles, i); if( VXIValueGetType(xmlFilePtr) == VALUE_STRING ) { xmlFile = VXIStringCStr((const VXIString *)xmlFilePtr); SBlogWchar2Latin1(xmlFile, narrowFileToParse, 4095); // Let the SAX parser do its job, it will call our handler callbacks try { xmlHandler->resetData(); parser->parse(narrowFileToParse); } catch (const XMLException& exception) { showExceptionMsg(exception); // skip and processing next file continue; } if( xmlHandler->getErrorTable().size() > 0 ) { ModuleMappingTable *map = new ModuleMappingTable; if( map ) { map->next = NULL; map->moduleName = xmlHandler->getModuleName(); map->errFileName = xmlFile; map->errTable = xmlHandler->getErrorTable(); if( errorLookUpTable == NULL ) { errorLookUpTable = map; } else { map->next = errorLookUpTable; errorLookUpTable = map; } } else { return VXIlog_RESULT_FAILURE; } } // end-if getErrorTable() } } return VXIlog_RESULT_SUCCESS; }
bool TMXMapInfo::parseXMLString(const std::string& xmlString) { size_t len = xmlString.size(); if (len <= 0) return false; SAXParser parser; if (false == parser.init("UTF-8") ) { return false; } parser.setDelegator(this); return parser.parse(xmlString.c_str(), len); }
void OptionsIO::loadConfiguration() { OptionsCont& oc = OptionsCont::getOptions(); if (!oc.exists("configuration-file") || !oc.isSet("configuration-file")) { return; } std::string path = oc.getString("configuration-file"); if (!FileHelpers::exists(path)) { throw ProcessError("Could not find configuration '" + oc.getString("configuration-file") + "'."); } PROGRESS_BEGIN_MESSAGE("Loading configuration"); // build parser SAXParser parser; parser.setValidationScheme(SAXParser::Val_Auto); parser.setDoNamespaces(false); parser.setDoSchema(false); // start the parsing OptionsLoader handler; try { parser.setDocumentHandler(&handler); parser.setErrorHandler(&handler); parser.parse(path.c_str()); if (handler.errorOccured()) { throw ProcessError("Could not load configuration '" + path + "'."); } } catch (const XMLException& e) { throw ProcessError("Could not load configuration '" + path + "':\n " + TplConvert<XMLCh>::_2str(e.getMessage())); } oc.relocateFiles(path); PROGRESS_DONE_MESSAGE(); }
void SAXParserTest::testRSS() { SAXParser parser; WhitespaceFilter filter(&parser); TestEntityResolver resolver; filter.setEntityResolver(&resolver); parser.setFeature(XMLReader::FEATURE_EXTERNAL_GENERAL_ENTITIES, true); parser.setFeature(XMLReader::FEATURE_EXTERNAL_PARAMETER_ENTITIES, true); std::istringstream istr(RSS); Poco::FileOutputStream ostr("rss.xml"); XMLWriter writer(ostr, XMLWriter::CANONICAL | XMLWriter::PRETTY_PRINT); filter.setContentHandler(&writer); filter.setDTDHandler(&writer); filter.setProperty(XMLReader::PROPERTY_LEXICAL_HANDLER, static_cast<Poco::XML::LexicalHandler*>(&writer)); InputSource source(istr); filter.parse(&source); }
void AreFriends::handleResponse(istream & inputstream) const { SAXParser parser; parser.setFeature(XMLReader::FEATURE_NAMESPACES, true); parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true); parser.setContentHandler(const_cast<AreFriends*>(this)); try { SharedPtr<InputSource> inputSource(new InputSource(inputstream)); parser.parse(inputSource); mSuccessCallback.call(mFriendInfoList); } catch (Exception& e) { cerr << e.displayText() << endl; mErrorCallback.call(ErrorResponse(0,e.message(), KeyValue())); } }
/* Read the file. */ bool GQCFileData::Read(const std::string &fileName) { Clear(); // Initialize the XML4C2 system try { XMLPlatformUtils::Initialize(); } catch (const XMLException&) { return false; } bool status = false; SAXParser* parser = new SAXParser; parser->setValidationScheme(SAXParser::Val_Never); parser->setLoadExternalDTD(false); parser->setDoNamespaces(false); parser->setDoSchema(false); parser->setValidationSchemaFullChecking(false); SAXGenericReportHandlers handler(this); parser->setDocumentHandler(&handler); parser->setErrorHandler(&handler); try { parser->parse(fileName.c_str()); int errorCount = parser->getErrorCount(); if (errorCount == 0) { status = true; } } catch (...) { status = false; } delete parser; XMLPlatformUtils::Terminate(); return status; }
int main(int argc, char** argv) { // parse an XML document and print the generated SAX events if (argc < 2) { std::cout << "usage: " << argv[0] << ": <xmlfile>" << std::endl; return 1; } MyHandler handler; SAXParser parser; parser.setFeature(XMLReader::FEATURE_NAMESPACES, true); parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true); parser.setContentHandler(&handler); parser.setProperty(XMLReader::PROPERTY_LEXICAL_HANDLER, static_cast<LexicalHandler*>(&handler)); try { parser.parse(argv[1]); } catch (Poco::Exception& e) { std::cerr << e.displayText() << std::endl; return 2; } return 0; }
int InstallationParser::parse() { if (initialization() == 1) // Problem during the initialisation. return(1); SAXParser* parser = new SAXParser(); parser->setDoValidation(true); parser->setDoNamespaces(true); InstallationSAXHandler* handler = new InstallationSAXHandler(); parser->setDocumentHandler(handler); parser->setErrorHandler(handler); try { const char* file = xmlFile.c_str(); // Name of the descriptor file in the current directory. parser->parse(file); // Parsing procedure. handler->get_pkgname(pkgname); // Result of the parsing. handler->get_pkgUUID(pkgUUID); handler->get_implementations(codefiles); // Result of the parsing. } catch (const XMLException& toCatch) { // File not found for the parsing. std::cerr << "\nFile not found : '" << xmlFile << "'\n"; std::cerr << "Exception message is : \n" << toCatch.getMessage() << "\n"; return(4); } int err = handler->get_error(); // The error codes are got from the handler object. delete(handler); // Object deleted. delete(parser); // Object deleted. return(err); // 0, 2 or 3 is returned. }
bool Kumu::XMLElement::ParseString(const char* document, ui32_t doc_len) { if ( doc_len == 0 ) return false; init_xml_dom(); int errorCount = 0; SAXParser* parser = new SAXParser(); parser->setValidationScheme(SAXParser::Val_Always); parser->setDoNamespaces(true); // optional MyTreeHandler* docHandler = new MyTreeHandler(this); parser->setDocumentHandler(docHandler); parser->setErrorHandler(docHandler); try { MemBufInputSource xmlSource(reinterpret_cast<const XMLByte*>(document), static_cast<const unsigned int>(doc_len), "pidc_rules_file"); parser->parse(xmlSource); } catch (const XMLException& e) { char* message = XMLString::transcode(e.getMessage()); DefaultLogSink().Error("Parser error: %s\n", message); XMLString::release(&message); errorCount++; } catch (const SAXParseException& e) { char* message = XMLString::transcode(e.getMessage()); DefaultLogSink().Error("Parser error: %s at line %d\n", message, e.getLineNumber()); XMLString::release(&message); errorCount++; } catch (...) { DefaultLogSink().Error("Unexpected XML parser error\n"); errorCount++; } if ( errorCount == 0 ) m_NamespaceOwner = (void*)docHandler->TakeNamespaceMap(); delete parser; delete docHandler; return errorCount > 0 ? false : true; }
void PSAPAddrTest::setUp() { #if 0 // // Macros are defined for now by the Macro constructor // AEISAXHandler handler; SAXParser parser; parser.setFeature(XMLReader::FEATURE_NAMESPACES, true); parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true); parser.setContentHandler(&handler); parser.setProperty(XMLReader::PROPERTY_LEXICAL_HANDLER, static_cast<LexicalHandler*>(&handler)); parser.parse("../../../../../../test/iso/itu/osi/als/base/addr/Macros.xml"); #endif }
int main (int argc, char** argv) { if (false) { RFC_1006 rfc1006; NSAPAddr nsap; const char* n = rfc1006.parse(nsap, "+03+10.0.0.6:8080"); const char*p; Selector selector; p = selector.parse("////NS+a433bb93c1"); p = selector.parse("#124/NS"); p = selector.parse("\"abc\"/NS"); p = selector.parse("'3a'H/NS"); string sel = "#124"; TSAPService service("echo", "#124", "pippo", "bar"); TSAPServices services; services.add("echo", "#12", "pippo", "bar"); const OSIService* os = services.getServiceBySelector(selector); if (os != NULL) { os->entity(); } } { AEISAXHandler handler; SAXParser parser; parser.setFeature(XMLReader::FEATURE_NAMESPACES, true); parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true); parser.setContentHandler(&handler); parser.setProperty(XMLReader::PROPERTY_LEXICAL_HANDLER, static_cast<LexicalHandler*>(&handler)); try { parser.parse("AEI.xml"); ServiceFunctor functor(cout); //TSAP_SERVICES.print(functor); //SSAP_SERVICES.print(functor); //PSAP_SERVICES.print(functor); const OSIService* service = TSAP_SERVICES.getServiceByEntity("ssap"); cout << service->mangled() << endl; } catch (Poco::Exception& e) { std::cerr << e.displayText() << std::endl; } } #if 0 <config>
void SAXParserTest::testEncoding() { SAXParser parser; Poco::Latin9Encoding encoding; parser.addEncoding("ISO-8859-15", &encoding); std::istringstream istr(ENCODING); std::ostringstream ostr; XMLWriter writer(ostr, XMLWriter::WRITE_XML_DECLARATION, "ISO-8859-15", encoding); parser.setContentHandler(&writer); parser.setDTDHandler(&writer); parser.setProperty(XMLReader::PROPERTY_LEXICAL_HANDLER, static_cast<Poco::XML::LexicalHandler*>(&writer)); InputSource source(istr); parser.parse(&source); std::string xml = ostr.str(); assert (xml == ENCODING); }
bool streamXML(const string& filename, XMLStream& stream) { try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cerr << "XML initialization failed: " << message << endl; return false; } SAXParser parser; parser.setDoValidation(true); // optional. parser.setDoNamespaces(true); // optional StreamHandler docHandler(stream); parser.setDocumentHandler(&docHandler); parser.setErrorHandler((ErrorHandler*)&docHandler); try { parser.parse(filename.c_str()); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cerr << "XML Exception: " << message << endl; return false; } catch (const SAXParseException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cerr << "XML Parsing exception: " << message << endl; return false; } catch (const SAXException& e) { char* message = XMLString::transcode(e.getMessage()); cerr << "Bad file format exception: " << message << endl; return false; } catch (...) { cerr << "Unexpected Exception \n" ; return false; } return true; }
// --------------------------------------------------------------------------- // Program entry point // --------------------------------------------------------------------------- int main(int argC, char* argV[]) { // Initialize the XML4C system try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n" << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl; return 1; } // Check command line and extract arguments. if (argC < 2) { usage(); XMLPlatformUtils::Terminate(); return 1; } // We only have one required parameter, which is the file to process if ((argC != 2) || (*(argV[1]) == '-')) { usage(); XMLPlatformUtils::Terminate(); return 1; } const char* xmlFile = argV[1]; SAXParser::ValSchemes valScheme = SAXParser::Val_Auto; // // Create a DTD validator to be used for our validation work. Then create // a SAX parser object and pass it our validator. Then, according to what // we were told on the command line, set it to validate or not. He owns // the validator, so we have to allocate it. // int errorCount = 0; DTDValidator* valToUse = new DTDValidator; SAXParser* parser = new SAXParser(valToUse); parser->setValidationScheme(valScheme); // // Get the starting time and kick off the parse of the indicated // file. Catch any exceptions that might propogate out of it. // int errorCode = 0; try { parser->parse(xmlFile); errorCount = parser->getErrorCount(); } catch (const OutOfMemoryException&) { XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl; errorCode = 5; } catch (const XMLException& e) { XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << xmlFile << "'\n" << "Exception message is: \n" << StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl; errorCode = 4; } if(errorCode) { XMLPlatformUtils::Terminate(); return errorCode; } if (!errorCount) { // // Now we will get an enumerator for the element pool from the validator // and enumerate the elements, printing them as we go. For each element // we get an enumerator for its attributes and print them also. // DTDGrammar* grammar = (DTDGrammar*) valToUse->getGrammar(); NameIdPoolEnumerator<DTDElementDecl> elemEnum = grammar->getElemEnumerator(); if (elemEnum.hasMoreElements()) { XERCES_STD_QUALIFIER cout << "\nELEMENTS:\n----------------------------\n"; while(elemEnum.hasMoreElements()) { const DTDElementDecl& curElem = elemEnum.nextElement(); XERCES_STD_QUALIFIER cout << " Name: " << StrX(curElem.getFullName()) << "\n"; XERCES_STD_QUALIFIER cout << " Content Model: " << StrX(curElem.getFormattedContentModel()) << "\n"; // Get an enumerator for this guy's attributes if any if (curElem.hasAttDefs()) { XERCES_STD_QUALIFIER cout << " Attributes:\n"; XMLAttDefList& attList = curElem.getAttDefList(); for (unsigned int i=0; i<attList.getAttDefCount(); i++) { const XMLAttDef& curAttDef = attList.getAttDef(i); XERCES_STD_QUALIFIER cout << " Name:" << StrX(curAttDef.getFullName()) << ", Type: "; // Get the type and display it const XMLAttDef::AttTypes type = curAttDef.getType(); switch(type) { case XMLAttDef::CData : XERCES_STD_QUALIFIER cout << "CDATA"; break; case XMLAttDef::ID : XERCES_STD_QUALIFIER cout << "ID"; break; case XMLAttDef::IDRef : case XMLAttDef::IDRefs : XERCES_STD_QUALIFIER cout << "IDREF(S)"; break; case XMLAttDef::Entity : case XMLAttDef::Entities : XERCES_STD_QUALIFIER cout << "ENTITY(IES)"; break; case XMLAttDef::NmToken : case XMLAttDef::NmTokens : XERCES_STD_QUALIFIER cout << "NMTOKEN(S)"; break; case XMLAttDef::Notation : XERCES_STD_QUALIFIER cout << "NOTATION"; break; case XMLAttDef::Enumeration : XERCES_STD_QUALIFIER cout << "ENUMERATION"; break; default: break; } XERCES_STD_QUALIFIER cout << "\n"; } } XERCES_STD_QUALIFIER cout << XERCES_STD_QUALIFIER endl; } } else { XERCES_STD_QUALIFIER cout << "The validator has no elements to display\n" << XERCES_STD_QUALIFIER endl; } } else XERCES_STD_QUALIFIER cout << "\nErrors occurred, no output available\n" << XERCES_STD_QUALIFIER endl; // // Delete the parser itself. Must be done prior to calling Terminate, below. // delete parser; // And call the termination method XMLPlatformUtils::Terminate(); if (errorCount > 0) return 4; else return 0; }
int main (int argC, char *argV[]) { MemoryMonitor *staticMemMonitor = new MemoryMonitor(); // Initialize the XML4C system try { XMLPlatformUtils::Initialize(XMLUni::fgXercescDefaultLocale, 0, 0, staticMemMonitor); } catch (const XMLException& toCatch) { char *msg = XMLString::transcode(toCatch.getMessage()); XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n" << msg << XERCES_STD_QUALIFIER endl; XMLString::release(&msg); return 1; } // Check command line and extract arguments. if (argC < 2) { usage(); return 1; } const char* xmlFile = 0; AbstractDOMParser::ValSchemes domBuilderValScheme = AbstractDOMParser::Val_Auto; bool doNamespaces = false; bool doSchema = false; bool schemaFullChecking = false; bool doList = false; int numReps =1; int argInd; for (argInd = 1; argInd < argC; argInd++) { // Break out on first parm not starting with a dash if (argV[argInd][0] != '-') break; // Watch for special case help request if (!strcmp(argV[argInd], "-?")) { usage(); return 2; } else if (!strncmp(argV[argInd], "-v=", 3) || !strncmp(argV[argInd], "-V=", 3)) { const char* const parm = &argV[argInd][3]; if (!strcmp(parm, "never")) domBuilderValScheme = AbstractDOMParser::Val_Never; else if (!strcmp(parm, "auto")) domBuilderValScheme = AbstractDOMParser::Val_Auto; else if (!strcmp(parm, "always")) domBuilderValScheme = AbstractDOMParser::Val_Always; else { XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl; return 2; } } else if (!strcmp(argV[argInd], "-n") || !strcmp(argV[argInd], "-N")) { doNamespaces = true; } else if (!strcmp(argV[argInd], "-s") || !strcmp(argV[argInd], "-S")) { doSchema = true; } else if (!strcmp(argV[argInd], "-f") || !strcmp(argV[argInd], "-F")) { schemaFullChecking = true; } else if (!strcmp(argV[argInd], "-l") || !strcmp(argV[argInd], "-L")) { doList = true; } else if (!strncmp(argV[argInd], "-r=", 3) || !strncmp(argV[argInd], "-R=", 3)) { const char* const numStr = &argV[argInd][3]; XMLCh* numXStr = XMLString::transcode(numStr); numReps = XMLString::parseInt(numXStr); XMLString::release(&numXStr); } else { XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[argInd] << "', ignoring it\n" << XERCES_STD_QUALIFIER endl; } } // // There should be only one and only one parameter left, and that // should be the file name. // if (argInd != argC - 1) { usage(); return 1; } // Instantiate the DOM domBuilder with its memory manager. MemoryMonitor *domBuilderMemMonitor = new MemoryMonitor(); static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull }; DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS); DOMLSParser *domBuilder = ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0, domBuilderMemMonitor); DOMLSParserHandler domBuilderHandler; domBuilder->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, &domBuilderHandler); // Instantiate the SAX2 parser with its memory manager. MemoryMonitor *sax2MemMonitor = new MemoryMonitor(); SAX2XMLReader *sax2parser = XMLReaderFactory::createXMLReader(sax2MemMonitor); SAXErrorHandler saxErrorHandler; sax2parser->setErrorHandler(&saxErrorHandler); // Instantiate the SAX 1 parser with its memory manager. MemoryMonitor *sax1MemMonitor = new MemoryMonitor(); SAXParser *saxParser = new (sax1MemMonitor) SAXParser(0, sax1MemMonitor); saxParser->setErrorHandler(&saxErrorHandler); // set features domBuilder->getDomConfig()->setParameter(XMLUni::fgDOMNamespaces, doNamespaces); sax2parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, doNamespaces); saxParser->setDoNamespaces(doNamespaces); domBuilder->getDomConfig()->setParameter(XMLUni::fgXercesSchema, doSchema); sax2parser->setFeature(XMLUni::fgXercesSchema, doSchema); saxParser->setDoSchema(doSchema); domBuilder->getDomConfig()->setParameter(XMLUni::fgXercesHandleMultipleImports, true); sax2parser->setFeature(XMLUni::fgXercesHandleMultipleImports, true); saxParser->setHandleMultipleImports (true); domBuilder->getDomConfig()->setParameter(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking); sax2parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking); saxParser->setValidationSchemaFullChecking(schemaFullChecking); if (domBuilderValScheme == AbstractDOMParser::Val_Auto) { domBuilder->getDomConfig()->setParameter(XMLUni::fgDOMValidateIfSchema, true); sax2parser->setFeature(XMLUni::fgSAX2CoreValidation, true); sax2parser->setFeature(XMLUni::fgXercesDynamic, true); saxParser->setValidationScheme(SAXParser::Val_Auto); } else if (domBuilderValScheme == AbstractDOMParser::Val_Never) { domBuilder->getDomConfig()->setParameter(XMLUni::fgDOMValidate, false); sax2parser->setFeature(XMLUni::fgSAX2CoreValidation, false); saxParser->setValidationScheme(SAXParser::Val_Never); } else if (domBuilderValScheme == AbstractDOMParser::Val_Always) { domBuilder->getDomConfig()->setParameter(XMLUni::fgDOMValidate, true); sax2parser->setFeature(XMLUni::fgSAX2CoreValidation, true); sax2parser->setFeature(XMLUni::fgXercesDynamic, false); saxParser->setValidationScheme(SAXParser::Val_Always); } // enable datatype normalization - default is off domBuilder->getDomConfig()->setParameter(XMLUni::fgDOMDatatypeNormalization, true); XERCES_STD_QUALIFIER ifstream fin; bool more = true; // the input is a list file if (doList) fin.open(argV[argInd]); if (fin.fail()) { XERCES_STD_QUALIFIER cerr <<"Cannot open the list file: " << argV[argInd] << XERCES_STD_QUALIFIER endl; return 2; } while (more) { char fURI[1000]; //initialize the array to zeros memset(fURI,0,sizeof(fURI)); if (doList) { if (! fin.eof() ) { fin.getline (fURI, sizeof(fURI)); if (!*fURI) continue; else { xmlFile = fURI; XERCES_STD_QUALIFIER cerr << "==Parsing== " << xmlFile << XERCES_STD_QUALIFIER endl; } } else break; } else { xmlFile = argV[argInd]; more = false; } // parse numReps times (in case we need it for some reason) for (int i=0; i<numReps; i++) { XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0; try { // reset document pool domBuilder->resetDocumentPool(); doc = domBuilder->parseURI(xmlFile); if(doc && doc->getDocumentElement()) { XERCES_CPP_NAMESPACE_QUALIFIER DOMNodeList *list=NULL; if(doNamespaces) list=doc->getElementsByTagNameNS(doc->getDocumentElement()->getNamespaceURI(), doc->getDocumentElement()->getLocalName()); else list=doc->getElementsByTagName(doc->getDocumentElement()->getNodeName()); if(list==NULL) XERCES_STD_QUALIFIER cout << "getElementsByTagName didn't return a valid DOMNodeList." << XERCES_STD_QUALIFIER endl; else if(list->item(0)!=doc->getDocumentElement()) XERCES_STD_QUALIFIER cout << "getElementsByTagName didn't find the root element." << XERCES_STD_QUALIFIER endl; } sax2parser->parse(xmlFile); saxParser->parse(xmlFile); } catch (const OutOfMemoryException&) { XERCES_STD_QUALIFIER cerr << "OutOfMemoryException during parsing: '" << xmlFile << "'\n" << XERCES_STD_QUALIFIER endl;; continue; } catch (const XMLException& toCatch) { char *msg = XMLString::transcode(toCatch.getMessage()); XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << xmlFile << "'\n" << "Exception message is: \n" << msg << "\n" << XERCES_STD_QUALIFIER endl; XMLString::release(&msg); continue; } catch (const DOMException& toCatch) { const unsigned int maxChars = 2047; XMLCh errText[maxChars + 1]; XERCES_STD_QUALIFIER cerr << "\nDOM Error during parsing: '" << xmlFile << "'\n" << "DOMException code is: " << toCatch.code << XERCES_STD_QUALIFIER endl; if (DOMImplementation::loadDOMExceptionMsg(toCatch.code, errText, maxChars)) { char * msg = XMLString::transcode(errText); XERCES_STD_QUALIFIER cerr << "Message is: " << msg << XERCES_STD_QUALIFIER endl; continue; } } catch (...) { XERCES_STD_QUALIFIER cerr << "\nUnexpected exception during parsing: '" << xmlFile << "'\n"; continue; } } } // // Delete the domBuilder itself. Must be done prior to calling Terminate, below. // domBuilder->release(); delete sax2parser; delete saxParser; XERCES_STD_QUALIFIER cout << "At destruction, domBuilderMemMonitor has " << domBuilderMemMonitor->getTotalMemory() << " bytes." << XERCES_STD_QUALIFIER endl; XERCES_STD_QUALIFIER cout << "At destruction, sax2MemMonitor has " << sax2MemMonitor->getTotalMemory() << " bytes." << XERCES_STD_QUALIFIER endl; XERCES_STD_QUALIFIER cout << "At destruction, sax1MemMonitor has " << sax1MemMonitor->getTotalMemory() << " bytes." << XERCES_STD_QUALIFIER endl; delete domBuilderMemMonitor; delete sax2MemMonitor; delete sax1MemMonitor; XMLPlatformUtils::Terminate(); XERCES_STD_QUALIFIER cout << "At destruction, staticMemMonitor has " << staticMemMonitor->getTotalMemory() << " bytes." << XERCES_STD_QUALIFIER endl; delete staticMemMonitor; return 0; }
// --------------------------------------------------------------------------- // Program entry point // --------------------------------------------------------------------------- int main(int argC, char* argV[]) { // Initialize the XML4C system try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n" << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl; return 1; } int parmInd; for (parmInd = 1; parmInd < argC; parmInd++) { // Break out on first parm not starting with a dash if (argV[parmInd][0] != '-') break; // Watch for special case help request if (!strcmp(argV[parmInd], "-?")) { usage(); XMLPlatformUtils::Terminate(); return 2; } else if (!strncmp(argV[parmInd], "-v=", 3) || !strncmp(argV[parmInd], "-V=", 3)) { const char* const parm = &argV[parmInd][3]; if (!strcmp(parm, "never")) valScheme = SAXParser::Val_Never; else if (!strcmp(parm, "auto")) valScheme = SAXParser::Val_Auto; else if (!strcmp(parm, "always")) valScheme = SAXParser::Val_Always; else { XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl; XMLPlatformUtils::Terminate(); return 2; } } else if (!strcmp(argV[parmInd], "-n") || !strcmp(argV[parmInd], "-N")) { doNamespaces = true; } else if (!strcmp(argV[parmInd], "-s") || !strcmp(argV[parmInd], "-S")) { doSchema = true; } else if (!strcmp(argV[parmInd], "-f") || !strcmp(argV[parmInd], "-F")) { schemaFullChecking = true; } else { XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd] << "', ignoring it\n" << XERCES_STD_QUALIFIER endl; } } // // Create a SAX parser object. Then, according to what we were told on // the command line, set the options. // SAXParser* parser = new SAXParser; parser->setValidationScheme(valScheme); parser->setDoNamespaces(doNamespaces); parser->setDoSchema(doSchema); parser->setValidationSchemaFullChecking(schemaFullChecking); // // Create our SAX handler object and install it on the parser, as the // document and error handler. We are responsible for cleaning them // up, but since its just stack based here, there's nothing special // to do. // StdInParseHandlers handler; parser->setDocumentHandler(&handler); parser->setErrorHandler(&handler); unsigned long duration; int errorCount = 0; // create a faux scope so that 'src' destructor is called before // XMLPlatformUtils::Terminate { // // Kick off the parse and catch any exceptions. Create a standard // input input source and tell the parser to parse from that. // StdInInputSource src; try { const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis(); parser->parse(src); const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis(); duration = endMillis - startMillis; errorCount = parser->getErrorCount(); } catch (const OutOfMemoryException&) { XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl; errorCount = 2; return 4; } catch (const XMLException& e) { XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n" << StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl; errorCount = 1; return 4; } // Print out the stats that we collected and time taken if (!errorCount) { XERCES_STD_QUALIFIER cout << StrX(src.getSystemId()) << ": " << duration << " ms (" << handler.getElementCount() << " elems, " << handler.getAttrCount() << " attrs, " << handler.getSpaceCount() << " spaces, " << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl; } } // // Delete the parser itself. Must be done prior to calling Terminate, below. // delete parser; XMLPlatformUtils::Terminate(); if (errorCount > 0) return 4; else return 0; }
void process(char* const xmlFile) { // // Create a Schema validator to be used for our validation work. Then create // a SAX parser object and pass it our validator. Then, according to what // we were told on the command line, set it to validate or not. He owns // the validator, so we have to allocate it. // SAXParser parser; parser.setValidationScheme(SAXParser::Val_Always); parser.setDoNamespaces(true); parser.setDoSchema(true); parser.parse(xmlFile); if (parser.getErrorCount()) { XERCES_STD_QUALIFIER cout << "\nErrors occurred, no output available\n" << XERCES_STD_QUALIFIER endl; return; } if (!parser.getValidator().handlesSchema()) { XERCES_STD_QUALIFIER cout << "\n Non schema document, no output available\n" << XERCES_STD_QUALIFIER endl; return; } Grammar* rootGrammar = parser.getRootGrammar(); if (!rootGrammar || rootGrammar->getGrammarType() != Grammar::SchemaGrammarType) { XERCES_STD_QUALIFIER cout << "\n Non schema grammar, no output available\n" << XERCES_STD_QUALIFIER endl; return; } // // Now we will get an enumerator for the element pool from the validator // and enumerate the elements, printing them as we go. For each element // we get an enumerator for its attributes and print them also. // SchemaGrammar* grammar = (SchemaGrammar*) rootGrammar; RefHash3KeysIdPoolEnumerator<SchemaElementDecl> elemEnum = grammar->getElemEnumerator(); if (!elemEnum.hasMoreElements()) { XERCES_STD_QUALIFIER cout << "\nThe validator has no elements to display\n" << XERCES_STD_QUALIFIER endl; return; } while(elemEnum.hasMoreElements()) { const SchemaElementDecl& curElem = elemEnum.nextElement(); // Name XERCES_STD_QUALIFIER cout << "Name:\t\t\t" << StrX(curElem.getFullName()) << "\n"; // Model Type XERCES_STD_QUALIFIER cout << "Model Type:\t\t"; switch( curElem.getModelType() ) { case SchemaElementDecl::Empty: XERCES_STD_QUALIFIER cout << "Empty"; break; case SchemaElementDecl::Any: XERCES_STD_QUALIFIER cout << "Any"; break; case SchemaElementDecl::Mixed_Simple: XERCES_STD_QUALIFIER cout << "Mixed_Simple"; break; case SchemaElementDecl::Mixed_Complex: XERCES_STD_QUALIFIER cout << "Mixed_Complex"; break; case SchemaElementDecl::Children: XERCES_STD_QUALIFIER cout << "Children"; break; case SchemaElementDecl::Simple: XERCES_STD_QUALIFIER cout << "Simple"; break; case SchemaElementDecl::ElementOnlyEmpty: XERCES_STD_QUALIFIER cout << "ElementOnlyEmpty"; break; default: XERCES_STD_QUALIFIER cout << "Unknown"; break; } XERCES_STD_QUALIFIER cout << "\n"; // Create Reason XERCES_STD_QUALIFIER cout << "Create Reason:\t"; switch( curElem.getCreateReason() ) { case XMLElementDecl::NoReason: XERCES_STD_QUALIFIER cout << "Empty"; break; case XMLElementDecl::Declared: XERCES_STD_QUALIFIER cout << "Declared"; break; case XMLElementDecl::AttList: XERCES_STD_QUALIFIER cout << "AttList"; break; case XMLElementDecl::InContentModel: XERCES_STD_QUALIFIER cout << "InContentModel"; break; case XMLElementDecl::AsRootElem: XERCES_STD_QUALIFIER cout << "AsRootElem"; break; case XMLElementDecl::JustFaultIn: XERCES_STD_QUALIFIER cout << "JustFaultIn"; break; default: XERCES_STD_QUALIFIER cout << "Unknown"; break; } XERCES_STD_QUALIFIER cout << "\n"; // Content Spec Node processContentSpecNode( curElem.getContentSpec() ); // Misc Flags int mflags = curElem.getMiscFlags(); if( mflags !=0 ) { XERCES_STD_QUALIFIER cout << "Misc. Flags:\t"; } if ( mflags & SchemaSymbols::XSD_NILLABLE ) XERCES_STD_QUALIFIER cout << "Nillable "; if ( mflags & SchemaSymbols::XSD_ABSTRACT ) XERCES_STD_QUALIFIER cout << "Abstract "; if ( mflags & SchemaSymbols::XSD_FIXED ) XERCES_STD_QUALIFIER cout << "Fixed "; if( mflags !=0 ) { XERCES_STD_QUALIFIER cout << "\n"; } // Substitution Name SchemaElementDecl* subsGroup = curElem.getSubstitutionGroupElem(); if( subsGroup ) { const XMLCh* uriText = parser.getURIText(subsGroup->getURI()); XERCES_STD_QUALIFIER cout << "Substitution Name:\t" << StrX(uriText) << "," << StrX(subsGroup->getBaseName()) << "\n"; } // Content Model const XMLCh* fmtCntModel = curElem.getFormattedContentModel(); if( fmtCntModel != NULL ) { XERCES_STD_QUALIFIER cout << "Content Model:\t" << StrX(fmtCntModel) << "\n"; } const ComplexTypeInfo* ctype = curElem.getComplexTypeInfo(); if( ctype != NULL) { XERCES_STD_QUALIFIER cout << "ComplexType:\n"; XERCES_STD_QUALIFIER cout << "\tTypeName:\t" << StrX(ctype->getTypeName()) << "\n"; ContentSpecNode* cSpecNode = ctype->getContentSpec(); processContentSpecNode(cSpecNode, true ); } // Datatype DatatypeValidator* dtValidator = curElem.getDatatypeValidator(); processDatatypeValidator( dtValidator ); // Get an enumerator for this guy's attributes if any if ( curElem.hasAttDefs() ) { processAttributes( curElem.getAttDefList() ); } XERCES_STD_QUALIFIER cout << "--------------------------------------------"; XERCES_STD_QUALIFIER cout << XERCES_STD_QUALIFIER endl; } return; }
//------------------------------------------------------------------------ // // parse - This is the method that is invoked by the rest of // the test program to actually parse an XML file. // //------------------------------------------------------------------------ int ThreadParser::parse(int fileNum) { MemBufInputSource *mbis = 0; InFileInfo *fInfo = &gRunInfo.files[fileNum]; bool errors = false; fCheckSum = 0; if (gRunInfo.inMemory) { mbis = new MemBufInputSource((const XMLByte *) fInfo->fileContent, fInfo->fileSize, fInfo->uFileName, false); } try { if (gRunInfo.dom) { // Do a DOM parse fXercesDOMParser->resetDocumentPool(); if (gRunInfo.inMemory) fXercesDOMParser->parse(*mbis); else fXercesDOMParser->parse(fInfo->fileName); fDoc = fXercesDOMParser->getDocument(); domCheckSum(fDoc); } else if (gRunInfo.sax) { // Do a SAX1 parse if (gRunInfo.inMemory) fSAXParser->parse(*mbis); else fSAXParser->parse(fInfo->fileName); } else { // Do a SAX2 parse if (gRunInfo.inMemory) fSAX2Parser->parse(*mbis); else fSAX2Parser->parse(fInfo->fileName); } } catch (const OutOfMemoryException&) { fprintf(stderr, " during parsing: %s\n OutOfMemoryException.\n", fInfo->fileName); errors = true; } catch (const XMLException& e) { char *exceptionMessage = XMLString::transcode(e.getMessage()); fprintf(stderr, " during parsing: %s\n Exception message is: %s\n", fInfo->fileName, exceptionMessage); XMLString::release(&exceptionMessage); errors = true; } catch (const DOMException& toCatch) { fprintf(stderr, " during parsing: %s\n DOMException code is: %i\n", fInfo->fileName, toCatch.code); errors = true; } catch (const SAXParseException& e) { char *exceptionMessage = XMLString::transcode(e.getMessage()); fprintf(stderr, " during parsing: %s\n Exception message is: %s\n", fInfo->fileName, exceptionMessage); XMLString::release(&exceptionMessage); errors = true; } catch (...) { fprintf(stderr, "Unexpected exception during parsing\n"); errors = true; } delete mbis; if (errors) { fflush(stderr); return 0; // if errors occurred, return zero as if checksum = 0; } return fCheckSum; }
// --------------------------------------------------------------------------- // Program entry point // --------------------------------------------------------------------------- int main(int argC, char* argV[]) { // Initialize the XML4C2 system try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n" << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl; return 1; } SAXParser::ValSchemes valScheme = SAXParser::Val_Auto; bool doNamespaces = false; bool doSchema = false; bool schemaFullChecking = false; int argInd; for (argInd = 1; argInd < argC; argInd++) { // Break out on first parm not starting with a dash if (argV[argInd][0] != '-') { usage(); XMLPlatformUtils::Terminate(); return 1; } // Watch for special case help request if (!strcmp(argV[argInd], "-?")) { usage(); XMLPlatformUtils::Terminate(); return 1; } else if (!strncmp(argV[argInd], "-v=", 3) || !strncmp(argV[argInd], "-V=", 3)) { const char* const parm = &argV[argInd][3]; if (!strcmp(parm, "never")) valScheme = SAXParser::Val_Never; else if (!strcmp(parm, "auto")) valScheme = SAXParser::Val_Auto; else if (!strcmp(parm, "always")) valScheme = SAXParser::Val_Always; else { XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl; return 2; } } else if (!strcmp(argV[argInd], "-n") || !strcmp(argV[argInd], "-N")) { doNamespaces = true; } else if (!strcmp(argV[argInd], "-s") || !strcmp(argV[argInd], "-S")) { doSchema = true; } else if (!strcmp(argV[argInd], "-f") || !strcmp(argV[argInd], "-F")) { schemaFullChecking = true; } else { XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[argInd] << "', ignoring it\n" << XERCES_STD_QUALIFIER endl; } } // // Create a SAX parser object. Then, according to what we were told on // the command line, set it to validate or not. // SAXParser* parser = new SAXParser; parser->setValidationScheme(valScheme); parser->setDoNamespaces(doNamespaces); parser->setDoSchema(doSchema); parser->setValidationSchemaFullChecking(schemaFullChecking); // // Create our SAX handler object and install it on the parser, as the // document and error handlers. // MemParseHandlers handler; parser->setDocumentHandler(&handler); parser->setErrorHandler(&handler); // // Create MemBufferInputSource from the buffer containing the XML // statements. // // NOTE: We are using strlen() here, since we know that the chars in // our hard coded buffer are single byte chars!!! The parameter wants // the number of BYTES, not chars, so when you create a memory buffer // give it the byte size (which just happens to be the same here.) // MemBufInputSource* memBufIS = new MemBufInputSource ( (const XMLByte*)gXMLInMemBuf , strlen(gXMLInMemBuf) , gMemBufId , false ); // // Get the starting time and kick off the parse of the indicated // file. Catch any exceptions that might propogate out of it. // unsigned long duration; int errorCount = 0; int errorCode = 0; try { const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis(); parser->parse(*memBufIS); const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis(); duration = endMillis - startMillis; errorCount = parser->getErrorCount(); } catch (const OutOfMemoryException&) { XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl; errorCode = 5; } catch (const XMLException& e) { XERCES_STD_QUALIFIER cerr << "\nError during parsing memory stream:\n" << "Exception message is: \n" << StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl; errorCode = 4; } if(errorCode) { XMLPlatformUtils::Terminate(); return errorCode; } // Print out the stats that we collected and time taken. if (!errorCount) { XERCES_STD_QUALIFIER cout << "\nFinished parsing the memory buffer containing the following " << "XML statements:\n\n" << gXMLInMemBuf << "\n\n\n" << "Parsing took " << duration << " ms (" << handler.getElementCount() << " elements, " << handler.getAttrCount() << " attributes, " << handler.getSpaceCount() << " spaces, " << handler.getCharacterCount() << " characters).\n" << XERCES_STD_QUALIFIER endl; } // // Delete the parser itself. Must be done prior to calling Terminate, below. // delete parser; delete memBufIS; // And call the termination method XMLPlatformUtils::Terminate(); if (errorCount > 0) return 4; else return 0; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { #else int main (int argc, char* argv[]) { //int nlhs; //mxArray *plhs[3]; //int nrhs; //const mxArray *prhs[]; #endif #ifdef MAT if (nrhs < 1) { #else if (argc < 1) { #endif cout << "XML corpus file required"; exit(1); } // get the corpus file name #ifdef MAT int strlen = mxGetN(prhs[0])+1; char* corpus_file = (char*)mxCalloc(strlen, sizeof(char)); //mxCalloc is similar to malloc in C mxGetString(prhs[0], corpus_file, strlen); #else char* corpus_file = argv[1]; #endif #ifdef DEBUG cout << "Parsing document: " << corpus_file << "\n"; #endif try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cout << "Error during initialization! :\n" << message << "\n"; XMLString::release(&message); exit(1);//return 1; } SAXParser* parser = new SAXParser(); parser->setValidationScheme(SAXParser::Val_Always); parser->setDoNamespaces(true); // optional MovieSAXHandler* content = new MovieSAXHandler(); ErrorHandler* errHandler = (ErrorHandler*) content; parser->setDocumentHandler(content); parser->setErrorHandler(errHandler); try { parser->parse(corpus_file); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); exit(1);//return -1; } catch (const SAXParseException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); exit(1);//return -1; } catch (...) { cout << "Unexpected Exception \n" ; exit(1);//return -1; } #ifdef DEBUG cout << "Printing the inverted index: \n"; cout << InvertedIndex::instance() << "\n"; #endif #ifdef MAT // call this function to output all the matrices to the MATLAB environment. MATProcedure(nlhs, plhs, nrhs, prhs); #else // call this function to output features to files OUTProcedure(); #endif #ifdef DEBUG cout << "Done \n"; #endif delete InvertedIndex::instance(); delete parser; delete content; }
// --------------------------------------------------------------------------- // Program entry point // --------------------------------------------------------------------------- int main(int argC, char* argV[]) { // Check command line and extract arguments. if (argC < 2) { usage(); return 1; } const char* xmlFile = 0; SAXParser::ValSchemes valScheme = SAXParser::Val_Auto; bool doNamespaces = false; bool doSchema = false; bool schemaFullChecking = false; bool doList = false; bool errorOccurred = false; bool recognizeNEL = false; char localeStr[64]; memset(localeStr, 0, sizeof localeStr); int argInd; for (argInd = 1; argInd < argC; argInd++) { // Break out on first parm not starting with a dash if (argV[argInd][0] != '-') break; // Watch for special case help request if (!strcmp(argV[argInd], "-?")) { usage(); return 2; } else if (!strncmp(argV[argInd], "-v=", 3) || !strncmp(argV[argInd], "-V=", 3)) { const char* const parm = &argV[argInd][3]; if (!strcmp(parm, "never")) valScheme = SAXParser::Val_Never; else if (!strcmp(parm, "auto")) valScheme = SAXParser::Val_Auto; else if (!strcmp(parm, "always")) valScheme = SAXParser::Val_Always; else { XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl; return 2; } } else if (!strcmp(argV[argInd], "-n") || !strcmp(argV[argInd], "-N")) { doNamespaces = true; } else if (!strcmp(argV[argInd], "-s") || !strcmp(argV[argInd], "-S")) { doSchema = true; } else if (!strcmp(argV[argInd], "-f") || !strcmp(argV[argInd], "-F")) { schemaFullChecking = true; } else if (!strcmp(argV[argInd], "-l") || !strcmp(argV[argInd], "-L")) { doList = true; } else if (!strcmp(argV[argInd], "-special:nel")) { // turning this on will lead to non-standard compliance behaviour // it will recognize the unicode character 0x85 as new line character // instead of regular character as specified in XML 1.0 // do not turn this on unless really necessary recognizeNEL = true; } else if (!strncmp(argV[argInd], "-locale=", 8)) { // Get out the end of line strcpy(localeStr, &(argV[argInd][8])); } else { XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[argInd] << "', ignoring it\n" << XERCES_STD_QUALIFIER endl; } } // // There should at least one parameter left, and that // should be the file name(s). // if (argInd == argC) { usage(); return 1; } // Initialize the XML4C2 system try { if (strlen(localeStr)) { XMLPlatformUtils::Initialize(localeStr); } else { XMLPlatformUtils::Initialize(); } if (recognizeNEL) { XMLPlatformUtils::recognizeNEL(recognizeNEL); } } catch (const XMLException& toCatch) { XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n" << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl; return 1; } // // Create a SAX parser object. Then, according to what we were told on // the command line, set it to validate or not. // SAXParser* parser = new SAXParser; parser->setValidationScheme(valScheme); parser->setDoNamespaces(doNamespaces); parser->setDoSchema(doSchema); parser->setHandleMultipleImports (true); parser->setValidationSchemaFullChecking(schemaFullChecking); // // Create our SAX handler object and install it on the parser, as the // document and error handler. // SAXCountHandlers handler; parser->setDocumentHandler(&handler); parser->setErrorHandler(&handler); // // Get the starting time and kick off the parse of the indicated // file. Catch any exceptions that might propogate out of it. // unsigned long duration; XERCES_STD_QUALIFIER ifstream fin; // the input is a list file if (doList) fin.open(argV[argInd]); if (fin.fail()) { XERCES_STD_QUALIFIER cerr <<"Cannot open the list file: " << argV[argInd] << XERCES_STD_QUALIFIER endl; return 2; } while (true) { char fURI[1000]; //initialize the array to zeros memset(fURI,0,sizeof(fURI)); if (doList) { if (! fin.eof() ) { fin.getline (fURI, sizeof(fURI)); if (!*fURI) continue; else { xmlFile = fURI; XERCES_STD_QUALIFIER cerr << "==Parsing== " << xmlFile << XERCES_STD_QUALIFIER endl; } } else break; } else { if (argInd < argC) { xmlFile = argV[argInd]; argInd++; } else break; } //reset error count first handler.resetErrors(); try { const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis(); parser->parse(xmlFile); const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis(); duration = endMillis - startMillis; } catch (const OutOfMemoryException&) { XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl; errorOccurred = true; continue; } catch (const XMLException& e) { XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << xmlFile << "'\n" << "Exception message is: \n" << StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl; errorOccurred = true; continue; } catch (...) { XERCES_STD_QUALIFIER cerr << "\nUnexpected exception during parsing: '" << xmlFile << "'\n"; errorOccurred = true; continue; } // Print out the stats that we collected and time taken if (!handler.getSawErrors()) { XERCES_STD_QUALIFIER cout << xmlFile << ": " << duration << " ms (" << handler.getElementCount() << " elems, " << handler.getAttrCount() << " attrs, " << handler.getSpaceCount() << " spaces, " << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl; } else errorOccurred = true; } if (doList) fin.close(); // // Delete the parser itself. Must be done prior to calling Terminate, below. // delete parser; // And call the termination method XMLPlatformUtils::Terminate(); if (errorOccurred) return 4; else return 0; }
// --------------------------------------------------------------------------- // Program entry point // --------------------------------------------------------------------------- int main(int argC, char* argV[]) { // Initialize the XML4C system try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n" << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl; return 1; } // Check command line and extract arguments. if (argC < 2) { usage(); XMLPlatformUtils::Terminate(); return 1; } // See if non validating dom parser configuration is requested. int parmInd; for (parmInd = 1; parmInd < argC; parmInd++) { // Break out on first parm not starting with a dash if (argV[parmInd][0] != '-') break; // Watch for special case help request if (!strcmp(argV[parmInd], "-?")) { usage(); XMLPlatformUtils::Terminate(); return 2; } else if (!strncmp(argV[parmInd], "-v=", 3) || !strncmp(argV[parmInd], "-V=", 3)) { const char* const parm = &argV[parmInd][3]; if (!strcmp(parm, "never")) valScheme = SAXParser::Val_Never; else if (!strcmp(parm, "auto")) valScheme = SAXParser::Val_Auto; else if (!strcmp(parm, "always")) valScheme = SAXParser::Val_Always; else { XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl; XMLPlatformUtils::Terminate(); return 2; } } else if (!strcmp(argV[parmInd], "-n") || !strcmp(argV[parmInd], "-N")) { doNamespaces = true; } else if (!strcmp(argV[parmInd], "-s") || !strcmp(argV[parmInd], "-S")) { doSchema = true; } else if (!strcmp(argV[parmInd], "-f") || !strcmp(argV[parmInd], "-F")) { schemaFullChecking = true; } else { XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd] << "', ignoring it\n" << XERCES_STD_QUALIFIER endl; } } // // And now we have to have only one parameter left and it must be // the file name. // if (parmInd + 1 != argC) { usage(); XMLPlatformUtils::Terminate(); return 1; } xmlFile = argV[parmInd]; int errorCount = 0; // // Create a SAX parser object to use and create our SAX event handlers // and plug them in. // SAXParser* parser = new SAXParser; PParseHandlers handler; parser->setDocumentHandler(&handler); parser->setErrorHandler(&handler); parser->setValidationScheme(valScheme); parser->setDoNamespaces(doNamespaces); parser->setDoSchema(doSchema); parser->setValidationSchemaFullChecking(schemaFullChecking); // // Ok, lets do the progressive parse loop. On each time around the // loop, we look and see if the handler has found what its looking // for. When it does, we fall out then. // unsigned long duration; int errorCode = 0; try { // Create a progressive scan token XMLPScanToken token; const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis(); if (!parser->parseFirst(xmlFile, token)) { XERCES_STD_QUALIFIER cerr << "scanFirst() failed\n" << XERCES_STD_QUALIFIER endl; XMLPlatformUtils::Terminate(); return 1; } // // We started ok, so lets call scanNext() until we find what we want // or hit the end. // bool gotMore = true; while (gotMore && !parser->getErrorCount()) gotMore = parser->parseNext(token); const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis(); duration = endMillis - startMillis; errorCount = parser->getErrorCount(); // // Reset the parser-> In this simple progrma, since we just exit // now, its not technically required. But, in programs which // would remain open, you should reset after a progressive parse // in case you broke out before the end of the file. This insures // that all opened files, sockets, etc... are closed. // parser->parseReset(token); } catch (const OutOfMemoryException&) { XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl; errorCode = 5; } catch (const XMLException& toCatch) { XERCES_STD_QUALIFIER cerr << "\nAn error occurred: '" << xmlFile << "'\n" << "Exception message is: \n" << StrX(toCatch.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl; errorCode = 4; } if(errorCode) { XMLPlatformUtils::Terminate(); return errorCode; } if (!errorCount) { XERCES_STD_QUALIFIER cout << xmlFile << ": " << duration << " ms (" << handler.getElementCount() << " elems, " << handler.getAttrCount() << " attrs, " << handler.getSpaceCount() << " spaces, " << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl; } // // Delete the parser itself. Must be done prior to calling Terminate, below. // delete parser; // And call the termination method XMLPlatformUtils::Terminate(); if (errorCount > 0) return 4; else return 0; }
// --------------------------------------------------------------------------- // Program entry point // --------------------------------------------------------------------------- int main(int argc, char* args[]) { // Initialize the XML4C system try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n" << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl; return 1; } // We only have one parameter, which is the file to process // We only have one required parameter, which is the file to process if ((argc != 2) || (*(args[1]) == '-')) { usage(); XMLPlatformUtils::Terminate(); return 1; } const char* xmlFile = args[1]; // // Create a SAX parser object. Then, according to what we were told on // the command line, set it to validate or not. // SAXParser* parser = new SAXParser; // // Create our SAX handler object and install it on the parser, as the // document, entity and error handlers. // RedirectHandlers handler; parser->setDocumentHandler(&handler); parser->setErrorHandler(&handler); parser->setEntityResolver(&handler); // // Get the starting time and kick off the parse of the indicated file. // Catch any exceptions that might propogate out of it. // unsigned long duration; int errorCount = 0; int errorCode = 0; try { const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis(); parser->parse(xmlFile); const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis(); duration = endMillis - startMillis; errorCount = parser->getErrorCount(); } catch (const OutOfMemoryException&) { XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl; errorCode = 5; } catch (const XMLException& e) { XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << xmlFile << "'\n" << "Exception message is: \n" << StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl; errorCode = 4; } if(errorCode) { XMLPlatformUtils::Terminate(); return errorCode; } // Print out the stats that we collected and time taken. if (!errorCount) { XERCES_STD_QUALIFIER cout << xmlFile << ": " << duration << " ms (" << handler.getElementCount() << " elems, " << handler.getAttrCount() << " attrs, " << handler.getSpaceCount() << " spaces, " << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl; } // // Delete the parser itself. Must be done prior to calling Terminate, below. // delete parser; XMLPlatformUtils::Terminate(); if (errorCount > 0) return 4; else return 0; }