62 string elemName = xpp->getName();
73 if (t->getTypeId() != typeId)
74 error(
"Fatal error ,container's type is not same as the validated type",xpp);
80 if (!ssParser_->parseSchemaTag()){
97 if (!sParser_->isBasicType(typeId)){
99 const XSDType * pType = sParser_->getType(typeId);
101 if (sParser_->isImported(pType->getNamespace())) {
103 sParser_ = sParser_->getImportedSchemaParser(pType->getNamespace());
104 typeId =
const_cast<SchemaParser*
>(sParser_)->getTypeId(pType->getQname());
106 t->sParser_ = sParser_;
113 if (sParser_->getType(typeId) == 0
114 || sParser_->getType(typeId)->isSimple()) {
122 val = xpp->getText();
128 val += xpp->getText();
140 if (xpp->getName() != elemName)
141 error(
"Syntax error "+elemName,xpp);
144 error(
"Expected a closing tag for " + elemName,xpp);
160 static_cast<const ComplexType *
>(sParser_->getType(typeId));
167 (sParser_->getType(ct->getBaseTypeId()));
168 btCnt = t->getBaseTypeContainer(
true);
171 int attcnt = xpp->getAttributeCount();
173 for (
int i = 0; i < attcnt; i++) {
176 std::string attVal = xpp->getAttributeValue(
"", attName);
177 std::string attNsp = xpp->getAttributeNamespace(i);
178 if (!attNsp.empty() && attNsp != sParser_->getNamespace())
183 at = ct->getAttribute(attName);
186 at = bt->getAttribute(attName);
195 error(
"Unknown attribute \"" + attName +
"\"",xpp);
197 validate(attVal, at->getType(), atCnt, xpp);
201 checkAttributeOccurence(ct,xpp);
203 checkAttributeOccurence(bt,xpp);
212 if (xpp->getEventType() == xpp->TEXT){
213 val = xpp->getText();
214 validate(val, ct->getContentType(), t, xpp);
219 validate(val, ct->getContentType(), t, xpp);
224 if (xpp->getName() != elemName)
225 error(
"Syntax error",xpp);
228 error(
"Expected a closing tag for " + elemName,xpp);
235 bCm = bt ->getContents();
237 validateContentModel(xpp,
239 t->getChildContainer(cm,
true),
244 validateContentModel(xpp,
246 btCnt->getChildContainer(bCm,
true),
261 if (!ipTc && t)
delete t;
265 spe.
line=xpp->getLineNumber();
266 spe.
col=xpp->getColumnNumber();
277 const string & elemName,
289 for (ci=cit_b;ci!=cit_e;ci++){
291 ci->first.e->nOccurrences=0;
296 int t = btCnt->getTypeId();
297 const ComplexType* ct =
static_cast<const ComplexType*
>(btCnt->schemaParser()->getType(t));
298 bCm = ct->getContents();
302 switch (cm->getCompositor()) {
312 if (xpp->getName() == elemName)
319 if(!findElement(cit_b,cit_e,xpp->getName(),ci))
320 error(
"Could not find element " +xpp->getName()+
" in "+elemName,xpp);
321 ci->first.e->nOccurrences++;
323 validate(xpp, ci->first.e->getType(),
324 ipTc->getChildContainer(ci->first.e->getName(),
true));
332 for (ci=cit_b;ci!=cit_e;ci++){
334 (ci->first.e->nOccurrences<ci->first.e->getMin()||
335 ci->first.e->nOccurrences>ci->first.e->getMax()))
336 error(ci->first.e->getName()+
" did not meet occurrence constraints",xpp);
350 if (xpp->getName() == elemName)
358 (xpp->getName() != elemName)))
367 if ((xpp->getEventType() == xpp->END_TAG)&&
368 (xpp->getName() == elemName))
371 validateContentModel(xpp,ci->first.c,
372 ipTc->getChildContainer(ci->first.c,
true),
373 elemName,
true,btCnt);
379 if(cm->anyContents() ||
380 findElement(ci,cit_e,xpp->getName(), ci)){
382 ci->first.e->nOccurrences++;
383 validate(xpp,ci->first.e->getType(),
384 ipTc->getChildContainer(ci->first.e->getName(),
true));
386 }
else if (bCm && (bCm->anyContents() ||
387 findElement(bCm->begin(),bCm->end(),xpp->getName(), bci))){
389 TypeContainer * t = btCnt->getChildContainer(bCm,
true);
390 validate(xpp,bci->first.e->getType(),t->getChildContainer(bci->first.e->getName(),
true));
394 error(
"Could not find element " +xpp->getName()+
" in "+elemName,xpp);
404 for (ci=cit_b;ci!=cit_e;ci++){
406 (ci->first.e->nOccurrences<ci->first.e->getMin()||
407 ci->first.e->nOccurrences>ci->first.e->getMax()))
408 error(ci->first.e->getName()+
" did not meet occurrence constraints",xpp);
419 if(findElement(ci,cit_e,xpp->getName(), ci)) {
421 std::string choiceElem = xpp->getName();
424 ci->first.e->nOccurrences++;
425 validate(xpp, ci->first.e->getType(),
426 ipTc->getChildContainer(ci->first.e->getName(),
true));
428 }
while(xpp->getName() == choiceElem);
439 validateContentModel(xpp,ci->first.c,
440 ipTc->getChildContainer(ci->first.c,
true),
441 elemName,
true,btCnt);
443 catch (SchemaParserException spe){
447 validateContentModel(xpp,ci->first.c,
448 ipTc->getChildContainer(ci->first.c,
true),
449 elemName,
true,btCnt);
454 error(
"Could not find element " +xpp->getName()+
" in "+elemName,xpp);
465 (ci->first.e->nOccurrences<ci->first.e->getMin()||
466 ci->first.e->nOccurrences>ci->first.e->getMax()))
467 error(ci->first.e->getName()+
"did not meet occurrence constraints",xpp);
475 for (ci=cit_b;ci!=cit_e;ci++){
478 ci->first.e->nOccurrences=0;
497 int basetype = sParser_->getBasicContentType(typeId);
499 const XSDType * pType = sParser_->getType(typeId);
500 if (pType && !pType->isSimple()){
512 if (st && (st->isList() || st->isUnion())){
514 std::string val = *((std::string*)value);
515 ipTc->setValue(val,validateListOrUnion(st,val,xpp));
523 int x= *((
int*)value);
529 ipTc->setValue(x,st->isValidInt(x));
535 char c= *((
char*)value);
541 float f = *((
float*)value);
548 ipTc->setValue(f,st->isValidFloat(f));
555 double db = *((
double*)value);
561 long l = *((
long*)value);
568 unsigned long ul= *((
unsigned long*)value);
574 bool b = *((
bool*)value);
587 std::string val = *((std::string* )value);
594 ipTc->setValue(val,st->isValidString(val));
615 int basetype = sParser_->getBasicContentType(typeId);
621 const XSDType * pType = sParser_->getType(typeId);
623 !pType->isSimple() &&
629 if (pType && !pType->isSimple() &&
635 return validate(val,contentType,ipTc,xpp);
645 ipTc->setValAsString(val);
647 while(ipTc->isValueValid()){
649 extractSimpleType(val, basetype, ipTc, st, xpp);
652 if(!st || (st && (st->isList() || st->isUnion()))){
660 if (!sParser_->isBasicType(st->getBaseTypeId())){
662 st=
static_cast<const SimpleType*
>(sParser_->getType(st->getBaseTypeId()));
673SchemaValidator::extractSimpleType(
const std::string & val,
680 if (st && (st->isList() || st->isUnion())){
682 ipTc->setValue(val,validateListOrUnion(st,val,xpp));
686 istringstream istr(val);
701 ipTc->setValue(x,!istr.fail());
705 ipTc->setValue(x,!istr.fail() && st->isValidInt(x));
711 ipTc->setValue(c,!istr.fail());
717 ipTc->setValue(f,!istr.fail());
719 ipTc->setValue(f,!istr.fail() && st->isValidFloat(f));
726 ipTc->setValue(db,!istr.fail());
730 ipTc->setValue(l,!istr.fail());
735 ipTc->setValue(ul,!istr.fail());
744 ipTc->setValue(
true);
746 ipTc->setValue(
false);
753 q.setNamespace(xpp->getNamespace(q.getPrefix()));
766 ipTc->setValue(val,st->isValidString(val));
781SchemaValidator::validateListOrUnion(
const SimpleType* st,
782 const std::string &val,
789 while(s < val.length()){
790 while(val[s]==
' ')s++;
791 std::string t = val.substr(s,val.find(
' ',s)-s);
792 TypeContainer * tc =
validate(t,st->getBaseTypeId(),0,xpp);
793 if (!(tc && tc->isValueValid()))
799 }
else if (st->isUnion()){
801 std::list<int>::const_iterator it= st->unionTypes()->begin();
802 while (it!=st->unionTypes()->end()){
804 TypeContainer * tc =
validate(val,*it,0,xpp);
806 if (tc && tc->isValueValid())
836 std::cout<<
"Looking for "<< name<<
" found "<<ci->first.e->getName()<<std::endl;
838 if(ci->first.e->getName()==name ||
839 ci->first.e->getName() ==
"*")
849void SchemaValidator::error(
const std::string& mesg,
XmlPullParser* xpp)
852 SchemaParserException spe(mesg +
"\nError validating schema instance\n");
855 spe.line=xpp->getLineNumber();
856 spe.col=xpp->getColumnNumber();
863SchemaValidator::checkAttributeOccurence(
const ComplexType* ct ,
867 if (ct->getNumAttributes() > 0)
869 for (
int i = 0; i < ct->getNumAttributes(); i++)
871 const Attribute*at = ct->getAttribute(i);
876 string attVal = xpp->getAttributeValue(
"", at->getName());
879 if (at->isRequired())
880 error(
"Required attribute \"" + at->getName() +
881 "\" missing or empty",xpp);
900 std::string nsp = sParser_->getNamespace();
909 return instance1(tag,type_id);
913SchemaValidator::instance1(
const std::string &tag,
917 std::string nsp = sParser_->getNamespace();
918 static bool first =
false;
931 const XSDType * pType = sParser_->getType(type_id);
936 xmlStream_->
text(
"");
941 const ComplexType * ct =
942 static_cast<const ComplexType*
>(pType);
945 if (ct->getNumAttributes() > 0) {
947 for (
int i = 0; i < ct->getNumAttributes(); i++) {
949 const Attribute*at = ct->getAttribute(i);
950 xmlStream_->
attribute(sParser_->getNamespace(),at->getName(),
"");
957 xmlStream_->
text(
"");
961 ContentModel* cm=ct->getContents();
966 xmlStream_->
endTag(nsp,tag);
974SchemaValidator::instanceCM(ContentModel *cm)
982 switch (cm->getCompositor())
991 for (ci=cit_b;ci!=cit_e;ci++){
994 ci->first.e->getMax() > 0){
996 const SchemaParser* s1Parser = sParser_;
999 if (!ci->first.e->getTypeNamespace().empty() &&
1000 sParser_->isImported(ci->first.e->getTypeNamespace()) &&
1001 sParser_->getNamespace() != ci->first.e->getTypeNamespace()) {
1006 sParser_ = sParser_->getImportedSchemaParser(ci->first.e->getTypeNamespace());
1009 instance1(ci->first.e->getName(),t);
1010 sParser_ = s1Parser;
1015 instanceCM(ci->first.c);
1021 instanceCM(ci->first.g->getContents());
std::string getAttributeName(int index) const
int getContentType() const
std::list< ContentHolder >::iterator ContentsIterator
bool instance(const std::string &tag, Schema::Type type_id)
SchemaValidator(const SchemaParser *sp, std::ostream &os=std::cout)
TypeContainer * validate(XmlPullParser *xpp, int typeId, TypeContainer *ipTc=0)
TypeContainer * getAttributeContainer(std::string attName, bool create=false)
XmlSerializer & startTag(std::string nsp, std::string name)
XmlSerializer & text(std::string txt)
void setPrefix(std::string prefix, std::string nsp)
void startDocument(std::string encoding, bool standalone)
XmlSerializer & attribute(std::string nsp, std::string name, std::string value)
XmlSerializer & endTag(std::string nsp, std::string name)
const std::string SchemaInstaceUri