2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XML utility classes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "txXMLUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2011-09-19 13:07:17 -07:00
|
|
|
#include "nsGkAtoms.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "txStringUtils.h"
|
|
|
|
#include "txNamespaceMap.h"
|
|
|
|
#include "txXPathTreeWalker.h"
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
txExpandedName::init(const nsAString& aQName, txNamespaceMap* aResolver,
|
2011-11-10 11:26:13 -08:00
|
|
|
bool aUseDefault)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
const nsAFlatString& qName = PromiseFlatString(aQName);
|
|
|
|
const PRUnichar* colon;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool valid = XMLUtils::isValidQName(qName, &colon);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!valid) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (colon) {
|
|
|
|
nsCOMPtr<nsIAtom> prefix = do_GetAtom(Substring(qName.get(), colon));
|
|
|
|
PRInt32 namespaceID = aResolver->lookupNamespace(prefix);
|
|
|
|
if (namespaceID == kNameSpaceID_Unknown)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
mNamespaceID = namespaceID;
|
|
|
|
|
|
|
|
const PRUnichar *end;
|
|
|
|
qName.EndReading(end);
|
|
|
|
mLocalName = do_GetAtom(Substring(colon + 1, end));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mNamespaceID = aUseDefault ? aResolver->lookupNamespace(nsnull) :
|
|
|
|
kNameSpaceID_None;
|
|
|
|
mLocalName = do_GetAtom(aQName);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------/
|
|
|
|
//- Implementation of XMLUtils -/
|
|
|
|
//------------------------------/
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
XMLUtils::splitExpatName(const PRUnichar *aExpatName, nsIAtom **aPrefix,
|
|
|
|
nsIAtom **aLocalName, PRInt32* aNameSpaceID)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Expat can send the following:
|
|
|
|
* localName
|
|
|
|
* namespaceURI<separator>localName
|
|
|
|
* namespaceURI<separator>localName<separator>prefix
|
|
|
|
*/
|
|
|
|
|
|
|
|
const PRUnichar *uriEnd = nsnull;
|
|
|
|
const PRUnichar *nameEnd = nsnull;
|
|
|
|
const PRUnichar *pos;
|
|
|
|
for (pos = aExpatName; *pos; ++pos) {
|
|
|
|
if (*pos == kExpatSeparatorChar) {
|
|
|
|
if (uriEnd) {
|
|
|
|
nameEnd = pos;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uriEnd = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const PRUnichar *nameStart;
|
|
|
|
if (uriEnd) {
|
|
|
|
*aNameSpaceID =
|
|
|
|
txNamespaceManager::getNamespaceID(nsDependentSubstring(aExpatName,
|
|
|
|
uriEnd));
|
|
|
|
if (*aNameSpaceID == kNameSpaceID_Unknown) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nameStart = (uriEnd + 1);
|
|
|
|
if (nameEnd) {
|
|
|
|
const PRUnichar *prefixStart = nameEnd + 1;
|
2010-06-08 16:25:27 -07:00
|
|
|
*aPrefix = NS_NewAtom(Substring(prefixStart, pos));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!*aPrefix) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nameEnd = pos;
|
|
|
|
*aPrefix = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aNameSpaceID = kNameSpaceID_None;
|
|
|
|
nameStart = aExpatName;
|
|
|
|
nameEnd = pos;
|
|
|
|
*aPrefix = nsnull;
|
|
|
|
}
|
|
|
|
|
2010-06-08 16:25:27 -07:00
|
|
|
*aLocalName = NS_NewAtom(Substring(nameStart, nameEnd));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return *aLocalName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
XMLUtils::splitQName(const nsAString& aName, nsIAtom** aPrefix,
|
|
|
|
nsIAtom** aLocalName)
|
|
|
|
{
|
|
|
|
const nsAFlatString& qName = PromiseFlatString(aName);
|
|
|
|
const PRUnichar* colon;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool valid = XMLUtils::isValidQName(qName, &colon);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!valid) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (colon) {
|
|
|
|
const PRUnichar *end;
|
|
|
|
qName.EndReading(end);
|
|
|
|
|
|
|
|
*aPrefix = NS_NewAtom(Substring(qName.get(), colon));
|
|
|
|
*aLocalName = NS_NewAtom(Substring(colon + 1, end));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aPrefix = nsnull;
|
|
|
|
*aLocalName = NS_NewAtom(aName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsDependentSubstring XMLUtils::getLocalPart(const nsAString& src)
|
|
|
|
{
|
|
|
|
// Anything after ':' is the local part of the name
|
|
|
|
PRInt32 idx = src.FindChar(':');
|
|
|
|
if (idx == kNotFound) {
|
|
|
|
return Substring(src, 0, src.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(idx > 0, "This QName looks invalid.");
|
|
|
|
return Substring(src, idx + 1, src.Length() - (idx + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given string has only whitespace characters
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool XMLUtils::isWhitespace(const nsAFlatString& aText)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsAFlatString::const_char_iterator start, end;
|
|
|
|
aText.BeginReading(start);
|
|
|
|
aText.EndReading(end);
|
|
|
|
for ( ; start != end; ++start) {
|
|
|
|
if (!isWhitespace(*start)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalizes the value of a XML processing instruction
|
|
|
|
**/
|
|
|
|
void XMLUtils::normalizePIValue(nsAString& piValue)
|
|
|
|
{
|
|
|
|
nsAutoString origValue(piValue);
|
|
|
|
PRUint32 origLength = origValue.Length();
|
|
|
|
PRUint32 conversionLoop = 0;
|
|
|
|
PRUnichar prevCh = 0;
|
|
|
|
piValue.Truncate();
|
|
|
|
|
|
|
|
while (conversionLoop < origLength) {
|
|
|
|
PRUnichar ch = origValue.CharAt(conversionLoop);
|
|
|
|
switch (ch) {
|
|
|
|
case '>':
|
|
|
|
{
|
|
|
|
if (prevCh == '?') {
|
|
|
|
piValue.Append(PRUnichar(' '));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
piValue.Append(ch);
|
|
|
|
prevCh = ch;
|
|
|
|
++conversionLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
2011-11-10 11:26:13 -08:00
|
|
|
bool XMLUtils::getXMLSpacePreserve(const txXPathNode& aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsAutoString value;
|
|
|
|
txXPathTreeWalker walker(aNode);
|
|
|
|
do {
|
2011-09-19 13:07:17 -07:00
|
|
|
if (walker.getAttr(nsGkAtoms::space, kNameSpaceID_XML, value)) {
|
|
|
|
if (TX_StringEqualsAtom(value, nsGkAtoms::preserve)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-09-19 13:07:17 -07:00
|
|
|
if (TX_StringEqualsAtom(value, nsGkAtoms::_default)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (walker.moveToParent());
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|