//====START_GENERATED_PROLOG======================================
//
//
//   COMPONENT_NAME: odother
//
//   CLASSES: none
//
//   ORIGINS: 27
//
//
//   (C) COPYRIGHT International Business Machines Corp. 1995,1996
//   All Rights Reserved
//   Licensed Materials - Property of IBM
//   US Government Users Restricted Rights - Use, duplication or
//   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//
//   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
//   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
//   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
//   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
//   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
//   OR PERFORMANCE OF THIS SOFTWARE.
//
//====END_GENERATED_PROLOG========================================
//
// @(#) 1.3 nt/src/samples/JavaPart/tag.cpp, odjavapart, od96nt, odnt9714c 3/19/97 04:00:11 [ 4/2/97 17:55:04 ]

#ifndef IBMSample_TAG
#include "Tag.h"
#endif

#include "iodjava.h"
#include "jemppars.h"

#include <qprintf.h>
#include <stdio.h>
#include <sys/stat.h>


/*-------------------------------------------------------------------+
|                      AppletTag::AppletTag ()                       |
| ================================================================== |
|                                                                    |
| Function:    AppletTag default constructor...                      |
|                                                                    |
| Algorithm:   o  Set default values...                              |
|                                                                    |
+-------------------------------------------------------------------*/
AppletTag::AppletTag () : fWidth (100), fHeight (100)
{
	Initialize ();

	SetUrl ("file:////");
	SetCode ("JavaPart");
	SetCodeBase ("");
	SetArgs (NULL);
}


/*-------------------------------------------------------------------+
|                      AppletTag::AppletTag ()                       |
| ================================================================== |
|                                                                    |
| Function:    AppletTag copy constructor...                         |
|                                                                    |
| Algorithm:   o  Set default values...                              |
|                                                                    |
+-------------------------------------------------------------------*/
AppletTag::AppletTag (AppletTag& tag)
{
	Initialize ();

	SetUrl (tag.fUrl);
	SetCode (tag.fCode);
	SetCodeBase (tag.fCodeBase);
	SetArgs (tag.fArgs);
	fWidth = tag.fWidth;
	fHeight = tag.fHeight;
}


/*-------------------------------------------------------------------+
|                      AppletTag::AppletTag ()                       |
| ================================================================== |
|                                                                    |
| Function:    Construct an applet tag given all the values...       |
|                                                                    |
| Algorithm:   o  Set values directly...                             |
|                                                                    |
+-------------------------------------------------------------------*/
AppletTag::AppletTag (char* url, char* code, char* codeBase, char** args,
	int width, int height) : fWidth (width), fHeight (height)
{
	Initialize ();

	SetUrl (url);
	SetCode (code);
	SetCodeBase (codeBase);
	SetArgs (args);
}


