Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends
nsRDFXMLSerializer Class Reference

A helper class that can serialize RDF/XML from a datasource. More...

#include <nsRDFXMLSerializer.h>

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

List of all members.

Public Member Functions

void init (in nsIRDFDataSource aDataSource)
 Initialize the serializer with the specified datasource.
void addNameSpace (in nsIAtom aPrefix, in DOMString aURI)
 Add the specified namespace to the serializer.
void Serialize (in nsIOutputStream aStream)
 Serialize the contents of the datasource to aStream.

Static Public Member Functions

static NS_IMETHODIMP Create (nsISupports *aOuter, REFNSIID aIID, void **aResult)

Protected Member Functions

 nsRDFXMLSerializer ()
virtual ~nsRDFXMLSerializer ()
nsresult RegisterQName (nsIRDFResource *aResource)
nsresult GetQName (nsIRDFResource *aResource, nsCString &aQName)
already_AddRefed< nsIAtomEnsureNewPrefix ()
nsresult SerializeInlineAssertion (nsIOutputStream *aStream, nsIRDFResource *aResource, nsIRDFResource *aProperty, nsIRDFLiteral *aValue)
nsresult SerializeChildAssertion (nsIOutputStream *aStream, nsIRDFResource *aResource, nsIRDFResource *aProperty, nsIRDFNode *aValue)
nsresult SerializeProperty (nsIOutputStream *aStream, nsIRDFResource *aResource, nsIRDFResource *aProperty, PRBool aInline, PRInt32 *aSkipped)
PRBool IsContainerProperty (nsIRDFResource *aProperty)
nsresult SerializeDescription (nsIOutputStream *aStream, nsIRDFResource *aResource)
nsresult SerializeMember (nsIOutputStream *aStream, nsIRDFResource *aContainer, nsIRDFNode *aMember)
nsresult SerializeContainer (nsIOutputStream *aStream, nsIRDFResource *aContainer)
nsresult SerializePrologue (nsIOutputStream *aStream)
nsresult SerializeEpilogue (nsIOutputStream *aStream)
nsresult CollectNamespaces ()
PRBool IsA (nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, nsIRDFResource *aType)

Protected Attributes

nsCOMPtr< nsIRDFDataSourcemDataSource
nsNameSpaceMap mNameSpaces
nsXPIDLCString mBaseURLSpec
nsDataHashtable
< nsISupportsHashKey,
nsCString
mQNames
PRUint32 mPrefixID

Static Protected Attributes

static PRInt32 gRefCnt = 0
static nsIRDFResourcekRDF_instanceOf
static nsIRDFResourcekRDF_type
static nsIRDFResourcekRDF_nextVal
static nsIRDFResourcekRDF_Bag
static nsIRDFResourcekRDF_Seq
static nsIRDFResourcekRDF_Alt
static nsIRDFContainerUtilsgRDFC

Friends

class QNameCollector

Detailed Description

A helper class that can serialize RDF/XML from a datasource.

Implements both nsIRDFXMLSerializer and nsIRDFXMLSource.

Definition at line 62 of file nsRDFXMLSerializer.h.


Constructor & Destructor Documentation

Definition at line 130 of file nsRDFXMLSerializer.cpp.

Here is the caller graph for this function:

nsRDFXMLSerializer::~nsRDFXMLSerializer ( ) [protected, virtual]

Member Function Documentation

void nsIRDFXMLSerializer::addNameSpace ( in nsIAtom  aPrefix,
in DOMString  aURI 
) [inherited]

Add the specified namespace to the serializer.

Parameters:
aPrefixthe attribute namespace prefix
aURIthe namespace URI

Definition at line 1087 of file nsRDFXMLSerializer.cpp.

{
    // Iterate over all Triples to get namespaces for subject resource types
    // and Predicates and cache all the QNames we want to use.
    nsCOMPtr<rdfITripleVisitor> collector = 
        new QNameCollector(this);
    nsCOMPtr<rdfIDataSource> ds = do_QueryInterface(mDataSource); // XXX API
    NS_ENSURE_TRUE(collector && ds, NS_ERROR_FAILURE);
    return ds->VisitAllTriples(collector);
}

Here is the call graph for this function:

NS_IMETHODIMP nsRDFXMLSerializer::Create ( nsISupports *  aOuter,
REFNSIID  aIID,
void **  aResult 
) [static]

