1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
/*
* Copyright 2012 Zane U. Ji.
*
* This file is part of Xml Copy Editor.
*
* Xml Copy Editor is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* Xml Copy Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xml Copy Editor; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef XMLSCHEMAGENERATOR_H_
#define XMLSCHEMAGENERATOR_H_
#include <wx/wx.h>
#include <wx/textfile.h>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/validators/common/Grammar.hpp>
using namespace xercesc;
class XmlSchemaGenerator
{
public:
XmlSchemaGenerator ( bool inlineSimpleType = true );
virtual ~XmlSchemaGenerator();
const wxString &generate ( Grammar::GrammarType grammarType,
const wxString &filepath, const char *buffer, size_t len,
const wxString &encoding );
const wxString &getLastError() { return mLastError; }
static void addIndent ( wxString &str, size_t nIndent )
{
for ( size_t i = nIndent; i-- > 0; )
str << _T(" ");
}
static const wxChar *getEOL() { return wxTextFile::GetEOL(); }
protected:
class ChildData
{
public:
ChildData() : minOccurs ( 1 ), maxOccurs ( 1 ) {}
size_t minOccurs, maxOccurs;
std::set<wxString> precedence;
};
class ElmtData
{
public:
ElmtData() : useSequence ( true ), mixed ( false ) { }
// All occurs
std::set<const DOMElement*> nodes;
// Node name. Also used to indicate if the following data are valid
wxString name;
// These are not needed when we don't need to inline empty elements.
// The schema can be created right after we have all the data.
std::map<wxString, ChildData> children;
// Sequence of children
std::vector<wxString> sequence;
bool useSequence; // Use xs:sequence or xs:choice
// Specifies whether character data is allowed to appear between the
// child elements of this complexType element
bool mixed;
// Attribute name and default value
std::map<wxString, const XMLCh *> attrMap;
// Optional attributes
std::set<wxString> optAttrs;
wxString schema;
};
void findAllElements ( const DOMElement &element, size_t nIndent = 0 );
void generateData ( const DOMElement &element, size_t nIndent = 0 );
void generateData ( const wxString &elementName, size_t nIndent = 0 );
void outputSchema ( const DOMElement &element );
void generateSchema ( ElmtData &data, size_t nIndent );
void generateDTD ( ElmtData &data, size_t nIndent );
// Returns false if there is a loop dependence, which means that
// <xs:choice> has to be used.
bool getSequence ( std::vector<wxString> &sequence,
const std::map<wxString, ChildData> &elmtMap );
protected:
bool mInlineSimpleType;
Grammar::GrammarType mGrammarType;
std::map<wxString, ElmtData> mElements;
wxString mSchema, mLastError;
};
#endif /* XMLSCHEMAGENERATOR_H_ */
|