From a3b4ff0954d474ae2bbff4109c7dc834719066a0 Mon Sep 17 00:00:00 2001 From: "cbiesinger%web.de" Date: Fri, 18 Nov 2005 20:34:19 +0000 Subject: [PATCH] bug 231548 remove nsRDFParserUtils. This means we no longer unescape strings in RDF/XML files twice. patch by Chase Tingley / Rob Adams r=pike sr=shaver --- rdf/base/src/Makefile.in | 1 - rdf/base/src/nsRDFContentSink.cpp | 72 +++++---- rdf/base/src/nsRDFParserUtils.cpp | 256 ------------------------------ rdf/base/src/nsRDFParserUtils.h | 62 -------- rdf/base/src/rdfutil.cpp | 82 ---------- rdf/base/src/rdfutil.h | 9 -- 6 files changed, 40 insertions(+), 442 deletions(-) delete mode 100644 rdf/base/src/nsRDFParserUtils.cpp delete mode 100644 rdf/base/src/nsRDFParserUtils.h diff --git a/rdf/base/src/Makefile.in b/rdf/base/src/Makefile.in index 3ceacdee2136..f279b94d04db 100644 --- a/rdf/base/src/Makefile.in +++ b/rdf/base/src/Makefile.in @@ -65,7 +65,6 @@ CPPSRCS = \ nsRDFContentSink.cpp \ nsRDFContainer.cpp \ nsRDFContainerUtils.cpp \ - nsRDFParserUtils.cpp \ nsRDFService.cpp \ nsRDFXMLDataSource.cpp \ nsRDFXMLParser.cpp \ diff --git a/rdf/base/src/nsRDFContentSink.cpp b/rdf/base/src/nsRDFContentSink.cpp index 0d5141613293..f35f99c74e6a 100644 --- a/rdf/base/src/nsRDFContentSink.cpp +++ b/rdf/base/src/nsRDFContentSink.cpp @@ -83,7 +83,6 @@ #include "nsIURL.h" #include "nsIXMLContentSink.h" #include "nsRDFCID.h" -#include "nsRDFParserUtils.h" #include "nsVoidArray.h" #include "nsXPIDLString.h" #include "prlog.h" @@ -826,6 +825,14 @@ RDFContentSinkImpl::AddText(const PRUnichar* aText, PRInt32 aLength) return NS_OK; } +PRBool +rdf_RequiresAbsoluteURI(const nsString& uri) +{ + // cheap shot at figuring out if this requires an absolute url translation + return !(StringBeginsWith(uri, NS_LITERAL_STRING("urn:")) || + StringBeginsWith(uri, NS_LITERAL_STRING("chrome:"))); +} + nsresult RDFContentSinkImpl::GetIdAboutAttribute(const PRUnichar** aAttributes, nsIRDFResource** aResource, @@ -834,10 +841,6 @@ RDFContentSinkImpl::GetIdAboutAttribute(const PRUnichar** aAttributes, // This corresponds to the dirty work of production [6.5] nsresult rv; - nsCAutoString docURI; - rv = mDocumentURL->GetSpec(docURI); - if (NS_FAILED(rv)) return rv; - nsCOMPtr localName; for (; *aAttributes; aAttributes += 2) { const nsDependentSubstring& nameSpaceURI = @@ -858,20 +861,21 @@ RDFContentSinkImpl::GetIdAboutAttribute(const PRUnichar** aAttributes, if (aIsAnonymous) *aIsAnonymous = PR_FALSE; - nsAutoString uri(aAttributes[1]); - nsRDFParserUtils::StripAndConvert(uri); - - rdf_MakeAbsoluteURI(NS_ConvertUTF8toUCS2(docURI), uri); - - return gRDFService->GetUnicodeResource(uri, aResource); + nsAutoString relURI(aAttributes[1]); + if (rdf_RequiresAbsoluteURI(relURI)) { + nsCAutoString uri; + rv = mDocumentURL->Resolve(NS_ConvertUCS2toUTF8(aAttributes[1]), uri); + if (NS_FAILED(rv)) return rv; + + return gRDFService->GetResource(uri, + aResource); + } + return gRDFService->GetResource(NS_ConvertUCS2toUTF8(aAttributes[1]), + aResource); } else if (localName == kIdAtom) { if (aIsAnonymous) *aIsAnonymous = PR_FALSE; - - nsAutoString name(aAttributes[1]); - nsRDFParserUtils::StripAndConvert(name); - // In the spirit of leniency, we do not bother trying to // enforce that this be a valid "XML Name" (see // http://www.w3.org/TR/REC-xml#NT-Nmtoken), as per @@ -880,11 +884,14 @@ RDFContentSinkImpl::GetIdAboutAttribute(const PRUnichar** aAttributes, // Construct an in-line resource whose URI is the // document's URI plus the XML name specified in the ID // attribute. - name.Insert(PRUnichar('#'), 0); - - rdf_MakeAbsoluteURI(NS_ConvertUTF8toUCS2(docURI), name); + nsCAutoString name; + nsCAutoString ref('#'); + AppendUTF16toUTF8(aAttributes[1], ref); - return gRDFService->GetUnicodeResource(name, aResource); + rv = mDocumentURL->Resolve(ref, name); + if (NS_FAILED(rv)) return rv; + + return gRDFService->GetResource(name, aResource); } else if (localName == kAboutEachAtom) { // XXX we don't deal with aboutEach... @@ -923,17 +930,21 @@ RDFContentSinkImpl::GetResourceAttribute(const PRUnichar** aAttributes, // first thing that was specified and ignore the other. if (localName == kResourceAtom) { - nsAutoString uri(aAttributes[1]); - nsRDFParserUtils::StripAndConvert(uri); - // XXX Take the URI and make it fully qualified by // sticking it into the document's URL. This may not be // appropriate... - nsCAutoString documentURL; - mDocumentURL->GetSpec(documentURL); - rdf_MakeAbsoluteURI(NS_ConvertUTF8toUCS2(documentURL), uri); + nsAutoString relURI(aAttributes[1]); + if (rdf_RequiresAbsoluteURI(relURI)) { + nsresult rv; + nsCAutoString uri; - return gRDFService->GetUnicodeResource(uri, aResource); + rv = mDocumentURL->Resolve(NS_ConvertUCS2toUTF8(aAttributes[1]), uri); + if (NS_FAILED(rv)) return rv; + + return gRDFService->GetResource(uri, aResource); + } + return gRDFService->GetResource(NS_ConvertUCS2toUTF8(aAttributes[1]), + aResource); } } return NS_ERROR_FAILURE; @@ -977,9 +988,6 @@ RDFContentSinkImpl::AddProperties(const PRUnichar** aAttributes, } } - nsAutoString v(aAttributes[1]); - nsRDFParserUtils::StripAndConvert(v); - const char* attrName; localName->GetUTF8String(&attrName); @@ -991,7 +999,8 @@ RDFContentSinkImpl::AddProperties(const PRUnichar** aAttributes, gRDFService->GetResource(propertyStr, getter_AddRefs(property)); nsCOMPtr target; - gRDFService->GetLiteral(v.get(), getter_AddRefs(target)); + gRDFService->GetLiteral(aAttributes[1], + getter_AddRefs(target)); mDataSource->Assert(aSubject, property, target, PR_TRUE); } @@ -1007,8 +1016,7 @@ RDFContentSinkImpl::SetParseMode(const PRUnichar **aAttributes) SplitExpatName(aAttributes[0], getter_AddRefs(localName)); if (localName == kParseTypeAtom) { - nsAutoString v(aAttributes[1]); - nsRDFParserUtils::StripAndConvert(v); + nsDependentString v(aAttributes[1]); if (nameSpaceURI.IsEmpty() || nameSpaceURI.EqualsLiteral(RDF_NAMESPACE_URI)) { diff --git a/rdf/base/src/nsRDFParserUtils.cpp b/rdf/base/src/nsRDFParserUtils.cpp deleted file mode 100644 index 0404010df15f..000000000000 --- a/rdf/base/src/nsRDFParserUtils.cpp +++ /dev/null @@ -1,256 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is - * Netscape Communications Corporation. - * Portions created by the Initial Developer are Copyright (C) 1998 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * - * Alternatively, the contents of this file may be used under the terms of - * either of the GNU General Public License Version 2 or later (the "GPL"), - * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -/* - - Some useful parsing routines. - - This isn't the best place for them: I wish that they'd go into some - shared area (like mozilla/base). - - */ - -#include // XXX for atoi(), maybe this should go into nsCRT? -#include "nsIURL.h" -#include "nsString.h" -#include "nsRDFParserUtils.h" - -// XXX This totally sucks. I wish that mozilla/base had this code. -PRUnichar -nsRDFParserUtils::EntityToUnicode(const char* buf) -{ - if ((buf[0] == 'g') && - (buf[1] == 't') && - (buf[2] == '\0')) - return PRUnichar('>'); - - if ((buf[0] == 'l') && - (buf[1] == 't') && - (buf[2] == '\0')) - return PRUnichar('<'); - - if ((buf[0] == 'a') && - (buf[1] == 'm') && - (buf[2] == 'p') && - (buf[3] == '\0')) - return PRUnichar('&'); - - if ((buf[0] == 'a') && - (buf[1] == 'p') && - (buf[2] == 'o') && - (buf[3] == 's') && - (buf[4] == '\0')) - return PRUnichar('\''); - - if ((buf[0] == 'q') && - (buf[1] == 'u') && - (buf[2] == 'o') && - (buf[3] == 't') && - (buf[4] == '\0')) - return PRUnichar('"'); - - NS_NOTYETIMPLEMENTED("look this up in the declared-entity table"); - return PRUnichar('?'); -} - -// XXX Code copied from nsHTMLContentSink. It should be shared. -void -nsRDFParserUtils::StripAndConvert(nsString& aResult) -{ - if ( !aResult.IsEmpty() ) { - // Strip quotes if present - PRUnichar first = aResult.First(); - if ((first == '"') || (first == '\'')) { - if (aResult.Last() == first) { - aResult.Cut(0, 1); - PRInt32 pos = aResult.Length() - 1; - if (pos >= 0) { - aResult.Cut(pos, 1); - } - } else { - // Mismatched quotes - leave them in - } - } - } - - // Reduce any entities - // XXX Note: as coded today, this will only convert well formed - // entities. This may not be compatible enough. - // XXX there is a table in navigator that translates some numeric entities - // should we be doing that? If so then it needs to live in two places (bad) - // so we should add a translate numeric entity method from the parser... - char cbuf[100]; - PRUint32 i = 0; - while (i < aResult.Length()) { - // If we have the start of an entity (and it's not at the end of - // our string) then translate the entity into it's unicode value. - if ((aResult.CharAt(i++) == '&') && (i < aResult.Length())) { - PRInt32 start = i - 1; - PRUnichar e = aResult.CharAt(i); - if (e == '#') { - // Convert a numeric character reference - i++; - char* cp = cbuf; - char* limit = cp + sizeof(cbuf) - 1; - PRBool ok = PR_FALSE; - PRUint32 slen = aResult.Length(); - while ((i < slen) && (cp < limit)) { - PRUnichar f = aResult.CharAt(i); - if (f == ';') { - i++; - ok = PR_TRUE; - break; - } - if ((f >= '0') && (f <= '9')) { - *cp++ = char(f); - i++; - continue; - } - break; - } - if (!ok || (cp == cbuf)) { - continue; - } - *cp = '\0'; - if (cp - cbuf > 5) { - continue; - } - PRInt32 ch = PRInt32( ::atoi(cbuf) ); - if (ch > 65535) { - continue; - } - - // Remove entity from string and replace it with the integer - // value. - aResult.Cut(start, i - start); - aResult.Insert(PRUnichar(ch), start); - i = start + 1; - } - else if (((e >= 'A') && (e <= 'Z')) || - ((e >= 'a') && (e <= 'z'))) { - // Convert a named entity - i++; - char* cp = cbuf; - char* limit = cp + sizeof(cbuf) - 1; - *cp++ = char(e); - PRBool ok = PR_FALSE; - PRUint32 slen = aResult.Length(); - while ((i < slen) && (cp < limit)) { - PRUnichar f = aResult.CharAt(i); - if (f == ';') { - i++; - ok = PR_TRUE; - break; - } - if (((f >= '0') && (f <= '9')) || - ((f >= 'A') && (f <= 'Z')) || - ((f >= 'a') && (f <= 'z'))) { - *cp++ = char(f); - i++; - continue; - } - break; - } - if (!ok || (cp == cbuf)) { - continue; - } - *cp = '\0'; - PRInt32 ch; - - // XXX Um, here's where we should be converting a - // named entity. I removed this to avoid a link-time - // dependency on core raptor. - ch = EntityToUnicode(cbuf); - - if (ch < 0) { - continue; - } - - // Remove entity from string and replace it with the integer - // value. - aResult.Cut(start, i - start); - aResult.Insert(PRUnichar(ch), start); - i = start + 1; - } - else if (e == '{') { - // Convert a script entity - // XXX write me! - NS_NOTYETIMPLEMENTED("convert a script entity"); - } - } - } -} - -nsresult -nsRDFParserUtils::GetQuotedAttributeValue(const nsString& aSource, - const nsString& aAttribute, - nsString& aValue) -{ -static const char kQuote = '\"'; -static const char kApostrophe = '\''; - - PRInt32 offset; - PRInt32 endOffset = -1; - nsresult result = NS_OK; - - offset = aSource.Find(aAttribute, 0); - if (-1 != offset) { - offset = aSource.FindChar('=', offset); - - PRUnichar next = aSource.CharAt(++offset); - if (kQuote == next) { - endOffset = aSource.FindChar(kQuote, ++offset); - } - else if (kApostrophe == next) { - endOffset = aSource.FindChar(kApostrophe, ++offset); - } - - if (-1 != endOffset) { - aSource.Mid(aValue, offset, endOffset-offset); - } - else { - // Mismatched quotes - return an error - result = NS_ERROR_FAILURE; - } - } - else { - aValue.Truncate(); - } - - return result; -} - diff --git a/rdf/base/src/nsRDFParserUtils.h b/rdf/base/src/nsRDFParserUtils.h deleted file mode 100644 index 3e109e4d5513..000000000000 --- a/rdf/base/src/nsRDFParserUtils.h +++ /dev/null @@ -1,62 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Mozilla Communicator client code. - * - * The Initial Developer of the Original Code is - * Netscape Communications Corporation. - * Portions created by the Initial Developer are Copyright (C) 1998 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * - * Alternatively, the contents of this file may be used under the terms of - * either of the GNU General Public License Version 2 or later (the "GPL"), - * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - - -/* - - Some useful parsing routines. - - */ - -#ifndef nsRDFParserUtils_h__ -#define nsRDFParserUtils_h__ - -class nsRDFParserUtils { -public: - static PRUnichar - EntityToUnicode(const char* buf); - - static void - StripAndConvert(nsString& aResult); - - static nsresult - GetQuotedAttributeValue(const nsString& aSource, - const nsString& aAttribute, - nsString& aValue); -}; - -#endif // nsRDFPasrserUtils_h__ diff --git a/rdf/base/src/rdfutil.cpp b/rdf/base/src/rdfutil.cpp index d3d09b173aad..c524c7e61dab 100644 --- a/rdf/base/src/rdfutil.cpp +++ b/rdf/base/src/rdfutil.cpp @@ -86,88 +86,6 @@ rdf_MakeRelativeRef(const nsCSubstring& aBaseURI, nsCString& aURI) return NS_OK; } -static PRBool -rdf_RequiresAbsoluteURI(const nsString& uri) -{ - // cheap shot at figuring out if this requires an absolute url translation - return !(StringBeginsWith(uri, NS_LITERAL_STRING("urn:")) || - StringBeginsWith(uri, NS_LITERAL_STRING("chrome:")) || - StringBeginsWith(uri, NS_LITERAL_STRING("nc:"), - nsCaseInsensitiveStringComparator())); -} - -nsresult -rdf_MakeAbsoluteURI(const nsString& aBaseURI, nsString& aURI) -{ - nsresult rv; - nsAutoString result; - - if (!rdf_RequiresAbsoluteURI(aURI)) - return NS_OK; - - nsCOMPtr base; - rv = NS_NewURI(getter_AddRefs(base), aBaseURI); - if (NS_FAILED(rv)) return rv; - - rv = NS_MakeAbsoluteURI(result, aURI, base); - - if (NS_SUCCEEDED(rv)) { - aURI = result; - } - else { - // There are some ugly URIs (e.g., "NC:Foo") that netlib can't - // parse. If NS_MakeAbsoluteURL fails, then just punt and - // assume that aURI was already absolute. - } - - return NS_OK; -} - - -nsresult -rdf_MakeAbsoluteURI(nsIURI* aBase, nsString& aURI) -{ - nsresult rv; - - if (!rdf_RequiresAbsoluteURI(aURI)) - return NS_OK; - - nsAutoString result; - - rv = NS_MakeAbsoluteURI(result, aURI, aBase); - - if (NS_SUCCEEDED(rv)) { - aURI = result; - } - else { - // There are some ugly URIs (e.g., "NC:Foo") that netlib can't - // parse. If NS_MakeAbsoluteURL fails, then just punt and - // assume that aURI was already absolute. - } - - return NS_OK; -} - -nsresult -rdf_MakeAbsoluteURI(nsIURI* aBase, nsCString& aURI) -{ - nsresult rv; - nsXPIDLCString result; - - rv = NS_MakeAbsoluteURI(getter_Copies(result), aURI.get(), aBase); - - if (NS_SUCCEEDED(rv)) { - aURI.Assign(result); - } - else { - // There are some ugly URIs (e.g., "NC:Foo") that netlib can't - // parse. If NS_MakeAbsoluteURL fails, then just punt and - // assume that aURI was already absolute. - } - - return NS_OK; -} - void rdf_FormatDate(PRTime aTime, nsACString &aResult) { diff --git a/rdf/base/src/rdfutil.h b/rdf/base/src/rdfutil.h index 7def32cf8efe..be9306f54639 100644 --- a/rdf/base/src/rdfutil.h +++ b/rdf/base/src/rdfutil.h @@ -64,15 +64,6 @@ class nsIURI; nsresult rdf_MakeRelativeRef(const nsCSubstring& aBaseURI, nsCString& aURI); -nsresult -rdf_MakeAbsoluteURI(const nsString& aBaseURI, nsString& aURI); - -nsresult -rdf_MakeAbsoluteURI(nsIURI* aBaseURL, nsString& aURI); - -nsresult -rdf_MakeAbsoluteURI(nsIURI* aBaseURL, nsCString& aURI); - void rdf_FormatDate(PRTime aTime, nsACString &aResult);