2009-11-06 18:06:12 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2005-2007 Gerald Schmidt.
|
|
|
|
*
|
|
|
|
* 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; version 2 of the License.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <wx/wx.h>
|
|
|
|
#include <wx/filename.h>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "xmlpromptgenerator.h"
|
|
|
|
#include "xmlencodinghandler.h"
|
|
|
|
#include "readfile.h"
|
|
|
|
#include "replace.h"
|
|
|
|
#include "pathresolver.h"
|
|
|
|
|
|
|
|
#undef XMLCALL
|
|
|
|
#include "catalogresolver.h"
|
|
|
|
|
|
|
|
// Xerces-C req'd for Schema parsing
|
2012-08-13 15:22:22 +02:00
|
|
|
#include "wrapxerces.h" // Declaration of toString() and XERCES_TMPLSINC definition
|
2009-11-06 18:06:12 +01:00
|
|
|
|
|
|
|
#include <xercesc/util/NameIdPool.hpp>
|
|
|
|
#include <xercesc/util/PlatformUtils.hpp>
|
|
|
|
#include <xercesc/framework/XMLValidator.hpp>
|
|
|
|
#include <xercesc/parsers/SAXParser.hpp>
|
|
|
|
#include <xercesc/parsers/XercesDOMParser.hpp>
|
|
|
|
#include <xercesc/validators/schema/SchemaValidator.hpp>
|
|
|
|
#include <xercesc/validators/common/ContentSpecNode.hpp>
|
|
|
|
#include <xercesc/validators/schema/SchemaSymbols.hpp>
|
|
|
|
|
|
|
|
using namespace xercesc;
|
|
|
|
|
|
|
|
XmlPromptGenerator::XmlPromptGenerator (
|
2013-10-16 19:28:39 +02:00
|
|
|
const wxString& basePath,
|
|
|
|
const wxString& auxPath ) : d ( new PromptGeneratorData() )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
|
|
|
XML_SetUserData ( p, d.get() );
|
|
|
|
d->p = p;
|
|
|
|
d->basePath = basePath;
|
|
|
|
d->auxPath = auxPath;
|
|
|
|
d->isRootElement = true;
|
|
|
|
d->grammarFound = false;
|
|
|
|
d->attributeValueCutoff = 12; // this prevents enums being stored in their thousands
|
|
|
|
XML_SetParamEntityParsing ( p, XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE );
|
|
|
|
XML_SetElementHandler ( p, starthandler, endhandler );
|
|
|
|
XML_SetDoctypeDeclHandler ( p, doctypedeclstarthandler, doctypedeclendhandler );
|
|
|
|
XML_SetElementDeclHandler ( p, elementdeclhandler );
|
|
|
|
XML_SetAttlistDeclHandler ( p, attlistdeclhandler );
|
|
|
|
XML_SetEntityDeclHandler ( p, entitydeclhandler );
|
|
|
|
XML_SetExternalEntityRefHandlerArg ( p, d.get() );
|
|
|
|
XML_SetExternalEntityRefHandler ( p, externalentityrefhandler );
|
2013-10-16 19:28:39 +02:00
|
|
|
XML_SetBase ( p, d->basePath.utf8_str() );
|
2009-11-06 18:06:12 +01:00
|
|
|
|
|
|
|
if ( !auxPath.empty() )
|
|
|
|
XML_UseForeignDTD ( p, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
XmlPromptGenerator::~XmlPromptGenerator()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void XMLCALL XmlPromptGenerator::starthandler (
|
|
|
|
void *data,
|
|
|
|
const XML_Char *el,
|
|
|
|
const XML_Char **attr )
|
|
|
|
{
|
|
|
|
PromptGeneratorData *d;
|
|
|
|
d = ( PromptGeneratorData * ) data;
|
|
|
|
|
|
|
|
if (d->isRootElement)
|
|
|
|
{
|
|
|
|
d->rootElement = el;
|
|
|
|
handleSchema ( d, el, attr ); // experimental: schema has been pre-parsed
|
|
|
|
d->isRootElement = false;
|
|
|
|
if ( ! (d->elementMap.empty() ) )//if ( d->elementMap.size() == 1) // must be 1 for success
|
|
|
|
{
|
|
|
|
d->grammarFound = true;
|
|
|
|
XML_StopParser ( d->p, false );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d->push ( el );
|
|
|
|
|
2012-08-10 15:34:13 +02:00
|
|
|
wxString parent ( d->getParent().c_str(), wxConvUTF8 );
|
|
|
|
wxString element ( el, wxConvUTF8 );
|
2009-11-06 18:06:12 +01:00
|
|
|
|
|
|
|
// update elementMap
|
2012-08-10 15:34:13 +02:00
|
|
|
d->elementMap[parent].insert ( element );
|
2009-11-06 18:06:12 +01:00
|
|
|
|
2012-08-10 15:34:13 +02:00
|
|
|
wxString attributeName, attributeValue;
|
2009-11-06 18:06:12 +01:00
|
|
|
|
|
|
|
// update attributeMap
|
|
|
|
for ( ; *attr; attr += 2 )
|
|
|
|
{
|
2012-08-10 15:34:13 +02:00
|
|
|
attributeName = wxString ( *attr, wxConvUTF8 );
|
|
|
|
attributeValue = wxString ( * ( attr + 1 ), wxConvUTF8 );
|
2009-11-06 18:06:12 +01:00
|
|
|
|
|
|
|
if (d->attributeMap[element][attributeName].size() < d->attributeValueCutoff)
|
|
|
|
d->attributeMap[element][attributeName].insert ( attributeValue );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLCALL XmlPromptGenerator::endhandler ( void *data, const XML_Char *el )
|
|
|
|
{
|
|
|
|
PromptGeneratorData *d;
|
|
|
|
d = ( PromptGeneratorData * ) data;
|
|
|
|
d->pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XmlPromptGenerator::getGrammarFound()
|
|
|
|
{
|
|
|
|
return d->grammarFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlPromptGenerator::getAttributeMap (
|
2012-08-10 15:34:13 +02:00
|
|
|
std::map<wxString, std::map<wxString, std::set<wxString> > >
|
2009-11-06 18:06:12 +01:00
|
|
|
&attributeMap )
|
|
|
|
{
|
|
|
|
attributeMap = d->attributeMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlPromptGenerator::getRequiredAttributeMap (
|
2012-08-10 15:34:13 +02:00
|
|
|
std::map<wxString, std::set<wxString> >& requiredAttributeMap )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
|
|
|
requiredAttributeMap = d->requiredAttributeMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlPromptGenerator::getElementMap (
|
2012-08-10 15:34:13 +02:00
|
|
|
std::map<wxString, std::set<wxString> > &elementMap )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
|
|
|
elementMap = d->elementMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlPromptGenerator::getEntitySet (
|
2012-08-10 15:34:13 +02:00
|
|
|
std::set<wxString> &entitySet )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
|
|
|
entitySet = d->entitySet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlPromptGenerator::getElementStructureMap (
|
2012-08-10 15:34:13 +02:00
|
|
|
std::map<wxString, wxString> &elementStructureMap )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
|
|
|
elementStructureMap = d->elementStructureMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlers for DOCTYPE handling
|
|
|
|
|
|
|
|
void XMLCALL XmlPromptGenerator::doctypedeclstarthandler (
|
|
|
|
void *data,
|
|
|
|
const XML_Char *doctypeName,
|
|
|
|
const XML_Char *sysid,
|
|
|
|
const XML_Char *pubid,
|
|
|
|
int has_internal_subset )
|
|
|
|
{
|
|
|
|
PromptGeneratorData *d;
|
|
|
|
d = ( PromptGeneratorData * ) data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLCALL XmlPromptGenerator::doctypedeclendhandler ( void *data )
|
|
|
|
{
|
|
|
|
PromptGeneratorData *d;
|
|
|
|
d = ( PromptGeneratorData * ) data;
|
|
|
|
if ( !d->elementMap.empty() )
|
|
|
|
{
|
|
|
|
d->grammarFound = true;
|
|
|
|
XML_StopParser ( d->p, false ); // experimental
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLCALL XmlPromptGenerator::elementdeclhandler (
|
|
|
|
void *data,
|
|
|
|
const XML_Char *name,
|
|
|
|
XML_Content *model )
|
|
|
|
{
|
|
|
|
PromptGeneratorData *d;
|
|
|
|
d = ( PromptGeneratorData * ) data;
|
|
|
|
|
2012-08-10 15:34:13 +02:00
|
|
|
wxString myElement ( name, wxConvUTF8 );
|
2009-11-06 18:06:12 +01:00
|
|
|
|
2012-08-10 15:34:13 +02:00
|
|
|
getContent ( *model, d->elementStructureMap[myElement], d->elementMap[myElement] );
|
2012-08-06 14:21:10 +02:00
|
|
|
|
|
|
|
XML_FreeContentModel ( d->p, model );
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlPromptGenerator::getContent (
|
|
|
|
const XML_Content &content,
|
2012-08-10 15:34:13 +02:00
|
|
|
wxString &contentModel,
|
|
|
|
std::set<wxString> &list )
|
2012-08-06 14:21:10 +02:00
|
|
|
{
|
2012-08-10 15:34:13 +02:00
|
|
|
wxString name;
|
2012-08-06 14:21:10 +02:00
|
|
|
switch ( content.type )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2012-08-06 14:21:10 +02:00
|
|
|
case XML_CTYPE_EMPTY:
|
2012-08-10 15:34:13 +02:00
|
|
|
contentModel += _T("EMPTY");
|
2012-08-06 14:21:10 +02:00
|
|
|
return;
|
|
|
|
case XML_CTYPE_ANY:
|
2012-08-10 15:34:13 +02:00
|
|
|
contentModel += _T("ANY");
|
2012-08-06 14:21:10 +02:00
|
|
|
return;
|
|
|
|
case XML_CTYPE_NAME:
|
2012-08-10 15:34:13 +02:00
|
|
|
name = wxString ( content.name, wxConvUTF8 );
|
|
|
|
list.insert ( name );
|
|
|
|
contentModel += name;
|
2012-08-06 14:21:10 +02:00
|
|
|
break;
|
|
|
|
case XML_CTYPE_CHOICE:
|
|
|
|
case XML_CTYPE_SEQ:
|
|
|
|
case XML_CTYPE_MIXED:
|
|
|
|
default:
|
2012-08-10 15:34:13 +02:00
|
|
|
wxString sep;
|
|
|
|
sep = ( content.type == XML_CTYPE_CHOICE ) ? _T("|") : _T(",");
|
|
|
|
contentModel += ( content.type == XML_CTYPE_MIXED ) ? _T("(#PCDATA|") : _T("(");
|
2012-08-06 14:21:10 +02:00
|
|
|
for ( unsigned i = 0; i < content.numchildren; i++ )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2012-08-06 14:21:10 +02:00
|
|
|
if ( i > 0 )
|
|
|
|
contentModel += sep;
|
|
|
|
getContent ( content.children[i], contentModel, list);
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
2012-08-10 15:34:13 +02:00
|
|
|
contentModel += _T(")");
|
2012-08-06 14:21:10 +02:00
|
|
|
break;
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
|
2012-08-06 14:21:10 +02:00
|
|
|
switch ( content.quant )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2012-08-06 14:21:10 +02:00
|
|
|
case XML_CQUANT_OPT:
|
2012-08-10 15:34:13 +02:00
|
|
|
contentModel += _T("?");
|
2012-08-06 14:21:10 +02:00
|
|
|
break;
|
|
|
|
case XML_CQUANT_REP:
|
2012-08-10 15:34:13 +02:00
|
|
|
contentModel += _T("*");
|
2012-08-06 14:21:10 +02:00
|
|
|
break;
|
|
|
|
case XML_CQUANT_PLUS:
|
2012-08-10 15:34:13 +02:00
|
|
|
contentModel += _T("+");
|
2012-08-06 14:21:10 +02:00
|
|
|
break;
|
|
|
|
case XML_CQUANT_NONE:
|
|
|
|
default:
|
|
|
|
break;
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLCALL XmlPromptGenerator::attlistdeclhandler (
|
|
|
|
void *data,
|
|
|
|
const XML_Char *elname,
|
|
|
|
const XML_Char *attname,
|
|
|
|
const XML_Char *att_type,
|
|
|
|
const XML_Char *dflt,
|
|
|
|
int isrequired )
|
|
|
|
{
|
|
|
|
PromptGeneratorData *d;
|
|
|
|
d = ( PromptGeneratorData * ) data;
|
|
|
|
|
2012-08-18 17:41:27 +02:00
|
|
|
wxString element ( elname, wxConvUTF8 );
|
|
|
|
wxString attribute ( attname, wxConvUTF8 );
|
|
|
|
std::set<wxString> &attributeValues = d->attributeMap[element][attribute];
|
2009-11-06 18:06:12 +01:00
|
|
|
if ( *att_type == '(' ) // change to exclude _known_ identifiers?
|
|
|
|
{
|
2012-08-06 17:27:22 +02:00
|
|
|
const char *s, *word;
|
|
|
|
s = att_type;
|
2009-11-06 18:06:12 +01:00
|
|
|
|
2012-08-03 16:41:01 +02:00
|
|
|
do {
|
|
|
|
s++;
|
|
|
|
word = s;
|
2012-08-06 17:27:22 +02:00
|
|
|
while ( *s != '|' && *s != ')' )
|
2012-08-03 16:41:01 +02:00
|
|
|
s++;
|
|
|
|
|
2012-08-10 15:34:13 +02:00
|
|
|
wxString currentValue ( word, wxConvUTF8, s - word );
|
2009-11-06 18:06:12 +01:00
|
|
|
attributeValues.insert ( currentValue );
|
2012-08-03 16:41:01 +02:00
|
|
|
|
|
|
|
while ( *s != '|' && *s != ')')
|
|
|
|
s++;
|
2012-08-06 17:27:22 +02:00
|
|
|
} while ( *s != ')' );
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( isrequired )
|
|
|
|
{
|
2012-08-18 17:41:27 +02:00
|
|
|
d->requiredAttributeMap[element].insert ( attribute );
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int XMLCALL XmlPromptGenerator::externalentityrefhandler (
|
|
|
|
XML_Parser p,
|
|
|
|
const XML_Char *context,
|
|
|
|
const XML_Char *base,
|
|
|
|
const XML_Char *systemId,
|
|
|
|
const XML_Char *publicId )
|
|
|
|
{
|
|
|
|
PromptGeneratorData *d;
|
|
|
|
d = ( PromptGeneratorData * ) p; // arg is set to user data in c'tor
|
|
|
|
|
2012-08-27 14:25:08 +02:00
|
|
|
int ret;
|
2009-11-06 18:06:12 +01:00
|
|
|
std::string buffer;
|
|
|
|
|
|
|
|
// auxPath req'd?
|
|
|
|
if ( !systemId && !publicId )
|
|
|
|
{
|
2013-10-16 19:28:39 +02:00
|
|
|
ReadFile::run ( ( const char * ) d->auxPath.mb_str(), buffer );
|
2009-11-06 18:06:12 +01:00
|
|
|
if ( buffer.empty() )
|
|
|
|
{
|
2012-08-27 14:25:08 +02:00
|
|
|
return XML_STATUS_ERROR;
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
|
2013-10-16 19:28:39 +02:00
|
|
|
d->encoding = XmlEncodingHandler::get ( buffer );
|
|
|
|
XML_Parser dtdParser = XML_ExternalEntityParserCreate ( d->p, context, d->encoding.c_str() );
|
2009-11-06 18:06:12 +01:00
|
|
|
if ( !dtdParser )
|
2012-08-27 14:25:08 +02:00
|
|
|
return XML_STATUS_ERROR;
|
2013-10-16 19:28:39 +02:00
|
|
|
XML_SetBase ( dtdParser, d->auxPath.utf8_str() );
|
2012-08-27 14:25:08 +02:00
|
|
|
ret = XML_Parse ( dtdParser, buffer.c_str(), buffer.size(), true );
|
|
|
|
XML_ParserFree ( dtdParser );
|
|
|
|
return ret;
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
|
2013-10-26 15:15:42 +02:00
|
|
|
wxString widePublicId ( publicId, wxConvUTF8 );
|
|
|
|
wxString wideSystemId ( systemId, wxConvUTF8 );
|
2012-08-30 16:37:01 +02:00
|
|
|
CatalogResolver cr;
|
2013-10-26 15:15:42 +02:00
|
|
|
wideSystemId = cr.catalogResolve ( widePublicId, wideSystemId );
|
2012-08-30 16:37:01 +02:00
|
|
|
|
2013-10-26 15:15:42 +02:00
|
|
|
if ( wideSystemId.empty() )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
|
|
|
if ( systemId )
|
2013-10-26 15:15:42 +02:00
|
|
|
wideSystemId = wxString ( systemId, wxConvUTF8 );
|
2009-11-06 18:06:12 +01:00
|
|
|
if ( base )
|
|
|
|
{
|
2013-10-26 15:15:42 +02:00
|
|
|
wxString test = PathResolver::run ( wideSystemId,
|
|
|
|
wxString ( base, wxConvUTF8 ) );
|
2009-11-06 18:06:12 +01:00
|
|
|
if ( !test.empty() )
|
|
|
|
{
|
2013-10-26 15:15:42 +02:00
|
|
|
wideSystemId = test;
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-26 15:15:42 +02:00
|
|
|
std::string localName;
|
|
|
|
localName = wideSystemId.mb_str ( wxConvLocal );
|
|
|
|
if ( !localName.empty() )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2013-10-26 15:15:42 +02:00
|
|
|
ReadFile::run ( localName, buffer );
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string encoding = XmlEncodingHandler::get ( buffer );
|
|
|
|
XML_Parser dtdParser = XML_ExternalEntityParserCreate ( d->p, context, encoding.c_str() );
|
|
|
|
if ( !dtdParser )
|
2012-08-27 14:25:08 +02:00
|
|
|
return XML_STATUS_ERROR;
|
2009-11-06 18:06:12 +01:00
|
|
|
|
2013-10-26 15:15:42 +02:00
|
|
|
XML_SetBase ( dtdParser, wideSystemId.utf8_str() );
|
2009-11-06 18:06:12 +01:00
|
|
|
|
2012-08-27 14:25:08 +02:00
|
|
|
ret = XML_Parse ( dtdParser, buffer.c_str(), buffer.size(), true );
|
|
|
|
XML_ParserFree ( dtdParser );
|
|
|
|
return ret;
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XMLCALL XmlPromptGenerator::entitydeclhandler (
|
|
|
|
void *data,
|
|
|
|
const XML_Char *entityName,
|
|
|
|
int is_parameter_entity,
|
|
|
|
const XML_Char *value,
|
|
|
|
int value_length,
|
|
|
|
const XML_Char *base,
|
|
|
|
const XML_Char *systemId,
|
|
|
|
const XML_Char *publicId,
|
|
|
|
const XML_Char *notationName )
|
|
|
|
{
|
|
|
|
PromptGeneratorData *d;
|
|
|
|
d = ( PromptGeneratorData * ) data; // arg is set to user data in c'tor
|
|
|
|
|
|
|
|
if (
|
|
|
|
entityName &&
|
|
|
|
!is_parameter_entity &&
|
|
|
|
!systemId &&
|
|
|
|
!publicId &&
|
|
|
|
!notationName )
|
|
|
|
{
|
2012-08-10 15:34:13 +02:00
|
|
|
d->entitySet.insert ( wxString ( entityName, wxConvUTF8 ) );
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlPromptGenerator::handleSchema (
|
|
|
|
PromptGeneratorData *d,
|
|
|
|
const XML_Char *el,
|
|
|
|
const XML_Char **attr )
|
|
|
|
{
|
2012-08-10 15:34:13 +02:00
|
|
|
if ( !d->isRootElement )
|
|
|
|
return;
|
2009-11-06 18:06:12 +01:00
|
|
|
// first check for XML Schema association
|
2012-08-11 11:12:28 +02:00
|
|
|
const char **schemaAttr = ( const char ** ) attr; // now redundant; could use attr
|
2013-10-16 19:28:39 +02:00
|
|
|
wxString path;
|
2012-08-10 15:34:13 +02:00
|
|
|
for ( ; *schemaAttr; schemaAttr += 2 )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
|
|
|
// no namespace
|
2012-08-10 15:34:13 +02:00
|
|
|
if ( !strcmp ( *schemaAttr, "xsi:noNamespaceSchemaLocation" ) )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2013-10-16 19:28:39 +02:00
|
|
|
path = wxString ( schemaAttr[1], wxConvUTF8 );
|
2009-11-06 18:06:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// with namespace -- check if this works
|
2012-08-10 15:34:13 +02:00
|
|
|
else if ( !strcmp ( *schemaAttr, "xsi:schemaLocation" ) )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2012-08-10 15:34:13 +02:00
|
|
|
const char *searchIterator = * ( schemaAttr + 1 );
|
|
|
|
while ( *searchIterator && *searchIterator != ' ' && *searchIterator != '\t' && *searchIterator != '\n' )
|
|
|
|
searchIterator++;
|
2009-11-06 18:06:12 +01:00
|
|
|
if ( *searchIterator )
|
|
|
|
{
|
2013-10-16 19:28:39 +02:00
|
|
|
path = wxString ( searchIterator + 1, wxConvUTF8 );
|
2009-11-06 18:06:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( path.empty() )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-16 19:28:39 +02:00
|
|
|
|
|
|
|
wxString schemaPath = PathResolver::run ( path, ( d->auxPath.empty() ) ? d->basePath : d->auxPath);
|
2009-11-06 18:06:12 +01:00
|
|
|
|
2012-08-11 07:32:20 +02:00
|
|
|
std::auto_ptr<XercesDOMParser> parser ( new XercesDOMParser() );
|
2009-11-06 18:06:12 +01:00
|
|
|
parser->setDoNamespaces ( true );
|
|
|
|
parser->setDoSchema ( true );
|
|
|
|
parser->setValidationSchemaFullChecking ( true );
|
|
|
|
|
2013-10-19 02:44:17 +02:00
|
|
|
Grammar *rootGrammar = parser->loadGrammar
|
2013-10-25 13:23:34 +02:00
|
|
|
( ( const XMLCh * ) WrapXerces::toString ( schemaPath ).GetData()
|
2013-10-19 02:44:17 +02:00
|
|
|
, Grammar::SchemaGrammarType
|
|
|
|
);
|
2009-11-06 18:06:12 +01:00
|
|
|
if ( !rootGrammar )
|
|
|
|
{
|
2012-08-11 01:50:42 +02:00
|
|
|
return;
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SchemaGrammar* grammar = ( SchemaGrammar* ) rootGrammar;
|
|
|
|
RefHash3KeysIdPoolEnumerator<SchemaElementDecl> elemEnum = grammar->getElemEnumerator();
|
|
|
|
|
|
|
|
if ( !elemEnum.hasMoreElements() )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-16 19:49:57 +02:00
|
|
|
SubstitutionMap substitutions;
|
|
|
|
buildSubstitutionMap ( substitutions, *grammar );
|
|
|
|
|
2009-11-06 18:06:12 +01:00
|
|
|
while ( elemEnum.hasMoreElements() )
|
|
|
|
{
|
|
|
|
const SchemaElementDecl& curElem = elemEnum.nextElement();
|
|
|
|
|
2012-08-10 15:34:13 +02:00
|
|
|
wxString element;
|
2012-08-06 13:50:48 +02:00
|
|
|
|
2009-11-06 18:06:12 +01:00
|
|
|
const QName *qnm = curElem.getElementName();
|
2012-08-10 15:34:13 +02:00
|
|
|
if ( qnm == NULL )
|
|
|
|
continue;
|
|
|
|
element = WrapXerces::toString ( qnm->getRawName() ); // this includes any prefix:localname combinations
|
2009-11-06 18:06:12 +01:00
|
|
|
if ( element.empty() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const XMLCh* fmtCntModel = curElem.getFormattedContentModel();
|
|
|
|
if ( fmtCntModel != NULL ) // tbd: this does not yet pick up prefix:localname combinations
|
|
|
|
{
|
2012-08-10 15:34:13 +02:00
|
|
|
wxString structure = WrapXerces::toString ( fmtCntModel );
|
|
|
|
d->elementStructureMap[element] = structure;
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
2012-08-06 13:50:48 +02:00
|
|
|
const ContentSpecNode *spec = curElem.getContentSpec();
|
|
|
|
if ( spec != NULL )
|
|
|
|
{
|
2012-08-16 19:49:57 +02:00
|
|
|
getContent ( d->elementMap[element], spec, substitutions );
|
2012-08-06 13:50:48 +02:00
|
|
|
}
|
|
|
|
|
2009-11-06 18:06:12 +01:00
|
|
|
// fetch attributes
|
2012-08-06 17:27:22 +02:00
|
|
|
if ( !curElem.hasAttDefs() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
XMLAttDefList& attIter = curElem.getAttDefList();
|
|
|
|
for ( unsigned int i = 0; i < attIter.getAttDefCount(); i++ )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2012-08-10 15:34:13 +02:00
|
|
|
wxString attribute, attributeValue;
|
2012-08-06 17:27:22 +02:00
|
|
|
|
|
|
|
XMLAttDef& attr = attIter.getAttDef ( i );
|
|
|
|
XMLAttDef::DefAttTypes ty = attr.getDefaultType();
|
|
|
|
if ( ty == XMLAttDef::Prohibited )
|
|
|
|
continue;
|
|
|
|
SchemaAttDef *pAttr = ( SchemaAttDef * ) &attr;
|
|
|
|
|
|
|
|
const QName *qnm = pAttr->getAttName();
|
2012-08-10 15:34:13 +02:00
|
|
|
if ( qnm == NULL )
|
|
|
|
continue;
|
|
|
|
attribute = WrapXerces::toString ( qnm->getRawName() );
|
2012-08-06 17:27:22 +02:00
|
|
|
if ( attribute.empty() )
|
|
|
|
continue;
|
2009-11-06 18:06:12 +01:00
|
|
|
|
2012-08-06 17:27:22 +02:00
|
|
|
// Value
|
2012-08-10 15:34:13 +02:00
|
|
|
attributeValue = WrapXerces::toString ( pAttr->getValue() );
|
2012-08-06 17:27:22 +02:00
|
|
|
d->attributeMap[element][attribute].insert( attributeValue );
|
2012-08-10 15:34:13 +02:00
|
|
|
|
2012-08-06 17:27:22 +02:00
|
|
|
if ( ty == XMLAttDef::Required || ty == XMLAttDef::Required_And_Fixed)
|
|
|
|
d->requiredAttributeMap[element].insert ( attribute );
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-06 13:50:48 +02:00
|
|
|
|
2012-08-16 19:49:57 +02:00
|
|
|
void XmlPromptGenerator::buildSubstitutionMap (
|
|
|
|
SubstitutionMap &substitutions,
|
|
|
|
const SchemaGrammar &grammar )
|
|
|
|
{
|
|
|
|
substitutions.clear();
|
|
|
|
|
|
|
|
RefHash2KeysTableOfEnumerator<ElemVector> list ( grammar.getValidSubstitutionGroups() );
|
|
|
|
if ( !list.hasMoreElements() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ( list.hasMoreElements() )
|
|
|
|
{
|
|
|
|
const ElemVector &elmts = list.nextElement();
|
|
|
|
|
|
|
|
const QName *qnm;
|
|
|
|
const SchemaElementDecl *cur, *substitution;
|
|
|
|
substitution = elmts.elementAt ( 0 )->getSubstitutionGroupElem();
|
|
|
|
|
|
|
|
size_t index = elmts.size();
|
|
|
|
while ( index-- > 0 )
|
|
|
|
{
|
|
|
|
cur = elmts.elementAt ( index );
|
|
|
|
qnm = cur->getElementName();
|
|
|
|
wxString element = WrapXerces::toString ( qnm->getRawName() );
|
|
|
|
|
|
|
|
substitutions[substitution].insert ( element );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 13:50:48 +02:00
|
|
|
void XmlPromptGenerator::getContent (
|
2012-08-16 19:49:57 +02:00
|
|
|
std::set<wxString> &list,
|
2012-08-06 13:50:48 +02:00
|
|
|
const ContentSpecNode *spec,
|
2012-08-16 19:49:57 +02:00
|
|
|
SubstitutionMap &substitutions )
|
2012-08-06 13:50:48 +02:00
|
|
|
{
|
|
|
|
//if ( spec == NULL) return;
|
|
|
|
|
|
|
|
const QName *qnm = spec->getElement();
|
|
|
|
if ( qnm )
|
|
|
|
{
|
2012-08-16 19:49:57 +02:00
|
|
|
const SchemaElementDecl *elem = (const SchemaElementDecl *)spec->getElementDecl();
|
|
|
|
SubstitutionMap::const_iterator itr = substitutions.find ( elem );
|
2012-09-24 16:05:20 +02:00
|
|
|
if ( itr == substitutions.end() && elem != NULL )
|
2012-08-16 19:49:57 +02:00
|
|
|
itr = substitutions.find ( elem->getSubstitutionGroupElem() );
|
|
|
|
if ( itr != substitutions.end() )
|
|
|
|
{
|
|
|
|
list.insert ( itr->second.begin(), itr->second.end() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxString element = WrapXerces::toString ( qnm->getRawName() );
|
|
|
|
if ( !element.IsEmpty() )
|
|
|
|
list.insert( element );
|
|
|
|
}
|
2012-08-06 13:50:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( spec->getFirst() != NULL)
|
2012-08-16 19:49:57 +02:00
|
|
|
getContent( list, spec->getFirst(), substitutions );
|
2012-08-06 13:50:48 +02:00
|
|
|
if ( spec->getSecond() != NULL)
|
2012-08-16 19:49:57 +02:00
|
|
|
getContent( list, spec->getSecond(), substitutions );
|
2012-08-06 13:50:48 +02:00
|
|
|
}
|