?? xmlconfiguration.cpp
字號:
//
// XMLConfiguration.cpp
//
// $Id: //poco/Main/Util/src/XMLConfiguration.cpp#1 $
//
// Copyright (c) 2004-2005, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "Util/XMLConfiguration.h"
#include "SAX/InputSource.h"
#include "DOM/DOMParser.h"
#include "DOM/Element.h"
#include "DOM/Attr.h"
#include "Foundation/Exception.h"
#include "Foundation/NumberParser.h"
#include "Foundation/NumberFormatter.h"
#include <set>
Util_BEGIN
XMLConfiguration::XMLConfiguration()
{
}
XMLConfiguration::XMLConfiguration(XML::InputSource* pInputSource)
{
load(pInputSource);
}
XMLConfiguration::XMLConfiguration(std::istream& istr)
{
load(istr);
}
XMLConfiguration::XMLConfiguration(const std::string& path)
{
load(path);
}
XMLConfiguration::XMLConfiguration(const XML::Document* pDocument)
{
load(pDocument);
}
XMLConfiguration::XMLConfiguration(const XML::Node* pNode)
{
load(pNode);
}
XMLConfiguration::~XMLConfiguration()
{
}
void XMLConfiguration::load(XML::InputSource* pInputSource)
{
poco_check_ptr (pInputSource);
XML::DOMParser parser;
parser.setFeature(XML::XMLReader::FEATURE_NAMESPACES, false);
parser.setFeature(XML::DOMParser::FEATURE_WHITESPACE, true);
XML::AutoPtr<XML::Document> pDoc = parser.parse(pInputSource);
load(pDoc);
}
void XMLConfiguration::load(std::istream& istr)
{
XML::InputSource src(istr);
load(&src);
}
void XMLConfiguration::load(const std::string& path)
{
XML::InputSource src(path);
load(&src);
}
void XMLConfiguration::load(const XML::Document* pDocument)
{
poco_check_ptr (pDocument);
_pDocument = XML::AutoPtr<XML::Document>(const_cast<XML::Document*>(pDocument), true);
_pRoot = XML::AutoPtr<XML::Node>(pDocument->documentElement(), true);
}
void XMLConfiguration::load(const XML::Node* pNode)
{
poco_check_ptr (pNode);
if (pNode->nodeType() == XML::Node::DOCUMENT_NODE)
{
load(static_cast<const XML::Document*>(pNode));
}
else
{
_pDocument = XML::AutoPtr<XML::Document>(pNode->ownerDocument(), true);
_pRoot = XML::AutoPtr<XML::Node>(const_cast<XML::Node*>(pNode), true);
}
}
bool XMLConfiguration::getRaw(const std::string& key, std::string& value) const
{
const XML::Node* pNode = findNode(key);
if (pNode)
{
value = pNode->innerText();
return true;
}
else return false;
}
void XMLConfiguration::setRaw(const std::string& key, const std::string& value)
{
throw Foundation::NotImplementedException("Setting a property in an XMLConfiguration");
}
void XMLConfiguration::enumerate(const std::string& key, Keys& range) const
{
using Foundation::NumberFormatter;
std::multiset<std::string> keys;
const XML::Node* pNode = findNode(key);
if (pNode)
{
const XML::Node* pChild = pNode->firstChild();
while (pChild)
{
if (pChild->nodeType() == XML::Node::ELEMENT_NODE)
{
const std::string& nodeName = pChild->nodeName();
int n = (int) keys.count(nodeName);
if (n)
range.push_back(nodeName + "[" + NumberFormatter::format(n) + "]");
else
range.push_back(nodeName);
keys.insert(nodeName);
}
pChild = pChild->nextSibling();
}
}
}
const XML::Node* XMLConfiguration::findNode(const std::string& key) const
{
std::string::const_iterator it = key.begin();
return findNode(it, key.end(), _pRoot);
}
const XML::Node* XMLConfiguration::findNode(std::string::const_iterator& it, const std::string::const_iterator& end, const XML::Node* pNode)
{
if (pNode && it != end)
{
if (*it == '[')
{
++it;
if (it != end && *it == '@')
{
++it;
std::string attr;
while (it != end && *it != ']') attr += *it++;
if (it != end) ++it;
return findAttribute(attr, pNode);
}
else
{
std::string index;
while (it != end && *it != ']') index += *it++;
if (it != end) ++it;
return findNode(it, end, findElement(Foundation::NumberParser::parse(index), pNode));
}
}
else
{
while (it != end && *it == '.') ++it;
std::string key;
while (it != end && *it != '.' && *it != '[') key += *it++;
return findNode(it, end, findElement(key, pNode));
}
}
else return pNode;
}
const XML::Node* XMLConfiguration::findElement(const std::string& name, const XML::Node* pNode)
{
const XML::Node* pChild = pNode->firstChild();
while (pChild)
{
if (pChild->nodeType() == XML::Node::ELEMENT_NODE && pChild->nodeName() == name)
return pChild;
pChild = pChild->nextSibling();
}
return 0;
}
const XML::Node* XMLConfiguration::findElement(int index, const XML::Node* pNode)
{
const XML::Node* pRefNode = pNode;
if (index > 0)
{
pNode = pNode->nextSibling();
while (pNode)
{
if (pNode->nodeName() == pRefNode->nodeName())
{
if (--index == 0) break;
}
pNode = pNode->nextSibling();
}
}
return pNode;
}
const XML::Node* XMLConfiguration::findAttribute(const std::string& name, const XML::Node* pNode)
{
const XML::Element* pElem = dynamic_cast<const XML::Element*>(pNode);
if (pElem)
return pElem->getAttributeNode(name);
else
return 0;
}
Util_END
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -