00001 /*MT* 00002 00003 MediaTomb - http://www.mediatomb.cc/ 00004 00005 xpath.cc - this file is part of MediaTomb. 00006 00007 Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>, 00008 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc> 00009 00010 Copyright (C) 2006-2010 Gena Batyan <bgeradz@mediatomb.cc>, 00011 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>, 00012 Leonhard Wimmer <leo@mediatomb.cc> 00013 00014 MediaTomb is free software; you can redistribute it and/or modify 00015 it under the terms of the GNU General Public License version 2 00016 as published by the Free Software Foundation. 00017 00018 MediaTomb is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 GNU General Public License for more details. 00022 00023 You should have received a copy of the GNU General Public License 00024 version 2 along with MediaTomb; if not, write to the Free Software 00025 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00026 00027 $Id: xpath.cc 2081 2010-03-23 20:18:00Z lww $ 00028 */ 00029 00031 00032 #ifdef HAVE_CONFIG_H 00033 #include "autoconfig.h" 00034 #endif 00035 00036 #include "common.h" 00037 #include "tools.h" 00038 #include "xpath.h" 00039 00040 using namespace zmm; 00041 using namespace mxml; 00042 00043 XPath::XPath(Ref<Element> context) : Object() 00044 { 00045 this->context = context; 00046 } 00047 00048 Ref<Element> XPath::getElement(String xpath) 00049 { 00050 String axisPart = getAxisPart(xpath); 00051 if (axisPart != nil) 00052 { 00053 throw _Exception(_("XPath::getElement: unexpected axis in ") + xpath); 00054 } 00055 return elementAtPath(xpath); 00056 } 00057 00058 String XPath::getText(String xpath) 00059 { 00060 String axisPart = getAxisPart(xpath); 00061 String pathPart = getPathPart(xpath); 00062 00063 Ref<Element> el = elementAtPath(pathPart); 00064 if (el == nil) 00065 return nil; 00066 00067 if (axisPart == nil) 00068 return el->getText(); 00069 00070 String axis = getAxis(axisPart); 00071 String spec = getSpec(axisPart); 00072 00073 if (axis != "attribute") 00074 { 00075 throw _Exception(_("XPath::getText: unexpected axis: ") + axis); 00076 } 00077 00078 return el->getAttribute(spec); 00079 } 00080 00081 String XPath::getPathPart(String xpath) 00082 { 00083 int slashPos = xpath.rindex('/'); 00084 if (slashPos < 0) 00085 return xpath; 00086 if (strstr(xpath.c_str() + slashPos, "::")) 00087 { 00088 return xpath.substring(0, slashPos); 00089 } 00090 return xpath; 00091 } 00092 00093 Ref<Element> XPath::elementAtPath(String path) 00094 { 00095 Ref<Element> cur = context; 00096 Ref<Array<StringBase> > parts = split_string(path, '/'); 00097 00098 for (int i = 0; i < parts->size(); i++) 00099 { 00100 String part = parts->get(i); 00101 cur = cur->getChildByName(part); 00102 if (cur == nil) 00103 break; 00104 } 00105 return cur; 00106 } 00107 00108 String XPath::getAxisPart(String xpath) 00109 { 00110 int slashPos = xpath.rindex('/'); 00111 if (slashPos < 0) 00112 slashPos = 0; 00113 if (strstr(xpath.c_str() + slashPos, "::")) 00114 { 00115 return xpath.substring(slashPos + 1); 00116 } 00117 return nil; 00118 } 00119 00120 String XPath::getAxis(String axisPart) 00121 { 00122 char *pos = strstr(axisPart.c_str(), "::"); 00123 return axisPart.substring(0, pos - axisPart.c_str()); 00124 } 00125 00126 String XPath::getSpec(String axisPart) 00127 { 00128 char *pos = strstr(axisPart.c_str(), "::"); 00129 return pos + 2; 00130 }
1.6.1