Definition at line 78 of file nsRDFXMLSerializer.cpp.

{
    if (aOuter)
        return NS_ERROR_NO_AGGREGATION;

    nsCOMPtr<nsIRDFXMLSerializer> result = new nsRDFXMLSerializer();
    if (! result)
        return NS_ERROR_OUT_OF_MEMORY;
    // The serializer object is here, addref gRefCnt so that the
    // destructor can safely release it.
    gRefCnt++;

    nsresult rv;
    rv = result->QueryInterface(aIID, aResult);

    if (NS_FAILED(rv)) return rv;

    if (gRefCnt == 1) do {
        nsCOMPtr<nsIRDFService> rdf = do_GetService("@mozilla.org/rdf/rdf-service;1", &rv);
        if (NS_FAILED(rv)) break;

        rv = rdf->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "instanceOf"),
                              &kRDF_instanceOf);
        if (NS_FAILED(rv)) break;

        rv = rdf->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "type"),
                              &kRDF_type);
        if (NS_FAILED(rv)) break;

        rv = rdf->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "nextVal"),
                              &kRDF_nextVal);
        if (NS_FAILED(rv)) break;

        rv = rdf->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "Bag"),
                              &kRDF_Bag);
        if (NS_FAILED(rv)) break;

        rv = rdf->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "Seq"),
                              &kRDF_Seq);
        if (NS_FAILED(rv)) break;

        rv = rdf->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "Alt"),
                              &kRDF_Alt);
        if (NS_FAILED(rv)) break;

        rv = CallGetService("@mozilla.org/rdf/container-utils;1", &gRDFC);
        if (NS_FAILED(rv)) break;
    } while (0);

    return rv;
}

Here is the call graph for this function:

Definition at line 221 of file nsRDFXMLSerializer.cpp.

