1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is TransforMiiX XSLT processor code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * The MITRE Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1999
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Keith Visco <kvisco@ziplink.net> (Original Author)
24 : * Lidong <lidong520@263.net>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : /*
41 : * XML utility classes
42 : */
43 :
44 : #include "txXMLUtils.h"
45 : #include "nsString.h"
46 : #include "nsReadableUtils.h"
47 : #include "nsGkAtoms.h"
48 : #include "txStringUtils.h"
49 : #include "txNamespaceMap.h"
50 : #include "txXPathTreeWalker.h"
51 :
52 : nsresult
53 0 : txExpandedName::init(const nsAString& aQName, txNamespaceMap* aResolver,
54 : bool aUseDefault)
55 : {
56 0 : const nsAFlatString& qName = PromiseFlatString(aQName);
57 : const PRUnichar* colon;
58 0 : bool valid = XMLUtils::isValidQName(qName, &colon);
59 0 : if (!valid) {
60 0 : return NS_ERROR_FAILURE;
61 : }
62 :
63 0 : if (colon) {
64 0 : nsCOMPtr<nsIAtom> prefix = do_GetAtom(Substring(qName.get(), colon));
65 0 : PRInt32 namespaceID = aResolver->lookupNamespace(prefix);
66 0 : if (namespaceID == kNameSpaceID_Unknown)
67 0 : return NS_ERROR_FAILURE;
68 0 : mNamespaceID = namespaceID;
69 :
70 : const PRUnichar *end;
71 0 : qName.EndReading(end);
72 0 : mLocalName = do_GetAtom(Substring(colon + 1, end));
73 : }
74 : else {
75 : mNamespaceID = aUseDefault ? aResolver->lookupNamespace(nsnull) :
76 0 : kNameSpaceID_None;
77 0 : mLocalName = do_GetAtom(aQName);
78 : }
79 0 : return NS_OK;
80 : }
81 :
82 : //------------------------------/
83 : //- Implementation of XMLUtils -/
84 : //------------------------------/
85 :
86 : // static
87 : nsresult
88 0 : XMLUtils::splitExpatName(const PRUnichar *aExpatName, nsIAtom **aPrefix,
89 : nsIAtom **aLocalName, PRInt32* aNameSpaceID)
90 : {
91 : /**
92 : * Expat can send the following:
93 : * localName
94 : * namespaceURI<separator>localName
95 : * namespaceURI<separator>localName<separator>prefix
96 : */
97 :
98 0 : const PRUnichar *uriEnd = nsnull;
99 0 : const PRUnichar *nameEnd = nsnull;
100 : const PRUnichar *pos;
101 0 : for (pos = aExpatName; *pos; ++pos) {
102 0 : if (*pos == kExpatSeparatorChar) {
103 0 : if (uriEnd) {
104 0 : nameEnd = pos;
105 : }
106 : else {
107 0 : uriEnd = pos;
108 : }
109 : }
110 : }
111 :
112 : const PRUnichar *nameStart;
113 0 : if (uriEnd) {
114 : *aNameSpaceID =
115 : txNamespaceManager::getNamespaceID(nsDependentSubstring(aExpatName,
116 0 : uriEnd));
117 0 : if (*aNameSpaceID == kNameSpaceID_Unknown) {
118 0 : return NS_ERROR_FAILURE;
119 : }
120 :
121 0 : nameStart = (uriEnd + 1);
122 0 : if (nameEnd) {
123 0 : const PRUnichar *prefixStart = nameEnd + 1;
124 0 : *aPrefix = NS_NewAtom(Substring(prefixStart, pos));
125 0 : if (!*aPrefix) {
126 0 : return NS_ERROR_OUT_OF_MEMORY;
127 : }
128 : }
129 : else {
130 0 : nameEnd = pos;
131 0 : *aPrefix = nsnull;
132 : }
133 : }
134 : else {
135 0 : *aNameSpaceID = kNameSpaceID_None;
136 0 : nameStart = aExpatName;
137 0 : nameEnd = pos;
138 0 : *aPrefix = nsnull;
139 : }
140 :
141 0 : *aLocalName = NS_NewAtom(Substring(nameStart, nameEnd));
142 :
143 0 : return *aLocalName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
144 : }
145 :
146 : nsresult
147 0 : XMLUtils::splitQName(const nsAString& aName, nsIAtom** aPrefix,
148 : nsIAtom** aLocalName)
149 : {
150 0 : const nsAFlatString& qName = PromiseFlatString(aName);
151 : const PRUnichar* colon;
152 0 : bool valid = XMLUtils::isValidQName(qName, &colon);
153 0 : if (!valid) {
154 0 : return NS_ERROR_FAILURE;
155 : }
156 :
157 0 : if (colon) {
158 : const PRUnichar *end;
159 0 : qName.EndReading(end);
160 :
161 0 : *aPrefix = NS_NewAtom(Substring(qName.get(), colon));
162 0 : *aLocalName = NS_NewAtom(Substring(colon + 1, end));
163 : }
164 : else {
165 0 : *aPrefix = nsnull;
166 0 : *aLocalName = NS_NewAtom(aName);
167 : }
168 :
169 0 : return NS_OK;
170 : }
171 :
172 0 : const nsDependentSubstring XMLUtils::getLocalPart(const nsAString& src)
173 : {
174 : // Anything after ':' is the local part of the name
175 0 : PRInt32 idx = src.FindChar(':');
176 0 : if (idx == kNotFound) {
177 0 : return Substring(src, 0, src.Length());
178 : }
179 :
180 0 : NS_ASSERTION(idx > 0, "This QName looks invalid.");
181 0 : return Substring(src, idx + 1, src.Length() - (idx + 1));
182 : }
183 :
184 : /**
185 : * Returns true if the given string has only whitespace characters
186 : */
187 0 : bool XMLUtils::isWhitespace(const nsAFlatString& aText)
188 : {
189 : nsAFlatString::const_char_iterator start, end;
190 0 : aText.BeginReading(start);
191 0 : aText.EndReading(end);
192 0 : for ( ; start != end; ++start) {
193 0 : if (!isWhitespace(*start)) {
194 0 : return false;
195 : }
196 : }
197 0 : return true;
198 : }
199 :
200 : /**
201 : * Normalizes the value of a XML processing instruction
202 : **/
203 0 : void XMLUtils::normalizePIValue(nsAString& piValue)
204 : {
205 0 : nsAutoString origValue(piValue);
206 0 : PRUint32 origLength = origValue.Length();
207 0 : PRUint32 conversionLoop = 0;
208 0 : PRUnichar prevCh = 0;
209 0 : piValue.Truncate();
210 :
211 0 : while (conversionLoop < origLength) {
212 0 : PRUnichar ch = origValue.CharAt(conversionLoop);
213 0 : switch (ch) {
214 : case '>':
215 : {
216 0 : if (prevCh == '?') {
217 0 : piValue.Append(PRUnichar(' '));
218 : }
219 0 : break;
220 : }
221 : default:
222 : {
223 0 : break;
224 : }
225 : }
226 0 : piValue.Append(ch);
227 0 : prevCh = ch;
228 0 : ++conversionLoop;
229 : }
230 0 : }
231 :
232 : //static
233 0 : bool XMLUtils::getXMLSpacePreserve(const txXPathNode& aNode)
234 : {
235 0 : nsAutoString value;
236 0 : txXPathTreeWalker walker(aNode);
237 0 : do {
238 0 : if (walker.getAttr(nsGkAtoms::space, kNameSpaceID_XML, value)) {
239 0 : if (TX_StringEqualsAtom(value, nsGkAtoms::preserve)) {
240 0 : return true;
241 : }
242 0 : if (TX_StringEqualsAtom(value, nsGkAtoms::_default)) {
243 0 : return false;
244 : }
245 : }
246 : } while (walker.moveToParent());
247 :
248 0 : return false;
249 : }
|