1 | // |
---|
2 | // C++ Implementation: itpp_ext |
---|
3 | // |
---|
4 | // Description: |
---|
5 | // |
---|
6 | // |
---|
7 | // Author: smidl <smidl@utia.cas.cz>, (C) 2008 |
---|
8 | // |
---|
9 | // Copyright: See COPYING file that comes with this distribution |
---|
10 | // |
---|
11 | // |
---|
12 | |
---|
13 | #include "userinfo.h" |
---|
14 | |
---|
15 | BindingFrame::BindingFrame() |
---|
16 | : dummy() |
---|
17 | { |
---|
18 | } |
---|
19 | |
---|
20 | string BindingFrame::XMLCh2str( const XMLCh* const XMLCh_str ) |
---|
21 | { |
---|
22 | char *local = XMLString::transcode( XMLCh_str ); |
---|
23 | string res = local; |
---|
24 | XMLString::release( &local ); |
---|
25 | return res; |
---|
26 | } |
---|
27 | |
---|
28 | |
---|
29 | Attribute::Attribute( string attributeName ) |
---|
30 | : dummy(), |
---|
31 | transcodedAttributeName( XMLString::transcode( attributeName.c_str() ) ) |
---|
32 | { |
---|
33 | } |
---|
34 | |
---|
35 | Attribute::~Attribute() |
---|
36 | { |
---|
37 | XMLString::release( (XMLCh**)&transcodedAttributeName ); |
---|
38 | } |
---|
39 | |
---|
40 | string& Attribute::Get( DOMElement &element ) const |
---|
41 | { |
---|
42 | const XMLCh* const transcoded_str = element.getAttribute( transcodedAttributeName ); |
---|
43 | return *new string( XMLString::transcode( transcoded_str ) ); |
---|
44 | } |
---|
45 | |
---|
46 | void Attribute::Set( DOMElement &element, const string &str ) const |
---|
47 | { |
---|
48 | if( !str.length() ) return; |
---|
49 | const XMLCh* transcoded_str = XMLString::transcode(str.c_str()); |
---|
50 | element.setAttribute( transcodedAttributeName, transcoded_str ); |
---|
51 | XMLString::release( (XMLCh**) &transcoded_str ); |
---|
52 | } |
---|
53 | |
---|
54 | const Attribute Attribute::help( "help" ); |
---|
55 | |
---|
56 | const Attribute Attribute::type( "type" ); |
---|
57 | |
---|
58 | const Attribute Attribute::value( "value" ); |
---|
59 | |
---|
60 | |
---|
61 | UserInfo::StringToUIMap::MappedString2UI& UserInfo::StringToUIMap::privateMap() |
---|
62 | { |
---|
63 | static MappedString2UI var; |
---|
64 | return var; |
---|
65 | } |
---|
66 | |
---|
67 | void UserInfo::StringToUIMap::Add( string key, pUserInfo pInstance ) |
---|
68 | { |
---|
69 | privateMap().insert( make_pair( key, pInstance ) ); |
---|
70 | } |
---|
71 | |
---|
72 | UserInfo::pUserInfo UserInfo::StringToUIMap::Retrieve( string key ) |
---|
73 | { |
---|
74 | MappedString2UI::const_iterator iter = privateMap().find( key ); |
---|
75 | if( iter == privateMap().end()) return NULL; |
---|
76 | else return iter->second; |
---|
77 | } |
---|
78 | |
---|
79 | RootElement::RootElement(const char* fileName ) |
---|
80 | : dummy(), |
---|
81 | transcodedFileName( XMLString::transcode( fileName ) ) |
---|
82 | { |
---|
83 | // get a serializer, an instance of DOMWriter (the "LS" stands for load-save). |
---|
84 | pImplementation = DOMImplementationRegistry::getDOMImplementation( XMLString::transcode( "LS" )); |
---|
85 | |
---|
86 | pSerializer = ( (DOMImplementationLS*)pImplementation )->createDOMWriter(); |
---|
87 | // set user specified end of line sequence and output encoding |
---|
88 | pSerializer->setNewLine( XMLString::transcode( "\n" ) ); |
---|
89 | // optionally, set the format-pretty-print feature |
---|
90 | if (pSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true)) |
---|
91 | pSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true); |
---|
92 | |
---|
93 | pDoc = NULL; |
---|
94 | Clean(); |
---|
95 | } |
---|
96 | |
---|
97 | RootElement::~RootElement() |
---|
98 | { |
---|
99 | XMLString::release( (XMLCh**)&transcodedFileName ); |
---|
100 | pSerializer->release(); |
---|
101 | pDoc->release(); |
---|
102 | |
---|
103 | AssertXercesIsAlive dummy; // TODO je zde treba? |
---|
104 | } |
---|
105 | |
---|
106 | void RootElement::Clean() |
---|
107 | { |
---|
108 | if( pDoc ) pDoc->release(); |
---|
109 | pDoc = pImplementation->createDocument( |
---|
110 | XMLString::transcode( "M3K USER INFO" ), |
---|
111 | XMLString::transcode( "ROOT" ), NULL ); |
---|
112 | } |
---|
113 | |
---|
114 | //! loads root element from a file |
---|
115 | bool RootElement::Load( void ) |
---|
116 | { |
---|
117 | const LocalFileInputSource inputSource( transcodedFileName ); |
---|
118 | |
---|
119 | //! This DOMWriter is used to import external data from xml files |
---|
120 | XercesDOMParser parser; |
---|
121 | |
---|
122 | parser.setValidationScheme(XercesDOMParser::Val_Auto); |
---|
123 | parser.setDoNamespaces(false); |
---|
124 | parser.setDoSchema(false); |
---|
125 | |
---|
126 | parser.parse( inputSource ); |
---|
127 | if( parser.getErrorCount() ) |
---|
128 | return false; |
---|
129 | |
---|
130 | DOMDocument * newDoc = parser.adoptDocument(); |
---|
131 | if( newDoc == NULL ) |
---|
132 | return false; |
---|
133 | |
---|
134 | if( pDoc ) |
---|
135 | pDoc->release(); |
---|
136 | |
---|
137 | pDoc = newDoc; |
---|
138 | return true; |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | //! Save UserInfo to the file (typically with an XML extension) |
---|
143 | void RootElement::Save ( void ) |
---|
144 | { |
---|
145 | LocalFileFormatTarget outputTarget( transcodedFileName ); |
---|
146 | pSerializer->writeNode( &outputTarget, *pDoc); |
---|
147 | } |
---|
148 | |
---|
149 | RootElement::operator DOMElement& () |
---|
150 | { |
---|
151 | return *(pDoc->getDocumentElement()); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | template<> const TypedUserInfo<bool>& TypedUserInfo<bool>::instance = BoolUI(); |
---|
156 | template<> const TypedUserInfo<int>& TypedUserInfo<int>::instance = IntUI(); |
---|
157 | template<> const TypedUserInfo<double>& TypedUserInfo<double>::instance = DoubleUI(); |
---|
158 | template<> const TypedUserInfo<string>& TypedUserInfo<string>::instance = StringUI(); |
---|