{
    nsCAutoString qname;
    nsCOMPtr<nsIAtom> prefix;
    PRBool isNewPrefix;
    do {
        isNewPrefix = PR_TRUE;
        qname.AssignLiteral("NS");
        qname.AppendInt(++mPrefixID, 10);
        prefix = do_GetAtom(qname);
        nsNameSpaceMap::const_iterator iter = mNameSpaces.first();
        while (iter != mNameSpaces.last() && isNewPrefix) {
            isNewPrefix = (iter->mPrefix != prefix);
            ++iter;
        } 
    } while (!isNewPrefix);
    nsIAtom* outPrefix = nsnull;
    prefix.swap(outPrefix);
    return outPrefix;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsRDFXMLSerializer::GetQName ( nsIRDFResource aResource,
nsCString aQName 
) [protected]

Definition at line 285 of file nsRDFXMLSerializer.cpp.

{
    return mQNames.Get(aResource, &aQName) ? NS_OK : NS_ERROR_UNEXPECTED;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsIRDFXMLSerializer::init ( in nsIRDFDataSource  aDataSource) [inherited]

Initialize the serializer with the specified datasource.

Parameters:
aDataSourcethe datasource from which data will be serialized
PRBool nsRDFXMLSerializer::IsA ( nsIRDFDataSource aDataSource,
nsIRDFResource aResource,
nsIRDFResource aType 
) [protected]

Definition at line 1152 of file nsRDFXMLSerializer.cpp.

{
    nsresult rv;

    PRBool result;
    rv = aDataSource->HasAssertion(aResource, kRDF_instanceOf, aType, PR_TRUE, &result);
    if (NS_FAILED(rv)) return PR_FALSE;

    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 291 of file nsRDFXMLSerializer.cpp.

{
    // Return `true' if the property is an internal property related
    // to being a container.
    if (aProperty == kRDF_instanceOf)
        return PR_TRUE;

    if (aProperty == kRDF_nextVal)
        return PR_TRUE;

    PRBool isOrdinal = PR_FALSE;
    gRDFC->IsOrdinalProperty(aProperty, &isOrdinal);
    if (isOrdinal)
        return PR_TRUE;

    return PR_FALSE;
} 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 247 of file nsRDFXMLSerializer.cpp.

{
    nsCAutoString uri, qname;
    aResource->GetValueUTF8(uri);

    nsNameSpaceMap::const_iterator iter = mNameSpaces.GetNameSpaceOf(uri);
    if (iter != mNameSpaces.last()) {
        NS_ENSURE_TRUE(iter->mPrefix, NS_ERROR_UNEXPECTED);
        iter->mPrefix->ToUTF8String(qname);
        qname.Append(':');
        qname += StringTail(uri, uri.Length() - iter->mURI.Length());
        return mQNames.Put(aResource, qname) ? NS_OK : NS_ERROR_FAILURE;
    }

    // Okay, so we don't have it in our map. Try to make one up. This
    // is very bogus.
    PRInt32 i = uri.RFindChar('#'); // first try a '#'
    if (i == -1) {
        i = uri.RFindChar('/');
        if (i == -1) {
            // Okay, just punt and assume there is _no_ namespace on
            // this thing...
            return mQNames.Put(aResource, uri) ? NS_OK : NS_ERROR_FAILURE;
        }
    }

    // Take whatever is to the right of the '#' or '/' and call it the
    // local name, make up a prefix.
    nsCOMPtr<nsIAtom> prefix = EnsureNewPrefix();
    mNameSpaces.Put(StringHead(uri, i+1), prefix);
    prefix->ToUTF8String(qname);
    qname.Append(':');
    qname += StringTail(uri, uri.Length() - (i + 1));

    return mQNames.Put(aResource, qname) ? NS_OK : NS_ERROR_FAILURE;
}

Here is the call graph for this function:

Serialize the contents of the datasource to aStream.

Parameters:
aStreamthe output stream the will recieve the RDF/XML. Currently, the output stream need only implement the |write()| method.

Here is the caller graph for this function:

nsresult nsRDFXMLSerializer::SerializeChildAssertion ( nsIOutputStream aStream,
nsIRDFResource aResource,
nsIRDFResource aProperty,
nsIRDFNode aValue 
) [protected]

Definition at line 423 of file nsRDFXMLSerializer.cpp.

{
    nsCString qname;
    nsresult rv = GetQName(aProperty, qname);
    NS_ENSURE_SUCCESS(rv, rv);

    rv = rdf_BlockingWrite(aStream, "    <", 5);
    if (NS_FAILED(rv)) return rv;
    rv = rdf_BlockingWrite(aStream, qname);
    if (NS_FAILED(rv)) return rv;

    nsCOMPtr<nsIRDFResource> resource;
    nsCOMPtr<nsIRDFLiteral> literal;
    nsCOMPtr<nsIRDFInt> number;
    nsCOMPtr<nsIRDFDate> date;

    if ((resource = do_QueryInterface(aValue)) != nsnull) {
        nsCAutoString uri;
        resource->GetValueUTF8(uri);

        rdf_MakeRelativeRef(mBaseURLSpec, uri);
        rdf_EscapeAttributeValue(uri);

        rv = rdf_BlockingWrite(aStream, kRDFResource1,
                               sizeof(kRDFResource1) - 1);
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, uri);
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, kRDFResource2,
                               sizeof(kRDFResource2) - 1);
        if (NS_FAILED(rv)) return rv;

        goto no_close_tag;
    }
    else if ((literal = do_QueryInterface(aValue)) != nsnull) {
        const PRUnichar *value;
        literal->GetValueConst(&value);
        NS_ConvertUTF16toUTF8 s(value);

        rdf_EscapeAmpersandsAndAngleBrackets(s);

        rv = rdf_BlockingWrite(aStream, ">", 1);
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, s);
        if (NS_FAILED(rv)) return rv;
    }
    else if ((number = do_QueryInterface(aValue)) != nsnull) {
        PRInt32 value;
        number->GetValue(&value);

        nsCAutoString n;
        n.AppendInt(value);

        rv = rdf_BlockingWrite(aStream, kRDFParseTypeInteger, 
                               sizeof(kRDFParseTypeInteger) - 1);
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, n);
        if (NS_FAILED(rv)) return rv;
    }
    else if ((date = do_QueryInterface(aValue)) != nsnull) {
        PRTime value;
        date->GetValue(&value);

        nsCAutoString s;
        rdf_FormatDate(value, s);

        rv = rdf_BlockingWrite(aStream, kRDFParseTypeDate, 
                               sizeof(kRDFParseTypeDate) - 1);
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, s);
        if (NS_FAILED(rv)) return rv;
    }
    else {
        // XXX it doesn't support nsIRDFResource _or_ nsIRDFLiteral???
        // We should serialize nsIRDFInt, nsIRDFDate, etc...
        NS_WARNING("unknown RDF node type");

        rv = rdf_BlockingWrite(aStream, kRDFUnknown, sizeof(kRDFUnknown) - 1);
        if (NS_FAILED(rv)) return rv;
    }

    rv = rdf_BlockingWrite(aStream, "</", 2);
    if (NS_FAILED(rv)) return rv;
    rv = rdf_BlockingWrite(aStream, qname);
    if (NS_FAILED(rv)) return rv;
    return rdf_BlockingWrite(aStream, ">\n", 2);

 no_close_tag:
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsRDFXMLSerializer::SerializeContainer ( nsIOutputStream aStream,
nsIRDFResource aContainer 
) [protected]

