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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
/*
* 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
*/
#include "xmlschemagenerator.h"
#include "wrapxerces.h"
#include <algorithm>
#include <boost/scoped_ptr.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOMNamedNodeMap.hpp>
#include <xercesc/dom/DOMAttr.hpp>
#include "xercescatalogresolver.h"
const static size_t maxReservedSchemaBuffer = 1024 * 1024;
const static size_t maxElementSchemaBuffer = 1024;
XmlSchemaGenerator::XmlSchemaGenerator ( bool inlineSimpleType /*= true*/)
: mInlineSimpleType ( inlineSimpleType )
, mGrammarType ( Grammar::SchemaGrammarType )
{
}
XmlSchemaGenerator::~XmlSchemaGenerator()
{
}
const wxString &XmlSchemaGenerator::generate (
Grammar::GrammarType grammarType
, const wxString &filepath
, const char *buffer
, size_t len
, const wxString &encoding
)
{
mGrammarType = grammarType;
mElements.clear();
mSchema.Clear();
XercesCatalogResolver catalogResolver;
boost::scoped_ptr<XercesDOMParser> parser ( new XercesDOMParser() );
parser->setDoNamespaces ( true );
parser->setDoSchema ( true );
parser->setValidationSchemaFullChecking ( false );
parser->setEntityResolver ( &catalogResolver );
MemBufInputSource source ( ( const XMLByte * ) buffer, len,
filepath.mb_str( wxConvLocal ) );
if ( !wxIsEmpty ( encoding ) )
source.setEncoding ( (const XMLCh *)
WrapXerces::toString ( encoding ).GetData() );
try {
//XMLPlatformUtils::fgSSE2ok = false;
parser->parse ( source );
}
catch ( XMLException& e )
{
mLastError = WrapXerces::toString ( e.getMessage() );
return mSchema;
}
xercesc::DOMDocument *doc = parser->getDocument();
if ( doc == NULL )
{
mLastError = _ ("Failed to load xml file.");
return mSchema;
}
size_t size = len / 3;
if ( size > maxReservedSchemaBuffer ) size = maxReservedSchemaBuffer;
mSchema.Alloc ( size );
if ( mGrammarType == Grammar::SchemaGrammarType )
mSchema << _T("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
<< getEOL()
<< _T("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\">")
<< getEOL();
xercesc::DOMElement *root = doc->getDocumentElement();
if ( root != NULL )
{
findAllElements ( *root );
generateData ( *root, 1 );
if ( mInlineSimpleType && mGrammarType == Grammar::SchemaGrammarType )
outputSchema ( *root );
}
if ( mGrammarType == Grammar::SchemaGrammarType )
mSchema << _T("</xs:schema>") << getEOL();
mSchema.Shrink();
return mSchema;
}
void XmlSchemaGenerator::findAllElements ( const DOMElement &element,
size_t nIndent /*= 0*/)
{
wxString tagName = WrapXerces::toString ( element.getTagName() );
mElements[tagName].nodes.insert ( &element );
DOMElement *child = WrapXerces::getFirstElementChild ( element );
while ( child )
{
findAllElements ( *child, nIndent );
child = WrapXerces::getNextElementSibling ( *child );
}
}
void XmlSchemaGenerator::generateData ( const DOMElement &element,
size_t nIndent /*= 0*/)
{
wxString name = WrapXerces::toString ( element.getTagName() );
if ( mElements[name].name.empty() )
{ // Only generate data once
generateData ( name, nIndent );
}
DOMElement *child = WrapXerces::getFirstElementChild ( element );
while ( child )
{
generateData ( *child, nIndent );
child = WrapXerces::getNextElementSibling ( *child );
}
}
void XmlSchemaGenerator::generateData ( const wxString &elementName,
size_t nIndent /*= 0*/)
{
ElmtData &data = mElements[elementName];
std::set<const DOMElement *>::iterator elmtItr;
data.name = elementName;
//Content
std::map<wxString, ChildData> &childMap = data.children;
std::map<wxString, ChildData>::iterator itr;
std::set<wxString> precedence;
elmtItr = data.nodes.begin();
for ( ; elmtItr != data.nodes.end(); ++elmtItr )
{
std::map<wxString, size_t> countMap;
DOMNode *child = ( **elmtItr ).getFirstChild();
for ( ; child != NULL; child = child->getNextSibling() )
{
DOMNode::NodeType type = child->getNodeType();
if ( type != DOMNode::ELEMENT_NODE )
{
if ( type == DOMNode::TEXT_NODE )
{ // Check for mixed content
wxString value = WrapXerces::toString ( child->getNodeValue() );
if ( !value.Trim().Trim ( false ).empty() )
data.mixed = true;
}
continue;
}
wxString name = WrapXerces::toString ( child->getNodeName() );
childMap[name].precedence.insert ( precedence.begin(), precedence.end() );
childMap[name].precedence.erase ( name ); // Don't depend on oneself
precedence.insert ( name );
countMap[name] += 1;
}
precedence.clear();
std::map<wxString, size_t>::iterator countItr = countMap.begin();
for ( ; countItr != countMap.end(); ++countItr )
{
if ( childMap[countItr->first].maxOccurs < countItr->second )
childMap[countItr->first].maxOccurs = countItr->second;
}
if ( childMap.size() == countMap.size() )
continue;
for ( itr = childMap.begin(); itr != childMap.end(); ++itr )
{
if ( countMap.find ( itr->first ) != countMap.end() )
continue;
itr->second.minOccurs = 0;
}
}
// Attribute
std::map<wxString, const XMLCh *> &attrMap = data.attrMap;
std::set<wxString> &optAttrs = data.optAttrs;
std::map<wxString, const XMLCh *>::iterator attrItr;
elmtItr = data.nodes.begin();
for ( ; elmtItr != data.nodes.end(); ++elmtItr )
{
DOMNamedNodeMap *attrs = ( **elmtItr ).getAttributes();
if ( attrs == NULL )
{
for ( attrItr = attrMap.begin(); attrItr != attrMap.end(); ++attrItr )
optAttrs.insert ( attrItr->first );
continue;
}
wxString name;
DOMAttr *attr;
size_t i = attrs->getLength();
while ( i-- > 0 )
{
attr = ( DOMAttr* ) attrs->item ( i );
name = WrapXerces::toString ( attr->getName() );
if ( attr->getPrefix() != NULL )
{
wxLogDebug ( _T("Ignore: %s"), name.c_str() );
continue;
}
if ( elmtItr != data.nodes.begin() ) // Not the first node
if ( attrMap.find ( name ) == attrMap.end() ) // Not in the map
optAttrs.insert ( name );
if ( attr->getSpecified() )
attrMap[name]; // Initialize attribute map
else
attrMap[name] = attr->getValue();
}
if ( attrMap.size() == optAttrs.size() )
continue;
for ( attrItr = attrMap.begin(); attrItr != attrMap.end(); ++attrItr )
{
if ( attrs->getNamedItem ( ( const XMLCh * )
WrapXerces::toString ( attrItr->first ).GetData() ) == NULL )
{
optAttrs.insert ( attrItr->first );
}
}
}
// Deal with sequence
wxLogDebug ( _T("%s:"), elementName.c_str() );
data.useSequence = getSequence ( data.sequence, childMap );
// Now we have the data of the element
if ( mGrammarType == Grammar::DTDGrammarType )
{
generateDTD ( data, nIndent );
mSchema << data.schema;
}
else if ( !mInlineSimpleType )
{ // Or wait until all data are available
generateSchema ( data, nIndent );
mSchema << data.schema;
}
}
void XmlSchemaGenerator::outputSchema ( const DOMElement &element )
{
wxString tagName = WrapXerces::toString ( element.getTagName() );
ElmtData &data = mElements[tagName];
if ( data.schema.empty() )
{
if ( mGrammarType == Grammar::SchemaGrammarType )
generateSchema ( data, 1 );
else
generateDTD ( data, 1 );
mSchema << data.schema;
}
DOMElement *child = WrapXerces::getFirstElementChild ( element );
while ( child )
{
outputSchema ( *child );
child = WrapXerces::getNextElementSibling ( *child );
}
}
void XmlSchemaGenerator::generateSchema ( ElmtData &data, size_t nIndent )
{
wxString &schema = data.schema;
if ( data.children.empty() && data.attrMap.empty() )
{
if ( !mInlineSimpleType )
{
addIndent ( schema, nIndent );
schema << _T("<xs:element name=\"") << data.name
<< _T("\" type=\"xs:string\"/>") << getEOL();
}
return;
}
schema.Alloc ( maxElementSchemaBuffer );
addIndent ( schema, nIndent++ );
schema << _T("<xs:element name=\"") << data.name << _T("\">") << getEOL();
addIndent ( schema, nIndent++ );
if ( data.mixed )
schema << _T("<xs:complexType mixed=\"true\">") << getEOL();
else
schema << _T("<xs:complexType>") << getEOL();
if ( !data.children.empty() )
{
size_t minOccurs = 1, maxOccurs = 1, minTotal = 0;
std::map<wxString, ChildData>::const_iterator itr;
for ( itr = data.children.begin(); itr != data.children.end(); ++itr )
{
if ( itr->second.minOccurs < minOccurs )
minOccurs = itr->second.minOccurs;
if ( itr->second.maxOccurs > maxOccurs )
maxOccurs = itr->second.maxOccurs;
minTotal += itr->second.minOccurs;
}
addIndent ( schema, nIndent++ );
if ( data.useSequence )
{
schema << _T("<xs:sequence");
}
else
{
schema << _T("<xs:choice maxOccurs=\"unbounded\"");
}
//if (minOccurs != 1) schema << _T(" minOccurs=\"") << minOccurs << _T("\"");
//if (maxOccurs > 1) schema << _T(" maxOccurs=\"unbounded\"");
if ( minTotal == 0 ) schema << _T(" minOccurs=\"0\"");
schema << _T(">") << getEOL();
std::vector<wxString>::const_iterator seqItr;
seqItr = data.sequence.begin();
for ( ; seqItr != data.sequence.end(); ++seqItr )
{
const ChildData &child = data.children[*seqItr];
addIndent ( schema, nIndent );
if ( mInlineSimpleType )
{ // Check if it's a simple type
const ElmtData *childElmt = &mElements[*seqItr];
if ( childElmt->children.empty()
&& childElmt->attrMap.empty() )
{
schema << _T("<xs:element name=\"") << *seqItr
<< _T("\" type=\"xs:string\"/>") << getEOL();
continue;
}
}
schema << _T("<xs:element ref=\"") << *seqItr << _T("\"");
if ( data.useSequence )
{
if ( child.minOccurs == 0 )
{
schema << _T(" minOccurs=\"0\"");
}
if ( child.maxOccurs > 1 )
{
schema << _T(" maxOccurs=\"unbounded\"");
}
}
schema << _T("/>") << getEOL();
}
addIndent ( schema, --nIndent );
if ( data.useSequence )
{
schema << _T("</xs:sequence>") << getEOL();
}
else
{
schema << _T("</xs:choice>") << getEOL();
}
} // Child elements
std::map<wxString, const XMLCh *>::const_iterator attrItr;
attrItr = data.attrMap.begin();
for ( ; attrItr != data.attrMap.end(); ++attrItr )
{
addIndent ( schema, nIndent );
schema << _T("<xs:attribute name=\"") << attrItr->first
<< _T("\" type=\"xs:string\"");
if ( attrItr->second != NULL )
{
schema << _T(" default=\"")
<< WrapXerces::toString ( attrItr->second ) << _T("\"");
}
else if ( data.optAttrs.find ( attrItr->first )
== data.optAttrs.end() )
{
schema << _T(" use=\"required\"");
}
schema << _T("/>") << getEOL();
}
addIndent ( schema, --nIndent );
schema << _T("</xs:complexType>") << getEOL();
addIndent ( schema, --nIndent );
schema << _T("</xs:element>") << getEOL();
schema.Shrink();
}
void XmlSchemaGenerator::generateDTD ( ElmtData &data, size_t WXUNUSED ( nIndent ) )
{
wxString &schema = data.schema;
schema.Alloc ( maxElementSchemaBuffer );
schema << _T("<!ELEMENT ") << data.name;
if (data.sequence.empty())
{
schema << _T(" (#PCDATA)");
}
else
{
const wxChar *separator = _T(" (");
std::vector<wxString>::const_iterator seqItr;
seqItr = data.sequence.begin();
if (data.useSequence)
{
for ( ; seqItr != data.sequence.end(); ++seqItr )
{
schema << separator << *seqItr;
separator = _T(", ");
const ChildData &child = data.children[*seqItr];
if ( child.minOccurs == 0 )
schema << ( child.maxOccurs > 1 ? _T("*") : _T("?") );
else if ( child.maxOccurs > 1 )
schema << _T("+");
}
schema << _T(")");
}
else
{
size_t minTotal = 0;
for ( ; seqItr != data.sequence.end(); ++seqItr )
{
schema << separator << *seqItr;
separator = _T(" | ");
minTotal += data.children[*seqItr].maxOccurs;
}
schema << ( minTotal > 0 ? _T(")+") : _T(")*") );
}
}
schema << _T(">") << getEOL();
if ( !data.attrMap.empty() )
{
const static wxString indent =
wxString ( getEOL() ) + _T(" ");
schema << _T("<!ATTLIST ") << data.name;
std::map<wxString, const XMLCh *>::const_iterator attrItr;
attrItr = data.attrMap.begin();
for ( ; attrItr != data.attrMap.end(); ++attrItr )
{
schema << indent << attrItr->first << _T(" CDATA");
if ( attrItr->second != NULL ) // Has default value
schema << _T(" \"") << WrapXerces::toString ( attrItr->second ) << _T("\"");
else if ( data.optAttrs.find ( attrItr->first ) == data.optAttrs.end() )
schema << _T(" #REQUIRED");
else
schema << _T(" #IMPLIED");
}
schema << _T(">") << getEOL();
}
schema.Shrink();
}
bool XmlSchemaGenerator::getSequence ( std::vector<wxString> &sequence,
const std::map<wxString, ChildData> &elmtMap )
{
bool deadlock = false;
sequence.clear();
std::vector<wxString>::iterator seqItr, seqFindItr;
std::set<wxString>::const_iterator prevItr, prevEnd;
std::map<wxString, ChildData>::const_iterator itr;
bool retry;
do
{
retry = false;
for ( itr = elmtMap.begin(); itr != elmtMap.end(); ++itr )
{
seqFindItr = std::find ( sequence.begin(), sequence.end(),
itr->first );
if ( seqFindItr != sequence.end() )
continue;
seqItr = sequence.begin();
prevItr = itr->second.precedence.begin();
prevEnd = itr->second.precedence.end();
for ( ; prevItr != prevEnd; ++prevItr )
{ // Find last index of dependent elements
seqFindItr = std::find ( sequence.begin(), sequence.end(),
*prevItr );
if ( seqFindItr != sequence.end() )
{
if ( seqItr < seqFindItr )
{
seqItr = seqFindItr;
}
continue;
}
const std::set<wxString> &previous =
elmtMap.find ( *prevItr )->second.precedence;
if ( previous.find ( itr->first ) == previous.end() )
{ // Not a deadlock
retry = true;
break;
}
else
{
deadlock = true;
}
}
if ( prevItr != prevEnd )
continue; // The preceding doesn't exist
if ( seqItr != sequence.end() )
{
++seqItr;
}
sequence.insert ( seqItr, itr->first );
wxLogDebug ( _T(" %s"), itr->first.c_str() );
}
} while ( retry );
return !deadlock;
}
|