1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */
/*
* XML utility classes
*/
#include "txXMLUtils.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsGkAtoms.h"
#include "txStringUtils.h"
#include "txNamespaceMap.h"
#include "txXPathTreeWalker.h"
#include "nsContentUtils.h"
//------------------------------/
//- Implementation of XMLUtils -/
//------------------------------/
// static
nsresult
XMLUtils::splitExpatName(const char16_t *aExpatName, nsIAtom **aPrefix,
nsIAtom **aLocalName, int32_t* aNameSpaceID)
{
/**
* Expat can send the following:
* localName
* namespaceURI<separator>localName
* namespaceURI<separator>localName<separator>prefix
*/
const char16_t *uriEnd = nullptr;
const char16_t *nameEnd = nullptr;
const char16_t *pos;
for (pos = aExpatName; *pos; ++pos) {
if (*pos == kExpatSeparatorChar) {
if (uriEnd) {
nameEnd = pos;
}
else {
uriEnd = pos;
}
}
}
const char16_t *nameStart;
if (uriEnd) {
*aNameSpaceID =
txNamespaceManager::getNamespaceID(nsDependentSubstring(aExpatName,
uriEnd));
if (*aNameSpaceID == kNameSpaceID_Unknown) {
return NS_ERROR_FAILURE;
}
nameStart = (uriEnd + 1);
if (nameEnd) {
const char16_t *prefixStart = nameEnd + 1;
*aPrefix = NS_Atomize(Substring(prefixStart, pos)).take();
if (!*aPrefix) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
else {
nameEnd = pos;
*aPrefix = nullptr;
}
}
else {
*aNameSpaceID = kNameSpaceID_None;
nameStart = aExpatName;
nameEnd = pos;
*aPrefix = nullptr;
}
*aLocalName = NS_Atomize(Substring(nameStart, nameEnd)).take();
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 char16_t* colon;
bool valid = XMLUtils::isValidQName(qName, &colon);
if (!valid) {
return NS_ERROR_FAILURE;
}
if (colon) {
const char16_t *end;
qName.EndReading(end);
*aPrefix = NS_Atomize(Substring(qName.get(), colon)).take();
*aLocalName = NS_Atomize(Substring(colon + 1, end)).take();
}
else {
*aPrefix = nullptr;
*aLocalName = NS_Atomize(aName).take();
}
return NS_OK;
}
/**
* Returns true if the given string has only whitespace characters
*/
bool XMLUtils::isWhitespace(const nsAFlatString& aText)
{
nsAFlatString::const_char_iterator start, end;
aText.BeginReading(start);
aText.EndReading(end);
for ( ; start != end; ++start) {
if (!isWhitespace(*start)) {
return false;
}
}
return true;
}
/**
* Normalizes the value of a XML processing instruction
**/
void XMLUtils::normalizePIValue(nsAString& piValue)
{
nsAutoString origValue(piValue);
uint32_t origLength = origValue.Length();
uint32_t conversionLoop = 0;
char16_t prevCh = 0;
piValue.Truncate();
while (conversionLoop < origLength) {
char16_t ch = origValue.CharAt(conversionLoop);
switch (ch) {
case '>':
{
if (prevCh == '?') {
piValue.Append(char16_t(' '));
}
break;
}
default:
{
break;
}
}
piValue.Append(ch);
prevCh = ch;
++conversionLoop;
}
}
//static
bool XMLUtils::isValidQName(const nsAFlatString& aQName,
const char16_t** aColon)
{
return NS_SUCCEEDED(nsContentUtils::CheckQName(aQName, true, aColon));
}
//static
bool XMLUtils::getXMLSpacePreserve(const txXPathNode& aNode)
{
nsAutoString value;
txXPathTreeWalker walker(aNode);
do {
if (walker.getAttr(nsGkAtoms::space, kNameSpaceID_XML, value)) {
if (TX_StringEqualsAtom(value, nsGkAtoms::preserve)) {
return true;
}
if (TX_StringEqualsAtom(value, nsGkAtoms::_default)) {
return false;
}
}
} while (walker.moveToParent());
return false;
}
|