bool VRMLBodyLoader::load(BodyPtr body, const std::string& filename)
{
    body->clearDevices();
    body->clearExtraJoints();
    return impl->load(body.get(), filename);
}
bool BodyLoaderImpl::load(BodyPtr& body, const std::string& filename)
{
    bool result = false;

    filesystem::path orgpath(filename);
    string ext = getExtension(orgpath);
    string modelFilename;
    MappingPtr info;

    try {
        if(ext != "yaml"){
            modelFilename = filename;
        } else {
            YAMLReader parser;
            info = parser.loadDocument(filename)->toMapping();
            filesystem::path mpath(info->get("modelFile").toString());
            if(mpath.has_root_path()){
                modelFilename = getNativePathString(mpath);
            } else {
                modelFilename = getNativePathString(orgpath.parent_path() / mpath);
            }
            ext = getExtension(mpath);
        }

        LoaderMap::iterator p = loaderMap.find(ext);
        if(p != loaderMap.end()){
            loader = p->second;
        } else {
            boost::lock_guard<boost::mutex> lock(loaderFactoryMapMutex);
            LoaderFactoryMap::iterator q = loaderFactoryMap.find(ext);
            if(q != loaderFactoryMap.end()){
                LoaderFactory factory = q->second;
                loader = factory();
                loaderMap[ext] = loader;
            }
        }

        if(!loader){
            (*os) << str(boost::format(_("The file format of \"%1%\" is not supported by the body loader.\n"))
                         % getFilename(filesystem::path(modelFilename)));

        } else {
            loader->setMessageSink(*os);
            loader->setVerbose(isVerbose);
            loader->setShapeLoadingEnabled(isShapeLoadingEnabled);
            
            int dn = defaultDivisionNumber;
            if(info){
                Mapping& geometryInfo = *info->findMapping("geometry");
                if(geometryInfo.isValid()){
                    geometryInfo.read("divisionNumber", dn);
                }
            }
            if(dn > 0){
                loader->setDefaultDivisionNumber(dn);
            }

            if(defaultCreaseAngle >= 0.0){
                loader->setDefaultCreaseAngle(defaultCreaseAngle);
            }
            
            body->clearDevices();
            body->clearExtraJoints();
            if(info){
                body->resetInfo(info.get());
            } else {
                body->info()->clear();
            }

            result = loader->load(body, modelFilename);
        }
        
    } catch(const ValueNode::Exception& ex){
        (*os) << ex.message();
    } catch(const nonexistent_key_error& error){
        if(const std::string* message = boost::get_error_info<error_info_message>(error)){
            (*os) << *message;
        }
    } catch(const std::exception& ex){
        (*os) << ex.what();
    }
    os->flush();
    
    return result;
}