void serializer::serialize(EObject_ptr obj) { m_root_obj = obj; m_impl = DOMImplementationRegistry::getDOMImplementation(X("Core")); if (m_impl) { EClass_ptr cl = obj->eClass(); EPackage_ptr pkg = cl->getEPackage(); ::ecorecpp::mapping::type_traits::string_t const& ns_uri = pkg->getNsURI(); m_doc = m_impl->createDocument( (ns_uri.empty()) ? X("NULL") : W(ns_uri), // root element namespace URI. W(get_type(obj)), // root element name 0); // document type object (DTD) m_root = m_doc->getDocumentElement(); // common attributes // xmlns:xmi="http://www.omg.org/XMI" m_root->setAttribute(X("xmlns:xmi"), X("http://www.omg.org/XMI")); // xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" m_root->setAttribute(X("xmlns:xsi"), X("http://www.w3.org/2001/XMLSchema-instance")); // xmi:version="2.0" m_root->setAttribute(X("xmi:version"), X("2.0")); serialize_node(m_root, obj); // write // TODO: outta here DOMLSSerializer *theSerializer = ((DOMImplementationLS*) m_impl)->createLSSerializer(); DOMLSOutput *theOutputDesc = ((DOMImplementationLS*) m_impl)->createLSOutput(); DOMConfiguration* serializerConfig = theSerializer->getDomConfig(); // TODO: set as option if (serializerConfig->canSetParameter( XMLUni::fgDOMWRTFormatPrettyPrint, true)) serializerConfig->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true); XMLFormatTarget *myFormTarget; myFormTarget = new LocalFileFormatTarget(X(m_file)); theOutputDesc->setByteStream(myFormTarget); theSerializer->write(m_doc, theOutputDesc); theOutputDesc->release(); theSerializer->release(); delete myFormTarget; } else throw "Error"; }
DOMLSSerializer * createWriter() { DOMLSSerializer *writer = getImplementation()->createLSSerializer(); DOMConfiguration *config = writer->getDomConfig(); if(config->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true)) config->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true); return writer; }
int XmlParser::commit(const char* xmlFile) { try { // Obtain DOM implementation supporting Load/Save DOMImplementationLS* pImplementation = dynamic_cast<DOMImplementationLS *>(DOMImplementationRegistry::getDOMImplementation(DualString("LS").asXMLString())); if (pImplementation == NULL){ throw( std::runtime_error( "Unable to obtain suitable DOMImplementation!" ) ) ; } DOMLSSerializer *pSerializer = pImplementation->createLSSerializer(); DOMLSOutput *pOutput = pImplementation->createLSOutput(); #if 1 // Change output format to be pretty (but it isn't) DOMConfiguration *pConfiguration = pSerializer->getDomConfig(); if (pConfiguration->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true)) pConfiguration->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true); #if 0 // Overrides above but seems to have little effect! if (pConfiguration->canSetParameter(XMLUni::fgDOMWRTCanonicalForm, true)) pConfiguration->setParameter(XMLUni::fgDOMWRTCanonicalForm, true); #endif #if 1 // if (pConfiguration->canSetParameter(XMLUni::fgDOMWRTEntities, true)) pConfiguration->setParameter(XMLUni::fgDOMWRTEntities, true); #endif #endif LocalFileFormatTarget *pTarget = new LocalFileFormatTarget(DualString( xmlFile ).asXMLString()); pOutput->setByteStream(pTarget); // mergeDocument->normalizeDocument(); // Needed? pSerializer->write(mergeDocument, pOutput); delete pTarget; pOutput->release(); pSerializer->release(); } catch( const xercesc::XMLException& e ){ return -1; } return 0; }
string XmlUtil::convertDomToString() { if(!doc) throw "DOM is empty"; // FIXME add an exception type for this DOMImplementationLS* implLS = dynamic_cast<DOMImplementationLS*>(impl); DOMLSSerializer* theSerializer = implLS->createLSSerializer(); DOMConfiguration* serializerConfig = theSerializer->getDomConfig(); if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true)) serializerConfig->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true); string stringTemp = XMLString::transcode (theSerializer->writeToString(doc)); theSerializer->release(); /*string stringDump; for (string::iterator it = stringTemp.begin() ; it < stringTemp.end(); ++it) { if (!isspace (*it)) stringDump += *it; } return stringDump;*/ return stringTemp; }
bool XMLIO::Write (DOMImplementation* impl, DOMNode* node, string filename) { XMLFormatTarget* mft; if (filename.empty()) mft = new StdOutFormatTarget(); else mft = new LocalFileFormatTarget (StrX(filename).XMLchar()); // Xerces 2 #ifdef XSEC_XERCES_HAS_SETIDATTRIBUTE DOMWriter* serializer = ((DOMImplementationLS*)impl)->createDOMWriter(); if (serializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true)) serializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true); serializer->writeNode(mft, *node); serializer->release(); #endif #ifdef XSEC_XERCES_HAS_BOOLSETIDATTRIBUTE DOMLSSerializer* serializer = ((DOMImplementationLS*) impl)->createLSSerializer(); DOMLSOutput* output = ((DOMImplementationLS*)impl)->createLSOutput(); DOMConfiguration* configuration = serializer->getDomConfig(); if (configuration->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true)) configuration->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true); output->setByteStream (mft); serializer->write(node, output); output->release(); serializer->release(); #endif return true; }
// --------------------------------------------------------------------------- // // main // // --------------------------------------------------------------------------- int main(int argC, char* argV[]) { char *testFileName; char *outputFileName; for (int argInd = 1; argInd < argC; argInd++) { if (!strcmp(argV[argInd], "-?") || !strcmp(argV[argInd], "-h")) { /* print help and exit */ usage(); return 2; } } if (argC < 3){ usage(); return 2; } testFileName = argV[argC-2]; outputFileName = argV[argC-1]; // 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; } //============================================================================ // Instantiate the DOM parser to use for the source documents //============================================================================ static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull }; DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS); DOMLSParser *parser = ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0); DOMConfiguration *config = parser->getDomConfig(); config->setParameter(XMLUni::fgDOMNamespaces, true); config->setParameter(XMLUni::fgXercesSchema, true); config->setParameter(XMLUni::fgXercesSchemaFullChecking, true); if(config->canSetParameter(XMLUni::fgXercesDoXInclude, true)){ config->setParameter(XMLUni::fgXercesDoXInclude, true); } // enable datatype normalization - default is off //config->setParameter(XMLUni::fgDOMDatatypeNormalization, true); // And create our error handler and install it XIncludeErrorHandler errorHandler; config->setParameter(XMLUni::fgDOMErrorHandler, &errorHandler); XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0; try { // load up the test source document XERCES_STD_QUALIFIER cerr << "Parse " << testFileName << " in progress ..."; parser->resetDocumentPool(); doc = parser->parseURI(testFileName); XERCES_STD_QUALIFIER cerr << " finished." << XERCES_STD_QUALIFIER endl; } catch (const XMLException& toCatch) { XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << testFileName << "'\n" << "Exception message is: \n" << StrX(toCatch.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl; } catch (const DOMException& toCatch) { const unsigned int maxChars = 2047; XMLCh errText[maxChars + 1]; XERCES_STD_QUALIFIER cerr << "\nDOM Error during parsing: '" << testFileName << "'\n" << "DOMException code is: " << toCatch.code << XERCES_STD_QUALIFIER endl; if (DOMImplementation::loadDOMExceptionMsg(toCatch.code, errText, maxChars)) XERCES_STD_QUALIFIER cerr << "Message is: " << StrX(errText) << XERCES_STD_QUALIFIER endl; } catch (...) { XERCES_STD_QUALIFIER cerr << "\nUnexpected exception during parsing: '" << testFileName << "'\n"; } if (!errorHandler.getSawErrors() && doc) { DOMLSSerializer *writer = ((DOMImplementationLS*)impl)->createLSSerializer(); DOMLSOutput *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput(); try { // write out the results XERCES_STD_QUALIFIER cerr << "Writing result to: " << outputFileName << XERCES_STD_QUALIFIER endl; XMLFormatTarget *myFormTarget = new LocalFileFormatTarget(outputFileName); theOutputDesc->setByteStream(myFormTarget); writer->write(doc, theOutputDesc); delete myFormTarget; } catch (const XMLException& toCatch) { XERCES_STD_QUALIFIER cerr << "\nXMLException during writing: '" << testFileName << "'\n" << "Exception message is: \n" << StrX(toCatch.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl; } catch (const DOMException& toCatch) { const unsigned int maxChars = 2047; XMLCh errText[maxChars + 1]; XERCES_STD_QUALIFIER cerr << "\nDOM Error during writing: '" << testFileName << "'\n" << "DOMException code is: " << toCatch.code << XERCES_STD_QUALIFIER endl; if (DOMImplementation::loadDOMExceptionMsg(toCatch.code, errText, maxChars)) XERCES_STD_QUALIFIER cerr << "Message is: " << StrX(errText) << XERCES_STD_QUALIFIER endl; } catch (...) { XERCES_STD_QUALIFIER cerr << "\nUnexpected exception during writing: '" << testFileName << "'\n"; } writer->release(); theOutputDesc->release(); } // // Delete the parser itself. Must be done prior to calling Terminate, below. // parser->release(); // And call the termination method XMLPlatformUtils::Terminate(); return 0; }
void CTibiaItem::saveItemLists() { if (!xmlInitialised) { XMLPlatformUtils::Initialize(); xmlInitialised = 1; } XercesDOMParser *parser = new XercesDOMParser(); try { //int itemNr; char pathBuf[2048]; sprintf(pathBuf, "%s\\data\\tibiaauto-items.xml", CInstallPath::getInstallPath().c_str()); DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("Core")); xercesc::DOMDocument *doc = impl->createDocument(0, XMLString::transcode("item-definitions"), 0); doc->createComment((const unsigned short *)"<!-- Tibia Items for Tibia -->"); DOMElement *root = doc->getDocumentElement(); //ITEMS DOMNode *itemsNode = doc->createElement(XMLString::transcode("items")); root->appendChild(itemsNode); //recursively save data structure to XML saveItemsBranch(itemsNode, itemTree, doc); //FOOD DOMNode *foodNode = doc->createElement(XMLString::transcode("foods")); root->appendChild(foodNode); { int size = foodList.GetCount(); for (int i = 0; i < size; i++) { char buf[512]; DOMElement* itemElem = doc->createElement(XMLString::transcode("item")); foodNode->appendChild(itemElem); char* name = foodList.GetTextAtIndex(i); int id = foodList.GetValueAtIndex(i); int time = foodList.GetExtraInfoAtIndex(i); sprintf(buf, "0x%x", id); itemElem->setAttribute(XMLString::transcode("id"), XMLString::transcode(buf)); itemElem->setAttribute(XMLString::transcode("name"), XMLString::transcode(name)); sprintf(buf, "%d", time); itemElem->setAttribute(XMLString::transcode("time"), XMLString::transcode(buf)); } } //CONSTS /* never any need to save constants anymore DOMNode *constsNode = doc->createElement(XMLString::transcode("consts")); root->appendChild(constsNode); int size = constCodeList.GetCount(); for (int i=0;i<size;i++){ char buf[512]; DOMElement* itemElem = doc->createElement(XMLString::transcode("const")); constsNode->appendChild(itemElem); char* code=constCodeList.GetTextAtIndex(i); int value=constCodeList.GetValueAtIndex(i); sprintf(buf, "0x%x", value); itemElem->setAttribute(XMLString::transcode("value"), XMLString::transcode(buf)); itemElem->setAttribute(XMLString::transcode("code"), XMLString::transcode(code)); } */ XMLCh tempStr[100]; XMLString::transcode("LS", tempStr, 99); impl = DOMImplementationRegistry::getDOMImplementation(tempStr); DOMLSSerializer* theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer(); DOMConfiguration* dc = theSerializer->getDomConfig(); if (dc->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true)) dc->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true); XMLFormatTarget *outFile = new LocalFileFormatTarget(pathBuf); DOMLSOutput *lsOut = ((DOMImplementationLS*)impl)->createLSOutput(); lsOut->setByteStream(outFile); theSerializer->write(doc, lsOut); theSerializer->release(); lsOut->release(); delete outFile; } catch (...) { AfxMessageBox("Unable to save item definitions!"); } delete parser; }
void CTibiaItem::saveItemLists() { if (!xmlInitialised) { XMLPlatformUtils::Initialize(); xmlInitialised = 1; } char installPath[1024] = { '\0' }; unsigned long installPathLen = 1023; HKEY hkey = NULL; if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Tibia Auto\\", 0, KEY_READ, &hkey)) { RegQueryValueEx(hkey, TEXT("Install_Dir"), NULL, NULL, (unsigned char *)installPath, &installPathLen); RegCloseKey(hkey); } if (!strlen(installPath)) { AfxMessageBox("ERROR! Unable to read TA install directory! Please reinstall!"); PostQuitMessage(-1); return; } XercesDOMParser *parser = new XercesDOMParser(); try { //int itemNr; char pathBuf[2048]; sprintf(pathBuf, "%s\\data\\tibiaauto-items.xml", installPath); DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("Core")); xercesc::DOMDocument *doc = impl->createDocument(0, XMLString::transcode("item-definitions"), 0); doc->createComment((const unsigned short *)"<!-- Tibia Items for Tibia -->"); DOMElement *root = doc->getDocumentElement(); //ITEMS DOMNode *itemsNode = doc->createElement(XMLString::transcode("items")); root->appendChild(itemsNode); //recursively save data structure to XML saveItemsBranch(itemsNode, itemTree, doc); //FOOD DOMNode *foodNode = doc->createElement(XMLString::transcode("foods")); root->appendChild(foodNode); { int size = foodList.GetCount(); for (int i = 0; i < size; i++) { char buf[512]; DOMElement* itemElem = doc->createElement(XMLString::transcode("item")); foodNode->appendChild(itemElem); char* name = foodList.GetTextAtIndex(i); int id = foodList.GetValueAtIndex(i); int time = foodList.GetExtraInfoAtIndex(i); sprintf(buf, "0x%x", id); itemElem->setAttribute(XMLString::transcode("id"), XMLString::transcode(buf)); itemElem->setAttribute(XMLString::transcode("name"), XMLString::transcode(name)); sprintf(buf, "%d", time); itemElem->setAttribute(XMLString::transcode("time"), XMLString::transcode(buf)); } } //CONSTS /* never any need to save constants anymore DOMNode *constsNode = doc->createElement(XMLString::transcode("consts")); root->appendChild(constsNode); int size = constCodeList.GetCount(); for (int i=0;i<size;i++){ char buf[512]; DOMElement* itemElem = doc->createElement(XMLString::transcode("const")); constsNode->appendChild(itemElem); char* code=constCodeList.GetTextAtIndex(i); int value=constCodeList.GetValueAtIndex(i); sprintf(buf, "0x%x", value); itemElem->setAttribute(XMLString::transcode("value"), XMLString::transcode(buf)); itemElem->setAttribute(XMLString::transcode("code"), XMLString::transcode(code)); } */ XMLCh tempStr[100]; XMLString::transcode("LS", tempStr, 99); impl = DOMImplementationRegistry::getDOMImplementation(tempStr); DOMLSSerializer* theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer(); DOMConfiguration* dc = theSerializer->getDomConfig(); if (dc->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true)) dc->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true); XMLFormatTarget *outFile = new LocalFileFormatTarget(pathBuf); DOMLSOutput *lsOut = ((DOMImplementationLS*)impl)->createLSOutput(); lsOut->setByteStream(outFile); theSerializer->write(doc, lsOut); theSerializer->release(); lsOut->release(); delete outFile; } catch (...) { AfxMessageBox("Unable to save item definitions!"); } delete parser; }
bool ServerConfigXMLReader::readServerConfig(const std::string & path) { this->serverConfig = ServerConfig(); try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { char * message = XMLString::transcode(toCatch.getMessage()); cout << "Error during initialization! :\n" << message << "\n"; XMLString::release(&message); return false; } XercesDOMParser * parser = new XercesDOMParser(); //parser->setValidationScheme(XercesDOMParser::Val_Auto); parser->setDoNamespaces(true); parser->setDoXInclude(true); //parser->setValidationScheme(XercesDOMParser::Val_Always); // parser->setDoSchema(true); // parser->setValidationSchemaFullChecking(true); XMLParseErrorReporter * xmlParseErrorReporter = new XMLParseErrorReporter();; parser->setErrorHandler(xmlParseErrorReporter); try { parser->parse(path.c_str()); } catch (const XMLException & toCatch) { char * message = XMLString::transcode(toCatch.getMessage()); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return false; } catch (const DOMException& toCatch) { char * message = XMLString::transcode(toCatch.msg); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return false; } catch (...) { cout << "Unexpected Exception \n" ; return false; } DOMDocument * domDocument = parser->getDocument(); removeBaseAttr(domDocument); // get a serializer, an instance of DOMLSSerializer XMLCh tempStr[3] = {chLatin_L, chLatin_S, chNull}; DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr); DOMLSSerializer *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer(); DOMLSOutput *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput(); // set user specified output encoding theOutputDesc->setEncoding(0); XMLDOMErrorReporter * xmlDOMErrorReporter = new XMLDOMErrorReporter(); DOMConfiguration * serializerConfig = theSerializer->getDomConfig(); serializerConfig->setParameter(XMLUni::fgDOMErrorHandler, xmlDOMErrorReporter); // set feature if the serializer supports the feature/mode if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTSplitCdataSections, true)) serializerConfig->setParameter(XMLUni::fgDOMWRTSplitCdataSections, true); if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true)) serializerConfig->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true); if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, false)) serializerConfig->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, false); if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTBOM, false)) serializerConfig->setParameter(XMLUni::fgDOMWRTBOM, false); /* XMLFormatTarget * myFormTarget = new StdOutFormatTarget(); theOutputDesc->setByteStream(myFormTarget); theSerializer->write(domDocument, theOutputDesc); */ MemBufFormatTarget * myFormTarget = new MemBufFormatTarget(); theOutputDesc->setByteStream(myFormTarget); theSerializer->write(domDocument, theOutputDesc); /* XMLFormatTarget * myFormTarget2 = new StdOutFormatTarget(); theOutputDesc->setByteStream(myFormTarget2); theSerializer->write(domDocument, theOutputDesc); */ MemBufInputSource * memInput = new MemBufInputSource(myFormTarget->getRawBuffer(), myFormTarget->getLen(), path.c_str()); XercesDOMParser * parser2 = new XercesDOMParser(); parser2->setDoNamespaces(true); parser2->setDoXInclude(true); // parser2->setValidationScheme(XercesDOMParser::Val_Always); // parser2->setDoSchema(true); // parser2->setValidationSchemaFullChecking(true); parser2->setErrorHandler(xmlParseErrorReporter); try { parser2->parse(*memInput); } catch (const XMLException & toCatch) { char * message = XMLString::transcode(toCatch.getMessage()); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return false; } catch (const DOMException& toCatch) { char * message = XMLString::transcode(toCatch.msg); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return false; } catch (...) { cout << "Unexpected Exception \n" ; return false; } DOMDocument * domDocument2 = parser2->getDocument(); XMLNodeFilter * xmlNodeFilter = new XMLNodeFilter(); DOMTreeWalker * domTreeWalker = domDocument->createTreeWalker(domDocument2, DOMNodeFilter::SHOW_ALL, xmlNodeFilter, true); this->serverConfig = extractServerConfig(domDocument2, domTreeWalker); domTreeWalker->release(); delete xmlNodeFilter; delete xmlDOMErrorReporter; delete xmlParseErrorReporter; delete parser; XMLPlatformUtils::Terminate(); return true; }