1 | // Copyright (C) 2010 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package com.google.caja.parser.html; |
16 | |
17 | /** |
18 | * Maps XML qualified names like {@code prefix:localName} including ones in the |
19 | * special {@code xmlns:namespaceName} namespace to valid XML local names |
20 | * {@code f:fixupName}. |
21 | * <p> |
22 | * This lets us shuttle names through several stages: |
23 | * <ol> |
24 | * <li>From the {@link Html5ElementStack} where we know attribute names but not |
25 | * where elements begin and end. |
26 | * <p>In this stage, XML elements and attributes have a no namespace and a local |
27 | * name containing a colon. |
28 | * <li>Through the {@link CajaTreeBuilder} which builds a DOM that cannot |
29 | * have attributes whose names start with {@code xmlns:}. At this stage, |
30 | * we do not have all the parent context to make namespace declarations, so |
31 | * we shuttle names outside the default HTML namespace into non namespaced |
32 | * elements. |
33 | * <p>The Xerces DOM implementation can deal with {@code ':'}s in local names |
34 | * for non-namespaced elements and attributes, but rejects any attributes |
35 | * whose names start with {@code xmlns:}. |
36 | * <p>In this stage, XML elements and attributes have a no namespace and a local |
37 | * name containing a colon. |
38 | * <li>To the {@link DomParser} which walks the resulting DOM to find elements |
39 | * and attributes with namespace prefixes that need to be fixed and rewrites |
40 | * them. |
41 | * <p>After this stage finishes, all elements and attributes are properly |
42 | * namespaced. |
43 | * </ol> |
44 | * |
45 | * @see DomParser#fixup |
46 | * |
47 | * @author mikesamuel@gmail.com |
48 | */ |
49 | final class AttributeNameFixup { |
50 | /** A prefix of all names of attributes that need to be fixed up. */ |
51 | static final String PREFIX = "f:"; |
52 | /** A prefix of all fixup names that are encoded namespace declarations. */ |
53 | static final String XMLNS_PREFIX = fixupNameFromQname("xmlns:"); |
54 | |
55 | static String fixupNameFromQname(String qname) { |
56 | int n = qname.length(); |
57 | StringBuilder adjName = new StringBuilder(n + 16); |
58 | adjName.append(PREFIX); |
59 | int pos = 0; |
60 | for (int i = 0; i < n; ++i) { |
61 | char ch = qname.charAt(i); |
62 | switch (ch) { |
63 | // '9' and ':' are adjacent, so this packs well. |
64 | case '9': |
65 | adjName.append(qname, pos, i).append("99"); |
66 | pos = i + 1; |
67 | break; |
68 | case ':': |
69 | adjName.append(qname, pos, i).append("90"); |
70 | pos = i + 1; |
71 | break; |
72 | } |
73 | } |
74 | return adjName.append(qname, pos, n).toString(); |
75 | } |
76 | |
77 | static String qnameFromFixupName(String fixupName) { |
78 | int pos = PREFIX.length(); |
79 | StringBuilder adjName = null; |
80 | int n = fixupName.length(); |
81 | for (int i = pos; i < n; ++i) { |
82 | char ch = fixupName.charAt(i); |
83 | if (ch == '9') { |
84 | if (adjName == null) { adjName = new StringBuilder(n); } |
85 | adjName.append(fixupName, pos, i); |
86 | adjName.append(fixupName.charAt(++i) == '9' ? '9' : ':'); |
87 | pos = i + 1; |
88 | } |
89 | } |
90 | if (adjName == null) { return fixupName.substring(pos); } |
91 | return adjName.append(fixupName, pos, n).toString(); |
92 | } |
93 | } |