• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/xpath/XPath.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/xml/xpath/XPath.h>
00018 #include <spl/xml/xpath/private/XPathParser.h>
00019 
00020 XPath::XPath()
00021 : m_ops()
00022 {
00023 }
00024 
00025 XPath::XPath(const String& xp)
00026 : m_ops()
00027 {
00028         XPathParser parser;
00029         m_ops = parser.Parse(xp);
00030 }
00031 
00032 XPath::XPath(const XPath& xp)
00033 : m_ops(xp.m_ops)
00034 {
00035 }
00036 
00037 XPath::~XPath()
00038 {
00039 }
00040 
00041 XPath& XPath::operator =(const XPath& xp)
00042 {
00043         m_ops = xp.m_ops;
00044         return *this;
00045 }
00046 
00047 XmlNodeListPtr XPath::SelectNodes(XmlNodePtr context)
00048 {
00049         return SelectNodes(context, true);
00050 }
00051 
00052 XmlNodePtr XPath::SelectSingleNode(XmlNodePtr context)
00053 {
00054         XmlNodeListPtr nodes = SelectNodes(context, false);
00055         if (nodes->Count() == 0)
00056         {
00057                 return XmlNodePtr();
00058         }
00059         return nodes->Item(0);
00060 }
00061 
00062 XmlNodeListPtr XPath::SelectNodes(Array<XPathOperatorPtr>&ops, XmlNodePtr context)
00063 {
00064         return SelectNodes(ops, context, true);
00065 }
00066 
00069 XmlNodeListPtr XPath::SelectNodes(Array<XPathOperatorPtr>&ops, XmlNodePtr context, bool findAll)
00070 {
00071         XmlNodeListPtr contextList(new XmlNodeList());
00072         contextList->Add(context);
00073         
00074         for(Array<XPathOperatorPtr>::Iterator iterOps(ops.Begin()); iterOps.Next();)
00075         {
00076                 XmlNodeListPtr innerCtxList(new XmlNodeList());
00077                 
00078                 for(Vector<XmlNodePtr>::Iterator iterCtx(contextList->Begin()); iterCtx.Next();)
00079                 {
00080                         if (iterOps.CurrentRef()->IsMatch(iterCtx.Current()))
00081                         {
00082                                 innerCtxList->Add(iterOps.CurrentRef()->NextContext(iterCtx.Current()));
00083                         }
00084                 }
00085                 contextList = innerCtxList;
00086         }
00087 
00088         return contextList;
00089 }
00090 
00091 #if defined(DEBUG) || defined(_DEBUG)
00092 void XPath::CheckMem() const
00093 {
00094         m_ops.CheckMem();
00095 }
00096 
00097 void XPath::ValidateMem() const
00098 {
00099         m_ops.ValidateMem();
00100 }
00101 #endif