packagewxp.h

00001 
00002 // Name:        packagewxp.h
00003 // Purpose:     wxPackageXMLDescriptor
00004 // Author:      Francesco Montorsi
00005 // Modified by:
00006 // Created:     2006-06-29
00007 // RCS-ID:      $Id: packagewxp.h,v 1.8 2007/01/23 08:45:55 frm Exp $
00008 // Copyright:   (c) Francesco Montorsi
00009 // Licence:     wxWidgets licence
00011 
00012 #ifndef _PACKAGE_WXP_H_
00013 #define _PACKAGE_WXP_H_
00014 
00015 // Includes
00016 #include <wx/xml/xml.h>
00017 #include <wx/wfstream.h>
00018 #include <wx/dynarray.h>
00019 
00020 #include "wxp/wxp.h"
00021 
00022 class wxArray;
00023 class wxPackageXMLDescriptor;
00024 extern wxPackageXMLDescriptor wxEmptyPackageXMLDescriptor;
00025 
00026 
00027 
00028 // ----------------------------------------------------------------------------
00029 // wxPackageXMLDescriptor
00030 // ----------------------------------------------------------------------------
00031 
00032 // The version of the WXP XML files recognized by the applications:
00033 #define WXP_VERSION_NUMBER          1.1
00034 #define WXP_VERSION_STRING          wxT("1.1")
00035 
00038 class wxPackageXMLDescriptor : public wxPackageXML
00039 {
00040     DECLARE_DYNAMIC_CLASS(wxPackageXMLDescriptor)
00041 
00042 public:
00043     wxPackageXMLDescriptor() { Reset(); }
00044     virtual ~wxPackageXMLDescriptor() {}
00045 
00046     // reset all infos
00047     void Reset()
00048     {
00049         m_pBasics = m_pLicensing = m_pDetails = m_pDep = m_pBuildSystem = m_pPackaging = NULL;
00050         m_pId = m_pDescription = m_pCategory = m_pKeywords = NULL;
00051         m_pWebsite = m_pCreationDate = NULL;
00052         m_pLicense = m_pOrganization = m_pCost = m_pCredits = NULL;
00053         m_pCopyright = m_pAuthors = m_pMaintainers = NULL;
00054         m_pStatus = m_pProgLanguage = m_pSamples = NULL;
00055         m_pDownloads = m_pLogo = m_pRSS = m_pScreenshots = m_pDocs = NULL;
00056         m_pSupportedPorts = m_pTestedPorts = NULL;
00057         m_pFormats = m_pOptions = m_pStages = NULL;
00058         m_pRoot = m_pContents = m_pFileName = m_pUpload = m_pCompress = NULL;
00059 
00060         for (size_t i=0; i<wxPDT_MAX; i++)
00061             m_pDependencies[i] = NULL;
00062 
00063         m_nIndentationStep = 4;
00064     }
00065 
00066 public:         // getters
00067 
00069     const wxPackageInfo &GetPackageInfo() const
00070         { return m_info; }
00071 
00072 public:         // setters
00073 
00076     void SetPackageInfo(const wxPackageInfo &);
00077 
00078 public:         // load & save
00079 
00080     // NB: all wxPackageXMLDescriptor API is written with the assumption that
00081     //     white spaces are not removed during loading. So, just make it impossible
00082     //     to load the document removing them:
00083 
00084     bool Load(const wxString &filename)
00085     { 
00086         wxFileInputStream stream(filename);
00087         if (!stream.Ok())
00088             return false;
00089         return Load(stream);
00090     }
00091 
00092     bool Load(wxInputStream &stream)
00093     {
00094         SetRoot(NULL);
00095         Reset();
00096 
00097         if (!wxPackageXML::Load(stream, wxT("UTF-8"), wxXMLDOC_KEEP_WHITESPACE_NODES))
00098             return false;
00099         return Parse();
00100     }
00101 
00102     // does the real parsing of the XML
00103     bool Parse();
00104 
00105     bool Save(const wxString &filename) const
00106         { return wxPackageXML::Save(filename, -1); }
00107     bool Save(wxOutputStream &stream) const
00108         { return wxPackageXML::Save(stream, -1); }
00109 
00110     wxPackageXMLDescriptor& operator=(const wxPackageXMLDescriptor& doc)
00111     {
00112         Reset();
00113 
00114         // use wxXmlDocument's operator=
00115         ((wxXmlDocument&)(*this)) = (wxXmlDocument&)doc;
00116 
00117         // now update all our internal pointers
00118         m_pBasics = FindNode(wxT("basics"));
00119         m_pLicensing = FindNode(wxT("licensing"));
00120         m_pDetails = FindNode(wxT("details"));
00121         m_pDep = FindNode(wxT("dependencies"));
00122         m_pBuildSystem = FindNode(wxT("build-system"));
00123         m_pPackaging = FindNode(wxT("packaging"));
00124 
00125         // BASIC nodes
00126         m_pId = FindNode(m_pBasics, wxT("id"));
00127         m_pDescription = FindNode(m_pBasics, wxT("description"));
00128         m_pCategory = FindNode(m_pBasics, wxT("category"));
00129         m_pKeywords = FindNode(m_pBasics, wxT("keywords"));
00130         m_pWebsite = FindNode(m_pBasics, wxT("website"));
00131         m_pCreationDate = FindNode(m_pBasics, wxT("creation-date"));
00132 
00133         // LICENSING
00134         m_pLicense = FindNode(m_pLicensing, wxT("license"));
00135         m_pOrganization = FindNode(m_pLicensing, wxT("organization"));
00136         m_pCost = FindNode(m_pLicensing, wxT("cost"));
00137         m_pCredits = FindNode(m_pLicensing, wxT("credits"));
00138         m_pCopyright = FindNode(m_pLicensing, wxT("copyright"));
00139         m_pAuthors = FindNode(m_pLicensing, wxT("authors"));
00140         m_pMaintainers = FindNode(m_pLicensing, wxT("maintainers"));
00141 
00142         // PACKAGING
00143         m_pRoot = FindNode(m_pPackaging, wxT("root"));
00144         m_pFileName = FindNode(m_pPackaging, wxT("filename"));
00145         m_pContents = FindNode(m_pPackaging, wxT("contents"));
00146         m_pUpload = FindNode(m_pPackaging, wxT("upload"));
00147         m_pCompress = FindNode(m_pPackaging, wxT("compress"));
00148 
00149         // DETAILS
00150         m_pStatus = FindNode(m_pDetails, wxT("status"));
00151         m_pProgLanguage = FindNode(m_pDetails, wxT("proglanguage"));
00152         m_pSamples = FindNode(m_pDetails, wxT("samples"));
00153         m_pDownloads = FindNode(m_pDetails, wxT("download"));
00154         m_pLogo = FindNode(m_pDetails, wxT("logo"));
00155         m_pRSS = FindNode(m_pDetails, wxT("rss"));
00156         m_pScreenshots = FindNode(m_pDetails, wxT("screenshots"));
00157         m_pDocs = FindNode(m_pDetails, wxT("documentation"));
00158 
00159         // DEPENDENCIES
00160         m_pSupportedPorts = FindNode(m_pDep, wxT("supported-ports"));
00161         m_pTestedPorts = FindNode(m_pDep, wxT("tested-ports"));
00162         m_pDependencies[wxPDT_REQUIRED] = 
00163             FindNode(m_pDep, wxT("dependency"), wxT("type"), wxT("required"));
00164         m_pDependencies[wxPDT_SUGGESTED] = 
00165             FindNode(m_pDep, wxT("dependency"), wxT("type"), wxT("suggested"));
00166 
00167         // BUILD SYSTEM
00168         m_pFormats = FindNode(m_pBuildSystem, wxT("formats"));
00169         m_pOptions = FindNode(m_pBuildSystem, wxT("options"));
00170         m_pStages = FindNode(m_pBuildSystem, wxT("stages"));
00171 
00172         return *this;
00173     }
00174 
00175     wxXmlNode *FindNode(const wxString &name,
00176                         const wxString &propname = wxEmptyString,
00177                         const wxString &propval = wxEmptyString) const
00178     {
00179         if (!GetRoot())
00180             return NULL;
00181 
00182         return DoFindNode(GetRoot(), name, propname, propval);
00183     }
00184 
00185     wxXmlNode *FindNode(wxXmlNode *node,
00186                         const wxString &name,
00187                         const wxString &propname = wxEmptyString,
00188                         const wxString &propval = wxEmptyString) const
00189     {
00190         if (!node)
00191             return NULL;
00192 
00193         return DoFindNode(node, name, propname, propval);
00194     }
00195 
00196 protected:      // load helpers
00197 
00198     wxString GetHrefProp(const wxXmlNode *p) const;
00199     wxString GetLocalRefProp(const wxXmlNode *p) const;
00200 
00201     bool LoadBasics(const wxXmlNode *n, wxPackageInfo &);
00202     bool LoadPackaging(const wxXmlNode *n, wxPackageInfo &);
00203     bool LoadDetails(const wxXmlNode *n, wxPackageInfo &);
00204     bool LoadLicensing(const wxXmlNode *n, wxPackageInfo &);
00205     bool LoadDependencies(const wxXmlNode *n, wxPackageInfo &);
00206     bool LoadBuildSystem(const wxXmlNode *n, wxPackageInfo &);
00207 
00208     // LoadBuildSystem helpers
00209     bool LoadStages(const wxXmlNode *n, wxPackageInfo &p);
00210 
00211     // LoadDependencies helper
00212     bool LoadWxportTags(const wxXmlNode *port, const wxVersionRange &str,
00213                         wxArrayInt *ports, wxVersionRangeArray *ver);
00214 
00215     // LoadPackaging helper
00216 
00217     bool LoadContentsTag(const wxXmlNode *tag,
00218                          wxArrayString *dirs,
00219                          wxArrayString *files,
00220                          wxArrayString *patterns);
00221 
00222     // misc helpers
00223 
00224     // this should really go in wxXmlNode class...
00225     wxXmlNode *DoFindNode(wxXmlNode *node, const wxString &name,
00226                           const wxString &propname, const wxString &propval) const
00227     {
00228         if (node->GetName() == name)
00229         {
00230             // need to check also for a property?
00231             if (propname.IsEmpty() ||
00232                 node->GetPropVal(propname, wxEmptyString) == propval)
00233             {
00234                 // name matches and also the property matches (if required)
00235                 return node;
00236             }
00237         }
00238 
00239         wxXmlNode *child = node->GetChildren();
00240         while (child) 
00241         {
00242             wxXmlNode *ret = DoFindNode(child, name, propname, propval);
00243             if (ret)
00244                 return ret;     // found!
00245 
00246             child = child->GetNext();
00247         }
00248 
00249         // not found
00250         return NULL;
00251     }
00252 
00253 protected:      // save helpers
00254 
00255     // the callback function type used by ResetEnumeration
00256     typedef void (*FormatNodeFnc)(wxXmlNode *n, size_t i, 
00257                                   const wxPackageInfo &userdata);
00258 
00259     void ResetEnumeration(wxXmlNode *parent, int parentIndentLevel,
00260                           const wxString &nodename, size_t max,
00261                           FormatNodeFnc f, const wxPackageInfo &pkg,
00262                           bool eraseChildren = true);
00263 
00264     void CreateSiblingIfMissing(wxXmlNode *prev, wxXmlNode **node, 
00265                                 const wxString &name);
00266 
00267     void CreateChildIfMissing(wxXmlNode *parent, wxXmlNode *prev,
00268                               wxXmlNode **node, const wxString &name,
00269                               int level);
00270 
00271     void CreateMissingNodes();
00272 
00273     // sets the first CDATA or text node contents of the given node to given text
00274     void SetNodeContent(wxXmlNode *node, const wxString &text);
00275 
00276     // adds "href" and "localref" properties to the given node and init them
00277     // with the given wxPackageDataWithRef object
00278     void SetRefProp(wxXmlNode *node, const wxPackageDataWithRef &ref);
00279 
00280     // removes recursively all properties named "name" from the given node;
00281     // if "name" is empty all children are removed.
00282     void RemoveAllPropertiesFrom(wxXmlNode *node,
00283                                  const wxString &name = wxEmptyString);
00284 
00285     // removes recursively all children named "name" from given node;
00286     // if "name" is empty all children are removed.
00287     void RemoveAllChildrenFrom(wxXmlNode *node, 
00288                                const wxString &name = wxEmptyString);
00289 
00290 protected:      // pointers to the nodes
00291 
00292     // when modifying these remember to update operator= and Reset() functions
00293 
00294     // main section nodes
00295     wxXmlNode *m_pBasics, *m_pLicensing, *m_pDetails, *m_pDep, *m_pBuildSystem,
00296               *m_pPackaging;
00297 
00298     // BASIC xml nodes
00299     wxXmlNode *m_pId, *m_pDescription, *m_pCategory, *m_pKeywords, *m_pWebsite,
00300               *m_pCreationDate;
00301 
00302     // PACKAGING xml nodes
00303     wxXmlNode *m_pRoot, *m_pFileName, *m_pContents, *m_pUpload, *m_pCompress;
00304 
00305     // LICENSING xml nodes
00306     wxXmlNode *m_pLicense, *m_pOrganization, *m_pCost, *m_pCredits, *m_pCopyright,
00307               *m_pAuthors, *m_pMaintainers;
00308 
00309     // DETAILS xml nodes
00310     wxXmlNode *m_pStatus, *m_pProgLanguage, *m_pSamples, *m_pDownloads,
00311               *m_pLogo, *m_pRSS, *m_pScreenshots, *m_pDocs;
00312 
00313     // DEPENDENCIES xml nodes
00314     wxXmlNode *m_pSupportedPorts, *m_pTestedPorts, *m_pDependencies[wxPDT_MAX];
00315 
00316     // BUILD SYSTEM xml nodes
00317     wxXmlNode *m_pFormats, *m_pOptions, *m_pStages;
00318 
00319 private:
00320 
00321     // the class containing all info for this package:
00322     wxPackageInfo m_info;
00323 };
00324 
00325 
00326 
00327 #endif      // _PACKAGE_WXP_H_
00328 

Generated on Thu Feb 1 22:14:31 2007 for wxWidgets Package Manager by  doxygen 1.5.1-p1