Definition at line 856 of file nsRDFXMLSerializer.cpp.

{
    nsresult rv;
    nsCAutoString tag;

    // Decide if it's a sequence, bag, or alternation, and print the
    // appropriate tag-open sequence

    if (IsA(mDataSource, aContainer, kRDF_Bag)) {
        tag.AssignLiteral("RDF:Bag");
    }
    else if (IsA(mDataSource, aContainer, kRDF_Seq)) {
        tag.AssignLiteral("RDF:Seq");
    }
    else if (IsA(mDataSource, aContainer, kRDF_Alt)) {
        tag.AssignLiteral("RDF:Alt");
    }
    else {
        NS_ASSERTION(PR_FALSE, "huh? this is _not_ a container.");
        return NS_ERROR_UNEXPECTED;
    }

    rv = rdf_BlockingWrite(aStream, "  <", 3);
    if (NS_FAILED(rv)) return rv;
    rv = rdf_BlockingWrite(aStream, tag);
    if (NS_FAILED(rv)) return rv;


    // Unfortunately, we always need to print out the identity of the
    // resource, even if was constructed "anonymously". We need to do
    // this because we never really know who else might be referring
    // to it...

    nsCAutoString uri;
    if (NS_SUCCEEDED(aContainer->GetValueUTF8(uri))) {
        rdf_MakeRelativeRef(mBaseURLSpec, uri);

        rdf_EscapeAttributeValue(uri);

        if (uri.First() == '#') {
            // Okay, it's actually identified as an element in the
            // current document, not trying to decorate some absolute
            // URI. We can use the 'ID=' attribute...

            uri.Cut(0, 1); // chop the '#'
            rv = rdf_BlockingWrite(aStream, kIDAttr, sizeof(kIDAttr) - 1);
            if (NS_FAILED(rv)) return rv;
        }
        else {
            // We need to cheat and spit out an illegal 'about=' on
            // the sequence. 
            rv = rdf_BlockingWrite(aStream, kAboutAttr,
                                   sizeof(kAboutAttr) - 1);
            if (NS_FAILED(rv)) return rv;
        }

        rv = rdf_BlockingWrite(aStream, uri);
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, "\"", 1);
        if (NS_FAILED(rv)) return rv;
    }

    rv = rdf_BlockingWrite(aStream, ">\n", 2);
    if (NS_FAILED(rv)) return rv;

    // First iterate through each of the ordinal elements (the RDF/XML
    // syntax doesn't allow us to place properties on RDF container
    // elements).
    nsCOMPtr<nsISimpleEnumerator> elements;
    rv = NS_NewContainerEnumerator(mDataSource, aContainer, getter_AddRefs(elements));

    if (NS_SUCCEEDED(rv)) {
        while (1) {
            PRBool hasMore;
            rv = elements->HasMoreElements(&hasMore);
            if (NS_FAILED(rv)) break;

            if (! hasMore)
                break;

            nsCOMPtr<nsISupports> isupports;
            elements->GetNext(getter_AddRefs(isupports));

            nsCOMPtr<nsIRDFNode> element = do_QueryInterface(isupports);
            NS_ASSERTION(element != nsnull, "not an nsIRDFNode");
            if (! element)
                continue;

            SerializeMember(aStream, aContainer, element);
        }
    }

    // close the container tag
    rv = rdf_BlockingWrite(aStream, "  </", 4);
    if (NS_FAILED(rv)) return rv;
    tag.Append(">\n", 2);
    rv = rdf_BlockingWrite(aStream, tag);
    if (NS_FAILED(rv)) return rv;

    // Now, we iterate through _all_ of the arcs, in case someone has
    // applied properties to the bag itself. These'll be placed in a
    // separate RDF:Description element.
    nsCOMPtr<nsISimpleEnumerator> arcs;
    mDataSource->ArcLabelsOut(aContainer, getter_AddRefs(arcs));

    PRBool wroteDescription = PR_FALSE;
    while (! wroteDescription) {
        PRBool hasMore = PR_FALSE;
        rv = arcs->HasMoreElements(&hasMore);
        if (NS_FAILED(rv)) break;

        if (! hasMore)
            break;

        nsIRDFResource* property;
        rv = arcs->GetNext((nsISupports**) &property);
        if (NS_FAILED(rv)) break;

        // If it's a membership property, then output a "LI"
        // tag. Otherwise, output a property.
        if (! IsContainerProperty(property)) {
            rv = SerializeDescription(aStream, aContainer);
            wroteDescription = PR_TRUE;
        }

        NS_RELEASE(property);
        if (NS_FAILED(rv))
            break;
    }

    return NS_OK;
}