/*-------------------------------------------------------------------+
|                      AppletTag::AppletTag ()                       |
| ================================================================== |
|                                                                    |
| Function:    Construct an applet tag given an html file...         |
|                                                                    |
| Algorithm:   o  ...                                                |
|                                                                    |
+-------------------------------------------------------------------*/
AppletTag::AppletTag (char* filename) : fWidth (100), fHeight (100)
{
	Initialize ();

	/*
	 *  No need to continue if filename is null...
	 */
	if (filename == NULL) {
		SetUrl ("file:////");
		SetCode ("JavaPart");
		SetCodeBase ("");
		return;
	}

	/*
	 *  Save a copy of the URL...
	 */
	char url [255];
	strcpy (url, filename);

	/*
	 *  Determine if given filename is a URL to a local file...
	 */
	int count = strlen ("file:///");
	if (strnicmp (filename, "file:///", count) == 0) {

		// skip 'file:///'...
		filename += count;

		// convert forward slashes to backslashes
		Convert (filename, '/', '\\');
	}

	/*
	 *  If we cannot stat the file, return the default AppletTag info...
	 */
#if defined(__IBMCPP__)
	struct stat info;
#else
	struct _stat info;
#endif // defined(__IBMCPP__)
	if (_stat (filename, &info)) {

		/* Should display an error window at this point...  */

		qprintf ("stat (%s) failed!\n", filename);

		SetUrl ("file:////");
		SetCode ("JavaPart");
		SetCodeBase ("");
		SetArgs (NULL);

		return;
	}

	/*
	 *  Now parse the html file...
	 */
	char *buffer = new char [info.st_size];
	FILE* fp = fopen (filename, "r");
	fread (buffer, sizeof (char), info.st_size, fp);
	fclose (fp);

	AppletTagStructEx* tag = jeAppletTagParse (buffer);
	if (tag) {

		if (tag->sourceURL)
			SetUrl (tag->sourceURL);
		else
			SetUrl (url);

		if (tag->code)
			SetCode (tag->code);
		else
			SetCode ("");

		if (tag->codebase)
			SetCodeBase (tag->codebase);
		else
			SetCodeBase ("");

		fWidth  = tag->width;
		fHeight = tag->height;


		/*
		 *  Determine the number of extra arguments...
		 */
		fNumArgs = 0;
		AppletAttribute *attrib = tag->attributes;
		while (attrib != NULL) {
			attrib = attrib->next;
			fNumArgs++;
		}
		fArgs = new char* [fNumArgs];

		attrib = tag->attributes;
		for (int i=0; i<fNumArgs; i++) {
			fArgs [i] = new char [2 + strlen (attrib->name) 
                                                + strlen (attrib->value) ];
			strcpy (fArgs [i], attrib->name );
			strcat (fArgs [i], "=" );
			strcat (fArgs [i], attrib->value );

			attrib = attrib->next;
		}
		fArgs [i] = NULL;
	}
	jeAppletTagDelete (tag);
}


/*-------------------------------------------------------------------+
|                     AppletTag::~AppletTag ()                       |
| ================================================================== |
|                                                                    |
| Function:    AppletTag destructor                                  |
|                                                                    |
+-------------------------------------------------------------------*/
AppletTag::~AppletTag ()
{
	delete fUrl;
	delete fCode;
	delete fCodeBase;
	delete [] fArgs;
}


/*-------------------------------------------------------------------+
|                      AppletTag::operator=()                        |
| ================================================================== |
|                                                                    |
| Function:    Assignment operator (deep copy)...                    |
|                                                                    |
+-------------------------------------------------------------------*/
AppletTag& AppletTag::operator=(AppletTag& tag)
{
	if (this != &tag) {
		delete fUrl;
		delete fCode;
		delete fCodeBase;
		delete [] fArgs;

		SetUrl (tag.fUrl);
		SetCode (tag.fCode);
		SetCodeBase (tag.fCodeBase);
		SetArgs (tag.fArgs);
		fWidth = tag.fWidth;
		fHeight = tag.fHeight;
	}
	return *this;
}


/*-------------------------------------------------------------------+
|                       AppletTag::Convert ()                        |
| ================================================================== |
|                                                                    |
| Function:    ...                                                   |
|                                                                    |
+-------------------------------------------------------------------*/
void AppletTag::Convert (char* szText, char oldChar, char newChar)
{
	char* pch = strchr (szText, oldChar);
	while (pch) {
		*pch = newChar;
		pch = strchr (pch, oldChar);
	}
}


/*-------------------------------------------------------------------+
|                     AppletTag::Initialize ()                       |
| ================================================================== |
|                                                                    |
| Function:    ...                                                   |
|                                                                    |
| Algorithm:   o                                                     |
|                                                                    |
+-------------------------------------------------------------------*/
void AppletTag::Initialize ()
{
	fUrl  = fCode = fCodeBase = NULL;
	fArgs = NULL;
}


