From f7d79e393832093c51a31a545b9851c9dd82c13c Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Thu, 22 Jul 2010 18:16:52 +0000 Subject: [PATCH] Update ctors to use new attribute shortcuts. --- xmltooling/impl/MemoryStorageService.cpp | 10 +- .../security/impl/AbstractPKIXTrustEngine.cpp | 8 +- .../security/impl/ChainingCredentialResolver.cpp | 10 +- xmltooling/security/impl/ChainingTrustEngine.cpp | 8 +- .../security/impl/FilesystemCredentialResolver.cpp | 126 +++++++-------------- xmltooling/security/impl/InlineKeyResolver.cpp | 2 +- xmltooling/security/impl/StaticPKIXTrustEngine.cpp | 18 ++- xmltooling/security/impl/TrustEngine.cpp | 8 +- xmltooling/util/ReloadableXMLFile.cpp | 66 +++++------ xmltooling/util/XMLHelper.cpp | 37 ++++++ xmltooling/util/XMLHelper.h | 44 ++++++- 11 files changed, 171 insertions(+), 166 deletions(-) diff --git a/xmltooling/impl/MemoryStorageService.cpp b/xmltooling/impl/MemoryStorageService.cpp index 7592ce8..36e35ba 100644 --- a/xmltooling/impl/MemoryStorageService.cpp +++ b/xmltooling/impl/MemoryStorageService.cpp @@ -121,16 +121,10 @@ namespace xmltooling { static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l); MemoryStorageService::MemoryStorageService(const DOMElement* e) - : m_lock(nullptr), shutdown_wait(nullptr), cleanup_thread(nullptr), shutdown(false), m_cleanupInterval(0), + : m_lock(nullptr), shutdown_wait(nullptr), cleanup_thread(nullptr), shutdown(false), + m_cleanupInterval(XMLHelper::getAttrInt(e, 900, cleanupInterval)), m_log(Category::getInstance(XMLTOOLING_LOGCAT".StorageService")) { - const XMLCh* tag=e ? e->getAttributeNS(nullptr,cleanupInterval) : nullptr; - if (tag && *tag) { - m_cleanupInterval = XMLString::parseInt(tag); - } - if (!m_cleanupInterval) - m_cleanupInterval=900; - m_lock = RWLock::create(); shutdown_wait = CondWait::create(); cleanup_thread = Thread::create(&cleanup_fn, (void*)this); diff --git a/xmltooling/security/impl/AbstractPKIXTrustEngine.cpp b/xmltooling/security/impl/AbstractPKIXTrustEngine.cpp index 0798115..0f401ea 100644 --- a/xmltooling/security/impl/AbstractPKIXTrustEngine.cpp +++ b/xmltooling/security/impl/AbstractPKIXTrustEngine.cpp @@ -161,6 +161,8 @@ namespace { return false; } + + static XMLCh fullCRLChain[] = UNICODE_LITERAL_12(f,u,l,l,C,R,L,C,h,a,i,n); }; AbstractPKIXTrustEngine::PKIXValidationInfoIterator::PKIXValidationInfoIterator() @@ -171,11 +173,9 @@ AbstractPKIXTrustEngine::PKIXValidationInfoIterator::~PKIXValidationInfoIterator { } -AbstractPKIXTrustEngine::AbstractPKIXTrustEngine(const xercesc::DOMElement* e) : TrustEngine(e), m_fullCRLChain(false) +AbstractPKIXTrustEngine::AbstractPKIXTrustEngine(const xercesc::DOMElement* e) + : TrustEngine(e), m_fullCRLChain(XMLHelper::getAttrBool(e, false, fullCRLChain)) { - static XMLCh fullCRLChain[] = UNICODE_LITERAL_12(f,u,l,l,C,R,L,C,h,a,i,n); - const XMLCh* flag = e ? e->getAttributeNS(nullptr, fullCRLChain) : nullptr; - m_fullCRLChain = (flag && (*flag == xercesc::chLatin_t || *flag == xercesc::chDigit_1)); } AbstractPKIXTrustEngine::~AbstractPKIXTrustEngine() diff --git a/xmltooling/security/impl/ChainingCredentialResolver.cpp b/xmltooling/security/impl/ChainingCredentialResolver.cpp index eefbc29..0a86d44 100644 --- a/xmltooling/security/impl/ChainingCredentialResolver.cpp +++ b/xmltooling/security/impl/ChainingCredentialResolver.cpp @@ -78,7 +78,7 @@ namespace xmltooling { } static const XMLCh _CredentialResolver[] = UNICODE_LITERAL_18(C,r,e,d,e,n,t,i,a,l,R,e,s,o,l,v,e,r); - static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e); + static const XMLCh type[] = UNICODE_LITERAL_4(t,y,p,e); }; ChainingCredentialResolver::ChainingCredentialResolver(const DOMElement* e) @@ -89,11 +89,11 @@ ChainingCredentialResolver::ChainingCredentialResolver(const DOMElement* e) // Load up the chain of resolvers. e = e ? XMLHelper::getFirstChildElement(e, _CredentialResolver) : nullptr; while (e) { - auto_ptr_char type(e->getAttributeNS(nullptr,_type)); - if (type.get() && *(type.get())) { - log.info("building CredentialResolver of type %s", type.get()); + string t = XMLHelper::getAttrString(e, nullptr, type); + if (!t.empty()) { + log.info("building CredentialResolver of type %s", t.c_str()); try { - m_resolvers.push_back(conf.CredentialResolverManager.newPlugin(type.get(),e)); + m_resolvers.push_back(conf.CredentialResolverManager.newPlugin(t.c_str(), e)); } catch (exception& ex) { log.error("caught exception processing embedded CredentialResolver element: %s", ex.what()); diff --git a/xmltooling/security/impl/ChainingTrustEngine.cpp b/xmltooling/security/impl/ChainingTrustEngine.cpp index f1a92fc..da00a5b 100644 --- a/xmltooling/security/impl/ChainingTrustEngine.cpp +++ b/xmltooling/security/impl/ChainingTrustEngine.cpp @@ -51,10 +51,10 @@ ChainingTrustEngine::ChainingTrustEngine(const DOMElement* e) : TrustEngine(e) { e = e ? XMLHelper::getFirstChildElement(e, _TrustEngine) : nullptr; while (e) { try { - auto_ptr_char temp(e->getAttributeNS(nullptr,type)); - if (temp.get() && *temp.get()) { - log.info("building TrustEngine of type %s", temp.get()); - TrustEngine* engine = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(temp.get(), e); + string t = XMLHelper::getAttrString(e, nullptr, type); + if (!t.empty()) { + log.info("building TrustEngine of type %s", t.c_str()); + TrustEngine* engine = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), e); m_engines.push_back(engine); SignatureTrustEngine* sig = dynamic_cast(engine); if (sig) diff --git a/xmltooling/security/impl/FilesystemCredentialResolver.cpp b/xmltooling/security/impl/FilesystemCredentialResolver.cpp index 7009214..fcdd747 100644 --- a/xmltooling/security/impl/FilesystemCredentialResolver.cpp +++ b/xmltooling/security/impl/FilesystemCredentialResolver.cpp @@ -320,13 +320,13 @@ FilesystemCredentialResolver::FilesystemCredentialResolver(const DOMElement* e) Category& log=Category::getInstance(XMLTOOLING_LOGCAT".CredentialResolver."FILESYSTEM_CREDENTIAL_RESOLVER); // Default to disable X509IssuerSerial due to schema validation issues. - m_keyinfomask = + m_keyinfomask = XMLHelper::getAttrInt(e, Credential::KEYINFO_KEY_NAME | Credential::KEYINFO_KEY_VALUE | X509Credential::KEYINFO_X509_CERTIFICATE | - X509Credential::KEYINFO_X509_SUBJECTNAME; - if (e && e->hasAttributeNS(nullptr,keyInfoMask)) - m_keyinfomask = XMLString::parseInt(e->getAttributeNS(nullptr,keyInfoMask)); + X509Credential::KEYINFO_X509_SUBJECTNAME, + keyInfoMask + ); if (e && (e->hasAttributeNS(nullptr,_certificate) || e->hasAttributeNS(nullptr,_key))) { // Dummy up a simple file resolver config using these attributes. @@ -363,31 +363,19 @@ FilesystemCredentialResolver::FilesystemCredentialResolver(const DOMElement* e) const DOMElement* root = e; // Save off usage bits. - const XMLCh* usage = root->getAttributeNS(nullptr,_use); - if (usage && *usage) { - auto_ptr_char u(usage); - if (!strcmp(u.get(), "signing")) - m_usage = Credential::SIGNING_CREDENTIAL | Credential::TLS_CREDENTIAL; - else if (!strcmp(u.get(), "TLS")) - m_usage = Credential::TLS_CREDENTIAL; - else if (!strcmp(u.get(), "encryption")) - m_usage = Credential::ENCRYPTION_CREDENTIAL; - } + string usage = XMLHelper::getAttrString(root, nullptr, _use); + if (usage == "signing") + m_usage = Credential::SIGNING_CREDENTIAL | Credential::TLS_CREDENTIAL; + else if (usage == "TLS") + m_usage = Credential::TLS_CREDENTIAL; + else if (usage == "encryption") + m_usage = Credential::ENCRYPTION_CREDENTIAL; // Move to Key. const DOMElement* keynode = XMLHelper::getFirstChildElement(root,Key); if (keynode) { - prop = keynode->getAttributeNS(nullptr,_format); - if (prop && *prop) { - auto_ptr_char f(prop); - m_key.format = f.get(); - } - - prop = keynode->getAttributeNS(nullptr,password); - if (prop && *prop) { - auto_ptr_char kp(prop); - m_keypass = kp.get(); - } + m_key.format = XMLHelper::getAttrString(keynode, nullptr, _format); + m_keypass = XMLHelper::getAttrString(keynode, nullptr, password); if ((e=XMLHelper::getFirstChildElement(keynode,Path)) && e->hasChildNodes()) { prop = e->getFirstChild()->getNodeValue(); @@ -395,24 +383,18 @@ FilesystemCredentialResolver::FilesystemCredentialResolver(const DOMElement* e) m_key.source = kpath.get(); XMLToolingConfig::getConfig().getPathResolver()->resolve(m_key.source, PathResolver::XMLTOOLING_CFG_FILE); m_key.local = true; - prop = e->getAttributeNS(nullptr,_reloadChanges); - if (prop && (*prop==chLatin_f) || (*prop==chDigit_0)) - m_key.reloadChanges = false; + m_key.reloadChanges = XMLHelper::getAttrBool(e, true, _reloadChanges); } else if ((e=XMLHelper::getFirstChildElement(keynode,_URL)) && e->hasChildNodes()) { prop = e->getFirstChild()->getNodeValue(); auto_ptr_char kpath(prop); m_key.source = kpath.get(); m_key.local = false; - prop = e->getAttributeNS(nullptr,backingFilePath); - if (!prop || !*prop) + m_key.backing = XMLHelper::getAttrString(e, nullptr, backingFilePath); + if (m_key.backing.empty()) throw XMLSecurityException("FilesystemCredentialResolver can't access key, backingFilePath missing from URL element."); - auto_ptr_char b(prop); - m_key.backing = b.get(); XMLToolingConfig::getConfig().getPathResolver()->resolve(m_key.backing, PathResolver::XMLTOOLING_RUN_FILE); - prop = e->getAttributeNS(nullptr,_reloadInterval); - if (prop && *prop) - m_key.reloadInterval = XMLString::parseInt(prop); + m_key.reloadInterval = XMLHelper::getAttrInt(e, 0, _reloadInterval); } else { log.error("Path/URL element missing inside Key element"); @@ -431,54 +413,42 @@ FilesystemCredentialResolver::FilesystemCredentialResolver(const DOMElement* e) } // Check for CRL. - const DOMElement* crlnode = XMLHelper::getFirstChildElement(root,CRL); + const DOMElement* crlnode = XMLHelper::getFirstChildElement(root, CRL); if (crlnode) { - const XMLCh* crlformat = crlnode->getAttributeNS(nullptr,_format); - e = XMLHelper::getFirstChildElement(crlnode,Path); + string crlformat = XMLHelper::getAttrString(crlnode, nullptr, _format); + e = XMLHelper::getFirstChildElement(crlnode, Path); while (e) { if (e->hasChildNodes()) { m_crls.push_back(ManagedCRL()); ManagedResource& crl = m_crls.back(); - if (crlformat && *crlformat) { - auto_ptr_char f(crlformat); - crl.format = f.get(); - } + crl.format = crlformat; prop = e->getFirstChild()->getNodeValue(); auto_ptr_char crlpath(prop); crl.source = crlpath.get(); XMLToolingConfig::getConfig().getPathResolver()->resolve(crl.source, PathResolver::XMLTOOLING_CFG_FILE); crl.local = true; - prop = e->getAttributeNS(nullptr,_reloadChanges); - if (prop && (*prop==chLatin_f) || (*prop==chDigit_0)) - crl.reloadChanges = false; + crl.reloadChanges = XMLHelper::getAttrBool(e, true, _reloadChanges); } - e = XMLHelper::getNextSiblingElement(e,Path); + e = XMLHelper::getNextSiblingElement(e, Path); } - e=XMLHelper::getFirstChildElement(crlnode,_URL); + e = XMLHelper::getFirstChildElement(crlnode, _URL); while (e) { if (e->hasChildNodes()) { m_crls.push_back(ManagedCRL()); ManagedResource& crl = m_crls.back(); - if (crlformat && *crlformat) { - auto_ptr_char f(crlformat); - crl.format = f.get(); - } + crl.format = crlformat; prop = e->getFirstChild()->getNodeValue(); auto_ptr_char crlpath(prop); crl.source = crlpath.get(); crl.local = false; - prop = e->getAttributeNS(nullptr,backingFilePath); - if (!prop || !*prop) + crl.backing = XMLHelper::getAttrString(e, nullptr, backingFilePath); + if (crl.backing.empty()) throw XMLSecurityException("FilesystemCredentialResolver can't access CRL, backingFilePath missing from URL element."); - auto_ptr_char b(prop); - crl.backing = b.get(); XMLToolingConfig::getConfig().getPathResolver()->resolve(crl.backing, PathResolver::XMLTOOLING_RUN_FILE); - prop = e->getAttributeNS(nullptr,_reloadInterval); - if (prop && *prop) - crl.reloadInterval = XMLString::parseInt(prop); + crl.reloadInterval = XMLHelper::getAttrInt(e, 0, _reloadInterval); } - e = XMLHelper::getNextSiblingElement(e,_URL); + e = XMLHelper::getNextSiblingElement(e, _URL); } if (m_crls.empty()) { log.error("Path/URL element missing inside CRL element"); @@ -489,56 +459,36 @@ FilesystemCredentialResolver::FilesystemCredentialResolver(const DOMElement* e) // Check for Certificate DOMElement* certnode = XMLHelper::getFirstChildElement(root,Certificate); if (certnode) { - prop = certnode->getAttributeNS(nullptr,password); - if (prop && *prop) { - auto_ptr_char certpass(prop); - m_certpass = certpass.get(); - } - - const XMLCh* certformat = certnode->getAttributeNS(nullptr,_format); - - const XMLCh* extractFlag = certnode->getAttributeNS(nullptr, extractNames); - if (extractFlag && (*extractFlag == chLatin_f || *extractFlag == chDigit_0)) - m_extractNames = false; + m_certpass = XMLHelper::getAttrString(certnode, nullptr, password); + string certformat = XMLHelper::getAttrString(certnode, nullptr, _format); + m_extractNames = XMLHelper::getAttrBool(certnode, true, extractNames); e = XMLHelper::getFirstChildElement(certnode); while (e) { if (e->hasChildNodes() && (XMLString::equals(e->getLocalName(), Path) || XMLString::equals(e->getLocalName(), CAPath))) { m_certs.push_back(ManagedCert()); ManagedResource& cert = m_certs.back(); - if (certformat && *certformat) { - auto_ptr_char f(certformat); - cert.format = f.get(); - } + cert.format = certformat; prop = e->getFirstChild()->getNodeValue(); auto_ptr_char certpath(prop); cert.source = certpath.get(); XMLToolingConfig::getConfig().getPathResolver()->resolve(cert.source, PathResolver::XMLTOOLING_CFG_FILE); cert.local = true; - prop = e->getAttributeNS(nullptr,_reloadChanges); - if (prop && (*prop==chLatin_f) || (*prop==chDigit_0)) - cert.reloadChanges = false; + cert.reloadChanges = XMLHelper::getAttrBool(e, true, _reloadChanges); } else if (e->hasChildNodes() && XMLString::equals(e->getLocalName(), _URL)) { m_certs.push_back(ManagedCert()); ManagedResource& cert = m_certs.back(); - if (certformat && *certformat) { - auto_ptr_char f(certformat); - cert.format = f.get(); - } + cert.format = certformat; prop = e->getFirstChild()->getNodeValue(); auto_ptr_char certpath(prop); cert.source = certpath.get(); cert.local = false; - prop = e->getAttributeNS(nullptr,backingFilePath); - if (!prop || !*prop) + cert.backing = XMLHelper::getAttrString(e, nullptr, backingFilePath); + if (cert.backing.empty()) throw XMLSecurityException("FilesystemCredentialResolver can't access certificate, backingFilePath missing from URL element."); - auto_ptr_char b(prop); - cert.backing = b.get(); XMLToolingConfig::getConfig().getPathResolver()->resolve(cert.backing, PathResolver::XMLTOOLING_RUN_FILE); - prop = e->getAttributeNS(nullptr,_reloadInterval); - if (prop && *prop) - cert.reloadInterval = XMLString::parseInt(prop); + cert.reloadInterval = XMLHelper::getAttrInt(e, 0, _reloadInterval); } e = XMLHelper::getNextSiblingElement(e); } diff --git a/xmltooling/security/impl/InlineKeyResolver.cpp b/xmltooling/security/impl/InlineKeyResolver.cpp index 9ed4dee..08eef79 100644 --- a/xmltooling/security/impl/InlineKeyResolver.cpp +++ b/xmltooling/security/impl/InlineKeyResolver.cpp @@ -126,7 +126,7 @@ namespace xmltooling { { public: InlineKeyResolver(const DOMElement* e) - : m_followRefs(XMLHelper::getNodeValueAsBool(e ? e->getAttributeNodeNS(nullptr, keyInfoReferences) : nullptr, false)) { + : m_followRefs(XMLHelper::getAttrBool(e, false, keyInfoReferences)) { } virtual ~InlineKeyResolver() {} diff --git a/xmltooling/security/impl/StaticPKIXTrustEngine.cpp b/xmltooling/security/impl/StaticPKIXTrustEngine.cpp index 8a94206..06fde64 100644 --- a/xmltooling/security/impl/StaticPKIXTrustEngine.cpp +++ b/xmltooling/security/impl/StaticPKIXTrustEngine.cpp @@ -126,22 +126,18 @@ namespace xmltooling { }; }; -StaticPKIXTrustEngine::StaticPKIXTrustEngine(const DOMElement* e) : AbstractPKIXTrustEngine(e), m_depth(1), m_credResolver(nullptr) +StaticPKIXTrustEngine::StaticPKIXTrustEngine(const DOMElement* e) + : AbstractPKIXTrustEngine(e), m_depth(XMLHelper::getAttrInt(e, 1, verifyDepth)), m_credResolver(nullptr) { - const XMLCh* depth = e ? e->getAttributeNS(nullptr, verifyDepth) : nullptr; - if (depth && *depth) - m_depth = XMLString::parseInt(depth); - - if (e && e->hasAttributeNS(nullptr,certificate)) { + if (e && e->hasAttributeNS(nullptr, certificate)) { // Simple File resolver config rooted here. - m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(FILESYSTEM_CREDENTIAL_RESOLVER,e); + m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(FILESYSTEM_CREDENTIAL_RESOLVER, e); } else { e = e ? XMLHelper::getFirstChildElement(e, _CredentialResolver) : nullptr; - auto_ptr_char t(e ? e->getAttributeNS(nullptr,type) : nullptr); - if (t.get()) { - m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(t.get(),e); - } + string t = XMLHelper::getAttrString(e, nullptr, type); + if (!t.empty()) + m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(t.c_str(), e); else throw XMLSecurityException("Missing element, or no type attribute found"); } diff --git a/xmltooling/security/impl/TrustEngine.cpp b/xmltooling/security/impl/TrustEngine.cpp index 7dc9ed4..05f2b15 100644 --- a/xmltooling/security/impl/TrustEngine.cpp +++ b/xmltooling/security/impl/TrustEngine.cpp @@ -52,11 +52,11 @@ static const XMLCh type[] = UNICODE_LITERAL_4(t,y,p,e); TrustEngine::TrustEngine(const DOMElement* e) : m_keyInfoResolver(nullptr) { - DOMElement* child = e ? XMLHelper::getFirstChildElement(e,_KeyInfoResolver) : nullptr; + DOMElement* child = e ? XMLHelper::getFirstChildElement(e, _KeyInfoResolver) : nullptr; if (child) { - auto_ptr_char t(child->getAttributeNS(nullptr,type)); - if (t.get()) - m_keyInfoResolver = XMLToolingConfig::getConfig().KeyInfoResolverManager.newPlugin(t.get(),child); + string t = XMLHelper::getAttrString(child, nullptr, type); + if (!t.empty()) + m_keyInfoResolver = XMLToolingConfig::getConfig().KeyInfoResolverManager.newPlugin(t.c_str(), child); else throw UnknownExtensionException(" element found with no type attribute"); } diff --git a/xmltooling/util/ReloadableXMLFile.cpp b/xmltooling/util/ReloadableXMLFile.cpp index 2fdc7db..4499c43 100644 --- a/xmltooling/util/ReloadableXMLFile.cpp +++ b/xmltooling/util/ReloadableXMLFile.cpp @@ -138,44 +138,39 @@ ReloadableXMLFile::ReloadableXMLFile(const DOMElement* e, Category& log, bool st } if (source && *source) { - const XMLCh* flag=e->getAttributeNS(nullptr,validate); - m_validate=(XMLString::equals(flag,xmlconstants::XML_TRUE) || XMLString::equals(flag,xmlconstants::XML_ONE)); + m_validate = XMLHelper::getAttrBool(e, false, validate); auto_ptr_char temp(source); - m_source=temp.get(); + m_source = temp.get(); if (!m_local && !strstr(m_source.c_str(),"://")) { log.warn("deprecated usage of uri/url attribute for a local resource, use path instead"); - m_local=true; + m_local = true; } #ifndef XMLTOOLING_LITE // Check for signature bits. - if (e && e->hasAttributeNS(nullptr, certificate)) { + if (e->hasAttributeNS(nullptr, certificate)) { // Use a file-based credential resolver rooted here. m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(FILESYSTEM_CREDENTIAL_RESOLVER, e); } else { - const DOMElement* sub = e ? XMLHelper::getFirstChildElement(e, _CredentialResolver) : nullptr; - auto_ptr_char t(sub ? sub->getAttributeNS(nullptr, type) : nullptr); - if (t.get()) { - m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(t.get(), sub); + const DOMElement* sub = XMLHelper::getFirstChildElement(e, _CredentialResolver); + string t(XMLHelper::getAttrString(sub, nullptr, type)); + if (!t.empty()) { + m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(t.c_str(), sub); } else { - sub = e ? XMLHelper::getFirstChildElement(e, _TrustEngine) : nullptr; - auto_ptr_char t2(sub ? sub->getAttributeNS(nullptr, type) : nullptr); - if (t2.get()) { - TrustEngine* trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t2.get(), sub); + sub = XMLHelper::getFirstChildElement(e, _TrustEngine); + t = XMLHelper::getAttrString(sub, nullptr, type); + if (!t.empty()) { + TrustEngine* trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), sub); if (!(m_trust = dynamic_cast(trust))) { delete trust; throw XMLToolingException("TrustEngine-based ReloadableXMLFile requires a SignatureTrustEngine plugin."); } - flag = e->getAttributeNS(nullptr, signerName); - if (flag && *flag) { - auto_ptr_char sn(flag); - m_signerName = sn.get(); - } + m_signerName = XMLHelper::getAttrString(e, nullptr, signerName); } } } @@ -184,8 +179,8 @@ ReloadableXMLFile::ReloadableXMLFile(const DOMElement* e, Category& log, bool st if (m_local) { XMLToolingConfig::getConfig().getPathResolver()->resolve(m_source, PathResolver::XMLTOOLING_CFG_FILE); - flag=e->getAttributeNS(nullptr,reloadChanges); - if (!XMLString::equals(flag,xmlconstants::XML_FALSE) && !XMLString::equals(flag,xmlconstants::XML_ZERO)) { + bool flag = XMLHelper::getAttrBool(e, true, reloadChanges); + if (flag) { #ifdef WIN32 struct _stat stat_buf; if (_stat(m_source.c_str(), &stat_buf) == 0) @@ -193,19 +188,17 @@ ReloadableXMLFile::ReloadableXMLFile(const DOMElement* e, Category& log, bool st struct stat stat_buf; if (stat(m_source.c_str(), &stat_buf) == 0) #endif - m_filestamp=stat_buf.st_mtime; + m_filestamp = stat_buf.st_mtime; else throw IOException("Unable to access local file ($1)", params(1,m_source.c_str())); - m_lock=RWLock::create(); + m_lock = RWLock::create(); } log.debug("using local resource (%s), will %smonitor for changes", m_source.c_str(), m_lock ? "" : "not "); } else { log.debug("using remote resource (%s)", m_source.c_str()); - source = e->getAttributeNS(nullptr,backingFilePath); - if (source && *source) { - auto_ptr_char temp2(source); - m_backing=temp2.get(); + m_backing = XMLHelper::getAttrString(e, nullptr, backingFilePath); + if (!m_backing.empty()) { XMLToolingConfig::getConfig().getPathResolver()->resolve(m_backing, PathResolver::XMLTOOLING_RUN_FILE); log.debug("backup remote resource to (%s)", m_backing.c_str()); try { @@ -221,15 +214,12 @@ ReloadableXMLFile::ReloadableXMLFile(const DOMElement* e, Category& log, bool st catch (exception&) { } } - source = e->getAttributeNS(nullptr,reloadInterval); - if (!source || !*source) - source = e->getAttributeNS(nullptr,maxRefreshDelay); - if (source && *source) { - m_reloadInterval = XMLString::parseInt(source); - if (m_reloadInterval > 0) { - m_log.debug("will reload remote resource at most every %d seconds", m_reloadInterval); - m_lock=RWLock::create(); - } + m_reloadInterval = XMLHelper::getAttrInt(e, 0, reloadInterval); + if (m_reloadInterval == 0) + m_reloadInterval = XMLHelper::getAttrInt(e, 0, maxRefreshDelay); + if (m_reloadInterval > 0) { + m_log.debug("will reload remote resource at most every %d seconds", m_reloadInterval); + m_lock = RWLock::create(); } m_filestamp = time(nullptr); // assume it gets loaded initially } @@ -241,11 +231,7 @@ ReloadableXMLFile::ReloadableXMLFile(const DOMElement* e, Category& log, bool st log.debug("no resource uri/path/name supplied, will load inline configuration"); } - source = e->getAttributeNS(nullptr, id); - if (source && *source) { - auto_ptr_char tempid(source); - m_id = tempid.get(); - } + m_id = XMLHelper::getAttrString(e, nullptr, id); } ReloadableXMLFile::~ReloadableXMLFile() diff --git a/xmltooling/util/XMLHelper.cpp b/xmltooling/util/XMLHelper.cpp index bbeb383..20d4058 100644 --- a/xmltooling/util/XMLHelper.cpp +++ b/xmltooling/util/XMLHelper.cpp @@ -298,6 +298,43 @@ DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* return e; } +string XMLHelper::getAttrString(const DOMElement* e, const char* defValue, const XMLCh* localName, const XMLCh* ns) +{ + if (e) { + auto_ptr_char val(e->getAttributeNS(ns, localName)); + if (val.get() && *val.get()) + return val.get(); + } + return defValue ? defValue : ""; +} + +int XMLHelper::getAttrInt(const DOMElement* e, int defValue, const XMLCh* localName, const XMLCh* ns) +{ + if (e) { + const XMLCh* val = e->getAttributeNS(ns, localName); + if (val && *val) { + int i = XMLString::parseInt(val); + if (i) + return i; + } + } + return defValue; +} + +bool XMLHelper::getAttrBool(const DOMElement* e, bool defValue, const XMLCh* localName, const XMLCh* ns) +{ + if (e) { + const XMLCh* val = e->getAttributeNS(ns, localName); + if (val) { + if (*val == chLatin_t || *val == chDigit_1) + return true; + if (*val == chLatin_f || *val == chDigit_0) + return false; + } + } + return defValue; +} + void XMLHelper::serialize(const DOMNode* n, std::string& buf, bool pretty) { static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull }; diff --git a/xmltooling/util/XMLHelper.h b/xmltooling/util/XMLHelper.h index c7f810f..7198823 100644 --- a/xmltooling/util/XMLHelper.h +++ b/xmltooling/util/XMLHelper.h @@ -275,7 +275,7 @@ namespace xmltooling { /** * Returns the content of the first Text node found in the element, if any. * This is roughly similar to the DOM getTextContent function, but only - * examples the immediate children of the element. + * examines the immediate children of the element. * * @param e element to examine * @return the content of the first Text node found, or nullptr @@ -283,6 +283,48 @@ namespace xmltooling { static const XMLCh* getTextContent(const xercesc::DOMElement* e); /** + * Returns the content of the specified attribute node as a string, + * or the default value, if the attribute is not present. + * + * @param e element to examine (may be nullptr) + * @param defValue default value to return + * @param localName local name of attribute + * @param ns namespace of attribute + * @return the specified attribute's value, or the specified default + */ + static std::string getAttrString( + const xercesc::DOMElement* e, const char* defValue, const XMLCh* localName, const XMLCh* ns=nullptr + ); + + /** + * Returns the content of the specified attribute node as an integer, + * or the default value, if the attribute is not present. + * + * @param e element to examine (may be nullptr) + * @param defValue default value to return + * @param localName local name of attribute + * @param ns namespace of attribute + * @return the specified attribute's value, or the specified default + */ + static int getAttrInt( + const xercesc::DOMElement* e, int defValue, const XMLCh* localName, const XMLCh* ns=nullptr + ); + + /** + * Returns the content of the specified attribute node as a boolean, + * or the default value, if the attribute is not present. + * + * @param e element to examine (may be nullptr) + * @param defValue default value to return + * @param localName local name of attribute + * @param ns namespace of attribute + * @return the specified attribute's value, or the specified default + */ + static bool getAttrBool( + const xercesc::DOMElement* e, bool defValue, const XMLCh* localName, const XMLCh* ns=nullptr + ); + + /** * Serializes the DOM node provided into a buffer using UTF-8 encoding and * the default XML serializer available. No manipulation or formatting is applied. * -- 2.1.4