bool RegexUtil::replace(string& text, const string& pattern, const string& with, const bool& matchNewLine/* = false*/) { string ttext(text); string rettxt; regex_t regex; getRegex(regex, pattern, matchNewLine); regmatch_t pm; int reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); while (reti == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec() finds the next match */ if(!reti) { string match; match = ttext.substr(pm.rm_so, pm.rm_eo-pm.rm_so); rettxt += ttext.substr(0, pm.rm_so) + with; } else { rettxt += ttext; break; } ttext = ttext.substr(pm.rm_eo); pm.rm_eo = -1; pm.rm_so = -1; reti = regexec (®ex, ttext.c_str(), 1, &pm, 0); } if(!cacheRegexes)regfree(®ex); if(ttext!="")rettxt += ttext; if(text==rettxt) return false; text = rettxt; while(replace(text, pattern, with, matchNewLine)) {} return true; }
vector<string> RegexUtil::search(const string& text, const string& pattern, const bool& matchNewLine/* = false*/) { vector<string> vec; string ttext(text); regex_t regex; getRegex(regex, pattern, matchNewLine); regmatch_t pm; int reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); while (reti == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec() finds the next match */ if(!reti) { string match; match = ttext.substr(pm.rm_so, pm.rm_eo-pm.rm_so); vec.push_back(match); } else { break; } ttext = ttext.substr(pm.rm_eo); pm.rm_eo = -1; pm.rm_so = -1; reti = regexec (®ex, ttext.c_str(), 1, &pm, 0); } if(!cacheRegexes)regfree(®ex); return vec; }
void NOTATIONDatatypeValidator::checkContent( const XMLCh* const content , ValidationContext* const context , bool asBase , MemoryManager* const manager ) { //validate against base validator if any NOTATIONDatatypeValidator *pBaseValidator = (NOTATIONDatatypeValidator*) this->getBaseValidator(); if (pBaseValidator) pBaseValidator->checkContent(content, context, true, manager); int thisFacetsDefined = getFacetsDefined(); // we check pattern first if ( (thisFacetsDefined & DatatypeValidator::FACET_PATTERN ) != 0 ) { if (getRegex()->matches(content, manager) ==false) { ThrowXMLwithMemMgr2(InvalidDatatypeValueException , XMLExcepts::VALUE_NotMatch_Pattern , content , getPattern() , manager); } } // if this is a base validator, we only need to check pattern facet // all other facet were inherited by the derived type if (asBase) return; checkValueSpace(content, manager); if ((thisFacetsDefined & DatatypeValidator::FACET_ENUMERATION) != 0 && (getEnumeration() != 0)) { XMLCh* normContent = XMLString::replicate(content, manager); ArrayJanitor<XMLCh> jan(normContent, manager); normalizeContent(normContent, manager); int i=0; int enumLength = getEnumeration()->size(); for ( ; i < enumLength; i++) { if (XMLString::equals(normContent, getEnumeration()->elementAt(i))) break; } if (i == enumLength) ThrowXMLwithMemMgr1(InvalidDatatypeValueException, XMLExcepts::VALUE_NotIn_Enumeration, content, manager); } checkAdditionalFacet(content, manager); }
bool RegexUtil::matches(const string& text, const string& pattern, const bool& matchNewLine/* = false*/) { string ttext(text); regex_t regex; getRegex(regex, pattern, matchNewLine); regmatch_t pm; int reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); if(!cacheRegexes)regfree(®ex); if (reti == 0) { /* while matches found */ return true; } return false; }
bool DocumentSearch::searchDocument(bool isForward) { if(m_textCursor->isNull()) { *m_textCursor = YFileCursor(); } QRegExp regex(getRegex()); YFileCursor match = m_document->find(regex, *m_textCursor, isForward); *m_textCursor = match; return !match.isNull(); }
int RegexUtil::find(const string& text, const string& pattern, const bool& matchNewLine/* = false*/) { string ttext(text); regex_t regex; getRegex(regex, pattern, matchNewLine); regmatch_t pm; int reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); if(!cacheRegexes)regfree(®ex); if (reti == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec() finds the next match */ return pm.rm_so; } return -1; }
// ----------------------------------------------------------------------- // Abstract interface from AbstractNumericValidator // ----------------------------------------------------------------------- void FloatDatatypeValidator::checkContent(const XMLCh* const content , ValidationContext* const context , bool asBase , MemoryManager* const manager) { //validate against base validator if any FloatDatatypeValidator *pBase = (FloatDatatypeValidator*) this->getBaseValidator(); if (pBase) pBase->checkContent(content, context, true, manager); // we check pattern first if ( (getFacetsDefined() & DatatypeValidator::FACET_PATTERN ) != 0 ) { if (getRegex()->matches(content, manager) ==false) { ThrowXMLwithMemMgr2(InvalidDatatypeValueException , XMLExcepts::VALUE_NotMatch_Pattern , content , getPattern() , manager); } } // if this is a base validator, we only need to check pattern facet // all other facet were inherited by the derived type if (asBase) return; XMLFloat theValue(content, manager); XMLFloat *theData = &theValue; if (getEnumeration() != 0) { int i=0; int enumLength = getEnumeration()->size(); for ( ; i < enumLength; i++) { if (compareValues(theData, (XMLFloat*) getEnumeration()->elementAt(i))==0) break; } if (i == enumLength) ThrowXMLwithMemMgr1(InvalidDatatypeValueException, XMLExcepts::VALUE_NotIn_Enumeration, content, manager); } boundsCheck(theData, manager); }
vector<string> RegexUtil::findWithGroups(const string& text, const string& pattern, const int& groupCount, const bool& matchNewLine /*= false*/) { vector<string> data; string ttext(text); regex_t regex; getRegex(regex, pattern, matchNewLine); regmatch_t pm[groupCount]; int reti = regexec(®ex, ttext.c_str(), groupCount, pm, 0); if(!cacheRegexes)regfree(®ex); if (reti == 0) { /* while matches found */ for (int i = 0; pm[i].rm_so != -1; i++) { string co = ttext.substr(pm[i].rm_so, pm[i].rm_eo-pm[i].rm_so); data.push_back(co); } } return data; }
void BooleanDatatypeValidator::checkContent( const XMLCh* const content , ValidationContext* const context , bool asBase , MemoryManager* const manager) { //validate against base validator if any BooleanDatatypeValidator *pBaseValidator = (BooleanDatatypeValidator*) this->getBaseValidator(); if (pBaseValidator !=0) pBaseValidator->checkContent(content, context, true, manager); // we check pattern first if ( (getFacetsDefined() & DatatypeValidator::FACET_PATTERN ) != 0 ) { if (getRegex()->matches(content, manager) ==false) { ThrowXMLwithMemMgr2(InvalidDatatypeValueException , XMLExcepts::VALUE_NotMatch_Pattern , content , getPattern() , manager); } } // if this is a base validator, we only need to check pattern facet // all other facet were inherited by the derived type if (asBase) return; unsigned int i = 0; for ( ; i < XMLUni::fgBooleanValueSpaceArraySize; i++ ) { if ( XMLString::equals(content, XMLUni::fgBooleanValueSpace[i])) break; } if (i == XMLUni::fgBooleanValueSpaceArraySize) ThrowXMLwithMemMgr2(InvalidDatatypeValueException , XMLExcepts::VALUE_Invalid_Name , content , SchemaSymbols::fgDT_BOOLEAN , manager); //Not valid boolean type }
// // here content is a list of items // void ListDatatypeValidator::checkContent( BaseRefVectorOf<XMLCh>* tokenVector , const XMLCh* const content , ValidationContext* const context , bool asBase , MemoryManager* const manager) { DatatypeValidator* bv = getBaseValidator(); if (bv->getType() == DatatypeValidator::List) ((ListDatatypeValidator*)bv)->checkContent(tokenVector, content, context, true, manager); else { // the ultimate itemType DTV for (unsigned int i = 0; i < tokenVector->size(); i++) bv->validate(tokenVector->elementAt(i), context, manager); } int thisFacetsDefined = getFacetsDefined(); // we check pattern first if ( (thisFacetsDefined & DatatypeValidator::FACET_PATTERN ) != 0 ) { // lazy construction if (getRegex() == 0) { try { setRegex(new (fMemoryManager) RegularExpression(getPattern(), SchemaSymbols::fgRegEx_XOption, fMemoryManager)); } catch (XMLException &e) { ThrowXMLwithMemMgr1(InvalidDatatypeValueException, XMLExcepts::RethrowError, e.getMessage(), manager); } } //check every item in the list as a whole if (getRegex()->matches(content, manager) == false) { ThrowXMLwithMemMgr2(InvalidDatatypeValueException , XMLExcepts::VALUE_NotMatch_Pattern , content , getPattern() , manager); } } // if this is a base validator, we only need to check pattern facet // all other facet were inherited by the derived type if (asBase) return; unsigned int tokenNumber = tokenVector->size(); if (((thisFacetsDefined & DatatypeValidator::FACET_MAXLENGTH) != 0) && (tokenNumber > getMaxLength())) { XMLCh value1[BUF_LEN+1]; XMLCh value2[BUF_LEN+1]; XMLString::binToText(tokenNumber, value1, BUF_LEN, 10, manager); XMLString::binToText(getMaxLength(), value2, BUF_LEN, 10, manager); ThrowXMLwithMemMgr3(InvalidDatatypeValueException , XMLExcepts::VALUE_GT_maxLen , getContent() , value1 , value2 , manager); } if (((thisFacetsDefined & DatatypeValidator::FACET_MINLENGTH) != 0) && (tokenNumber < getMinLength())) { XMLCh value1[BUF_LEN+1]; XMLCh value2[BUF_LEN+1]; XMLString::binToText(tokenNumber, value1, BUF_LEN, 10, manager); XMLString::binToText(getMinLength(), value2, BUF_LEN, 10, manager); ThrowXMLwithMemMgr3(InvalidDatatypeValueException , XMLExcepts::VALUE_LT_minLen , getContent() , value1 , value2 , manager); } if (((thisFacetsDefined & DatatypeValidator::FACET_LENGTH) != 0) && (tokenNumber != AbstractStringValidator::getLength())) { XMLCh value1[BUF_LEN+1]; XMLCh value2[BUF_LEN+1]; XMLString::binToText(tokenNumber, value1, BUF_LEN, 10, manager); XMLString::binToText(AbstractStringValidator::getLength(), value2, BUF_LEN, 10, manager); ThrowXMLwithMemMgr3(InvalidDatatypeValueException , XMLExcepts::VALUE_NE_Len , getContent() , value1 , value2 , manager); } if ((thisFacetsDefined & DatatypeValidator::FACET_ENUMERATION) != 0 && (getEnumeration() != 0)) { int i; int enumLength = getEnumeration()->size(); for ( i = 0; i < enumLength; i++) { //optimization: we do a lexical comparision first // this may be faster for string and its derived if (XMLString::equals(getEnumeration()->elementAt(i), getContent())) break; // a match found // do a value space check // this is needed for decimal (and probably other types // such as datetime related) // eg. // tokenVector = "1 2 3.0 4" vs enumeration = "1 2 3 4.0" // if (valueSpaceCheck(tokenVector, getEnumeration()->elementAt(i), manager)) break; } if (i == enumLength) ThrowXMLwithMemMgr1(InvalidDatatypeValueException, XMLExcepts::VALUE_NotIn_Enumeration, getContent(), manager); } // enumeration }
// ----------------------------------------------------------------------- // Abstract interface from AbstractNumericValidator // ----------------------------------------------------------------------- void DecimalDatatypeValidator::checkContent(const XMLCh* const content , ValidationContext* const context , bool asBase , MemoryManager* const manager) { //validate against base validator if any DecimalDatatypeValidator *pBase = (DecimalDatatypeValidator*) this->getBaseValidator(); if (pBase) pBase->checkContent(content, context, true, manager); int thisFacetsDefined = getFacetsDefined(); // we check pattern first if ( (thisFacetsDefined & DatatypeValidator::FACET_PATTERN ) != 0 ) { // lazy construction if (getRegex() ==0) { try { // REVISIT: cargillmem fMemoryManager vs manager setRegex(new (fMemoryManager) RegularExpression(getPattern(), SchemaSymbols::fgRegEx_XOption, fMemoryManager)); } catch (XMLException &e) { ThrowXMLwithMemMgr1(InvalidDatatypeValueException, XMLExcepts::RethrowError, e.getMessage(), manager); } } if (getRegex()->matches(content, manager) ==false) { ThrowXMLwithMemMgr2(InvalidDatatypeValueException , XMLExcepts::VALUE_NotMatch_Pattern , content , getPattern() , manager); } } // if this is a base validator, we only need to check pattern facet // all other facet were inherited by the derived type if (asBase) return; XMLCh *errorMsg = 0; try { XMLBigDecimal compareDataValue(content, manager); XMLBigDecimal* compareData = &compareDataValue; if (getEnumeration()) { int i=0; int enumLength = getEnumeration()->size(); for ( ; i < enumLength; i++) { if (compareValues(compareData, (XMLBigDecimal*) getEnumeration()->elementAt(i)) ==0 ) break; } if (i == enumLength) ThrowXMLwithMemMgr1(InvalidDatatypeValueException, XMLExcepts::VALUE_NotIn_Enumeration, content, manager); } boundsCheck(compareData, manager); if ( (thisFacetsDefined & DatatypeValidator::FACET_FRACTIONDIGITS) != 0 ) { if ( compareData->getScale() > fFractionDigits ) { XMLCh value1[BUF_LEN+1]; XMLCh value2[BUF_LEN+1]; XMLString::binToText(compareData->getScale(), value1, BUF_LEN, 10, manager); XMLString::binToText(fFractionDigits, value2, BUF_LEN, 10, manager); ThrowXMLwithMemMgr3(InvalidDatatypeFacetException , XMLExcepts::VALUE_exceed_fractDigit , compareData->getRawData() , value1 , value2 , manager); } } if ( (thisFacetsDefined & DatatypeValidator::FACET_TOTALDIGITS) != 0 ) { if ( compareData->getTotalDigit() > fTotalDigits ) { XMLCh value1[BUF_LEN+1]; XMLCh value2[BUF_LEN+1]; XMLString::binToText(compareData->getTotalDigit(), value1, BUF_LEN, 10, manager); XMLString::binToText(fTotalDigits, value2, BUF_LEN, 10, manager); ThrowXMLwithMemMgr3(InvalidDatatypeFacetException , XMLExcepts::VALUE_exceed_totalDigit , compareData->getRawData() , value1 , value2 , manager); } /*** E2-44 totalDigits ... by restricting it to numbers that are expressible as i � 10^-n where i and n are integers such that |i| < 10^totalDigits and 0 <= n <= totalDigits. ***/ if ( compareData->getScale() > fTotalDigits ) { XMLCh value1[BUF_LEN+1]; XMLCh value2[BUF_LEN+1]; XMLString::binToText(compareData->getScale(), value1, BUF_LEN, 10, manager); XMLString::binToText(fTotalDigits, value2, BUF_LEN, 10, manager); ThrowXMLwithMemMgr3(InvalidDatatypeFacetException , XMLExcepts::VALUE_exceed_totalDigit , compareData->getRawData() , value1 , value2 , manager); } } } catch (XMLException &e) { errorMsg = XMLString::replicate(e.getMessage(), manager); } if(errorMsg) { ArrayJanitor<XMLCh> jan(errorMsg, manager); ThrowXMLwithMemMgr1(InvalidDatatypeFacetException, XMLExcepts::RethrowError, errorMsg, manager); } }
// // 1) the bottom level UnionDTV would check against // pattern and enumeration as well // 2) each UnionDTV(s) above the bottom level UnionDTV and // below the native UnionDTV (the top level DTV) // would check against pattern only. // 3) the natvie Union DTV (the top level DTV) would invoke // memberTypeValidator to validate // void UnionDatatypeValidator::checkContent(const XMLCh* const content , ValidationContext* const context , bool asBase , MemoryManager* const manager) { DatatypeValidator* bv = getBaseValidator(); if (bv) ((UnionDatatypeValidator*)bv)->checkContent(content, context, true, manager); else { // 3) native union type // check content against each member type validator in Union // report an error only in case content is not valid against all member datatypes. // bool memTypeValid = false; for ( unsigned int i = 0; i < fMemberTypeValidators->size(); ++i ) { if ( memTypeValid ) break; try { fMemberTypeValidators->elementAt(i)->validate(content, context, manager); memTypeValid = true; //set the validator of the type actually used to validate the content DatatypeValidator *dtv = fMemberTypeValidators->elementAt(i); fValidatedDatatype = dtv; // context will be null during schema construction if(context) context->setValidatingMemberType(dtv); } catch (XMLException&) { //absorbed } } // for if ( !memTypeValid ) { ThrowXMLwithMemMgr1(InvalidDatatypeValueException , XMLExcepts::VALUE_no_match_memberType , content , manager); //( "Content '"+content+"' does not match any union types" ); } } // 1) and 2). we check pattern first if ( (getFacetsDefined() & DatatypeValidator::FACET_PATTERN ) != 0 ) { if (getRegex()->matches(content, manager) == false) { ThrowXMLwithMemMgr2(InvalidDatatypeValueException , XMLExcepts::VALUE_NotMatch_Pattern , content , getPattern() , manager); } } // if this is a base validator, we only need to check pattern facet // all other facet were inherited by the derived type if (asBase) return; if ((getFacetsDefined() & DatatypeValidator::FACET_ENUMERATION) != 0 && (getEnumeration() != 0)) { // If the content match (compare equal) any enumeration with // any of the member types, it is considerd valid. // RefVectorOf<DatatypeValidator>* memberDTV = getMemberTypeValidators(); RefArrayVectorOf<XMLCh>* tmpEnum = getEnumeration(); unsigned int memberTypeNumber = memberDTV->size(); unsigned int enumLength = tmpEnum->size(); for ( unsigned int memberIndex = 0; memberIndex < memberTypeNumber; ++memberIndex) { for ( unsigned int enumIndex = 0; enumIndex < enumLength; ++enumIndex) { try { if (memberDTV->elementAt(memberIndex)->compare(content, tmpEnum->elementAt(enumIndex), manager) == 0) return; } catch (XMLException&) { //absorbed } } // for enumIndex } // for memberIndex ThrowXMLwithMemMgr1(InvalidDatatypeValueException, XMLExcepts::VALUE_NotIn_Enumeration, content, manager); } // enumeration }
void DateTimeValidator::checkContent(const XMLCh* const content , ValidationContext* const context , bool asBase , MemoryManager* const manager) { //validate against base validator if any DateTimeValidator *pBaseValidator = (DateTimeValidator*) this->getBaseValidator(); if (pBaseValidator) pBaseValidator->checkContent(content, context, true, manager); int thisFacetsDefined = getFacetsDefined(); // we check pattern first if ( (thisFacetsDefined & DatatypeValidator::FACET_PATTERN ) != 0 ) { if (getRegex()->matches(content, manager) ==false) { ThrowXMLwithMemMgr2(InvalidDatatypeValueException , XMLExcepts::VALUE_NotMatch_Pattern , content , getPattern() , manager); } } // if this is a base validator, we only need to check pattern facet // all other facet were inherited by the derived type if (asBase) return; // the derived classes' parse() method constructs an // XMLDateTime object anc invokes appropriate XMLDateTime's // parser to parse the content. XMLDateTime dateTimeValue(content, manager); XMLDateTime* dateTime = &dateTimeValue; parse(dateTime); // must be < MaxExclusive if ((thisFacetsDefined & DatatypeValidator::FACET_MAXEXCLUSIVE) != 0) { if (compareValues(dateTime, getMaxExclusive()) != XMLDateTime::LESS_THAN) { REPORT_VALUE_ERROR( dateTime , getMaxExclusive() , XMLExcepts::VALUE_exceed_maxExcl , manager) } } // must be <= MaxInclusive if ((thisFacetsDefined & DatatypeValidator::FACET_MAXINCLUSIVE) != 0) { int result = compareValues(dateTime, getMaxInclusive()); if ( result == XMLDateTime::GREATER_THAN || result == XMLDateTime::INDETERMINATE ) { REPORT_VALUE_ERROR( dateTime , getMaxInclusive() , XMLExcepts::VALUE_exceed_maxIncl , manager) } }