/*-------------------------------------------------------------------+
|                   AppletTag::SetXX () functions                    |
| ================================================================== |
|                                                                    |
| Function:    AppletTag accessors                                   |
|                                                                    |
+-------------------------------------------------------------------*/
void AppletTag::SetUrl (char* url)
{
	if (fUrl) delete fUrl;

	fUrl = new char [strlen (url) + 1];
	strcpy (fUrl, url);
}
void AppletTag::SetCode (char* code)
{
	if (fCode) delete fCode;

	fCode = new char [strlen (code) + 1];
	strcpy (fCode, code);
}
void AppletTag::SetCodeBase (char* codeBase)
{
	if (fCodeBase) delete fCodeBase;

	fCodeBase = new char [strlen (codeBase) + 1];
	strcpy (fCodeBase, codeBase);
}
void AppletTag::SetArgs (char** args)
{
	if (fArgs) delete [] fArgs;

	fNumArgs = 0;
	for (char** p=args; args && *p != NULL; p++, fNumArgs++);

	p = args;
	fArgs = new char* [fNumArgs + 1];
	for (int i=0; i<fNumArgs; i++, *p++) {
		fArgs [i] = new char [strlen (*p) + 1];
		strcpy (fArgs [i], *p);
	}
	fArgs [i] = NULL;
}



/* Begin BeanTag stuff */


/*-------------------------------------------------------------------+
|                      BeanTag::BeanTag ()                           |
| ================================================================== |
|                                                                    |
| Function:    BeanTag default constructor...                        |
|                                                                    |
| Algorithm:   o  Set default values...                              |
|                                                                    |
+-------------------------------------------------------------------*/
BeanTag::BeanTag () : fWidth (144), fHeight (125), fClassName(NULL)
{
	SetBeanClassName ("sunw.demo.juggler.Juggler");
}


/*-------------------------------------------------------------------+
|                      BeanTag::BeanTag ()                           |
| ================================================================== |
|                                                                    |
| Function:    BeanTag copy constructor...                           |
|                                                                    |
| Algorithm:   o  Set default values...                              |
|                                                                    |
+-------------------------------------------------------------------*/
BeanTag::BeanTag (BeanTag& tag) : fClassName(NULL)
{
	SetBeanClassName (tag.fClassName);
	fWidth = tag.fWidth;
	fHeight = tag.fHeight;
}


/*-------------------------------------------------------------------+
|                      BeanTag::BeanTag ()                           |
| ================================================================== |
|                                                                    |
| Function:    BeanTag constructor from bean className only...       |
|                                                                    |
| Algorithm:   o  Set fClassName - defaults for other values...      |
|                                                                    |
+-------------------------------------------------------------------*/
BeanTag::BeanTag (char* className) : fWidth (144), fHeight (125), fClassName(NULL)
{
	SetBeanClassName (className);
}


/*-------------------------------------------------------------------+
|                      BeanTag::BeanTag ()                           |
| ================================================================== |
|                                                                    |
| Function:    Construct an Bean tag given all the values...         |
|                                                                    |
| Algorithm:   o  Set values directly...                             |
|                                                                    |
+-------------------------------------------------------------------*/
BeanTag::BeanTag (char* className, int width, int height) :
   fWidth (width), fHeight (height), fClassName(NULL)
{
	SetBeanClassName (className);
}


/*-------------------------------------------------------------------+
|                     BeanTag::~BeanTag ()                           |
| ================================================================== |
|                                                                    |
| Function:    BeanTag destructor                                    |
|                                                                    |
+-------------------------------------------------------------------*/
BeanTag::~BeanTag ()
{
	delete fClassName;
}


/*-------------------------------------------------------------------+
|                      BeanTag::operator=()                          |
| ================================================================== |
|                                                                    |
| Function:    Assignment operator (deep copy)...                    |
|                                                                    |
+-------------------------------------------------------------------*/
BeanTag& BeanTag::operator=(BeanTag& tag)
{
	if (this != &tag) {
		delete fClassName;

		SetBeanClassName (tag.fClassName);

		fWidth = tag.fWidth;
		fHeight = tag.fHeight;
	}
	return *this;
}


/*-------------------------------------------------------------------+
|                   BeanTag::SetXX () functions                      |
| ================================================================== |
|                                                                    |
| Function:    BeanTag accessors                                     |
|                                                                    |
+-------------------------------------------------------------------*/
void BeanTag::SetBeanClassName (char* className)
{
	if (fClassName) delete fClassName;

	fClassName = new char [strlen (className) + 1];
	strcpy (fClassName, className);
}
