Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2002-2024 German Aerospace Center (DLR) and others. 4 : // This program and the accompanying materials are made available under the 5 : // terms of the Eclipse Public License 2.0 which is available at 6 : // https://www.eclipse.org/legal/epl-2.0/ 7 : // This Source Code may also be made available under the following Secondary 8 : // Licenses when the conditions for such availability set forth in the Eclipse 9 : // Public License 2.0 are satisfied: GNU General Public License, version 2 10 : // or later which is available at 11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 13 : /****************************************************************************/ 14 : /// @file SUMOSAXAttributesImpl_Xerces.cpp 15 : /// @author Daniel Krajzewicz 16 : /// @author Jakob Erdmann 17 : /// @author Michael Behrisch 18 : /// @date Sept 2002 19 : /// 20 : // Encapsulated Xerces-SAX-attributes 21 : /****************************************************************************/ 22 : #include <config.h> 23 : 24 : #include <cassert> 25 : #include <xercesc/sax2/Attributes.hpp> 26 : #include <xercesc/sax2/DefaultHandler.hpp> 27 : #include <utils/common/RGBColor.h> 28 : #include <utils/common/StringTokenizer.h> 29 : #include <utils/common/StringUtils.h> 30 : #include <utils/common/StringUtils.h> 31 : #include <utils/geom/Boundary.h> 32 : #include <utils/geom/PositionVector.h> 33 : #include "XMLSubSys.h" 34 : #include "SUMOSAXAttributesImpl_Xerces.h" 35 : #include "SUMOSAXAttributesImpl_Cached.h" 36 : 37 : 38 : // =========================================================================== 39 : // class definitions 40 : // =========================================================================== 41 16577464 : SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs, 42 : const std::vector<XMLCh*>& predefinedTags, 43 : const std::vector<std::string>& predefinedTagsMML, 44 16577464 : const std::string& objectType) : 45 : SUMOSAXAttributes(objectType), 46 16577464 : myAttrs(attrs), 47 16577464 : myPredefinedTags(predefinedTags), 48 16577464 : myPredefinedTagsMML(predefinedTagsMML) { } 49 : 50 : 51 16577759 : SUMOSAXAttributesImpl_Xerces::~SUMOSAXAttributesImpl_Xerces() { 52 16577759 : } 53 : 54 : 55 : bool 56 29625375 : SUMOSAXAttributesImpl_Xerces::hasAttribute(int id) const { 57 : assert(id >= 0); 58 : assert(id < (int)myPredefinedTags.size()); 59 29625375 : return myAttrs.getIndex(myPredefinedTags[id]) >= 0; 60 : } 61 : 62 : 63 : std::string 64 109149826 : SUMOSAXAttributesImpl_Xerces::getString(int id, bool* isPresent) const { 65 109149826 : const XMLCh* const xString = getAttributeValueSecure(id); 66 109149826 : if (xString != nullptr) { 67 59115604 : return StringUtils::transcode(getAttributeValueSecure(id)); 68 : } 69 50034222 : *isPresent = false; 70 50034222 : return ""; 71 : } 72 : 73 : 74 : std::string 75 911051 : SUMOSAXAttributesImpl_Xerces::getStringSecure(int id, const std::string& str) const { 76 911051 : const XMLCh* utf16 = getAttributeValueSecure(id); 77 901475 : if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) { 78 : // TranscodeToStr and debug_new interact badly in this case; 79 : return str; 80 : } else { 81 733263 : return getString(id); 82 : } 83 : } 84 : 85 : 86 : const XMLCh* 87 169176481 : SUMOSAXAttributesImpl_Xerces::getAttributeValueSecure(int id) const { 88 : assert(id >= 0); 89 : assert(id < (int)myPredefinedTags.size()); 90 169176481 : return myAttrs.getValue(myPredefinedTags[id]); 91 : } 92 : 93 : 94 : double 95 9694 : SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const { 96 9694 : XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str()); 97 9694 : const std::string utf8 = StringUtils::transcode(myAttrs.getValue(t)); 98 9694 : XERCES_CPP_NAMESPACE::XMLString::release(&t); 99 19388 : return StringUtils::toDouble(utf8); 100 : } 101 : 102 : 103 : bool 104 761639 : SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const { 105 761639 : XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str()); 106 761639 : bool result = myAttrs.getIndex(t) >= 0; 107 761639 : XERCES_CPP_NAMESPACE::XMLString::release(&t); 108 761639 : return result; 109 : } 110 : 111 : 112 : std::string 113 709772 : SUMOSAXAttributesImpl_Xerces::getStringSecure(const std::string& id, 114 : const std::string& str) const { 115 709772 : XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str()); 116 709772 : const XMLCh* v = myAttrs.getValue(t); 117 709772 : XERCES_CPP_NAMESPACE::XMLString::release(&t); 118 709772 : if (v == nullptr) { 119 : return str; 120 : } else { 121 706542 : return StringUtils::transcode(v); 122 : } 123 : } 124 : 125 : 126 : std::string 127 1713 : SUMOSAXAttributesImpl_Xerces::getName(int attr) const { 128 : assert(attr >= 0); 129 : assert(attr < (int)myPredefinedTagsMML.size()); 130 1713 : return myPredefinedTagsMML[attr]; 131 : } 132 : 133 : 134 : void 135 136 : SUMOSAXAttributesImpl_Xerces::serialize(std::ostream& os) const { 136 460 : for (int i = 0; i < (int)myAttrs.getLength(); ++i) { 137 648 : os << " " << StringUtils::transcode(myAttrs.getLocalName(i)); 138 972 : os << "=\"" << StringUtils::transcode(myAttrs.getValue(i)) << "\""; 139 : } 140 136 : } 141 : 142 : 143 : std::vector<std::string> 144 0 : SUMOSAXAttributesImpl_Xerces::getAttributeNames() const { 145 : std::vector<std::string> result; 146 0 : for (int i = 0; i < (int)myAttrs.getLength(); ++i) { 147 0 : result.push_back(StringUtils::transcode(myAttrs.getLocalName(i))); 148 : } 149 0 : return result; 150 0 : } 151 : 152 : 153 : SUMOSAXAttributes* 154 10924 : SUMOSAXAttributesImpl_Xerces::clone() const { 155 : std::map<std::string, std::string> attrs; 156 87291 : for (int i = 0; i < (int)myAttrs.getLength(); ++i) { 157 152734 : attrs[StringUtils::transcode(myAttrs.getLocalName(i))] = StringUtils::transcode(myAttrs.getValue(i)); 158 : } 159 21848 : return new SUMOSAXAttributesImpl_Cached(attrs, myPredefinedTagsMML, getObjectType()); 160 : } 161 : 162 : 163 : /****************************************************************************/