Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

LCD_DocManager.cc

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 // $Id: LCD_DocManager.cc,v 1.11 2004/02/02 22:12:48 uid561 Exp $
00003 // ----------------------------------------------------------------------------
00004 //
00005 // LCD_DocManager.cxx
00006 //
00007 // Implementation file for the LCD classes supporting DOM:
00008 //
00009 //  LCD_DocumentImpl         implementation of above
00010 //  LCD_DocManager           keeps track of all active LCD_Document's
00011 //
00012 // Other relevant classes have implementation, if any, in header file:
00013 //  LCD_Document             largely virtual class describing interface
00014 //  LCD_DocListener          entirely virtual
00015 //
00016 // Classes to handle the set of all XML documents known to a program
00017 // (i.e., for which there are DOMs)
00018 
00019 // There is a class LCD_DocumentImpl (should perhaps be an implementation of an
00020 // interface) which acts as a small database describing a DOM.  There
00021 // will be one instance per DOM.
00022 
00023 // LCD_DocumentImpl has an enhanced set of query functions as compared to
00024 // DOM_Document.  (Maybe I should change the name to LCD_Document.
00025 // And maybe it should inherit from DOM_Document, except that,
00026 // according to the DOM parser's documentation, the DOM_* classes
00027 // cannot be subclassed.)
00028 
00029 // There is another class (LCD_DocManager) which keeps track of all
00030 // LCD_DocumentImpls.  There will be only one instance per program.
00031 
00032 // The class LCD_DocListener just defines an interface for signing up
00033 // to be notified of changes to a document.
00034 
00035 #include "xercesc/util/PlatformUtils.hpp"
00036 #include <algorithm>
00037 //#include <strstream>
00038 #include <list>
00039 //#include <iterator>
00040 #include <iostream>
00041 using std::cout;
00042 
00043 #include "LCD_DocManager.hh"
00044 #include "LCD_Document.hh"
00045 #include "LCD_DocErrorReporter.hh"
00046 XERCES_CPP_NAMESPACE_USE
00047 
00048 // LCD_DocManager methods, initialization
00049 std::list <LCD_DocumentImpl*> *LCD_DocManager::active = 0;
00050 bool                      LCD_DocManager::initDone = false;
00051 
00052 /*
00053  *Comment
00054  */
00055 
00056 int LCD_DocManager::deleteDocument(LCD_Document *pDoc) {
00057   // First check that we really were passed a LCD_DocumentImpl, not some other
00058   // class satisfying LCD_Document
00059   LCD_DocumentImpl *pDocImpl = dynamic_cast <LCD_DocumentImpl*>(pDoc);
00060   if ( pDocImpl ) {
00061     // see if we can find it among our list of docs
00062     std::list<LCD_DocumentImpl*>::iterator which;
00063 
00064     which = std::find(active->begin(), active->end(), pDocImpl);
00065     if (which != active->end()) {
00066 
00067       // delete it from list
00068       active->erase(which);
00069 
00070       // Actually get rid of it
00071       delete pDocImpl;
00072     }
00073     else {
00074       return 1;  // doc not found
00075     }
00076   }
00077   else {
00078     return 2;   // error code for bad arg.  Or throw an exception??
00079   }
00080   return 0;
00081 }
00082 
00083 // return list of LCD_DocumentImpls whose underlying document is of
00084 // specified type (all if name is "*")
00085 
00086 
00087 
00088 /*
00089  *Comment
00090  */
00091 std::vector<LCD_Document*> *LCD_DocManager::getLCD_Documents(DOMString
00092                                                              docTypeName) {
00093   std::vector<LCD_Document*> *docs = new std::vector<LCD_Document*>;
00094   std::list<LCD_DocumentImpl*>::iterator docIt = active->begin();
00095   bool                        wild = ( docTypeName.equals(DOMString("*")) );
00096 
00097   while (docIt != active->end() ) {
00098     if ((wild) || (docTypeName.equals((*docIt)->getDocTypeName()) ) ) {
00099       docs->push_back(*docIt);
00100     }
00101     docIt++;
00102   }
00103   return docs;
00104 
00105 }
00106 
00107 // Look in all known documents of specified type to find element of
00108 // specified type with  specified id.
00109 // Return first one found (maybe should also somehow indicate which
00110 // document it was found in??)
00111 
00112 
00113 /*
00114  *Comment
00115  */
00116 DOM_Element LCD_DocManager::getElement(DOMString id, DOMString elementName,
00117                                        DOMString docTypeName) {
00118   std::list<LCD_DocumentImpl*>::iterator docIt = active->begin();
00119   bool                           wild = (docTypeName.equals(DOMString("*")));
00120 
00121   while (docIt != active->end() ) {
00122     LCD_DocumentImpl* pDoc = *docIt;
00123     if ((wild) || (docTypeName.equals(pDoc->getDocTypeName()) )) {
00124       DOM_Element elt = pDoc->getElement(id, elementName);
00125       if (elt != 0) return elt;
00126     }
00127     docIt++;
00128   }
00129   return DOM_Element();
00130 }
00131 
00132 
00133 /*
00134  *Comment
00135  */
00136 LCD_Document *LCD_DocManager::makeDocument(const std::string& XMLfilespec) {
00137   LCD_DocumentImpl *pNewDoc;
00138 
00139   //  if (!initDone) {
00140   if (active == 0) {
00141     // Initialize the XML4C2 system
00142     try
00143       {
00144         XMLPlatformUtils::Initialize();
00145         active = new std::list<LCD_DocumentImpl*>;
00146         initDone = true;
00147       }
00148 
00149     // Help! what to do if this fails?
00150     // This sort of failure is probably fatal
00151     catch (const XMLException& toCatch)
00152       {
00153         cout << "Error during xml4c platform initialization! :\n"
00154              << toCatch.getMessage() << "\n";
00155         return 0;
00156       }
00157   }
00158 
00159   pNewDoc = new LCD_DocumentImpl(XMLfilespec);
00160 
00161   try {
00162     pNewDoc->parse();
00163 
00164   }
00165   catch (const XMLException& toCatch2) {  // can't parse document
00166     // help.
00167     // This sort of failure is maybe recoverable
00168     cout << toCatch2.getMessage() << "\n";
00169     delete pNewDoc;
00170     return 0;
00171   }
00172 
00173   // Did we get any errors?  If so, can't use the document
00174   if ((pNewDoc->errReporter->getErrorCount()) ||
00175       (pNewDoc->errReporter->getFatalCount())    ) {
00176     cout << "Invalid XML source; cannot create document\n";
00177     delete pNewDoc;
00178     return 0;
00179   }
00180 
00181   active->push_front(pNewDoc);
00182 
00183   return pNewDoc;
00184 }
00185 
00186 
00187 /*
00188  *Comment
00189  */
00190 LCD_Document *LCD_DocManager::getLCDDoc(DOM_Document domDoc) {
00191   std::list<LCD_DocumentImpl*>::iterator which = active->begin();
00192 
00193   while (which != active->end()) {
00194     if ( domDoc == (*which)->getDocument()) return *which;
00195     which++;
00196   }
00197   return 0;
00198 }
00199 

Generated on Thu Oct 7 18:44:46 2004 for LCDG4 by doxygen 1.3.4