Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Static Protected Member Functions
txStandaloneXSLTProcessor Class Reference

txStandaloneXSLTProcessor More...

#include <txStandaloneXSLTProcessor.h>

Inheritance diagram for txStandaloneXSLTProcessor:
Inheritance graph
[legend]
Collaboration diagram for txStandaloneXSLTProcessor:
Collaboration graph
[legend]

List of all members.

Public Member Functions

nsresult transform (nsACString &aXMLPath, ostream &aOut, ErrorObserver &aErr)
 Methods that print the result to a stream.
nsresult transform (nsACString &aXMLPath, nsACString &aXSLPath, ostream &aOut, ErrorObserver &aErr)
 Transform a XML document given by path with the given stylesheet.
nsresult transform (txXPathNode &aXMLDoc, ostream &aOut, ErrorObserver &aErr)
 Transform a XML document.
nsresult transform (txXPathNode &aXMLDoc, txStylesheet *aXSLNode, ostream &aOut, ErrorObserver &aErr)
 Transform a XML document with the given stylesheet.

Static Public Member Functions

static MBool init ()
 Initialisation and shutdown routines.
static void shutdown ()
static nsresult execute (txExecutionState &aEs)

Static Protected Member Functions

static void getHrefFromStylesheetPI (Document &xmlDocument, nsAString &href)
 Parses all XML Stylesheet PIs associated with the given XML document.
static void parseStylesheetPI (const nsAFlatString &data, nsAString &type, nsAString &href)
 Parses the contents of data, returns the type and href pseudo attributes.
static txXPathNodeparsePath (const nsACString &aPath, ErrorObserver &aErr)
 Create a Document from a path.

Detailed Description

txStandaloneXSLTProcessor

Use of the standalone TransforMiiX API:

The XSLT Processor need initialisation and shutdown by txStandaloneXSLTProcessor::Init(); and txStandaloneXSLTProcessor::Shutdown(); Be sure to always call these functions in pairs.

The API to transform documents consists of entry points to transform either one or two documents. If you provide one document, the stylesheet location is computed from the processing instruction. If that cannot be found, an error is issued.

The result is output to a stream.

Documents can be provided either by path or by DOM Document.

Stylesheet parameters XXX TODO

Definition at line 81 of file txStandaloneXSLTProcessor.h.


Member Function Documentation

nsresult txXSLTProcessor::execute ( txExecutionState aEs) [static, inherited]

Definition at line 93 of file txXSLTProcessor.cpp.

