Exemplo n.º 1
0
    bool serializeDOM(std::ostream& output, bool)
    {
	XMLCh tempStr[100];
	XMLString::transcode("LS", tempStr, 99);
	DOMImplementationLS *impl = dynamic_cast<DOMImplementationLS *>(DOMImplementationRegistry::getDOMImplementation(tempStr));
	DOMLSSerializer   *theSerializer = impl->createLSSerializer();
	DOMLSOutput       *theOutputDesc = impl->createLSOutput();

	bool rc = false;

	try
	{
	    MemBufFormatTarget *myFormTarget = new MemBufFormatTarget();

	    /* do the serialization through DOMLSSerializer::write(); */

	    theOutputDesc->setByteStream(myFormTarget);
	    theSerializer->write(inputFileDOM, theOutputDesc);

	    const XMLByte * data = myFormTarget->getRawBuffer();
	    XMLSize_t len = myFormTarget->getLen();

	    output.write( reinterpret_cast<const char *>(data), len );

	    delete myFormTarget;
	    rc = true;
	}
	catch (IOException& e ) {
	    cerr << StrX(e.getMessage());
	    cerr << "  Code is " << e.getCode();

	    if ( errno != 0 ) {
		cerr << ": " << strerror( errno );
	    }
	}
	catch (XMLException& e)
	{
	    cerr << "An error occurred during creation of output transcoder. Msg is:"
		 << endl
		 << StrX(e.getMessage());
	    if ( errno != 0 ) {
		cerr << ": " << strerror( errno );
	    }
	    cerr << endl;
	    rc = false;
	}
	delete theSerializer;
	return rc;
    }
