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
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libcore.xml;
import junit.framework.TestCase;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Tests that we both report and retrieve attributes using the appropriate
* names for different combinations of namespaces and namespace prefixes.
*/
public class NamespacedAttributesLookupTest extends TestCase {
private static final String SAX_PROPERTY_NS =
"http://xml.org/sax/features/namespaces";
private static final String SAX_PROPERTY_NS_PREFIXES =
"http://xml.org/sax/features/namespace-prefixes";
private static String xml = "<?xml version='1.0' encoding='UTF-8'?>" +
"<test xmlns='http://foo' xmlns:bar='http://bar' xmlns:baz='http://baz' baz:c='a'>" +
"<b c='w' bar:c='x'/>" +
"<bar:e baz:c='y' bar:c='z'/>" +
"</test>";
public void testNamespace() throws Exception {
List<String> expected = Arrays.asList(
"http://foo,test\n" +
" http://baz,c\n" +
" http://bar+c=null,\n" +
" bar:c=null\n",
"http://foo,b\n" +
" ,c\n" +
" http://bar,c\n" +
" http://bar+c=x,\n" +
" bar:c=x\n",
"http://bar,e\n" +
" http://baz,c\n" +
" http://bar,c\n" +
" http://bar+c=z,\n" +
" bar:c=z\n");
boolean namespace = true;
boolean namespacePrefixes = false;
assertEquals(expected, getStartElements(xml, namespace, namespacePrefixes));
}
public void testNamespacePrefixes() throws Exception {
List<String> expected = Arrays.asList(
"test\n" +
" xmlns\n" +
" xmlns:bar\n" +
" xmlns:baz\n" +
" baz:c\n" +
" http://bar+c=null,\n" +
" bar:c=null\n",
"b\n" +
" c\n" +
" bar:c\n" +
" http://bar+c=null,\n" +
" bar:c=x\n",
"bar:e\n" +
" baz:c\n" +
" bar:c\n" +
" http://bar+c=null,\n" +
" bar:c=z\n");
boolean namespace = false;
boolean namespacePrefixes = true;
assertEquals(expected, getStartElements(xml, namespace, namespacePrefixes));
}
public List<String> getStartElements(String xml, final boolean namespace, boolean namespacePrefixes)
throws Exception {
final List<String> result = new ArrayList<String>();
XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
reader.setFeature(SAX_PROPERTY_NS, namespace);
reader.setFeature(SAX_PROPERTY_NS_PREFIXES, namespacePrefixes);
reader.setContentHandler(new DefaultHandler() {
@Override public final void startElement(
String uri, String localName, String qName, Attributes attributes) {
StringBuilder serialized = new StringBuilder();
/*
* Only supply the uri+localName or qname depending on whether namespaces are
* enabled. It's an optional parameter and the RI only supplies one or the other.
*/
if (namespace) {
serialized.append(uri).append(",");
serialized.append(localName);
} else {
serialized.append(qName);
}
for (int i = 0; i < attributes.getLength(); i++) {
serialized.append("\n ");
if (namespace) {
serialized.append(attributes.getURI(i)).append(",");
serialized.append(attributes.getLocalName(i));
} else {
serialized.append(attributes.getQName(i));
}
}
serialized.append("\n http://bar+c=")
.append(attributes.getValue("http://bar", "c")).append(",")
.append("\n bar:c=")
.append(attributes.getValue("bar:c"))
.append("\n");
result.add(serialized.toString());
}
});
reader.parse(new InputSource(new StringReader(xml)));
return result;
}
}
|