Skip to content

Commit 0dfb749

Browse files
committed
better check on wrefs
1 parent 04f7cdf commit 0dfb749

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

include/libfolia/folia_subclasses.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,8 @@ public: \
670670
friend std::vector<FoliaElement*> AbstractSpanAnnotation::wrefs() const;
671671
public:
672672
ADD_DEFAULT_CONSTRUCTORS( WordReference, AbstractWord );
673+
std::string tval() const { return _tval; };
674+
FoliaElement *ref() const { return _ref; };
673675
bool addable( const FoliaElement * ) const override;
674676
const bool& printable() const override {
675677
static bool t = true;
@@ -739,6 +741,8 @@ public: \
739741

740742
class AbstractSpanRole: public AbstractSpanAnnotation {
741743
// DO NOT USE AbstractSpanRole as a real node!!
744+
public:
745+
bool addable( const FoliaElement * ) const override;
742746
protected:
743747
ADD_PROTECTED_CONSTRUCTORS( AbstractSpanRole, AbstractSpanAnnotation );
744748
};

src/folia_subclasses.cxx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,41 @@ namespace folia {
10191019
}
10201020
}
10211021

1022+
bool AbstractSpanRole::addable( const FoliaElement *parent ) const {
1023+
/// test if a reference might succesfully appended to \em parent
1024+
/*!
1025+
* \param parent the node to check
1026+
* \return true if it doesn't throw
1027+
*
1028+
* \note It will allways throw an error, instead of returning false
1029+
*/
1030+
if ( !AbstractElement::addable( parent ) ){
1031+
return false;
1032+
}
1033+
// we must check wref children against the parent too
1034+
vector<WordReference*> wrefs = select<WordReference>();
1035+
for ( const auto& refs : wrefs ){
1036+
FoliaElement *ref = refs->ref();
1037+
string tval = refs->tval();
1038+
if ( !tval.empty() ){
1039+
string watt = ref->str(parent->textclass());
1040+
if ( watt.empty() ){
1041+
string msg = "no matching 't' value found in the '<w>' refered by "
1042+
"<wref id=\"" + ref->id() + "\" t=\""+ tval
1043+
+ "\"> for textclass '" + parent->textclass() + "'";
1044+
throw XmlError( this, msg );
1045+
}
1046+
else if ( watt != tval ){
1047+
string msg = "the 't' value of <wref id=\"" + ref->id()
1048+
+ "\" t=\""+ tval + "\"> for textclass '" + parent->textclass()
1049+
+ "' doesn't match any value of the refered word for that class";
1050+
throw XmlError( this, msg );
1051+
}
1052+
}
1053+
}
1054+
return true;
1055+
}
1056+
10221057
KWargs LinkReference::collectAttributes() const {
10231058
/// extract all Attribute-Value pairs for LinkReference
10241059
/*!
@@ -1461,6 +1496,7 @@ namespace folia {
14611496
if ( parent->isSubClass<AbstractSpanRole>() ){
14621497
// we should check the textclass of the layer above this.
14631498
// but due to recursion, it is not connected to that layer yet!
1499+
// this is checked later in AbstractSpanRole::addable( )
14641500
return true;
14651501
}
14661502
if ( !_tval.empty() ){

0 commit comments

Comments
 (0)