void daeDocument::addExternalReference( daeURI &uri ) { if ( uri.getContainer() == NULL || uri.getContainer()->getDocument() != this ) { return; } daeURI tempURI( *dae, uri.getURI(), true ); // Remove fragment referencedDocuments.appendUnique( tempURI.getURI() ); }
void daeURIResolver::attemptResolveURI(daeURI& uri) { int i,cnt = (int)_KnownResolvers.getCount(); daeBool foundProtocol = false; for(i=0;i<cnt;i++) if (_KnownResolvers[i]->isProtocolSupported(uri.getProtocol())) { foundProtocol = true; if (_KnownResolvers[i]->resolveURI(uri)) return; } #if defined(_DEBUG) && defined(WIN32) fprintf(stderr, "daeURIResolver::attemptResolveURI(%s) - failed\n", uri.getURI()); #endif if (!foundProtocol) { uri.setState(daeURI::uri_failed_unsupported_protocol); #if defined(_DEBUG) && defined(WIN32) fprintf(stderr,"**protocol '%s' is not supported**\n",uri.getProtocol()); fflush(stderr); #endif } else { #if defined(_DEBUG) && defined(WIN32) fprintf(stderr,"**file(%s/%s) or id(%s) failed to resolve\n", uri.getFilepath(),uri.getFile(),uri.getID()); fflush(stderr); #endif } }
daeInt daeTinyXMLPlugin::write(const daeURI& name, daeDocument *document, daeBool replace) { // Make sure database and document are both set if (!database) return DAE_ERR_INVALID_CALL; if(!document) return DAE_ERR_COLLECTION_DOES_NOT_EXIST; string fileName = cdom::uriToNativePath(name.str()); if (fileName.empty()) { daeErrorHandler::get()->handleError( "can't get path in write\n" ); return DAE_ERR_BACKEND_IO; } // If replace=false, don't replace existing files if(!replace) { // Using "stat" would be better, but it's not available on all platforms FILE *tempfd = fopen(fileName.c_str(), "r"); if(tempfd != NULL) { // File exists, return error fclose(tempfd); return DAE_ERR_BACKEND_FILE_EXISTS; } fclose(tempfd); } m_doc = new TiXmlDocument(name.getURI()); if (m_doc) { m_doc->SetTabSize(4); TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); m_doc->LinkEndChild( decl ); writeElement(document->getDomRoot()); // TODO check if writing to zae // if( 0 ) { // // Declare a printer // TiXmlPrinter printer; // // // attach it to the document you want to convert in to a std::string // m_doc->Accept(&printer); // // // Create a std::string and copy your document data in to the string // std::string str = printer.CStr(); // // // compress str and size to fileName // // } m_doc->SaveFile(fileName.c_str()); delete m_doc; m_doc = NULL; } return DAE_OK; }
daeInt daeTinyXMLPlugin::write(const daeURI& name, daeDocument *document, daeBool replace) { // Make sure database and document are both set if (!database) return DAE_ERR_INVALID_CALL; if(!document) return DAE_ERR_COLLECTION_DOES_NOT_EXIST; string fileName = cdom::uriToNativePath(name.str()); if (fileName.empty()) { daeErrorHandler::get()->handleError( "can't get path in write\n" ); return DAE_ERR_BACKEND_IO; } // If replace=false, don't replace existing files if(!replace) { // Using "stat" would be better, but it's not available on all platforms FILE *tempfd = fopen(fileName.c_str(), "r"); if(tempfd != NULL) { // File exists, return error fclose(tempfd); return DAE_ERR_BACKEND_FILE_EXISTS; } fclose(tempfd); } m_doc = new TiXmlDocument(name.getURI()); if (m_doc) { m_doc->SetTabSize(4); TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); m_doc->LinkEndChild( decl ); writeElement(document->getDomRoot()); m_doc->SaveFile(fileName.c_str()); delete m_doc; m_doc = NULL; } return DAE_OK; }
// This function needs to be re-entrant, it can be called recursively from inside of resolveAll // to load files that the first file depends on. daeInt daeLIBXMLPlugin::read(daeURI& uri, daeString docBuffer) { // Make sure topMeta has been set before proceeding if (topMeta == NULL) { return DAE_ERR_BACKEND_IO; } // Generate a version of the URI with the fragment removed daeURI fileURI(uri.getURI(),true); // Create the right type of xmlTextReader on the stack so this function can be re-entrant xmlTextReaderPtr reader; if(docBuffer) { // Load from memory (experimental) #if 0 //debug stuff printf("Reading %s from memory buffer\n", fileURI.getURI()); #endif reader = xmlReaderForDoc((xmlChar*)docBuffer, fileURI.getURI(), NULL,0); } else { // Load from URI #if 0 //debug stuff printf("Opening %s\n", fileURI.getURI()); #endif reader = xmlReaderForFile(fileURI.getURI(), NULL,0); } if(!reader) { printf( "no libxml2 reader\n"); return DAE_ERR_BACKEND_IO; } // Start parsing the file daeElementRef domObject = startParse(topMeta, reader); // Parsing done, free the xmlReader and error check to make sure we got a valid DOM back xmlFreeTextReader(reader); if (!domObject) { #if defined(_DEBUG) && defined(WIN32) fprintf(stderr,"daeLIBXMLPlugin::read(%s) failed - XML Parse Failed\n", fileURI.getFile()); fflush(stdout); #endif printf("not able to load\n"); return DAE_ERR_BACKEND_IO; } // Insert the document into the database, the Database will keep a ref on the main dom, so it won't gets deleted // until we clear the database daeDocument *document = NULL; int res = database->insertDocument(fileURI.getURI(),domObject,&document); if (res!= DAE_OK) return res; // Make a vector to store a list of the integration items that need to be processed later // postProcessDom will fill this in for us (this should probably not be done in the IOPlugin) std::vector<INTEGRATION_ITEM> intItems; //insert the elements into the database, for this DB the elements are the Collada object which have //an ID. //this function will fill the _integrationItems array as well postProcessDom(document, domObject, intItems); database->validate(); daeElement::resolveAll(); //create the integration objects int size = (int)intItems.size(); int i; for (i=0;i<size;i++) intItems[i].intObject->createFromChecked(intItems[i].element); for (i=0;i<size;i++) intItems[i].intObject->fromCOLLADAChecked(); for (i=0;i<size;i++) intItems[i].intObject->fromCOLLADAPostProcessChecked(); //clear the temporary integration items array intItems.clear(); return DAE_OK; }
void daeURI::setBaseURI(daeURI& uri) { ApplicationURI.reset(); ApplicationURI.setURI(uri.getURI()); }
daeBool daeLIBXMLResolver::resolveElement(daeURI& uri, daeString typeNameHint) { // Make sure the URI is validated if (uri.getState() == daeURI::uri_loaded) { uri.validate(); } daeElement* resolved = NULL; int status; // Does the URI have a document reference? if ( (uri.getFile() != NULL) && (strlen(uri.getFile())>0)) { // The URI contains a document reference, see if it is loaded and try to load it if it's not if (!_database->isDocumentLoaded(uri.getURI())) { if ( _loadExternalDocuments ) { _plugin->read(uri,NULL); } else { uri.setState( daeURI::uri_failed_external_document ); return false; } } // Try to find the id by searching this document only status = _database->getElement(&resolved,0,uri.getID(),typeNameHint,uri.getURI()); } else { // The URI was just a fragment, so try to find it in the document that contains it. // !!!GAC not sure if all these pointers will be set when we get here, so assert if any of them aren't daeElement *tempElement = uri.getContainer(); //assert(tempElement); daeDocument *tempDocument; if ( tempElement == NULL || (tempDocument = tempElement->getDocument()) == NULL ) { uri.setState(daeURI::uri_failed_missing_container); char msg[256]; sprintf(msg, "daeLIBXMLResolver::resolveElement() - failed to resolve %s\n", uri.getURI()); daeErrorHandler::get()->handleError( msg ); return false; } //assert(tempDocument); daeURI *tempURI = tempDocument->getDocumentURI(); //assert(tempURI); status = _database->getElement(&resolved,0,uri.getID(),typeNameHint,tempURI->getURI()); } uri.setElement(resolved); // Error if we didn't successfully resolve the uri if (status ||(resolved==NULL)) { uri.setState(daeURI::uri_failed_id_not_found); char msg[256]; sprintf(msg, "daeLIBXMLResolver::resolveElement() - failed to resolve %s\n", uri.getURI()); daeErrorHandler::get()->handleError( msg ); return false; } uri.setState(daeURI::uri_success); return true; }