Exemplo n.º 1
0
// MÉTODO ContentRDF :: createAction
int
ContentRDF::
createAction(string act, string actor, vector<string> arguments, string &content)
{

  vector<string>::iterator it;
  int errorCode = 0;

  try
    {
      // Inicialización del sistema.
      XMLPlatformUtils::Initialize();
    }
  
  catch(const XMLException& toCatch)
    {
      char *pMsg = XMLString::transcode(toCatch.getMessage());
      XERCES_STD_QUALIFIER cerr << "Error al inicializar xerces-c.\n"
				<< "  Mensaje de excepción:"
				<< pMsg;
      XMLString::release(&pMsg);
      return 1;
    }// Fin catch

  DOMImplementation* impl =  DOMImplementationRegistry::getDOMImplementation(X("Core"));

  if (impl != NULL) {
    
    try
      {

	// *** rdf:RDF es la raíz del mensaje.
	DOMDocument *action = impl->createDocument(0, X("RDF"), 0);
	action->setEncoding(X("UTF-8"));

	// *** Comentario inicial.
	//action->createComment(X("<?xml version=\"1.0\" standalone=\"no\">"));
	//action->createComment(X("<!DOCTYPE fipa-message SYSTEM \"aclRep.dtd\">"));
	
	// *** Atributos de rdf:RDF.
	DOMElement* rootElem = action->getDocumentElement();
	rootElem->setAttribute(X("xmlnsrdf"), X("http://www.w3.org/1999/02/22-rdf-syntax.ns#"));
	rootElem->setAttribute(X("xmlnsfipa"), X("http://www.fipa.org/schemas/fipa-rdf0#"));
	
	DOMElement* actionElem = action->createElement(X("Action"));
	rootElem->appendChild(actionElem);
	actionElem->setAttribute(X("ID"), X((actor + "Action").c_str()));
	
	// *** Actor.
	DOMElement* actorElem = action->createElement(X("Actor"));
	actionElem->appendChild(actorElem);
	DOMText* actorText = action->createTextNode(X(actor.c_str()));
	actorElem->appendChild(actorText);

	// *** Act.
	DOMElement* actElem = action->createElement(X("Act"));
	actionElem->appendChild(actElem);
	DOMText* actText = action->createTextNode(X(act.c_str()));
	actElem->appendChild(actText);

	// *** Arguments.
	DOMElement* argumentsElem = action->createElement(X("Argument"));
	actionElem->appendChild(argumentsElem);
	DOMElement* bagElem = action->createElement(X("Bag"));
	argumentsElem->appendChild(bagElem);

	// Lista de argumentos.
	DOMElement* argumentElem;
	DOMText* argumentText;

	for (it = arguments.begin(); it != arguments.end(); it++) {
	  argumentElem = action->createElement(X("Li"));
	  bagElem->appendChild(argumentElem);
	  argumentText = action->createTextNode(X((*it).c_str()));
	  argumentElem->appendChild(argumentText);
	}// Fin for

	// Serialización a través de DOMWriter
	XMLCh tempStr[100];
	XMLString::transcode("LS", tempStr, 99);
	DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
	DOMWriter *theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter();

	// Conversión a string
	XMLCh *buf = theSerializer->writeToString(*action);
	char *salida = XMLString::transcode(buf);
	string s1(salida);
	content = s1;
	
	XMLString::release(&buf);
	XMLString::release(&salida);

	delete theSerializer;

	XMLPlatformUtils::Terminate();

      }// Fin try
    
    catch (const OutOfMemoryException&)
      {
	XERCES_STD_QUALIFIER cerr << "Error debido a falta de memoria." << XERCES_STD_QUALIFIER endl;
	errorCode = 5;
      }// Fin catch
    catch (const DOMException& e)
      {
	XERCES_STD_QUALIFIER cerr << "DOMException en:  " << e.code << XERCES_STD_QUALIFIER endl;
	errorCode = 2;
      }// Fin catch
    catch (...)
      {
	XERCES_STD_QUALIFIER cerr << "Se produjo un error al crear el mensaje ACL." << XERCES_STD_QUALIFIER endl;
	errorCode = 3;
      }// Fin catch

  }// Fin if

  else {
    XERCES_STD_QUALIFIER cerr << "La implementación requerida no está disponible." << XERCES_STD_QUALIFIER endl;
    errorCode = 4;
  }// Fin else
    
  XMLPlatformUtils::Terminate();
  return errorCode;  

}
Exemplo n.º 2
0
/*=========================================================================+
	Build_Return_XML:
		Build the XML block that eTrust Admin expects the program exit
		to return.

		<eTExitReturn>
			<eTExitReturnCategory></eTExitReturnCategory>
			<eTExitReturnNative></eTExitReturnNative>
			<eTExitContinue></eTExitContinue>
			<eTExitLogMsg></eTExitLogMsg>
			<eTExitCustom></eTExitCustom>
		</eTExitReturn>

	    The return XML buffer provided by eTrust Admin is over 4000 bytes
		long.  There will be no problem fitting the entire output XML
		document into this buffer unless sLogMessage or the custom_msg built
		from the return values array is very long.  Thus, this code attempts
		first to build an XML buffer with all of the provided information
		and if it is too long does the following:
			case 1:  custom_msg is provided
				--> generate a failure exit return block with no custom_msg

			case 2:  sLogMessage is provided, but no custom_msg
				--> replace the very long sLogMessage with a short one.

+=========================================================================*/
void
ExitXMLBlock::Build_Return_XML(
		STATUS_T	tStatus,
		bool		bContinueEtaExecution,
		string		sLogMessage,
		string &	sReturnXML,						// OUT
		int			iMaxReturnLength,
		bool		bObscureValue  // = false
	)
{
	int iRc;
	string	suValue;
	UTF8	pszuValue[32];
	XMLCh*	pxcValue;

	pxcValue = UTF8toUTF16(UTFEXIT_EXITRETURN);
	DOMDocument* pDocument = ExitXMLBlock::g_pImplementation->createDocument(0, pxcValue, 0);
	delete pxcValue;
	DOMElement* pRootElement = pDocument->getDocumentElement();

	// ...eTrust Admin Category.
	suValue = Convert_Status_To_Category_String(tStatus);
	iRc = Add_Element(pDocument, pRootElement, UTFEXIT_EXITRETURNCATEGORY, suValue);

	// ...Program exit return code.
	sprintf(pszuValue, "%d", tStatus);
	iRc = Add_Element(pDocument, pRootElement, UTFEXIT_EXITRETURNNATIVE, pszuValue);

	// ...Should eTrust Admin continue execution?
	suValue = (bContinueEtaExecution ? UTFEXIT_TRUE : UTFEXIT_FALSE);
	iRc = Add_Element(pDocument, pRootElement, UTFEXIT_EXITCONTINUE, suValue);

	// ...Log message.
	if (!sLogMessage.empty()) {
		iRc = Add_Element(pDocument, pRootElement, UTFEXIT_EXITLOGMSG, sLogMessage);
	}

	// ...Custom message (used for return values today; and perhaps for
	// other exit-type specific purposes in the future).
	if (m_vsReturnValues.size() > 0) {
		pxcValue = UTF8toUTF16(UTFEXIT_EXITCUSTOM);
        DOMElement* pElement = pDocument->createElement(pxcValue);
		delete pxcValue;
        pRootElement->appendChild(pElement);

		// Add each return value as <eTFuncReturn>...</eTFuncReturn>
		for (unsigned int iIndex = 0; iIndex <  m_vsReturnValues.size(); iIndex++) {
			if (bObscureValue) {	// add the obscured attribute for passwords
				Add_Element(
					pDocument,
					pElement,
					UTFEXIT_CF_FUNCRETURN,
					m_vsReturnValues[iIndex],
					"obscured",
					"yes");
			} else {
				Add_Element(
					pDocument,
					pElement,
					UTFEXIT_CF_FUNCRETURN,
					m_vsReturnValues[iIndex]);
			}
		}
	}

	// Generate the return XML string
	DOMBuilder* pBuilder;	// Parser
	DOMWriter*	pWriter;	// Serializer

	pWriter = ExitXMLBlock::g_pImplementation->createDOMWriter();
	pBuilder = ExitXMLBlock::g_pImplementation->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
	pWriter->setFeature(XMLUni::fgDOMXMLDeclaration, false);
	pBuilder->resetDocumentPool();

	const XMLCh* pxcReturnXML = pWriter->writeToString(*pRootElement);
	sReturnXML = UTF16toUTF8(pxcReturnXML);

	if (pBuilder) {
		delete pBuilder;
	}
	if (pWriter) {
		delete pWriter;
	}
	
	if (pDocument) {
		delete pDocument;
		pDocument = NULL;
	}

	// Check to make sure it isn't too long for the return buffer
	int iLength = sReturnXML.length();
	if (iLength >= iMaxReturnLength) {
		sReturnXML = "";
		sLogMessage = "ERROR: Return XML buffer too long";

		// If too long, and a custom message was provided, fail the
		// call (and consequently do not report an output value).
		if (m_vsReturnValues.size() > 0) {
			m_vsReturnValues.clear();
			tStatus = E_FAILURE;
			bContinueEtaExecution = false;
		}
	}
}