Package node_manager_fkie :: Module xml_highlighter
[frames] | no frames]

Source Code for Module node_manager_fkie.xml_highlighter

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  4  # based on code of Timo Roehling 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions 
  9  # are met: 
 10  # 
 11  #  * Redistributions of source code must retain the above copyright 
 12  #    notice, this list of conditions and the following disclaimer. 
 13  #  * Redistributions in binary form must reproduce the above 
 14  #    copyright notice, this list of conditions and the following 
 15  #    disclaimer in the documentation and/or other materials provided 
 16  #    with the distribution. 
 17  #  * Neither the name of Fraunhofer nor the names of its 
 18  #    contributors may be used to endorse or promote products derived 
 19  #    from this software without specific prior written permission. 
 20  # 
 21  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 22  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 23  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 24  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 25  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 26  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 27  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 28  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 29  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 30  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 31  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 32  # POSSIBILITY OF SUCH DAMAGE. 
 33   
 34  from PySide import QtGui 
 35  from PySide import QtCore 
 36   
37 -class XmlHighlighter(QtGui.QSyntaxHighlighter):
38 ''' 39 Enabled the syntax highlightning for the ROS launch files. 40 ''' 41
42 - def __init__(self, parent=None):
43 QtGui.QSyntaxHighlighter.__init__(self, parent) 44 self.rules = [] 45 self.commentStart = QtCore.QRegExp("<!--") 46 self.commentEnd = QtCore.QRegExp("-->") 47 self.commentFormat = QtGui.QTextCharFormat() 48 f = QtGui.QTextCharFormat() 49 r = QtCore.QRegExp() 50 r.setMinimal(True) 51 f.setFontWeight(QtGui.QFont.Normal) 52 f.setForeground (QtCore.Qt.darkBlue) 53 tagList = ["\\blaunch\\b", "\\bnode\\b", "\\bmachine\\b", "\\binclude\\b", 54 "\\bremap\\b", "\\benv-loader\\b", "\\bparam\\b", "\\brosparam\\b", 55 "\\bgroup\\b", "\\btest\\b", "\\barg\\b"] 56 for tag in tagList: 57 r.setPattern(tag) 58 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 59 60 f.setForeground(QtCore.Qt.darkGreen) 61 attrList = ["\\bdeprecated=", "\\bpkg=", "\\btype=", "\\bname=", 62 "\\bargs=", "\\bmachine=", "\\brespawn=", "\\brequired=", 63 "\\bns=", "\\bclear_params=", "\\boutput=", "\\bcwd=", 64 "\\blaunch-prefix=", "\\baddress=", "\\benv-loader=", "\\bdefault=", 65 "\\buser=", "\\bpassword=", "\\btimeout=", "\\bros-root=", 66 "\\bros-package-path=", "\\bfile=", "\\bclear_params=", "\\bfrom=", 67 "\\bto=", "\\bvalue=", "\\btextfile=", "\\bbinfile=", 68 "\\bcommand=", "\\btest-name=", "\\btime-limit=", "\\bretry=", 69 "\\if=", "\\unless="] 70 for attr in attrList: 71 r.setPattern(attr) 72 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 73 74 f.setForeground(QtCore.Qt.magenta) 75 r.setPattern("\".*\"") 76 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 77 78 f.setForeground(QtGui.QColor(127,64,127)) 79 r.setPattern ("\\$\\(.*\\)") 80 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 81 82 f.setForeground (QtCore.Qt.lightGray) 83 r.setPattern ("<!DOCTYPE.*>") 84 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 85 r.setPattern ("<\\?xml.*\\?>") 86 self.rules.append((QtCore.QRegExp(r), QtGui.QTextCharFormat(f))) 87 88 self.commentFormat.setFontItalic(True) 89 self.commentFormat.setForeground(QtCore.Qt.darkGray)
90 91
92 - def highlightBlock(self, text):
93 for pattern, format in self.rules: 94 index = pattern.indexIn(text) 95 while index >= 0: 96 length = pattern.matchedLength() 97 self.setFormat(index, length, format) 98 index = pattern.indexIn(text, index + length) 99 100 self.setCurrentBlockState(0) 101 startIndex = 0 102 if self.previousBlockState() != 1: 103 startIndex = self.commentStart.indexIn(text) 104 while startIndex >= 0: 105 endIndex = self.commentEnd.indexIn(text, startIndex) 106 commentLength = 0 107 if endIndex == -1: 108 self.setCurrentBlockState(1) 109 commentLength = len(text) - startIndex 110 else: 111 commentLength = endIndex - startIndex + self.commentEnd.matchedLength() 112 self.setFormat(startIndex, commentLength, self.commentFormat) 113 startIndex = self.commentStart.indexIn(text, startIndex + commentLength)
114