Here is the call graph for this function:

Definition at line 588 of file nsRDFXMLSerializer.cpp.

{
    nsresult rv;

    PRBool isTypedNode = PR_FALSE;
    nsCString typeQName;

    nsCOMPtr<nsIRDFNode> typeNode;
    mDataSource->GetTarget(aResource, kRDF_type, PR_TRUE, getter_AddRefs(typeNode));
    if (typeNode) {
        nsCOMPtr<nsIRDFResource> type = do_QueryInterface(typeNode, &rv);
        if (type) {
            // Try to get a namespace prefix.  If none is available,
            // just treat the description as if it weren't a typed node 
            // after all and emit rdf:type as a normal property.  This 
            // seems preferable to using a bogus (invented) prefix.
            isTypedNode = NS_SUCCEEDED(GetQName(type, typeQName));
        }
    }

    nsCAutoString uri;
    rv = aResource->GetValueUTF8(uri);
    if (NS_FAILED(rv)) return rv;

    rdf_MakeRelativeRef(mBaseURLSpec, uri);
    rdf_EscapeAttributeValue(uri);

    // Emit an open tag and the subject
    if (isTypedNode) {
        rv = rdf_BlockingWrite(aStream, NS_LITERAL_STRING("  <"));
        if (NS_FAILED(rv)) return rv;
        // Watch out for the default namespace!
        rv = rdf_BlockingWrite(aStream, typeQName);
        if (NS_FAILED(rv)) return rv;
    }
    else {
        rv = rdf_BlockingWrite(aStream, kRDFDescriptionOpen,
                               sizeof(kRDFDescriptionOpen) - 1);
        if (NS_FAILED(rv)) return rv;
    }
    if (uri[0] == PRUnichar('#')) {
        uri.Cut(0, 1);
        rv = rdf_BlockingWrite(aStream, kIDAttr, sizeof(kIDAttr) - 1);
    }
    else {
        rv = rdf_BlockingWrite(aStream, kAboutAttr, sizeof(kAboutAttr) - 1);
    }
    if (NS_FAILED(rv)) return rv;

    uri.Append('"');
    rv = rdf_BlockingWrite(aStream, uri);
    if (NS_FAILED(rv)) return rv;

    // Any value that's a literal we can write out as an inline
    // attribute on the RDF:Description
    nsAutoVoidArray visited;
    PRInt32 skipped = 0;

    nsCOMPtr<nsISimpleEnumerator> arcs;
    mDataSource->ArcLabelsOut(aResource, getter_AddRefs(arcs));

    if (arcs) {
        // Don't re-serialize rdf:type later on
        if (isTypedNode)
            visited.AppendElement(kRDF_type);

        while (1) {
            PRBool hasMore = PR_FALSE;
            arcs->HasMoreElements(&hasMore);
            if (! hasMore)
                break;

            nsCOMPtr<nsISupports> isupports;
            arcs->GetNext(getter_AddRefs(isupports));

            nsCOMPtr<nsIRDFResource> property = do_QueryInterface(isupports);
            if (! property)
                continue;

            // Ignore properties that pertain to containers; we may be
            // called from SerializeContainer() if the container resource
            // has been assigned non-container properties.
            if (IsContainerProperty(property))
                continue;

            // Only serialize values for the property once.
            if (visited.IndexOf(property.get()) >= 0)
                continue;

            visited.AppendElement(property.get());

            SerializeProperty(aStream, aResource, property, PR_TRUE, &skipped);
        }
    }

    if (skipped) {
        // Close the RDF:Description tag.
        rv = rdf_BlockingWrite(aStream, NS_LITERAL_CSTRING(">\n"));
        if (NS_FAILED(rv)) return rv;

        // Now write out resources (which might have their own
        // substructure) as children.
        mDataSource->ArcLabelsOut(aResource, getter_AddRefs(arcs));

        if (arcs) {
            // Forget that we've visited anything
            visited.Clear();
            // ... except for rdf:type
            if (isTypedNode)
                visited.AppendElement(kRDF_type);

            while (1) {
                PRBool hasMore = PR_FALSE;
                arcs->HasMoreElements(&hasMore);
                if (! hasMore)
                    break;

                nsCOMPtr<nsISupports> isupports;
                arcs->GetNext(getter_AddRefs(isupports));

                nsCOMPtr<nsIRDFResource> property = do_QueryInterface(isupports);
                if (! property)
                    continue;

                // Ignore properties that pertain to containers; we may be
                // called from SerializeContainer() if the container
                // resource has been assigned non-container properties.
                if (IsContainerProperty(property))
                    continue;

                // have we already seen this property?  If so, don't write it
                // out again; serialize property will write each instance.
                if (visited.IndexOf(property.get()) >= 0)
                    continue;

                visited.AppendElement(property.get());

                SerializeProperty(aStream, aResource, property, PR_FALSE, &skipped);
            }
        }

        // Emit a proper close-tag.
        if (isTypedNode) {
            rv = rdf_BlockingWrite(aStream,  NS_LITERAL_CSTRING("  </"));
            if (NS_FAILED(rv)) return rv;
            // Watch out for the default namespace!
            rdf_BlockingWrite(aStream, typeQName);
            if (NS_FAILED(rv)) return rv;
            rdf_BlockingWrite(aStream, ">\n", 2);
            if (NS_FAILED(rv)) return rv;
        }
        else {
            rv = rdf_BlockingWrite(aStream, kRDFDescriptionClose,
                                   sizeof(kRDFDescriptionClose) - 1);
            if (NS_FAILED(rv)) return rv;
        }
    }
    else {
        // If we saw _no_ child properties, then we can don't need a
        // close-tag.
        rv = rdf_BlockingWrite(aStream, NS_LITERAL_CSTRING(" />\n"));
        if (NS_FAILED(rv)) return rv;
    }

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1036 of file nsRDFXMLSerializer.cpp.

{
    return rdf_BlockingWrite(aStream, NS_LITERAL_CSTRING("</RDF:RDF>\n"));
}

Here is the call graph for this function:

nsresult nsRDFXMLSerializer::SerializeInlineAssertion ( nsIOutputStream aStream,
nsIRDFResource aResource,
nsIRDFResource aProperty,
nsIRDFLiteral aValue 
) [protected]

Definition at line 394 of file nsRDFXMLSerializer.cpp.

{
    nsresult rv;
    nsCString qname;
    rv = GetQName(aProperty, qname);
    NS_ENSURE_SUCCESS(rv, rv);

    rv = rdf_BlockingWrite(aStream,
                           NS_LITERAL_CSTRING("\n                   "));
    if (NS_FAILED(rv)) return rv;

    const PRUnichar* value;
    aValue->GetValueConst(&value);
    NS_ConvertUTF16toUTF8 s(value);

    rdf_EscapeAttributeValue(s);

    rv = rdf_BlockingWrite(aStream, qname);
    if (NS_FAILED(rv)) return rv;
    rv = rdf_BlockingWrite(aStream, "=\"", 2);
    if (NS_FAILED(rv)) return rv;
    s.Append('"');
    return rdf_BlockingWrite(aStream, s);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsRDFXMLSerializer::SerializeMember ( nsIOutputStream aStream,
nsIRDFResource aContainer,
nsIRDFNode aMember 
) [protected]

Definition at line 757 of file nsRDFXMLSerializer.cpp.

{
    // If it's a resource, then output a "<RDF:li RDF:resource=... />"
    // tag, because we'll be dumping the resource separately. (We
    // iterate thru all the resources in the datasource,
    // remember?) Otherwise, output the literal value.

    nsCOMPtr<nsIRDFResource> resource;
    nsCOMPtr<nsIRDFLiteral> literal;
    nsCOMPtr<nsIRDFInt> number;
    nsCOMPtr<nsIRDFDate> date;

static const char kRDFLIOpen[] = "    <RDF:li";
    nsresult rv = rdf_BlockingWrite(aStream, kRDFLIOpen,
                                    sizeof(kRDFLIOpen) - 1); 
    if (NS_FAILED(rv)) return rv;

    if ((resource = do_QueryInterface(aMember)) != nsnull) {
        nsCAutoString uri;
        resource->GetValueUTF8(uri);

        rdf_MakeRelativeRef(mBaseURLSpec, uri);
        rdf_EscapeAttributeValue(uri);

        rv = rdf_BlockingWrite(aStream, kRDFResource1,
                               sizeof(kRDFResource1) - 1);
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, uri);
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, kRDFResource2,
                               sizeof(kRDFResource2) - 1);
        if (NS_FAILED(rv)) return rv;

        goto no_close_tag;
    }
    else if ((literal = do_QueryInterface(aMember)) != nsnull) {
        const PRUnichar *value;
        literal->GetValueConst(&value);
static const char kRDFLIOpenGT[] = ">";
        // close the '<RDF:LI' before adding the literal
        rv = rdf_BlockingWrite(aStream, kRDFLIOpenGT,
                               sizeof(kRDFLIOpenGT) - 1);
        if (NS_FAILED(rv)) return rv;

        NS_ConvertUTF16toUTF8 s(value);
        rdf_EscapeAmpersandsAndAngleBrackets(s);

        rv = rdf_BlockingWrite(aStream, s);
        if (NS_FAILED(rv)) return rv;
    }
    else if ((number = do_QueryInterface(aMember)) != nsnull) {
        PRInt32 value;
        number->GetValue(&value);

        nsCAutoString n;
        n.AppendInt(value);

        rv = rdf_BlockingWrite(aStream, kRDFParseTypeInteger, 
                               sizeof(kRDFParseTypeInteger) - 1);
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, n);
        if (NS_FAILED(rv)) return rv;
    }
    else if ((date = do_QueryInterface(aMember)) != nsnull) {
        PRTime value;
        date->GetValue(&value);

        nsCAutoString s;
        rdf_FormatDate(value, s);

        rv = rdf_BlockingWrite(aStream, kRDFParseTypeDate, 
                               sizeof(kRDFParseTypeDate) - 1);
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, s);
        if (NS_FAILED(rv)) return rv;
    }
    else {
        // XXX it doesn't support nsIRDFResource _or_ nsIRDFLiteral???
        // We should serialize nsIRDFInt, nsIRDFDate, etc...
        NS_WARNING("unknown RDF node type");

        rv = rdf_BlockingWrite(aStream, kRDFUnknown, sizeof(kRDFUnknown) - 1);
        if (NS_FAILED(rv)) return rv;
    }

    {
static const char kRDFLIClose[] = "</RDF:li>\n";
        rv = rdf_BlockingWrite(aStream, kRDFLIClose, sizeof(kRDFLIClose) - 1);
        if (NS_FAILED(rv)) return rv;
    }

 no_close_tag:
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 992 of file nsRDFXMLSerializer.cpp.

