def startElement(output, localname, namespace, prefix, attrs): """Wrapper to emit a start tag""" if readable: output.characters(u"\n") # for readability if useNamespaces: nsAttrs = {} for (att, value) in attrs.items(): nsAttrs[(None, att)] = value qnames = attrs.keys() output.startElementNS((namespace, localname), prefix + localname, xmlreader.AttributesNSImpl(nsAttrs, qnames)) else: output.startElement(prefix + localname, xmlreader.AttributesImpl(attrs))
def parse(self, source): self.__parsing = 1 try: # prepare source and create reader source = saxutils.prepare_input_source(source) input = libxml2.inputBuffer(source.getByteStream()) reader = input.newTextReader(source.getSystemId()) reader.SetErrorHandler(self._errorHandler, None) # configure reader reader.SetParserProp(libxml2.PARSER_LOADDTD, 1) reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS, 1) reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES, 0) reader.SetParserProp(libxml2.PARSER_VALIDATE, 0) # we reuse attribute maps (for a slight performance gain) attributesImpl = xmlreader.AttributesImpl({}) # start loop self._cont_handler.startDocument() while 1: r = reader.Read() # check for errors if r == 1: pass if self.__errors is not None: self._reportErrors(0) elif r == 0: if self.__errors is not None: self._reportErrors(0) break # end of parse else: if self.__errors is not None: self._reportErrors(1) else: self._err_handler.fatalError( SAXException("Read failed (no details available)")) break # fatal parse error # get node type nodeType = reader.NodeType() # Element if nodeType == 1: eltName = reader.Name() attributesImpl._attrs = attrs = {} while reader.MoveToNextAttribute(): attName = reader.Name() attrs[attName] = reader.Value() reader.MoveToElement() self._cont_handler.startElement(eltName, attributesImpl) if reader.IsEmptyElement(): self._cont_handler.endElement(eltName) # EndElement elif nodeType == 15: self._cont_handler.endElement(reader.Name()) # Text elif nodeType == 3: self._cont_handler.characters(reader.Value()) # SignificantWhitespace elif nodeType == 14: self._cont_handler.characters(reader.Value()) # EntityReference elif nodeType == 5: # Treating entity as such self._cont_handler.entity(reader.Name()) elif nodeType == 10: # We parse the doctype with a SAX parser nodeText = str(reader.CurrentNode()) entityDeclParser = libxml2.createPushParser( self._cont_handler, nodeText, len(nodeText), "doctype") entityDeclParser.parseChunk("", 0, 1) pass # Ignore all other node types if r == 0: self._cont_handler.endDocument() reader.Close() finally: self.__parsing = 0
def parse(self, source): self.__parsing = 1 try: # prepare source and create reader if type(source) in StringTypes: reader = libxml2.newTextReaderFilename(source) else: source = saxutils.prepare_input_source(source) input = libxml2.inputBuffer(source.getByteStream()) reader = input.newTextReader(source.getSystemId()) reader.SetErrorHandler(self._errorHandler, None) # configure reader reader.SetParserProp(libxml2.PARSER_LOADDTD, 1) reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS, 1) reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES, 1) reader.SetParserProp(libxml2.PARSER_VALIDATE, self.__validate) # we reuse attribute maps (for a slight performance gain) if self.__ns: attributesNSImpl = xmlreader.AttributesNSImpl({}, {}) else: attributesImpl = xmlreader.AttributesImpl({}) # prefixes to pop (for endPrefixMapping) prefixes = [] # start loop self._cont_handler.startDocument() while 1: r = reader.Read() # check for errors if r == 1: if not self.__errors is None: self._reportErrors(0) elif r == 0: if not self.__errors is None: self._reportErrors(0) break # end of parse else: if not self.__errors is None: self._reportErrors(1) else: self._err_handler.fatalError(\ SAXException("Read failed (no details available)")) break # fatal parse error # get node type nodeType = reader.NodeType() # Element if nodeType == 1: if self.__ns: eltName = (_d(reader.NamespaceUri()),\ _d(reader.LocalName())) eltQName = _d(reader.Name()) attributesNSImpl._attrs = attrs = {} attributesNSImpl._qnames = qnames = {} newPrefixes = [] while reader.MoveToNextAttribute(): qname = _d(reader.Name()) value = _d(reader.Value()) if qname.startswith("xmlns"): if len(qname) > 5: newPrefix = qname[6:] else: newPrefix = None newPrefixes.append(newPrefix) self._cont_handler.startPrefixMapping(\ newPrefix,value) if not self.__nspfx: continue # don't report xmlns attribute attName = (_d(reader.NamespaceUri()), _d(reader.LocalName())) qnames[attName] = qname attrs[attName] = value reader.MoveToElement() self._cont_handler.startElementNS( \ eltName,eltQName,attributesNSImpl) if reader.IsEmptyElement(): self._cont_handler.endElementNS(eltName, eltQName) for newPrefix in newPrefixes: self._cont_handler.endPrefixMapping(newPrefix) else: prefixes.append(newPrefixes) else: eltName = _d(reader.Name()) attributesImpl._attrs = attrs = {} while reader.MoveToNextAttribute(): attName = _d(reader.Name()) attrs[attName] = _d(reader.Value()) reader.MoveToElement() self._cont_handler.startElement( \ eltName,attributesImpl) if reader.IsEmptyElement(): self._cont_handler.endElement(eltName) # EndElement elif nodeType == 15: if self.__ns: self._cont_handler.endElementNS( \ (_d(reader.NamespaceUri()),_d(reader.LocalName())), _d(reader.Name())) for prefix in prefixes.pop(): self._cont_handler.endPrefixMapping(prefix) else: self._cont_handler.endElement(_d(reader.Name())) # Text elif nodeType == 3: self._cont_handler.characters(_d(reader.Value())) # Whitespace elif nodeType == 13: self._cont_handler.ignorableWhitespace(_d(reader.Value())) # SignificantWhitespace elif nodeType == 14: self._cont_handler.characters(_d(reader.Value())) # CDATA elif nodeType == 4: if not self.__lex_handler is None: self.__lex_handler.startCDATA() self._cont_handler.characters(_d(reader.Value())) if not self.__lex_handler is None: self.__lex_handler.endCDATA() # EntityReference elif nodeType == 5: if not self.__lex_handler is None: self.startEntity(_d(reader.Name())) reader.ResolveEntity() # EndEntity elif nodeType == 16: if not self.__lex_handler is None: self.endEntity(_d(reader.Name())) # ProcessingInstruction elif nodeType == 7: self._cont_handler.processingInstruction( \ _d(reader.Name()),_d(reader.Value())) # Comment elif nodeType == 8: if not self.__lex_handler is None: self.__lex_handler.comment(_d(reader.Value())) # DocumentType elif nodeType == 10: #if not self.__lex_handler is None: # self.__lex_handler.startDTD() pass # TODO (how to detect endDTD? on first non-dtd event?) # XmlDeclaration elif nodeType == 17: pass # TODO # Entity elif nodeType == 6: pass # TODO (entity decl) # Notation (decl) elif nodeType == 12: pass # TODO # Attribute (never in this loop) #elif nodeType == 2: # pass # Document (not exposed) #elif nodeType == 9: # pass # DocumentFragment (never returned by XmlReader) #elif nodeType == 11: # pass # None #elif nodeType == 0: # pass # - else: raise SAXException("Unexpected node type %d" % nodeType) if r == 0: self._cont_handler.endDocument() reader.Close() finally: self.__parsing = 0
def startElement(self, localname, attrs): def modify_style(style, old_style, new_style=None): styles = style.split(';') new_styles = [] if old_style is not None: match_to = old_style + ':' for s in styles: if len(s) > 0 and (old_style is None or not s.startswith(match_to)): new_styles.append(s) if new_style is not None: new_styles.append(new_style) return ';'.join(new_styles) dict = {} is_throwaway_layer = False is_slices = False is_hotspots = False is_shadows = False is_layer = False if localname == 'g': for key, value in attrs.items(): if key == 'inkscape:label': if value == 'slices': is_slices = True elif value == 'hotspots': is_hotspots = True elif value == 'shadows': is_shadows = True elif key == 'inkscape:groupmode': if value == 'layer': is_layer = True if mode_shadows in self.mode and is_shadows: # Only remove the shadows is_throwaway_layer = True elif mode_hotspots in self.mode and not (is_hotspots or is_slices): # Remove all layers but hotspots and slices if localname == 'g': is_throwaway_layer = True idict = {} idict.update(attrs) if 'style' not in attrs.keys(): idict['style'] = '' for key, value in idict.items(): alocalname = key if alocalname == 'style': had_style = True if alocalname == 'style' and is_slices: # Make slices invisible. Do not check the mode, because there is # no circumstances where we *want* to render slices value = modify_style(value, 'display', 'display:none') if alocalname == 'style' and is_hotspots: if mode_hotspots in self.mode: # Make hotspots visible in hotspots mode value = modify_style(value, 'display', 'display:inline') else: # Make hotspots invisible otherwise value = modify_style(value, 'display', 'display:none') if alocalname == 'style' and mode_invert in self.mode and is_layer and is_shadows: value = modify_style(value, None, 'filter:url(#InvertFilter)') dict[key] = value if self.in_throwaway_layer_stack[0] or is_throwaway_layer: self.in_throwaway_layer_stack.insert(0, True) else: self.in_throwaway_layer_stack.insert(0, False) attrs = xmlreader.AttributesImpl(dict) self._downstream.startElement(localname, attrs)
def startElement(self, localname, attrs): def modify_style(style, old_style, new_style=None): styles = style.split(";") new_styles = [] if old_style is not None: match_to = old_style + ":" for s in styles: if len(s) > 0 and (old_style is None or not s.startswith(match_to)): new_styles.append(s) if new_style is not None: new_styles.append(new_style) return ";".join(new_styles) dict = {} is_throwaway_layer = False is_slices = False is_hotspots = False is_shadows = False is_layer = False if localname == "g": for key, value in attrs.items(): if key == "inkscape:label": if value == "slices": is_slices = True elif value == "hotspots": is_hotspots = True elif value == "shadows": is_shadows = True elif key == "inkscape:groupmode": if value == "layer": is_layer = True if MODE_SHADOWS in self.mode and is_shadows: # Only remove the shadows is_throwaway_layer = True elif MODE_HOTSPOTS in self.mode and not (is_hotspots or is_slices): # Remove all layers but hotspots and slices if localname == "g": is_throwaway_layer = True idict = {} idict.update(attrs) if "style" not in attrs.keys(): idict["style"] = "" for key, value in idict.items(): alocalname = key if alocalname == "style": had_style = True if alocalname == "style" and is_slices: # Make slices invisible. Do not check the mode, because there is # no circumstances where we *want* to render slices value = modify_style(value, "display", "display:none") if alocalname == "style" and is_hotspots: if MODE_HOTSPOTS in self.mode: # Make hotspots visible in hotspots mode value = modify_style(value, "display", "display:inline") else: # Make hotspots invisible otherwise value = modify_style(value, "display", "display:none") if (alocalname == "style" and MODE_INVERT in self.mode and is_layer and is_shadows): value = modify_style(value, None, "filter:url(#InvertFilter)") dict[key] = value if self.in_throwaway_layer_stack[0] or is_throwaway_layer: self.in_throwaway_layer_stack.insert(0, True) else: self.in_throwaway_layer_stack.insert(0, False) attrs = xmlreader.AttributesImpl(dict) self._downstream.startElement(localname, attrs)
def startElement(self, localname, attrs): def modify_style (style, old_style, new_style=None): styles = style.split (';') new_styles = [] if old_style is not None: match_to = old_style + ':' for s in styles: if len (s) > 0 and (old_style is None or not s.startswith (match_to)): new_styles.append(s) if new_style is not None: new_styles.append(new_style) return ';'.join(new_styles) new_attrs = {} is_throwaway_layer = False is_slices = False is_hotspots = False is_shadows = False is_layer = False if localname == 'g': for key, value in attrs.items(): if key == 'inkscape:label': if value == 'slices': is_slices = True elif value == 'hotspots': is_hotspots = True elif value == 'shadow': is_shadows = True if 'shadows,' in self.mode and not self._dropshadow_id: warn("Could not enable drop shadow, 'Drop Shadow' filter does not exist in SVG") elif key == 'inkscape:groupmode': if value == 'layer': is_layer = True if localname == 'filter': try: if attrs['inkscape:label'] == 'Drop Shadow': self._dropshadow_id = attrs['id'] except KeyError: pass idict = {} idict.update(attrs) if 'style' not in attrs.keys(): idict['style'] = '' for key, value in idict.items(): alocalname = key if alocalname == 'style' and is_slices: value = modify_style(value, 'display', 'display:none') if alocalname == 'style' and is_hotspots: if 'hotspots,' in self.mode: value = modify_style(value, 'display', 'display:inline') else: value = modify_style(value, 'display', 'display:none') if is_shadows and alocalname == 'filter': if 'shadows,' in self.mode and self._dropshadow_id: value = 'url(#{})'.format(self._dropshadow_id) else: value = '' elif is_shadows and alocalname == 'style': if 'shadows,' in self.mode and self._dropshadow_id: value = modify_style(value, 'filter', 'filter:url(#{})'.format(self._dropshadow_id)) else: value = modify_style(value, 'filter', None) value = modify_style(value, 'shape-rendering', None) if value != '': new_attrs[key] = value if self.in_throwaway_layer_stack[0] or is_throwaway_layer: self.in_throwaway_layer_stack.insert(0, True) else: self.in_throwaway_layer_stack.insert(0, False) attrs = xmlreader.AttributesImpl(new_attrs) self._downstream.startElement(localname, attrs)