2005-11-02 10:42:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04: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/. */
|
2005-11-02 10:36:47 +03:00
|
|
|
|
2005-11-02 10:37:24 +03:00
|
|
|
/*
|
|
|
|
* XML utility classes
|
|
|
|
*/
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2005-11-02 20:34:14 +03:00
|
|
|
#include "txXMLUtils.h"
|
2005-11-02 10:39:22 +03:00
|
|
|
#include "nsString.h"
|
2005-11-02 10:39:36 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2011-09-20 00:07:17 +04:00
|
|
|
#include "nsGkAtoms.h"
|
2005-11-02 10:39:34 +03:00
|
|
|
#include "txStringUtils.h"
|
2005-11-02 10:40:09 +03:00
|
|
|
#include "txNamespaceMap.h"
|
2005-11-02 10:41:43 +03:00
|
|
|
#include "txXPathTreeWalker.h"
|
2013-01-23 23:40:52 +04:00
|
|
|
#include "nsContentUtils.h"
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2005-11-02 10:40:09 +03:00
|
|
|
nsresult
|
|
|
|
txExpandedName::init(const nsAString& aQName, txNamespaceMap* aResolver,
|
2011-11-10 23:26:13 +04:00
|
|
|
bool aUseDefault)
|
2005-11-02 10:37:24 +03:00
|
|
|
{
|
2005-11-02 10:41:54 +03:00
|
|
|
const nsAFlatString& qName = PromiseFlatString(aQName);
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* colon;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool valid = XMLUtils::isValidQName(qName, &colon);
|
2005-11-02 10:41:54 +03:00
|
|
|
if (!valid) {
|
2005-11-02 10:37:26 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2005-11-02 10:39:35 +03:00
|
|
|
}
|
2005-11-02 10:37:26 +03:00
|
|
|
|
2005-11-02 10:41:54 +03:00
|
|
|
if (colon) {
|
|
|
|
nsCOMPtr<nsIAtom> prefix = do_GetAtom(Substring(qName.get(), colon));
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t namespaceID = aResolver->lookupNamespace(prefix);
|
2005-11-02 10:37:26 +03:00
|
|
|
if (namespaceID == kNameSpaceID_Unknown)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
mNamespaceID = namespaceID;
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *end;
|
2005-11-02 10:41:54 +03:00
|
|
|
qName.EndReading(end);
|
|
|
|
mLocalName = do_GetAtom(Substring(colon + 1, end));
|
2005-11-02 10:37:26 +03:00
|
|
|
}
|
|
|
|
else {
|
2012-07-30 18:20:58 +04:00
|
|
|
mNamespaceID = aUseDefault ? aResolver->lookupNamespace(nullptr) :
|
2005-11-02 10:41:54 +03:00
|
|
|
kNameSpaceID_None;
|
2005-11-02 10:39:22 +03:00
|
|
|
mLocalName = do_GetAtom(aQName);
|
2005-11-02 10:37:26 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2005-11-02 10:37:24 +03:00
|
|
|
}
|
|
|
|
|
1999-08-28 12:19:56 +04:00
|
|
|
//------------------------------/
|
|
|
|
//- Implementation of XMLUtils -/
|
|
|
|
//------------------------------/
|
|
|
|
|
2005-11-02 10:42:27 +03:00
|
|
|
// static
|
2005-11-02 10:40:09 +03:00
|
|
|
nsresult
|
2014-01-04 19:02:17 +04:00
|
|
|
XMLUtils::splitExpatName(const char16_t *aExpatName, nsIAtom **aPrefix,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsIAtom **aLocalName, int32_t* aNameSpaceID)
|
2005-11-02 10:42:27 +03:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Expat can send the following:
|
|
|
|
* localName
|
|
|
|
* namespaceURI<separator>localName
|
|
|
|
* namespaceURI<separator>localName<separator>prefix
|
|
|
|
*/
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *uriEnd = nullptr;
|
|
|
|
const char16_t *nameEnd = nullptr;
|
|
|
|
const char16_t *pos;
|
2005-11-02 10:42:27 +03:00
|
|
|
for (pos = aExpatName; *pos; ++pos) {
|
2005-11-02 10:42:42 +03:00
|
|
|
if (*pos == kExpatSeparatorChar) {
|
2005-11-02 10:42:27 +03:00
|
|
|
if (uriEnd) {
|
|
|
|
nameEnd = pos;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uriEnd = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *nameStart;
|
2005-11-02 10:42:27 +03:00
|
|
|
if (uriEnd) {
|
|
|
|
*aNameSpaceID =
|
|
|
|
txNamespaceManager::getNamespaceID(nsDependentSubstring(aExpatName,
|
|
|
|
uriEnd));
|
|
|
|
if (*aNameSpaceID == kNameSpaceID_Unknown) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nameStart = (uriEnd + 1);
|
|
|
|
if (nameEnd) {
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *prefixStart = nameEnd + 1;
|
2014-03-15 23:00:15 +04:00
|
|
|
*aPrefix = NS_NewAtom(Substring(prefixStart, pos)).take();
|
2005-11-02 10:42:27 +03:00
|
|
|
if (!*aPrefix) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nameEnd = pos;
|
2012-07-30 18:20:58 +04:00
|
|
|
*aPrefix = nullptr;
|
2005-11-02 10:42:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aNameSpaceID = kNameSpaceID_None;
|
|
|
|
nameStart = aExpatName;
|
|
|
|
nameEnd = pos;
|
2012-07-30 18:20:58 +04:00
|
|
|
*aPrefix = nullptr;
|
2005-11-02 10:42:27 +03:00
|
|
|
}
|
|
|
|
|
2014-03-15 23:00:15 +04:00
|
|
|
*aLocalName = NS_NewAtom(Substring(nameStart, nameEnd)).take();
|
2005-11-02 10:42:27 +03:00
|
|
|
|
|
|
|
return *aLocalName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
XMLUtils::splitQName(const nsAString& aName, nsIAtom** aPrefix,
|
|
|
|
nsIAtom** aLocalName)
|
2005-11-02 10:40:09 +03:00
|
|
|
{
|
2005-11-02 10:41:54 +03:00
|
|
|
const nsAFlatString& qName = PromiseFlatString(aName);
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* colon;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool valid = XMLUtils::isValidQName(qName, &colon);
|
2005-11-02 10:41:54 +03:00
|
|
|
if (!valid) {
|
2005-11-02 10:40:09 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2005-11-02 10:41:54 +03:00
|
|
|
|
|
|
|
if (colon) {
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *end;
|
2005-11-02 10:41:54 +03:00
|
|
|
qName.EndReading(end);
|
|
|
|
|
2014-03-15 23:00:15 +04:00
|
|
|
*aPrefix = NS_NewAtom(Substring(qName.get(), colon)).take();
|
|
|
|
*aLocalName = NS_NewAtom(Substring(colon + 1, end)).take();
|
2005-11-02 10:40:09 +03:00
|
|
|
}
|
|
|
|
else {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aPrefix = nullptr;
|
2014-03-15 23:00:15 +04:00
|
|
|
*aLocalName = NS_NewAtom(aName).take();
|
2005-11-02 10:40:09 +03:00
|
|
|
}
|
2005-11-02 10:39:22 +03:00
|
|
|
|
2005-11-02 10:41:54 +03:00
|
|
|
return NS_OK;
|
2005-11-02 10:37:24 +03:00
|
|
|
}
|
1999-08-28 12:19:56 +04:00
|
|
|
|
1999-11-15 10:13:17 +03:00
|
|
|
/**
|
|
|
|
* Returns true if the given string has only whitespace characters
|
2005-11-02 10:39:22 +03:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool XMLUtils::isWhitespace(const nsAFlatString& aText)
|
2005-11-02 10:38:03 +03:00
|
|
|
{
|
2005-11-02 10:38:49 +03:00
|
|
|
nsAFlatString::const_char_iterator start, end;
|
2005-11-02 10:39:22 +03:00
|
|
|
aText.BeginReading(start);
|
|
|
|
aText.EndReading(end);
|
2005-11-02 10:38:49 +03:00
|
|
|
for ( ; start != end; ++start) {
|
|
|
|
if (!isWhitespace(*start)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
1999-11-15 10:13:17 +03:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-11-02 10:39:22 +03:00
|
|
|
}
|
|
|
|
|
1999-08-28 12:19:56 +04:00
|
|
|
/**
|
|
|
|
* Normalizes the value of a XML processing instruction
|
|
|
|
**/
|
2005-11-02 10:39:22 +03:00
|
|
|
void XMLUtils::normalizePIValue(nsAString& piValue)
|
2005-11-02 10:38:03 +03:00
|
|
|
{
|
2005-11-02 10:39:22 +03:00
|
|
|
nsAutoString origValue(piValue);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t origLength = origValue.Length();
|
|
|
|
uint32_t conversionLoop = 0;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t prevCh = 0;
|
2005-11-02 10:38:52 +03:00
|
|
|
piValue.Truncate();
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2005-11-02 10:38:03 +03:00
|
|
|
while (conversionLoop < origLength) {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t ch = origValue.CharAt(conversionLoop);
|
1999-08-28 12:19:56 +04:00
|
|
|
switch (ch) {
|
|
|
|
case '>':
|
2005-11-02 10:38:03 +03:00
|
|
|
{
|
|
|
|
if (prevCh == '?') {
|
2014-01-04 19:02:17 +04:00
|
|
|
piValue.Append(char16_t(' '));
|
1999-08-28 12:19:56 +04:00
|
|
|
}
|
|
|
|
break;
|
2005-11-02 10:38:03 +03:00
|
|
|
}
|
1999-08-28 12:19:56 +04:00
|
|
|
default:
|
2005-11-02 10:38:03 +03:00
|
|
|
{
|
1999-08-28 12:19:56 +04:00
|
|
|
break;
|
2005-11-02 10:38:03 +03:00
|
|
|
}
|
1999-08-28 12:19:56 +04:00
|
|
|
}
|
2005-11-02 10:38:52 +03:00
|
|
|
piValue.Append(ch);
|
1999-08-28 12:19:56 +04:00
|
|
|
prevCh = ch;
|
2005-11-02 10:38:03 +03:00
|
|
|
++conversionLoop;
|
1999-08-28 12:19:56 +04:00
|
|
|
}
|
2005-11-02 10:38:03 +03:00
|
|
|
}
|
2005-11-02 10:37:53 +03:00
|
|
|
|
2013-01-23 23:40:52 +04:00
|
|
|
//static
|
|
|
|
bool XMLUtils::isValidQName(const nsAFlatString& aQName,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t** aColon)
|
2013-01-23 23:40:52 +04:00
|
|
|
{
|
|
|
|
return NS_SUCCEEDED(nsContentUtils::CheckQName(aQName, true, aColon));
|
|
|
|
}
|
|
|
|
|
2005-11-02 10:38:26 +03:00
|
|
|
//static
|
2011-11-10 23:26:13 +04:00
|
|
|
bool XMLUtils::getXMLSpacePreserve(const txXPathNode& aNode)
|
2005-11-02 10:38:26 +03:00
|
|
|
{
|
2005-11-02 10:39:22 +03:00
|
|
|
nsAutoString value;
|
2005-11-02 10:41:43 +03:00
|
|
|
txXPathTreeWalker walker(aNode);
|
|
|
|
do {
|
2011-09-20 00:07:17 +04:00
|
|
|
if (walker.getAttr(nsGkAtoms::space, kNameSpaceID_XML, value)) {
|
|
|
|
if (TX_StringEqualsAtom(value, nsGkAtoms::preserve)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-11-02 10:41:43 +03:00
|
|
|
}
|
2011-09-20 00:07:17 +04:00
|
|
|
if (TX_StringEqualsAtom(value, nsGkAtoms::_default)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-02 10:38:26 +03:00
|
|
|
}
|
|
|
|
}
|
2005-11-02 10:41:43 +03:00
|
|
|
} while (walker.moveToParent());
|
2005-11-02 10:41:38 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-02 10:38:26 +03:00
|
|
|
}
|