E57 Foundation API v1.1.312
Aug. 10, 2011
|
example: register use of extended element names More...
Functions | |
int | main (int, char **) |
Example use of E57 extension functions. |
example: register use of extended element names
Also see listing at end of this page for source without line numbers (to cut&paste from).
00001 /*** Extensions.cpp example: register use of extended element names */ 00004 #include <iostream> 00005 #include "E57Foundation.h" 00006 using namespace e57; 00007 using namespace std; 00008 00010 int main(int /*argc*/, char** /*argv*/) { 00011 try { 00012 ImageFile imf("temp._e57", "w"); 00013 00014 imf.extensionsAdd("ext1", "http://www.example.com/MyExtension1"); 00015 00016 ustring uri; 00017 if (!imf.extensionsLookupPrefix("ext1", uri)) 00018 cout << "Oops, ext1 should have been defined" << endl; 00019 else 00020 cout << "ext1 is prefix for uri=" << uri << endl; 00021 00022 ustring prefix; 00023 if (!imf.extensionsLookupUri("http://www.example.com/MyExtension1", prefix)) 00024 cout << "Oops, URI http://www.example.com/MyExtension1 should have been defined" << endl; 00025 else 00026 cout << "URI http://www.example.com/MyExtension1 has prefix=" << prefix << endl; 00027 00028 cout << "Defined extensions:" << endl; 00029 for (int i = 0; i < imf.extensionsCount(); i++) 00030 cout << " prefix:" << imf.extensionsPrefix(i) << " URI:" << imf.extensionsUri(i) << endl; 00031 00032 imf.root().set("ext1:greeting", StringNode(imf, "Hello world.")); 00033 00034 imf.close(); // don't forget to explicitly close the ImageFile 00035 } catch(E57Exception& ex) { 00036 ex.report(__FILE__, __LINE__, __FUNCTION__); 00037 return(-1); 00038 } 00039 return(0); 00040 } 00041
This example program shows how to declare an E57 extension prefix and URI in an ImageFile. See the HelloWorld.cpp example for discussion of the use of include files, constructing an ImageFile, and the try/catch block to handle exceptions.
In source line 12 an write-mode ImageFile is created. In source line 14 an E57 extension is declared in the ImageFile, with prefix "ext1" and a given URI. The extension must be declared before the prefix is used in any element names. Source lines 17 and 23 show that given a prefix, the corresponding URI can be found (and vice versa). Source lines 28-30 show how to iterate over all extensions defined in the file. Note that the standardized default ASTM URI does not qualify as an "extension" and is not printed in the output. Finally, a StringNode is added to the ImageFile root on source line 32, using the "ext1" extension in the element name. If some unknown extension had been given instead in the element name on source line 32 (e.g. "ext2"), then the StructureNode::set function would have thrown an E57_ERROR_BAD_PATH_NAME exception.
In the XML listing below, the "ext1" prefix is declared in XML line 3, and is used in the element name in XML line 5.
The following console output is produced:
The XML section of the temp._e57
E57 file produced by this example program is as follows:
Here is the source code without line numbers to cut&paste from:
/*** Extensions.cpp example: register use of extended element names */ #include <iostream> #include "E57Foundation.h" using namespace e57; using namespace std; int main(int /*argc*/, char** /*argv*/) { try { ImageFile imf("temp._e57", "w"); imf.extensionsAdd("ext1", "http://www.example.com/MyExtension1"); ustring uri; if (!imf.extensionsLookupPrefix("ext1", uri)) cout << "Oops, ext1 should have been defined" << endl; else cout << "ext1 is prefix for uri=" << uri << endl; ustring prefix; if (!imf.extensionsLookupUri("http://www.example.com/MyExtension1", prefix)) cout << "Oops, URI http://www.example.com/MyExtension1 should have been defined" << endl; else cout << "URI http://www.example.com/MyExtension1 has prefix=" << prefix << endl; cout << "Defined extensions:" << endl; for (int i = 0; i < imf.extensionsCount(); i++) cout << " prefix:" << imf.extensionsPrefix(i) << " URI:" << imf.extensionsUri(i) << endl; imf.root().set("ext1:greeting", StringNode(imf, "Hello world.")); imf.close(); // don't forget to explicitly close the ImageFile } catch(E57Exception& ex) { ex.report(__FILE__, __LINE__, __FUNCTION__); return(-1); } return(0); }