Exemplo n.º 2
0
 /// Dump DOM tree using XercesC handles
 void dumpTree(DOMNode* doc, ostream& os) {
   if ( doc )  {
     DOMImplementation  *imp = DOMImplementationRegistry::getDOMImplementation(Strng_t("LS"));
     MemBufFormatTarget *tar = new MemBufFormatTarget();
     DOMLSOutput        *out = imp->createLSOutput();
     DOMLSSerializer    *wrt = imp->createLSSerializer();
     out->setByteStream(tar);
     wrt->getDomConfig()->setParameter(Strng_t("format-pretty-print"), true);
     wrt->write(doc, out);
     os << tar->getRawBuffer() << endl << flush;
     out->release();
     wrt->release();
     return;
   }
   printout(ERROR,"dumpTree","+++ Cannot dump invalid document.");
 }
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;
}
Exemplo n.º 4
0
/*!
	\brief It gets a list of DOM nodes containing a protocol, and formats them into _nbPDMLProto structures.

	This function converts each protocol and all its child fields. This function will call the FormatFieldsItem()
	for this purpose.

	\param PDMLDocument The DOMDocument associated to the current PDML packet.

	\return The number of '&lt;proto&gt;' that have been copied in the returned buffer, 
	nbFAILURE if some error occurred.
	The error message is stored in the m_errbuf internal buffer.
*/
int CPDMLReader::FormatProtoItem(DOMDocument *PDMLDocument)
{
DOMNodeList *ProtoList;
XMLCh TempBuffer[NETPDL_MAX_STRING + 1];
unsigned int ProtoNumber;
unsigned int TotNumProto;

	XMLString::transcode(PDML_PROTO, TempBuffer, NETPDL_MAX_STRING);

	// After parsing the '<packet>' fragment, let's list the <proto> contained into it
	ProtoList= PDMLDocument->getElementsByTagName(TempBuffer);

	// Get the number of protocols contained in this packet
	TotNumProto= ProtoList->getLength();

	// Check if the protocol structures allocated previously are enough. If not, let's allocate new structures
	if (TotNumProto >= m_maxNumProto)
	{
		if (UpdateProtoList(&m_maxNumProto, &m_protoList, m_errbuf, sizeof(m_errbuf)) == nbFAILURE)
			return nbFAILURE;
	}

	// Reset the buffer
	m_asciiBuffer.ClearBuffer(false /* resizing not permitted */);

	for (ProtoNumber= 0; ProtoNumber < TotNumProto; ProtoNumber++)
	{
	DOMElement *ProtoItem;

		// Set everything to zero
		memset(m_protoList[ProtoNumber], 0, sizeof(_nbPDMLProto));

		// Update pointer to the packet summary
		m_protoList[ProtoNumber]->PacketSummary= &m_packetSummary;

		// Updates the protocol chain
		if (ProtoNumber >= 1)
		{
			m_protoList[ProtoNumber - 1]->NextProto= m_protoList[ProtoNumber];
			m_protoList[ProtoNumber]->PreviousProto= m_protoList[ProtoNumber - 1];
		}

		ProtoItem= (DOMElement *) ProtoList->item(ProtoNumber);

		// Copies all the attributes of this proto in the new structure
 		if (AppendItemString(ProtoItem, PDML_FIELD_ATTR_NAME,
			&(m_protoList[ProtoNumber]->Name), &m_asciiBuffer, m_errbuf, sizeof(m_errbuf)) == nbFAILURE)
			return nbFAILURE;
 		if (AppendItemString(ProtoItem, PDML_FIELD_ATTR_LONGNAME,
			&(m_protoList[ProtoNumber]->LongName), &m_asciiBuffer, m_errbuf, sizeof(m_errbuf)) == nbFAILURE)
			return nbFAILURE;
		if (AppendItemLong(ProtoItem, PDML_FIELD_ATTR_SIZE,
			&(m_protoList[ProtoNumber]->Size), m_errbuf, sizeof(m_errbuf)) == nbFAILURE)
			return nbFAILURE;
		if (AppendItemLong(ProtoItem, PDML_FIELD_ATTR_POSITION,
			&(m_protoList[ProtoNumber]->Position), m_errbuf, sizeof(m_errbuf)) == nbFAILURE)
			return nbFAILURE;

#ifdef DOM_DEBUG
		// FULVIO This code is still present, because it should be needed in order to solve
		// a bug discovered on Dec 4, 2006
		// See my email in the mailbox
		// get a serializer, an instance of DOMWriter
		XMLCh tempStr[100];
		XMLString::transcode("LS", tempStr, 99);

		DOMImplementation *impl          = DOMImplementationRegistry::getDOMImplementation(tempStr);
		DOMWriter         *theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter();

		MemBufFormatTarget myFormTarget;

		theSerializer->writeNode(&myFormTarget, *ProtoItem);

		const XMLByte *Result= myFormTarget.getRawBuffer();

		delete theSerializer;
#endif

		if (ProtoItem->hasChildNodes() )
		{
			if (FormatFieldNodes(ProtoItem->getFirstChild(), m_protoList[ProtoNumber], NULL) == nbFAILURE)
				return nbFAILURE;
		}
	}

	// Update the pointer to the first protocol
	if (TotNumProto > 0)
		m_packetSummary.FirstProto= m_protoList[0];
	else
		m_packetSummary.FirstProto= NULL;

	return (int) ProtoNumber;
}
Exemplo n.º 5
0
int main(int argC, char*argV[])
{
        // Initialize the XML4C2 system.
        try
        {
                XMLPlatformUtils::Initialize();/*init*/
        }
        catch(const XMLException& toCatch)
        {
                char *pMsg = XMLString::transcode(toCatch.getMessage());
                XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.\n"
                        << "  Exception message:"
                        << pMsg;
                XMLString::release(&pMsg);
                return 1;
        }

        DOMImplementation* impl =  DOMImplementationRegistry::getDOMImplementation(X("LS"));/**Implementation*/
        DOMLSSerializer*   theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();/** createLSSerializer*/
        DOMLSOutput       *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput();/**createLSOutput*/
        MemBufFormatTarget* target = new MemBufFormatTarget();
        theOutputDesc->setByteStream(target);

        if (impl != NULL)
        {
                try
                {
                        DOMDocument* doc = impl->createDocument(
                                        0,                    // root element namespace URI. /**可用资源标识*/
                                        X("corporation"),     // root element name /**a large companny*/
                                        0);                   // document type object (DTD). /**mem mannager mode object*/
                        doc->setXmlStandalone(true);

                        DOMElement*  rootElem = doc->getDocumentElement();
                        rootElem->setAttribute(X("xmlns"), X("com:cmcc:corporation"));
                        rootElem->setAttribute(X("xmlns:xsi"), X("http://www.w3.org/2001/XMLSchema-instance"));
                        rootElem->setAttribute(X("xsi:schemaLocation"), X("com:cmcc:corporation corporation.xsd"));

                        //add node
                        DOMElement*  prodElem = doc->createElement(X("eid"));
                        rootElem->appendChild(prodElem);

                        DOMText* prodDataVal = doc->createTextNode(X("100000"));
                        prodElem->appendChild(prodDataVal);

                        XercesDOMParser* parser = new XercesDOMParser();

                        //add basic child
                        MemBufInputSource* basic_mem = new MemBufInputSource(
                                        (const XMLByte* )basic_string.c_str(),
                                        strlen(basic_string.c_str()),
                                        "basic",
                                        false);
                        parser->parse( *basic_mem );
                        DOMDocument* xmlDoc = parser->getDocument();
                        DOMElement* basic_root = xmlDoc->getDocumentElement();
                        DOMNode* newnode = doc->importNode((DOMNode* )basic_root, true);
                        rootElem->appendChild(newnode); 
                        delete basic_mem;

                        //add the rules child
                        MemBufInputSource* rules_mem = new MemBufInputSource(
                                        (const XMLByte* )rules_string.c_str(),
                                        rules_string.length(),
                                        "rules",
                                        false);
                        parser->parse( *rules_mem );
                        xmlDoc = parser->getDocument();
                        DOMElement* rules_root = xmlDoc->getDocumentElement();
                        newnode = doc->importNode((DOMNode* )rules_root, true);
                        rootElem->appendChild(newnode);
                        delete rules_mem;

                        //add the accounts child
                        MemBufInputSource* accounts_mem = new MemBufInputSource(
                                        (const XMLByte* )accounts_string.c_str(),
                                        accounts_string.length(),
                                        "accounts",
                                        false);
                        parser->parse( *accounts_mem );
                        xmlDoc = parser->getDocument();
                        DOMElement* accounts_root = xmlDoc->getDocumentElement();
                        newnode = doc->importNode((DOMNode* )accounts_root, true);
                        rootElem->appendChild(newnode);
                        delete accounts_mem;

                        theSerializer->write(doc, theOutputDesc);
                        std::cout<< target->getRawBuffer()<< std::endl;

                        delete target;
                        delete parser;
                        theOutputDesc->release();
                        theSerializer->release();
                        doc->release();
                }
                catch(const OutOfMemoryException&)
                {
                        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
                }
                catch (const DOMException& e)
                {
                        XERCES_STD_QUALIFIER cerr << "DOMException code is:  " << e.code << XERCES_STD_QUALIFIER endl;
                }
                catch (...)
                {
                        XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" << XERCES_STD_QUALIFIER endl;
                }
        }
        else
        {
                XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported" << XERCES_STD_QUALIFIER endl;
        }

        XMLPlatformUtils::Terminate();
        return 0;
}