def release(self): Util.FreeDocumentIndex(self.node) for doc in self.documents.values(): Util.FreeDocumentIndex(doc) ReleaseNode(doc) try: for rtf in self.rtfs: ReleaseNode(rtf) except: import traceback traceback.print_exc() pass self.documents = None self.rtfs = None return
def instantiate(self, context, processor): origState = context.copy() context.setNamespaces(self._nss) name = self._name.evaluate(context) namespace = self._namespace.evaluate(context) (prefix, local) = xml.dom.ext.SplitQName(name) if not namespace and prefix: namespace = context.processorNss[prefix] #FIXME: Use proper pysax AttributeList objects processor.writers[-1].startElement(name, namespace) for attr_set_name in self._useAttributeSets: split_name = Util.ExpandQName(attr_set_name, namespaces=context.processorNss) try: attr_set = processor.attributeSets[split_name] except KeyError: raise XsltException(Error.UNDEFINED_ATTRIBUTE_SET, attr_set_name) attr_set.use(context, processor) for child in self.childNodes: context = child.instantiate(context, processor)[0] processor.writers[-1].endElement(name) context.set(origState) return (context, )
def setup(self): self.__dict__['_nss'] = xml.dom.ext.GetAllNs(self) mode_attr = self.getAttributeNS(EMPTY_NAMESPACE, 'mode') if mode_attr == '': self.__dict__['_mode'] = None else: split_name = Util.ExpandQName(mode_attr, namespaces=self._nss) self.__dict__['_mode'] = split_name select = self.getAttributeNS(EMPTY_NAMESPACE, 'select') if select: parser = XPathParser.XPathParser() self.__dict__['_expr'] = parser.parseExpression(select) else: self.__dict__['_expr'] = None self.__dict__['_sortSpecs'] = [] self.__dict__['_params'] = [] for child in self.childNodes: #All children should be sort and with-param if child.namespaceURI == XSL_NAMESPACE: if child.localName == 'sort': self._sortSpecs.append(child) elif child.localName == 'with-param': self._params.append(child) else: raise XsltException(Error.ILLEGAL_APPLYTEMPLATE_CHILD) else: raise XsltException(Error.ILLEGAL_APPLYTEMPLATE_CHILD) return
def setup(self): self.__dict__['_nss'] = xml.dom.ext.GetAllNs(self) self.__dict__['_match'] = self.getAttributeNS(EMPTY_NAMESPACE, 'match') mode_attr = self.getAttributeNS(EMPTY_NAMESPACE, 'mode') if not mode_attr: self.__dict__['_mode'] = None else: split_name = Util.ExpandQName(mode_attr, namespaces=self._nss) self.__dict__['_mode'] = split_name name_attr = self.getAttributeNS(EMPTY_NAMESPACE, 'name') split_name = Util.ExpandQName(name_attr, namespaces=self._nss) self.__dict__['_name'] = split_name self.__dict__['_params'] = [] self.__dict__['_elements'] = [] for child in self.childNodes: if child.namespaceURI == XSL_NAMESPACE: if child.localName == 'param': self.__dict__['_params'].append(child) elif child.localName in ['choose', 'if']: self.__dict__['_elements'].append((1, child)) else: self.__dict__['_elements'].append((0, child)) else: self.__dict__['_elements'].append((0, child)) #A list of tuples #(pattern,qmatch,priority) #either pattern or qmatch will be present but not both self.__dict__['_patternInfo'] = [] if self._match: priority = self.getAttributeNS(EMPTY_NAMESPACE, 'priority') or None if priority is not None: try: priority = float(priority) except: raise XsltException(Error.ILLEGAL_TEMPLATE_PRIORITY) parser = XPatternParser.XPatternParser() shortcuts = parser.parsePattern(self._match).getMatchShortcuts() for pattern, (shortcut, extra_arg) in shortcuts: if priority is None: tpl_priority = pattern.priority else: tpl_priority = priority self.__dict__['_patternInfo'].append( (shortcut, extra_arg, tpl_priority))
def GenerateId(context, nodeSet=None): if nodeSet is not None and type(nodeSet) != type([]): raise XsltException(Error.WRONG_ARGUMENT_TYPE) if not nodeSet: return 'id' + ` id(context.node) ` else: node = Util.SortDocOrder(nodeSet)[0] return 'id' + ` id(node) `
def ElementAvailable(context, qname): split_name = Util.ExpandQName(Conversions.StringValue(qname), namespaces=context.processorNss) if g_extElements.has_key( split_name) or CoreFunctions.CoreFunctions.has_key(split_name): return CoreFunctions. True (context) else: return CoreFunctions. False (context)
def instantiate(self, context, processor): origState = context.copy() context.setNamespaces(self._nss) split_name = Util.ExpandQName(self._name, namespaces=self._nss) processor.attributeSets[split_name] = self self._varBindings = context.varBindings context.set(origState) return (context, )
def evaluate(self, context): lSet = self._left.evaluate(context) if type(lSet) != type([]): raise StringException( "Left Expression does not evaluate to a node set") rSet = self._right.evaluate(context) if type(rSet) != type([]): raise StringException( "Right Expression does not evaluate to a node set") set = Set.Union(lSet, rSet) set = Util.SortDocOrder(set) return set
def setup(self): self._nss = xml.dom.ext.GetAllNs(self) name_attr = self.getAttributeNS(EMPTY_NAMESPACE, 'name') split_name = Util.ExpandQName(name_attr, namespaces=self._nss) self._name = split_name self._select = self.getAttributeNS(EMPTY_NAMESPACE, 'select') if self._select: parser = XPathParser.XPathParser() self._expr = parser.parseExpression(self._select) else: self._expr = None return
def Key(context, qname, keyList): result = [] name = Util.ExpandQName(Conversions.StringValue(qname), namespaces=context.processorNss) if context.stylesheet.keys.has_key(name): a_dict = context.stylesheet.keys[name] if type(keyList) != type([]): keyList = [keyList] for key in keyList: key = Conversions.StringValue(key) result = result + a_dict.get(key, []) return result
def FormatNumber(context, number, formatString, decimalFormatName=None): decimal_format = '' num = Conversions.NumberValue(number) format_string = Conversions.StringValue(formatString) if decimalFormatName is not None: split_name = Util.ExpandQName(decimalFormatName, namespaces=context.processorNss) decimal_format = context.stylesheet.decimalFormats[split_name] else: decimal_format = context.stylesheet.decimalFormats[''] from Ft.Lib import routines result = routines.FormatNumber(num, format_string) return result
def execute(self, node, ignorePis=0, topLevelParams=None, writer=None, baseUri='', outputStream=None): """ Run the stylesheet processor against the given XML DOM node with the stylesheets that have been registered. Does not mutate the DOM If writer is None, use the XmlWriter, otherwise, use the supplied writer """ #FIXME: What about ws stripping? topLevelParams = topLevelParams or {} if len(self._stylesheets) == 0: raise XsltException(Error.NO_STYLESHEET) self._outputParams = self._stylesheets[0].outputParams if writer: self.writers = [writer] else: self.addHandler(self._outputParams, outputStream, 0) self._namedTemplates = {} tlp = topLevelParams.copy() for sty in self._stylesheets: sty.processImports(node, self, tlp) named = sty.getNamedTemplates() for name,template_info in named.items(): if not self._namedTemplates.has_key(name): self._namedTemplates[name] = template_info for sty in self._stylesheets: tlp = sty.prime(node, self, tlp) #Run the document through the style sheets self.writers[-1].startDocument() context = XsltContext.XsltContext(node, 1, 1, None, processor=self) try: self.applyTemplates(context, None) self.writers[-1].endDocument() Util.FreeDocumentIndex(node) result = self.writers[-1].getResult() finally: self._reset() context.release() return result
def SystemProperty(context, qname): uri, lname = Util.ExpandQName(Conversions.StringValue(qname), namespaces=context.processorNss) if uri == XSL_NAMESPACE: if lname == 'version': return 1.0 if lname == 'vendor': return "Fourthought Inc." if lname == 'vendor-url': return "http://4Suite.org" elif uri == 'http://xmlns.4suite.org/xslt/env-system-property': return os.environ.get(lname, '') elif uri == 'http://xmlns.4suite.org': if lname == 'version': return __version__ return ''
def LocalName(context, nodeSet=None): """Function: <string> local-name(<node-set>?)""" if nodeSet is None: node = context.node else: if type(nodeSet) != type([]): raise RuntimeException(RuntimeException.WRONG_ARGUMENTS, 'local-name', _("expected node set")) nodeSet = Util.SortDocOrder(nodeSet) if type(nodeSet) != type([]) or len(nodeSet) == 0: return '' node = nodeSet[0] en = ExpandedName(node) if en == None or en.localName == None: return '' return en.localName
def instantiate(self, context, processor): origState = context.copy() context.setNamespaces(self._nss) node = context.node if node.nodeType == Node.TEXT_NODE: processor.writers[-1].text(node.data) elif node.nodeType == Node.ELEMENT_NODE: #FIXME: Use proper pysax AttributeList objects processor.writers[-1].startElement(node.nodeName, node.namespaceURI) for attr_set_name in self._useAttributeSets: split_name = Util.ExpandQName(attr_set_name, namespaces=context.processorNss) try: attr_set = processor.attributeSets[split_name] except KeyError: raise XsltException(Error.UNDEFINED_ATTRIBUTE_SET, attr_set_name) attr_set.use(context, processor) for child in self.childNodes: context = child.instantiate(context, processor)[0] processor.writers[-1].endElement(node.nodeName) elif node.nodeType == Node.DOCUMENT_NODE: for child in self.childNodes: context = child.instantiate(context, processor)[0] elif node.nodeType == Node.ATTRIBUTE_NODE: if node.namespaceURI == XMLNS_NAMESPACE: nodeName = 'xmlns' + (node.localName and ':' + node.localName) processor.writers[-1].attribute(nodeName, node.nodeValue, node.namespaceURI) else: processor.writers[-1].attribute(node.nodeName, node.nodeValue, node.namespaceURI) elif node.nodeType == Node.PROCESSING_INSTRUCTION_NODE: processor.writers[-1].processingInstruction(node.target, node.data) elif node.nodeType == Node.COMMENT_NODE: processor.writers[-1].comment(node.data) else: raise Exception("Unknown Node Type %d" % node.nodeType) context.set(origState) return (context,)
def use(self, context, processor, used=None): if used is None: used = [] origState = context.copy() context.varBindings = self._varBindings for attr_set_name in self._useAttributeSets: split_name = Util.ExpandQName(attr_set_name, namespaces=context.processorNss) try: attr_set = processor.attributeSets[split_name] except KeyError: raise XsltException(Error.UNDEFINED_ATTRIBUTE_SET, attr_set_name) attr_set.use(context, processor) for child in self.childNodes: context = child.instantiate(context, processor)[0] context.set(origState) return context
def NamespaceUri(context, nodeSet=None): """Function: <string> namespace-uri(<node-set>?)""" if nodeSet is None: node = context.node else: if type(nodeSet) != type([]): raise RuntimeException( RuntimeException.WRONG_ARGUMENTS, "namespace-uri", _("expected node set"), ) nodeSet = Util.SortDocOrder(nodeSet) if type(nodeSet) != type([]) or len(nodeSet) == 0: return "" node = nodeSet[0] en = ExpandedName(node) if en == None or en.namespaceURI == None: return "" return en.namespaceURI
def Id(context, object): """Function: <node-set> id(<object>)""" id_list = [] if type(object) != type([]): st = Conversions.StringValue(object) id_list = string.split(st) else: for n in object: id_list.append(Conversions.StringValue(n)) rt = [] for id in id_list: doc = context.node.ownerDocument or context.node elements = Util.ElementsById(doc.documentElement, id) if len(elements) > 1: raise RuntimeException(RuntimeException.WRONG_ARGUMENTS, 'id', _("argument not unique")) elif elements: # Must be 1 rt.append(elements[0]) return rt
def instantiate(self, context, processor): origState = context.copy() context.setNamespaces(self._nss) processor.writers[-1].startElement(self._aliasNodeName, self._aliasUri, self._outputNss) for (name, uri, avt) in self.__attrs: value = avt.evaluate(context) processor.writers[-1].attribute(name, value, uri) for attr_set_name in self._useAttributeSets: split_name = Util.ExpandQName(attr_set_name, namespaces=context.processorNss) try: attr_set = processor.attributeSets[split_name] except KeyError: raise XsltException(Error.UNDEFINED_ATTRIBUTE_SET, attr_set_name) attr_set.use(context, processor) for child in self.childNodes: context = child.instantiate(context, processor)[0] processor.writers[-1].endElement(self._aliasNodeName) context.set(origState) return (context, )
def _instantiateMode(self, context): rt = self._mode.evaluate(context) split_name = Util.ExpandQName(rt, namespaces=self._nss) return split_name