2 Copyright (C) 1997-2017 JDERobot Developers Team 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; either version 2 of the License, or 7 (at your option) any later version. 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU Library General Public License for more details. 14 You should have received a copy of the GNU General Public License 15 along with this program; if not, see <http://www.gnu.org/licenses/>. 17 Authors : Okan Asik (asik.okan@gmail.com) 20 from xml.dom
import minidom
21 from visualstates.core.state
import State
22 from visualstates.core.namespace
import Namespace
23 from visualstates.configs.rosconfig
import RosConfig
36 if path.rfind(
'.xml') < 0:
40 def save(self, rootState, config, libraries, globalNamespace):
41 doc = minidom.Document()
42 root = doc.createElement(
'VisualStates')
46 if config
is not None:
47 root.appendChild(config.createNode(doc))
50 globalNamespaceElement = globalNamespace.createNode(doc, globalNamespace=
True)
51 root.appendChild(globalNamespaceElement)
54 libraryElement = doc.createElement(
'libraries')
56 libElement = doc.createElement(
'library')
57 libElement.appendChild(doc.createTextNode(lib))
58 libraryElement.appendChild(libElement)
59 root.appendChild(libraryElement)
62 xmlStr = doc.toprettyxml(indent=
' ')
67 stateElement = state.createElement(doc)
72 doc = minidom.parse(fullPath)
73 except xml.parsers.expat.ExpatError:
74 return None,
None,
None,
None 76 if len(doc.getElementsByTagName(
'VisualStates')) == 0:
77 return None,
None,
None,
None 80 globalNamespaceNode = doc.getElementsByTagName(
'VisualStates')[0].getElementsByTagName(
'global_namespace')[0]
81 globalNamespace = Namespace(
'',
'')
82 globalNamespace.parse(globalNamespaceNode)
84 rootNode = doc.getElementsByTagName(
'VisualStates')[0].getElementsByTagName(
'state')[0]
85 rootState = State(0,
'root',
True,
None)
86 rootState.parse(rootNode)
90 if len(doc.getElementsByTagName(
'VisualStates')[0].getElementsByTagName(
'config')) > 0:
91 configElement = doc.getElementsByTagName(
'VisualStates')[0].getElementsByTagName(
'config')[0]
93 config.loadNode(configElement)
97 libraryElements = doc.getElementsByTagName(
'VisualStates')[0].getElementsByTagName(
'libraries')
98 if len(libraryElements) > 0:
99 libraryElements = libraryElements[0].getElementsByTagName(
'library')
100 for libElement
in libraryElements:
101 if len(libElement.childNodes) > 0:
102 libraries.append(libElement.childNodes[0].nodeValue)
104 return rootState, config, libraries, globalNamespace
114 if self.fullPath.rfind(os.sep) >= 0:
119 name = name[0:name.rfind(
'.')]
125 if self.fullPath.rfind(os.sep) >= 0:
126 path = self.
fullPath[0:self.fullPath.rfind(os.sep)]
def createDocFromState(self, state, doc)
def setFullPath(self, path)
def save(self, rootState, config, libraries, globalNamespace)