aboutsummaryrefslogtreecommitdiff
path: root/rpki/relaxng_parser.py
blob: 53ec8f0d9516b521e9d63625a9977f0f4bdc8895 (plain) (blame)
1
2
3
4
5
6
7
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight
# $Id$
#
# Copyright (C) 2014  Dragon Research Labs ("DRL")
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND DRL DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL DRL BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

"""
Wrapper around lxml to extract various useful data and methods
from an XML-format RelaxNG schema.
"""

import lxml.etree

class RelaxNGParser(object):
    """
    Parse schema, extract XML namespace and protocol version (if any).
    Method calls are just passed along to the parsed RelaxNG schema.
    """

    def __init__(self, text):
        xml = lxml.etree.fromstring(text)
        self.schema = lxml.etree.RelaxNG(xml)
        ns = xml.get("ns")
        self.xmlns = "{" + ns + "}"
        self.nsmap = { None : ns }
        x = xml.xpath("ns0:define[@name = 'version']/ns0:value",
                      namespaces = dict(ns0 = "http://relaxng.org/ns/structure/1.0"))
        if len(x) == 1:
            self.version = x[0].text

    def __getattr__(self, name):
        return getattr(self.schema, name)