Skip to content

Commit

Permalink
Implemented fallback for backward compatibility in offset validation (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Mar 19, 2021
1 parent 35a6cf5 commit 56afe68
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
4 changes: 2 additions & 2 deletions include/libfolia/folia_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ namespace folia {
return res;
}
FoliaElement *postappend();
FoliaElement *get_reference() const;
FoliaElement *get_reference(bool trim_spaces=true) const;
std::string ref() const { return _ref; };
private:
void init();
Expand All @@ -1376,7 +1376,7 @@ namespace folia {
TEXT_FLAGS = TEXT_FLAGS::NONE ) const;
int offset() const { return _offset; };
FoliaElement *postappend();
FoliaElement *get_reference() const;
FoliaElement *get_reference(bool trim_spaces=true) const;
std::string ref() const { return _ref; };
private:
void init();
Expand Down
34 changes: 32 additions & 2 deletions src/folia_document.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1714,13 +1714,28 @@ namespace folia {
string msg = "Text for " + txt->parent()->xmltag() + "(ID="
+ txt->parent()->id() + ", textclass='" + txt->cls()
+ "'), has incorrect offset " + TiCC::toString(offset);


string ref = txt->ref();
if ( !ref.empty() ){
msg += " or invalid reference:" + ref;
}
msg += "\n\toriginal msg=";
msg += e.what();
throw UnresolvableTextContent( msg );

bool warn = false;
try {
txt->get_reference(false); //trim_spaces = false
msg += "\nHowever, according to the older rules (<v2.4.1) the offsets are accepted. So we are treating this as a warning rather than an error. We do recommend fixing this if this is a document you intend to publish.";
warn = true;
} catch (UnresolvableTextContent& e2) {
msg += "\n(also checked against older rules prior to FoLiA v2.4.1)";
}

if (warn)
cerr << "WARNING: " << msg << endl;
else
throw UnresolvableTextContent( msg );
}
}
}
Expand All @@ -1739,13 +1754,28 @@ namespace folia {
string msg = "Phoneme for " + phon->parent()->xmltag() + ", ID="
+ phon->parent()->id() + ", textclass='" + phon->cls()
+ "', has incorrect offset " + TiCC::toString(offset);


string ref = phon->ref();
if ( !ref.empty() ){
msg += " or invalid reference:" + ref;
}
msg += "\n\toriginal msg=";
msg += e.what();
throw UnresolvableTextContent( msg );

bool warn = false;
try {
phon->get_reference(false); //trim_spaces = false
msg += "\nHowever, according to the older rules (<v2.4.1) the offsets are accepted. So we are treating this as a warning rather than an error. We do recommend fixing this if this is a document you intend to publish.";
warn = true;
} catch (UnresolvableTextContent& e2) {
msg += "\n(also checked against older rules prior to FoLiA v2.4.1)";
}

if (warn)
cerr << "WARNING: " << msg << endl;
else
throw UnresolvableTextContent( msg );
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/folia_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3895,8 +3895,8 @@ namespace folia {
return 0;
}

FoliaElement *TextContent::get_reference() const {
/// get the FoliaElement _ref is refering to
FoliaElement *TextContent::get_reference(bool trim_spaces) const {
/// get the FoliaElement _ref is refering to and does offset validation
/*!
* \return the refered element OR the default parent when _ref is 0
*/
Expand All @@ -3921,8 +3921,10 @@ namespace folia {
throw UnresolvableTextContent( "Reference (ID " + _ref + ") has no such text (class=" + cls() + ")" );
}
else if ( doc()->checktext() || doc()->fixtext() ){
UnicodeString mt = this->text( this->cls(), TEXT_FLAGS::STRICT );
UnicodeString pt = ref->text( this->cls(), TEXT_FLAGS::STRICT );
TEXT_FLAGS flags = TEXT_FLAGS::STRICT;
if ( !trim_spaces ) flags |= TEXT_FLAGS::NO_TRIM_SPACES;
UnicodeString mt = this->text( this->cls(), flags );
UnicodeString pt = ref->text( this->cls(), flags );
UnicodeString sub( pt, this->offset(), mt.length() );
if ( mt != sub ){
if ( doc()->fixtext() ){
Expand Down Expand Up @@ -3992,7 +3994,7 @@ namespace folia {
return 0;
}

FoliaElement *PhonContent::get_reference() const {
FoliaElement *PhonContent::get_reference(bool trim_spaces) const {
/// get the FoliaElement _ref is refering to
/*!
* \return the refered element OR the default parent when _ref is 0
Expand All @@ -4018,8 +4020,10 @@ namespace folia {
throw UnresolvableTextContent( "Reference (ID " + _ref + ") has no such phonetic content (class=" + cls() + ")" );
}
else if ( doc()->checktext() || doc()->fixtext() ){
UnicodeString mt = this->phon( this->cls() );
UnicodeString pt = ref->phon( this->cls() );
TEXT_FLAGS flags = TEXT_FLAGS::STRICT;
if ( !trim_spaces ) flags |= TEXT_FLAGS::NO_TRIM_SPACES;
UnicodeString mt = this->phon( this->cls(), flags );
UnicodeString pt = ref->phon( this->cls(), flags );
UnicodeString sub( pt, this->offset(), mt.length() );
if ( mt != sub ){
if ( doc()->fixtext() ){
Expand Down

0 comments on commit 56afe68

Please sign in to comment.