Put unused attributes in the property tree while parsing.
Store unhandled xml attributes in a subnode named _attr_ for each node on parsing propertylist files. This allows retrieving them at a later stage where there is no access to the source xml file any more.
This commit is contained in:
+122
-90
@@ -46,7 +46,6 @@ using std::endl;
|
|||||||
#define DEFAULT_MODE (SGPropertyNode::READ|SGPropertyNode::WRITE)
|
#define DEFAULT_MODE (SGPropertyNode::READ|SGPropertyNode::WRITE)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// Property list visitor, for XML parsing.
|
// Property list visitor, for XML parsing.
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
@@ -84,6 +83,12 @@ private:
|
|||||||
State () : node(0), type(""), mode(DEFAULT_MODE), omit(false) {}
|
State () : node(0), type(""), mode(DEFAULT_MODE), omit(false) {}
|
||||||
State (SGPropertyNode * _node, const char * _type, int _mode, bool _omit)
|
State (SGPropertyNode * _node, const char * _type, int _mode, bool _omit)
|
||||||
: node(_node), type(_type), mode(_mode), omit(_omit) {}
|
: node(_node), type(_type), mode(_mode), omit(_omit) {}
|
||||||
|
bool hasChildren() const
|
||||||
|
{
|
||||||
|
int n_children = node->nChildren();
|
||||||
|
return n_children > 1
|
||||||
|
|| (n_children == 1 && node->getChild(0)->getNameString() != "_attr_");
|
||||||
|
}
|
||||||
SGPropertyNode * node;
|
SGPropertyNode * node;
|
||||||
string type;
|
string type;
|
||||||
int mode;
|
int mode;
|
||||||
@@ -135,19 +140,20 @@ PropsVisitor::endXML ()
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check a yes/no flag, with default.
|
* Set/unset a yes/no flag.
|
||||||
*/
|
*/
|
||||||
static bool
|
static void
|
||||||
checkFlag (const char * flag, const sg_location& location,
|
setFlag( int& mode,
|
||||||
bool defaultState = true)
|
int mask,
|
||||||
|
const std::string& flag,
|
||||||
|
const sg_location& location )
|
||||||
{
|
{
|
||||||
if (flag == 0)
|
if( flag == "y" )
|
||||||
return defaultState;
|
mode |= mask;
|
||||||
else if (!strcmp(flag, "y"))
|
else if( flag == "n" )
|
||||||
return true;
|
mode &= ~mask;
|
||||||
else if (!strcmp(flag, "n"))
|
else
|
||||||
return false;
|
{
|
||||||
else {
|
|
||||||
string message = "Unrecognized flag value '";
|
string message = "Unrecognized flag value '";
|
||||||
message += flag;
|
message += flag;
|
||||||
message += '\'';
|
message += '\'';
|
||||||
@@ -170,7 +176,7 @@ PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
|
|||||||
throw sg_io_exception(message, location, "SimGear Property Reader");
|
throw sg_io_exception(message, location, "SimGear Property Reader");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for an include.
|
// Check for an include.
|
||||||
attval = atts.getValue("include");
|
attval = atts.getValue("include");
|
||||||
if (attval != 0) {
|
if (attval != 0) {
|
||||||
try {
|
try {
|
||||||
@@ -193,7 +199,8 @@ PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
|
|||||||
|
|
||||||
else {
|
else {
|
||||||
State &st = state();
|
State &st = state();
|
||||||
// Get the index.
|
|
||||||
|
// Get the index.
|
||||||
attval = atts.getValue("n");
|
attval = atts.getValue("n");
|
||||||
int index = 0;
|
int index = 0;
|
||||||
string strName(name);
|
string strName(name);
|
||||||
@@ -205,7 +212,7 @@ PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
|
|||||||
st.counters[strName]++;
|
st.counters[strName]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Got the index, so grab the node.
|
// Got the index, so grab the node.
|
||||||
SGPropertyNode * node = st.node->getChild(strName, index, true);
|
SGPropertyNode * node = st.node->getChild(strName, index, true);
|
||||||
if (!node->getAttribute(SGPropertyNode::WRITE)) {
|
if (!node->getAttribute(SGPropertyNode::WRITE)) {
|
||||||
SG_LOG(SG_INPUT, SG_ALERT, "Not overwriting write-protected property "
|
SG_LOG(SG_INPUT, SG_ALERT, "Not overwriting write-protected property "
|
||||||
@@ -213,69 +220,90 @@ PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
|
|||||||
node = &null;
|
node = &null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the access-mode attributes,
|
// TODO use correct default mode (keep for now to match past behavior)
|
||||||
// but don't set yet (in case they
|
int mode = _default_mode | SGPropertyNode::READ | SGPropertyNode::WRITE;
|
||||||
// prevent us from recording the value).
|
int omit = false;
|
||||||
int mode = _default_mode;
|
const char* type = 0;
|
||||||
|
|
||||||
attval = atts.getValue("read");
|
SGPropertyNode* attr_node = NULL;
|
||||||
if (checkFlag(attval, location, true))
|
|
||||||
mode |= SGPropertyNode::READ;
|
|
||||||
attval = atts.getValue("write");
|
|
||||||
if (checkFlag(attval, location, true))
|
|
||||||
mode |= SGPropertyNode::WRITE;
|
|
||||||
attval = atts.getValue("archive");
|
|
||||||
if (checkFlag(attval, location, false))
|
|
||||||
mode |= SGPropertyNode::ARCHIVE;
|
|
||||||
attval = atts.getValue("trace-read");
|
|
||||||
if (checkFlag(attval, location, false))
|
|
||||||
mode |= SGPropertyNode::TRACE_READ;
|
|
||||||
attval = atts.getValue("trace-write");
|
|
||||||
if (checkFlag(attval, location, false))
|
|
||||||
mode |= SGPropertyNode::TRACE_WRITE;
|
|
||||||
attval = atts.getValue("userarchive");
|
|
||||||
if (checkFlag(attval, location, false))
|
|
||||||
mode |= SGPropertyNode::USERARCHIVE;
|
|
||||||
attval = atts.getValue("preserve");
|
|
||||||
if (checkFlag(attval, location, false))
|
|
||||||
mode |= SGPropertyNode::PRESERVE;
|
|
||||||
|
|
||||||
// Check for an alias.
|
for(int i = 0; i < atts.size(); ++i)
|
||||||
attval = atts.getValue("alias");
|
{
|
||||||
if (attval != 0) {
|
const std::string att_name = atts.getName(i);
|
||||||
if (!node->alias(attval))
|
const std::string val = atts.getValue(i);
|
||||||
SG_LOG(SG_INPUT, SG_ALERT, "Failed to set alias to " << attval
|
|
||||||
<< "\n at " << location.asString());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for an include.
|
// Get the access-mode attributes,
|
||||||
bool omit = false;
|
// but don't set yet (in case they
|
||||||
attval = atts.getValue("include");
|
// prevent us from recording the value).
|
||||||
if (attval != 0) {
|
if( att_name == "read" )
|
||||||
try {
|
setFlag(mode, SGPropertyNode::READ, val, location);
|
||||||
SGPath path = simgear::ResourceManager::instance()->findPath(attval, SGPath(_base).dir());
|
else if( att_name == "write" )
|
||||||
if (path.isNull())
|
setFlag(mode, SGPropertyNode::WRITE, val, location);
|
||||||
{
|
else if( att_name == "archive" )
|
||||||
string message ="Cannot open file ";
|
setFlag(mode, SGPropertyNode::ARCHIVE, val, location);
|
||||||
message += attval;
|
else if( att_name == "trace-read" )
|
||||||
throw sg_io_exception(message, location,
|
setFlag(mode, SGPropertyNode::TRACE_READ, val, location);
|
||||||
"SimGear Property Reader");
|
else if( att_name == "trace-write" )
|
||||||
}
|
setFlag(mode, SGPropertyNode::TRACE_WRITE, val, location);
|
||||||
readProperties(path.str(), node, 0, _extended);
|
else if( att_name == "userarchive" )
|
||||||
} catch (sg_io_exception &e) {
|
setFlag(mode, SGPropertyNode::USERARCHIVE, val, location);
|
||||||
setException(e);
|
else if( att_name == "preserve" )
|
||||||
|
setFlag(mode, SGPropertyNode::PRESERVE, val, location);
|
||||||
|
|
||||||
|
// Check for an alias.
|
||||||
|
else if( att_name == "alias" )
|
||||||
|
{
|
||||||
|
if( !node->alias(val) )
|
||||||
|
SG_LOG
|
||||||
|
(
|
||||||
|
SG_INPUT,
|
||||||
|
SG_ALERT,
|
||||||
|
"Failed to set alias to " << val << "\n at " << location.asString()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
attval = atts.getValue("omit-node");
|
// Check for an include.
|
||||||
omit = checkFlag(attval, location, false);
|
else if( att_name == "include" )
|
||||||
}
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SGPath path = simgear::ResourceManager::instance()
|
||||||
|
->findPath(val, SGPath(_base).dir());
|
||||||
|
if (path.isNull())
|
||||||
|
{
|
||||||
|
string message ="Cannot open file ";
|
||||||
|
message += val;
|
||||||
|
throw sg_io_exception(message, location, "SimGear Property Reader");
|
||||||
|
}
|
||||||
|
readProperties(path.str(), node, 0, _extended);
|
||||||
|
}
|
||||||
|
catch (sg_io_exception &e)
|
||||||
|
{
|
||||||
|
setException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const char *type = atts.getValue("type");
|
else if( att_name == "omit-node" )
|
||||||
// if a type is given and the node is tied,
|
setFlag(omit, 1, val, location);
|
||||||
// don't clear the value because
|
else if( att_name == "type" )
|
||||||
// clearValue() unties the property
|
{
|
||||||
if (type && false == node->isTied() )
|
type = atts.getValue(i);
|
||||||
node->clearValue();
|
|
||||||
|
// if a type is given and the node is tied,
|
||||||
|
// don't clear the value because
|
||||||
|
// clearValue() unties the property
|
||||||
|
if( !node->isTied() )
|
||||||
|
node->clearValue();
|
||||||
|
}
|
||||||
|
else if( att_name != "n" )
|
||||||
|
{
|
||||||
|
// Store all additional attributes in a special node named _attr_
|
||||||
|
if( !attr_node )
|
||||||
|
attr_node = node->getChild("_attr_", 0, true);
|
||||||
|
|
||||||
|
attr_node->setUnspecifiedValue(att_name.c_str(), val.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
push_state(node, type, mode, omit);
|
push_state(node, type, mode, omit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -287,14 +315,15 @@ PropsVisitor::endElement (const char * name)
|
|||||||
bool ret;
|
bool ret;
|
||||||
const sg_location location(getPath(), getLine(), getColumn());
|
const sg_location location(getPath(), getLine(), getColumn());
|
||||||
|
|
||||||
// If there are no children and it's
|
// If there are no children and it's
|
||||||
// not an alias, then it's a leaf value.
|
// not an alias, then it's a leaf value.
|
||||||
if (st.node->nChildren() == 0 && !st.node->isAlias()) {
|
if( !st.hasChildren() && !st.node->isAlias() )
|
||||||
|
{
|
||||||
if (st.type == "bool") {
|
if (st.type == "bool") {
|
||||||
if (_data == "true" || atoi(_data.c_str()) != 0)
|
if (_data == "true" || atoi(_data.c_str()) != 0)
|
||||||
ret = st.node->setBoolValue(true);
|
ret = st.node->setBoolValue(true);
|
||||||
else
|
else
|
||||||
ret = st.node->setBoolValue(false);
|
ret = st.node->setBoolValue(false);
|
||||||
} else if (st.type == "int") {
|
} else if (st.type == "int") {
|
||||||
ret = st.node->setIntValue(atoi(_data.c_str()));
|
ret = st.node->setIntValue(atoi(_data.c_str()));
|
||||||
} else if (st.type == "long") {
|
} else if (st.type == "long") {
|
||||||
@@ -319,19 +348,24 @@ PropsVisitor::endElement (const char * name)
|
|||||||
string message = "Unrecognized data type '";
|
string message = "Unrecognized data type '";
|
||||||
message += st.type;
|
message += st.type;
|
||||||
message += '\'';
|
message += '\'';
|
||||||
// FIXME: add location information
|
// FIXME: add location information
|
||||||
throw sg_io_exception(message, location, "SimGear Property Reader");
|
throw sg_io_exception(message, location, "SimGear Property Reader");
|
||||||
}
|
}
|
||||||
if (!ret)
|
if( !ret )
|
||||||
SG_LOG(SG_INPUT, SG_ALERT, "readProperties: Failed to set "
|
SG_LOG
|
||||||
<< st.node->getPath() << " to value \""
|
(
|
||||||
<< _data << "\" with type " << st.type << "\n at "
|
SG_INPUT,
|
||||||
<< location.asString());
|
SG_ALERT,
|
||||||
|
"readProperties: Failed to set " << st.node->getPath()
|
||||||
|
<< " to value \"" << _data
|
||||||
|
<< "\" with type " << st.type
|
||||||
|
<< "\n at " << location.asString()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the access-mode attributes now,
|
// Set the access-mode attributes now,
|
||||||
// once the value has already been
|
// once the value has already been
|
||||||
// assigned.
|
// assigned.
|
||||||
st.node->setAttributes(st.mode);
|
st.node->setAttributes(st.mode);
|
||||||
|
|
||||||
if (st.omit) {
|
if (st.omit) {
|
||||||
@@ -353,7 +387,7 @@ PropsVisitor::endElement (const char * name)
|
|||||||
void
|
void
|
||||||
PropsVisitor::data (const char * s, int length)
|
PropsVisitor::data (const char * s, int length)
|
||||||
{
|
{
|
||||||
if (state().node->nChildren() == 0)
|
if( !state().hasChildren() )
|
||||||
_data.append(string(s, length));
|
_data.append(string(s, length));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,7 +399,6 @@ PropsVisitor::warning (const char * message, int line, int column)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// Property list reader.
|
// Property list reader.
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
@@ -426,7 +459,7 @@ void readProperties (const char *buf, const int size,
|
|||||||
throw visitor.getException();
|
throw visitor.getException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// Property list writer.
|
// Property list writer.
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
@@ -634,7 +667,6 @@ writeProperties (const char* file, const SGPropertyNode * start_node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// Copy properties from one tree to another.
|
// Copy properties from one tree to another.
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Reference in New Issue
Block a user