Exemplo n.º 1
0
def autoUpgrade(source, **params):
    """Turn older CNXML (0.5/0.6) into newer (0.7).
    Checks version to determine if upgrade is needed.
    With a newer version, we may add additional stylesheets to the pipeline.
    Return tuple of (new_source, boolean_was_converted)
    """
    version = Recognizer(source).getVersion(
    )  # if wrong, we could end up doing this on every save
    stylesheets = []
    if version in ('0.7', '0.8'):
        pass  # Do nothing. 0.7 is the latest
    elif version == '0.6':
        stylesheets.append(UPGRADE_06_TO_07_XSL)
    else:
        stylesheets.append(UPGRADE_05_TO_06_XSL)
        stylesheets.append(UPGRADE_06_TO_07_XSL)

    if source and stylesheets:
        try:
            doc = XMLService.parseString(source)
            result = XMLService.xsltPipeline(doc, stylesheets, **params)
            return result, True
        except XMLService.XMLParserError:
            pass  # just stopping on parse error is okay; it'll send us to the fallback below
    return source, False
Exemplo n.º 2
0
def autoIds(source, prefix=None, force=False, **params):
    """For CNXML, fill in ids where the author has left none. Only for CNXML 0.6, and will check
    for CNXML version unless instructed otherwise.
    'prefix' will add the passed-in string to the beginning of the ids
    'force' doesn't check version itself, relies on caller to certify the XML is okay.
    """
    transformable = force or Recognizer(source).getVersion() in ('0.6', '0.7')
    # we want to make sure recognizer keeps working right; if wrong, we end up doing this on every save

    if prefix:
        params['id-prefix'] = prefix
    stylesheets = [CNXML_AUTOID_XSL]
    if source and stylesheets and transformable:
        try:
            doc = XMLService.parseString(source)   # may error if malformed
            # TODO: if slow, we could replace with Expat 2-pass method
            result = XMLService.xsltPipeline(doc, stylesheets, **params)
            return result
        except XMLService.XMLParserError:
            pass  # just stopping on parse error is okay; it'll send us to the fallback below
    return source
Exemplo n.º 3
0
def autoIds(source, prefix=None, force=False, **params):
    """For CNXML, fill in ids where the author has left none. Only for CNXML 0.6, and will check
    for CNXML version unless instructed otherwise.
    'prefix' will add the passed-in string to the beginning of the ids
    'force' doesn't check version itself, relies on caller to certify the XML is okay.
    """
    transformable = force or Recognizer(source).getVersion() in ('0.6', '0.7')
    # we want to make sure recognizer keeps working right; if wrong, we end up doing this on every save

    if prefix:
        params['id-prefix'] = prefix
    stylesheets = [CNXML_AUTOID_XSL]
    if source and stylesheets and transformable:
        try:
            doc = XMLService.parseString(source)  # may error if malformed
            # TODO: if slow, we could replace with Expat 2-pass method
            result = XMLService.xsltPipeline(doc, stylesheets, **params)
            return result
        except XMLService.XMLParserError:
            pass  # just stopping on parse error is okay; it'll send us to the fallback below
    return source
Exemplo n.º 4
0
def autoUpgrade(source, **params):
    """Turn older CNXML (0.5/0.6) into newer (0.7).
    Checks version to determine if upgrade is needed.
    With a newer version, we may add additional stylesheets to the pipeline.
    Return tuple of (new_source, boolean_was_converted)
    """
    version = Recognizer(source).getVersion() # if wrong, we could end up doing this on every save
    stylesheets = []
    if version == '0.7':
        pass # Do nothing. 0.7 is the latest
    elif version == '0.6':
        stylesheets.append(UPGRADE_06_TO_07_XSL)
    else:
        stylesheets.append(UPGRADE_05_TO_06_XSL)
        stylesheets.append(UPGRADE_06_TO_07_XSL)

    if source and stylesheets:
        try:
            doc = XMLService.parseString(source)
            result = XMLService.xsltPipeline(doc, stylesheets, **params)
            return result, True
        except XMLService.XMLParserError:
            pass  # just stopping on parse error is okay; it'll send us to the fallback below
    return source, False
Exemplo n.º 5
0
    def _parseDoc(self):
        """
        Return the parsed XML document

        """
        return XMLService.parseString(self.getSource())
Exemplo n.º 6
0
 def normalize(self):
     """Return 'normalized' document with entities expanded, etc."""
     return XMLService.normalize(self.getSource())
Exemplo n.º 7
0
 def validate(self):
     """Validate the CNXML document"""
     return XMLService.validate(self.getSource())
Exemplo n.º 8
0
    def _parseDoc(self):
        """
        Return the parsed XML document

        """
        return XMLService.parseString(self.getSource())
Exemplo n.º 9
0
 def normalize(self):
     """Return 'normalized' document with entities expanded, etc."""
     return XMLService.normalize(self.getSource())
Exemplo n.º 10
0
 def validate(self):
     """Validate the CNXML document"""
     schema_url = 'http://cnx.rice.edu/technology/cnxml/schema/rng/%s/cnxml.rng' % (
         self.getVersion())
     return XMLService.validate(self.getSource(), schema_url)
Exemplo n.º 11
0
 def validate(self):
     """Validate the CNXML document"""
     schema_url = 'http://cnx.rice.edu/technology/cnxml/schema/rng/%s/cnxml.rng' % (self.getVersion())
     return XMLService.validate(self.getSource(), schema_url)