DocumentSrc sample: vending documents

Below is a listing of the DocumentSrc.c sample code file. For instructions on obtaining the source for this sample POD, see Downloading and working with the PODS sample files.

/* 
 * Copyright 2004 iAnwhere Solutions, Inc. All rights reserved. 
 * 
 * documentsrc.c 
 * 
 */

#include "pods.h" 
#include "podsstartup.h" 
#include "documentsrc.h"

// This is the function that is invoked when the 
// document manager is searching for a document. If 
// the url matches our SAMPLE_URL, we generate a 
// document and return it to the document manager. 
static PODSDocument 
           *DocumentSrcDocumentForUrl(PODSDocumentSrc *podsDocSrc, PODSString url, PODSBoolean *handled) 
{ 
   DocumentSrc *self = (DocumentSrc *)podsDocSrc;

   if (0 == stricmp(url, SAMPLE_URL)) { 
      PODSDocument *doc = PODScreateDocument(self->documentMgr, SAMPLE_URL, PODS_HTML_TYPE); 
      ADOMHTMLDocument *dom = PODSgetDom(doc); 
      ADOMElement *body; 
      ADOMText *node;

      ADOMsetTitle(dom, (ADOMString)"Hello World!"); 

      body = ADOMcreateElement(dom, (ADOMString)"body"); 
      ADOMappendChild(dom, (ADOMNode *)body); 

      node = ADOMcreateTextNode(dom, (ADOMString)"Hello World!"); 
      ADOMappendChild(body, (ADOMNode *)node);

      if (handled) 
         *handled = PODS_TRUE;

      return doc;
   }

   return NULL; 
} 

static void DocumentSrcDestroy(PODSDocumentSrc *podsDocSrc) 
{ 
   DocumentSrc *self = (DocumentSrc *)podsDocSrc;

   free(self->vtable); 
   free(self); 
}

// Create our document source and fill in vtable 
// entries for documentForUrl and destroy 
   DocumentSrc *DocumentSrcNew(PODSDocumentMgr *documentMgr) 
{ 
   DocumentSrc *self = (DocumentSrc *)calloc(1, sizeof(DocumentSrc));

   self->vtable = (PODSDocumentSrcVTable *)calloc(1, sizeof(PODSDocumentSrcVTable)); 

   PODS_SET_METHOD(self->vtable, documentForUrl, DocumentSrcDocumentForUrl); 
   PODS_SET_METHOD(self->vtable, destroy, DocumentSrcDestroy);

   self->documentMgr = documentMgr; 

   return self; 
}