Click here to Skip to main content
15,894,017 members
Articles / Programming Languages / XML

XMLFoundation

Rate me:
Please Sign up or sign in to vote.
4.82/5 (12 votes)
2 Jul 20029 min read 75.2K   1.4K   34  
Obtaining data marked up in XML creates the need for Application Layer tools to easily and efficiently work with XML data.
// --------------------------------------------------------------------------
//					www.UnitedBusinessTechnologies.com
//			  Copyright (c) 1998 - 2002  All Rights Reserved.
//
// Source in this file is released to the public under the following license:
// --------------------------------------------------------------------------
// This toolkit may be used free of charge for any purpose including corporate
// and academic use.  For profit, and Non-Profit uses are permitted.
//
// This source code and any work derived from this source code must retain 
// this copyright at the top of each source file.
// 
// UBT welcomes any suggestions, improvements or new platform ports.
// email to: XMLFoundation@UnitedBusinessTechnologies.com
// --------------------------------------------------------------------------

#include "FileDataSource.h"
#include "GException.h"


CFileDataSource::~CFileDataSource()
{
}

CFileDataSource::CFileDataSource(const char **ppQueryAttributes)
	: CXMLDataSource(ppQueryAttributes,0,0)
{
	m_pDirectMemoryBuffer = 0;
	m_bProcessed = 0;
	m_bIsFile = 0;
}

void CFileDataSource::SetConnectionInfo(const char *szAddress, short nPort)
{
	// Port -2 means that the address is a path to a file
	if (nPort == -2 && szAddress && szAddress[0])
	{
		m_strFilePath = szAddress;
		m_bIsFile = 1;
	}
	// Port -1 means that the address is a path to a directory
	else if (nPort == -1 && szAddress && szAddress[0])
	{
		m_strFilePath = szAddress;
		if ( !( m_strFilePath.Right(1) == "/" || m_strFilePath.Right(1) == "\\") )
		{
	#ifdef _WIN32
			m_strFilePath += "\\";
	#else
			m_strFilePath += "/";
	#endif
		}
	}
	else
	{
		CXMLDataSource::SetConnectionInfo(szAddress, nPort);
	}
}

void CFileDataSource::SetConnectionInfo(const char *pzXML)
{
	m_pDirectMemoryBuffer = pzXML;
}



int CFileDataSource::release(const char *resultFromSend, void *PooledConnection, void *ppUserData)
{
	if (m_bProcessed)
	{
		delete (GString *)ppUserData;
		return 1;
	}
	return 0;
}

const char *CFileDataSource::send(const char *pzProcedureName, 
								  const char *xml, 
								  int nXMLLen, 
								  void *PooledConnection,
								  void **ppUserData,
								  const char *pzNamespace)
{
	// set to 0 assuming we will not service this request
	m_bProcessed = 0;
	const char *pReturnXML = 0;

	const char *pDir = 0;

	// If txml.txt has redirected all client requests to a disk directory
	const char *pDebugResponseFile = GetProfile().GetPath("TXML", "DebugXMLServerEmulation",false);
	if (pDebugResponseFile && pDebugResponseFile[0])
	{
		pDir = pDebugResponseFile;
	}

	// If we're configured to look for responses in a disk directory
	if (pDir || m_strFilePath.Length())
	{
		// calling SetConnectionInfo() overrides txml.txt default
		if (m_strFilePath.Length())
		{
			pDir = m_strFilePath;
		}
		
		// create the file name to look for in strInputFile
		GString strInputFile = pDir;
		if (!m_bIsFile)
		{
			strInputFile += pzProcedureName;
			strInputFile += ".xml";
		}

		// Allocate storage and keep it in the procedure call, not the DataSource.
		GString *strFileBuffer = new GString();
		*ppUserData = (void *)strFileBuffer;

		strFileBuffer->FromFile((const char *)strInputFile);
		pReturnXML = (const char *)*strFileBuffer;
		m_bProcessed = 1;// flag for delete
	}
	// or if we're configured to query from memory, return the location
	else if (m_pDirectMemoryBuffer)
	{
		pReturnXML = m_pDirectMemoryBuffer;
	}
	return pReturnXML;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder United Business Technologies
United States United States
http://about.me/brian.aberle
https://www.linkedin.com/in/brianaberle
http://SyrianRue.org/Brian

Comments and Discussions