{
static const char kXMLVersion[] = "<?xml version=\"1.0\"?>\n";

    nsresult rv;
    rv = rdf_BlockingWrite(aStream, kXMLVersion, sizeof(kXMLVersion) - 1);
    if (NS_FAILED(rv)) return rv;

    // global name space declarations
    rv = rdf_BlockingWrite(aStream, NS_LITERAL_CSTRING("<RDF:RDF "));
    if (NS_FAILED(rv)) return rv;

    nsNameSpaceMap::const_iterator first = mNameSpaces.first();
    nsNameSpaceMap::const_iterator last = mNameSpaces.last();
    for (nsNameSpaceMap::const_iterator entry = first; entry != last; ++entry) {
        if (entry != first) {
            rv = rdf_BlockingWrite(aStream, NS_LITERAL_CSTRING("\n         "));
            if (NS_FAILED(rv)) return rv;
        }
        rv = rdf_BlockingWrite(aStream, NS_LITERAL_CSTRING("xmlns"));
        if (NS_FAILED(rv)) return rv;

        if (entry->mPrefix) {
            rv = rdf_BlockingWrite(aStream, NS_LITERAL_CSTRING(":"));
            if (NS_FAILED(rv)) return rv;
            nsCAutoString prefix;
            entry->mPrefix->ToUTF8String(prefix);
            rv = rdf_BlockingWrite(aStream, prefix);
            if (NS_FAILED(rv)) return rv;
        }

        rv = rdf_BlockingWrite(aStream, NS_LITERAL_CSTRING("=\""));
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, entry->mURI);
        if (NS_FAILED(rv)) return rv;
        rv = rdf_BlockingWrite(aStream, NS_LITERAL_CSTRING("\""));
        if (NS_FAILED(rv)) return rv;
    }

    return rdf_BlockingWrite(aStream, NS_LITERAL_CSTRING(">\n"));
}