{
    nsresult rv = NS_OK;
    txInstruction* instr;
    while ((instr = aEs.getNextInstruction())) {
        rv = instr->execute(aEs);
        NS_ENSURE_SUCCESS(rv, rv);
    }

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void txStandaloneXSLTProcessor::getHrefFromStylesheetPI ( Document xmlDocument,
nsAString &  href 
) [static, protected]

Parses all XML Stylesheet PIs associated with the given XML document.

Parses the XML Stylesheet PIs associated with the given XML document.

If any stylesheet PIs are found with type="text/xsl" the href pseudo attribute value will be added to the given href argument. If multiple text/xsl stylesheet PIs are found, the one closest to the end of the document is used.

If a stylesheet PIs is found with type="text/xsl", type="text/xml" or type="application/xml" the href pseudo attribute value will be appended to the given href argument. If multiple XSLT stylesheet PIs are found, the first one is used.

Definition at line 243 of file txStandaloneXSLTProcessor.cpp.

{
    Node* node = xmlDocument.getFirstChild();
    nsAutoString type;
    nsAutoString tmpHref;
    while (node) {
        if (node->getNodeType() == Node::PROCESSING_INSTRUCTION_NODE) {
            nsAutoString target;
            node->getNodeName(target);
            if (target.EqualsLiteral("xml-stylesheet")) {
                nsAutoString data;
                node->getNodeValue(data);
                type.Truncate();
                tmpHref.Truncate();
                parseStylesheetPI(data, type, tmpHref);
                if (type.EqualsLiteral("text/xsl") ||
                    type.EqualsLiteral("text/xml") ||
                    type.EqualsLiteral("application/xml")) {
                    href = tmpHref;
                    return;
                }
            }
        }
        node = node->getNextSibling();
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

TX_LG_IMPL MBool txXSLTProcessor::init ( ) [static, inherited]

Initialisation and shutdown routines.

Initilizes and cleansup all dependant classes

Definition at line 54 of file txXSLTProcessor.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

txXPathNode * txStandaloneXSLTProcessor::parsePath ( const nsACString &  aPath,
ErrorObserver aErr 
) [static, protected]

Create a Document from a path.

Parameters:
aPathpath to the xml file
aErrErrorObserver
Returns:
Document XML Document, or null on error

Definition at line 359 of file txStandaloneXSLTProcessor.cpp.

{
    NS_ConvertASCIItoUCS2 path(aPath);

    ifstream xmlInput(PromiseFlatCString(aPath).get(), ios::in);
    if (!xmlInput) {
        aErr.receiveError(NS_LITERAL_STRING("Couldn't open ") + path);
        return 0;
    }
    // parse source
    txXPathNode* xmlDoc;
    nsAutoString errors;
    nsresult rv = txParseFromStream(xmlInput, path, errors, &xmlDoc);
    xmlInput.close();
    if (NS_FAILED(rv) || !xmlDoc) {
        aErr.receiveError(NS_LITERAL_STRING("Parsing error \"") + errors +
                          NS_LITERAL_STRING("\""));
    }
    return xmlDoc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void txStandaloneXSLTProcessor::parseStylesheetPI ( const nsAFlatString data,
nsAString &  type,
nsAString &  href 
) [static, protected]

Parses the contents of data, returns the type and href pseudo attributes.

Definition at line 290 of file txStandaloneXSLTProcessor.cpp.

{
  nsAFlatString::const_char_iterator start, end;
  aData.BeginReading(start);
  aData.EndReading(end);
  nsAFlatString::const_char_iterator iter;
  PRInt8 found = 0;

  while (start != end) {
    SKIP_WHITESPACE(start, end);
    iter = start;
    SKIP_ATTR_NAME(iter, end);

    // Remember the attr name.
    const nsAString & attrName = Substring(start, iter);

    // Now check whether this is a valid name="value" pair.
    start = iter;
    SKIP_WHITESPACE(start, end);
    if (*start != '=') {
      // No '=', so this is not a name="value" pair.  We don't know
      // what it is, and we have no way to handle it.
      break;
    }

    // Have to skip the value.
    ++start;
    SKIP_WHITESPACE(start, end);
    PRUnichar q = *start;
    if (q != QUOTE && q != APOSTROPHE) {
      // Not a valid quoted value, so bail.
      break;
    }

    ++start;  // Point to the first char of the value.
    iter = start;
    while (iter != end && *iter != q) {
      ++iter;
    }
    if (iter == end) {
      // Oops, unterminated quoted string.
      break;
    }
    
    // At this point attrName holds the name of the "attribute" and
    // the value is between start and iter.
    if (attrName.EqualsLiteral("type")) {
      aType = Substring(start, iter);
      ++found;
    }
    else if (attrName.EqualsLiteral("href")) {
      aHref = Substring(start, iter);
      ++found;
    }

    // Stop if we found both attributes
    if (found == 2) {
      break;
    }

    // Resume scanning after the end of the attribute value.
    start = iter;
    ++start;  // To move past the quote char.
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void txXSLTProcessor::shutdown ( ) [static, inherited]

Definition at line 80 of file txXSLTProcessor.cpp.

Here is the caller graph for this function:

nsresult txStandaloneXSLTProcessor::transform ( nsACString &  aXMLPath,
ostream &  aOut,
ErrorObserver aErr 
)

Methods that print the result to a stream.

txStandaloneXSLTProcessor

Transform a XML document given by path. The stylesheet is retrieved by a processing instruction, or an error is returned.

Parameters:
aXMLPathpath to the source document
aOutstream to which the result is feeded
aErrerror observer
Returns:
NS_OK if transformation was successful

Transform a XML document given by path. The stylesheet is retrieved by a processing instruction, or an error is returned.

Definition at line 125 of file txStandaloneXSLTProcessor.cpp.

{
    txXPathNode* xmlDoc = parsePath(aXMLPath, aErr);
    if (!xmlDoc) {
        return NS_ERROR_FAILURE;
    }

    // transform
    nsresult rv = transform(*xmlDoc, aOut, aErr);

    delete xmlDoc;

    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult txStandaloneXSLTProcessor::transform ( nsACString &  aXMLPath,
nsACString &  aXSLPath,
ostream &  aOut,
ErrorObserver aErr 
)

Transform a XML document given by path with the given stylesheet.

Parameters:
aXMLPathpath to the source document
aXSLPathpath to the style document
aOutstream to which the result is feeded
aErrerror observer
Returns:
NS_OK if transformation was successful

Definition at line 146 of file txStandaloneXSLTProcessor.cpp.

{
    txXPathNode* xmlDoc = parsePath(aXMLPath, aErr);
    if (!xmlDoc) {
        return NS_ERROR_FAILURE;
    }
    txParsedURL path;
    path.init(NS_ConvertASCIItoUCS2(aXSLPath));
    nsRefPtr<txStylesheet> style;
    nsresult rv = TX_CompileStylesheetPath(path, getter_AddRefs(style));
    if (NS_FAILED(rv)) {
        delete xmlDoc;
        return rv;
    }
    // transform
    rv = transform(*xmlDoc, style, aOut, aErr);

    delete xmlDoc;

    return rv;
}

Here is the call graph for this function:

nsresult txStandaloneXSLTProcessor::transform ( txXPathNode aXMLDoc,
ostream &  aOut,
ErrorObserver aErr 
)

Transform a XML document.

The stylesheet is retrieved by a processing instruction, or an error is returned.

Parameters:
aXMLDocsource document
aOutstream to which the result is feeded
aErrerror observer
Returns:
NS_OK if transformation was successful

The stylesheet is retrieved by a processing instruction, or an error is returned.

Definition at line 176 of file txStandaloneXSLTProcessor.cpp.

{
    Document* xmlDoc;
    nsresult rv = txXPathNativeNode::getDocument(aXMLDoc, &xmlDoc);
    NS_ENSURE_SUCCESS(rv, rv);

    // get stylesheet path
    nsAutoString stylePath, basePath;
    xmlDoc->getBaseURI(basePath);
    getHrefFromStylesheetPI(*xmlDoc, stylePath);
    txParsedURL base, ref, resolved;
    base.init(basePath);
    ref.init(stylePath);
    base.resolve(ref, resolved);

    nsRefPtr<txStylesheet> style;
    rv = TX_CompileStylesheetPath(resolved, getter_AddRefs(style));
    NS_ENSURE_SUCCESS(rv, rv);

    // transform
    rv = transform(aXMLDoc, style, aOut, aErr);

    return rv;
}

Here is the call graph for this function:

nsresult txStandaloneXSLTProcessor::transform ( txXPathNode aXMLDoc,
txStylesheet aXSLNode,
ostream &  aOut,
ErrorObserver aErr 
)

Transform a XML document with the given stylesheet.

Processes the given XML Document using the given XSL document and prints the results to the given ostream argument.

Parameters:
aXMLDocsource document
aXSLNodestyle node
aOutstream to which the result is feeded
aErrerror observer
Returns:
NS_OK if transformation was successful

Definition at line 207 of file txStandaloneXSLTProcessor.cpp.

{
    // Create a new txEvalState
    txExecutionState es(aStylesheet);

    // XXX todo es.addErrorObserver(aErr);

    txStandaloneHandlerFactory handlerFactory(&es, &aOut);

#ifndef XP_WIN
    bool sync = aOut.sync_with_stdio(false);
#endif
    es.mOutputHandlerFactory = &handlerFactory;

    es.init(aSource, nsnull);

    // Process root of XML source document
    nsresult rv = txXSLTProcessor::execute(es);
    es.end(rv);

#ifndef XP_WIN
    aOut.sync_with_stdio(sync);
#endif

    return rv;
}

Here is the call graph for this function:


The documentation for this class was generated from the following files: