|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 |
|
7 """ |
|
8 Module implementing a class for reading a highlighting styles XML file. |
|
9 """ |
|
10 |
|
11 from PyQt4.QtGui import QColor, QFont |
|
12 |
|
13 from .Config import highlightingStylesFileFormatVersion |
|
14 from .XMLStreamReaderBase import XMLStreamReaderBase |
|
15 |
|
16 class HighlightingStylesReader(XMLStreamReaderBase): |
|
17 """ |
|
18 Class for reading a highlighting styles XML file. |
|
19 """ |
|
20 supportedVersions = ["4.3"] |
|
21 |
|
22 def __init__(self, device, lexers): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param device reference to the I/O device to read from (QIODevice) |
|
27 @param lexers list of lexer objects for which to export the styles |
|
28 """ |
|
29 XMLStreamReaderBase.__init__(self, device) |
|
30 |
|
31 self.lexers = lexers |
|
32 |
|
33 self.version = "" |
|
34 |
|
35 def readXML(self): |
|
36 """ |
|
37 Public method to read and parse the XML document. |
|
38 """ |
|
39 while not self.atEnd(): |
|
40 self.readNext() |
|
41 if self.isStartElement(): |
|
42 if self.name() == "HighlightingStyles": |
|
43 self.version = self.attribute("version", |
|
44 highlightingStylesFileFormatVersion) |
|
45 if self.version not in self.supportedVersions: |
|
46 self.raiseUnsupportedFormatVersion(self.version) |
|
47 elif self.name() == "Lexer": |
|
48 self.__readLexer() |
|
49 else: |
|
50 self.raiseUnexpectedStartTag(self.name()) |
|
51 |
|
52 self.showErrorMessage() |
|
53 |
|
54 def __readLexer(self): |
|
55 """ |
|
56 Private method to read the lexer info. |
|
57 """ |
|
58 language = self.attribute("name") |
|
59 if language and language in self.lexers: |
|
60 lexer = self.lexers[language] |
|
61 else: |
|
62 lexer = None |
|
63 |
|
64 while not self.atEnd(): |
|
65 self.readNext() |
|
66 if self.isEndElement() and self.name() == "Lexer": |
|
67 break |
|
68 |
|
69 if self.isStartElement(): |
|
70 if self.name() == "Style": |
|
71 self.__readStyle(lexer) |
|
72 else: |
|
73 self.raiseUnexpectedStartTag(self.name()) |
|
74 |
|
75 def __readStyle(self, lexer): |
|
76 """ |
|
77 Private method to read the style info. |
|
78 |
|
79 @param lexer reference to the lexer object |
|
80 """ |
|
81 if lexer is not None: |
|
82 style = self.attribute("style") |
|
83 if style: |
|
84 style = int(style) |
|
85 |
|
86 color = self.attribute("color") |
|
87 if color: |
|
88 color = QColor(color) |
|
89 else: |
|
90 color = lexer.defaultColor(style) |
|
91 lexer.setColor(color, style) |
|
92 |
|
93 paper = self.attribute("paper") |
|
94 if paper: |
|
95 paper = QColor(paper) |
|
96 else: |
|
97 paper = lexer.defaultPaper(style) |
|
98 lexer.setPaper(paper, style) |
|
99 |
|
100 fontStr = self.attribute("font") |
|
101 if fontStr: |
|
102 font = QFont() |
|
103 font.fromString(fontStr) |
|
104 else: |
|
105 font = lexer.defaultFont(style) |
|
106 lexer.setFont(font, style) |
|
107 |
|
108 eolfill = self.attribute("eolfill") |
|
109 if eolfill: |
|
110 eolfill = self.toBool(eolfill) |
|
111 if eolfill is None: |
|
112 eolfill = lexer.defaulEolFill(style) |
|
113 else: |
|
114 eolfill = lexer.defaulEolFill(style) |
|
115 lexer.setEolFill(eolfill, style) |
|
116 |
|
117 while not self.atEnd(): |
|
118 self.readNext() |
|
119 if self.isEndElement() and self.name() == "Style": |
|
120 break |
|
121 |
|
122 if self.isStartElement(): |
|
123 self.raiseUnexpectedStartTag(self.name()) |