Here is the call graph for this function:

nsresult nsRDFXMLSerializer::SerializeProperty ( nsIOutputStream aStream,
nsIRDFResource aResource,
nsIRDFResource aProperty,
PRBool  aInline,
PRInt32 aSkipped 
) [protected]

Definition at line 518 of file nsRDFXMLSerializer.cpp.

{
    nsresult rv = NS_OK;

    PRInt32 skipped = 0;

    nsCOMPtr<nsISimpleEnumerator> assertions;
    mDataSource->GetTargets(aResource, aProperty, PR_TRUE, getter_AddRefs(assertions));
    if (! assertions)
        return NS_ERROR_FAILURE;

    // Serializing the assertion inline is ok as long as the property has
    // only one target value, and it is a literal that doesn't include line
    // breaks.
    PRBool needsChild = PR_FALSE;

    while (1) {
        PRBool hasMore = PR_FALSE;
        assertions->HasMoreElements(&hasMore);
        if (! hasMore)
            break;

        nsCOMPtr<nsISupports> isupports;
        assertions->GetNext(getter_AddRefs(isupports));
        nsCOMPtr<nsIRDFLiteral> literal = do_QueryInterface(isupports);
        needsChild |= (!literal);

        if (!needsChild) {
            assertions->HasMoreElements(&needsChild);
            if (!needsChild) {
                const PRUnichar* literalVal = nsnull;
                literal->GetValueConst(&literalVal);
                if (literalVal) {
                    for (; *literalVal; literalVal++) {
                        if (*literalVal == PRUnichar('\n') ||
                            *literalVal == PRUnichar('\r')) {
                            needsChild = PR_TRUE;
                            break;
                        }
                    }
                }
            }
        }

        if (aInline && !needsChild) {
            rv = SerializeInlineAssertion(aStream, aResource, aProperty, literal);
        }
        else if (!aInline && needsChild) {
            nsCOMPtr<nsIRDFNode> value = do_QueryInterface(isupports);
            rv = SerializeChildAssertion(aStream, aResource, aProperty, value);
        }
        else {
            ++skipped;
            rv = NS_OK;
        }

        if (NS_FAILED(rv))
            break;
    }

    *aSkipped += skipped;
    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class QNameCollector [friend]

Definition at line 138 of file nsRDFXMLSerializer.h.


Member Data Documentation

Definition at line 149 of file nsRDFXMLSerializer.h.

PRInt32 nsRDFXMLSerializer::gRefCnt = 0 [static, protected]

Definition at line 142 of file nsRDFXMLSerializer.h.

Definition at line 148 of file nsRDFXMLSerializer.h.

Definition at line 146 of file nsRDFXMLSerializer.h.

Definition at line 143 of file nsRDFXMLSerializer.h.

Definition at line 145 of file nsRDFXMLSerializer.h.

Definition at line 147 of file nsRDFXMLSerializer.h.

Definition at line 144 of file nsRDFXMLSerializer.h.

Definition at line 134 of file nsRDFXMLSerializer.h.

Definition at line 132 of file nsRDFXMLSerializer.h.

Definition at line 133 of file nsRDFXMLSerializer.h.

Definition at line 140 of file nsRDFXMLSerializer.h.

Definition at line 137 of file nsRDFXMLSerializer.h.


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