| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.z.IntHashMap; |
| 11 |
import net.sf.saxon.om.*; |
| 12 |
import net.sf.saxon.trans.Err; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 15 |
import net.sf.saxon.value.Whitespace; |
| 16 |
|
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.List; |
| 19 |
import java.util.StringTokenizer; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class XSLCharacterMap extends StyleElement { |
| 26 |
|
| 27 |
String use; |
| 28 |
|
| 29 |
|
| 30 |
List<XSLCharacterMap> characterMapElements = null; |
| 31 |
|
| 32 |
|
| 33 |
boolean validated = false; |
| 34 |
|
| 35 |
|
| 36 |
boolean redundant = false; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
@Override |
| 47 |
public boolean isDeclaration() { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public StructuredQName getCharacterMapName() { |
| 58 |
StructuredQName name = getObjectName(); |
| 59 |
if (name == null) { |
| 60 |
try { |
| 61 |
return makeQName(getAttributeValue("", "name")); |
| 62 |
} catch (Exception err) { |
| 63 |
|
| 64 |
return new StructuredQName("", "", "unnamedCharacterMap_" + hashCode()); |
| 65 |
} |
| 66 |
} |
| 67 |
return name; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public boolean isRedundant() { |
| 81 |
return redundant; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public void prepareAttributes() throws XPathException { |
| 91 |
|
| 92 |
String name = null; |
| 93 |
use = null; |
| 94 |
|
| 95 |
AttributeCollection atts = getAttributeList(); |
| 96 |
|
| 97 |
for (int a = 0; a < atts.getLength(); a++) { |
| 98 |
String f = atts.getQName(a); |
| 99 |
if (f.equals("name")) { |
| 100 |
name = Whitespace.trim(atts.getValue(a)); |
| 101 |
} else if (f.equals("use-character-maps")) { |
| 102 |
use = atts.getValue(a); |
| 103 |
} else { |
| 104 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (name == null) { |
| 109 |
reportAbsence("name"); |
| 110 |
name = "unnamedCharacterMap_" + hashCode(); |
| 111 |
} |
| 112 |
|
| 113 |
try { |
| 114 |
setObjectName(makeQName(name)); |
| 115 |
} catch (XPathException err) { |
| 116 |
compileError(err.getMessage(), "XTSE0020"); |
| 117 |
name = "unnamedCharacterMap_" + hashCode(); |
| 118 |
setObjectName(new StructuredQName("", "", name)); |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 124 |
|
| 125 |
if (validated) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
checkTopLevel("XTSE0010", false); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
iterateAxis(AxisInfo.CHILD).forEachOrFail(child -> { |
| 136 |
if (!(child instanceof XSLOutputCharacter)) { |
| 137 |
compileError("Only xsl:output-character is allowed within xsl:character-map", "XTSE0010"); |
| 138 |
} |
| 139 |
}); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
PrincipalStylesheetModule psm = getPrincipalStylesheetModule(); |
| 145 |
ComponentDeclaration other = psm.getCharacterMap(getObjectName()); |
| 146 |
if (other.getSourceElement() != this) { |
| 147 |
if (decl.getPrecedence() == other.getPrecedence()) { |
| 148 |
compileError("There are two character-maps with the same name and import precedence", "XTSE1580"); |
| 149 |
} else if (decl.getPrecedence() < other.getPrecedence()) { |
| 150 |
redundant = true; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if (use != null) { |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
characterMapElements = new ArrayList<XSLCharacterMap>(5); |
| 161 |
StringTokenizer st = new StringTokenizer(use, " \t\n\r", false); |
| 162 |
|
| 163 |
while (st.hasMoreTokens()) { |
| 164 |
String displayname = st.nextToken(); |
| 165 |
try { |
| 166 |
String[] parts = NameChecker.getQNameParts(displayname); |
| 167 |
String uri = getURIForPrefix(parts[0], false); |
| 168 |
if (uri == null) { |
| 169 |
compileError("Undeclared namespace prefix " + Err.wrap(parts[0]) |
| 170 |
+ " in character map name", "XTSE0280"); |
| 171 |
} |
| 172 |
StructuredQName qn = new StructuredQName(parts[0], uri, parts[1]); |
| 173 |
ComponentDeclaration charMapDecl = psm.getCharacterMap(qn); |
| 174 |
if (charMapDecl == null) { |
| 175 |
compileError("No character-map named '" + displayname + "' has been defined", "XTSE1590"); |
| 176 |
} else { |
| 177 |
XSLCharacterMap ref = (XSLCharacterMap) charMapDecl.getSourceElement(); |
| 178 |
characterMapElements.add(ref); |
| 179 |
} |
| 180 |
} catch (QNameException err) { |
| 181 |
compileError("Invalid character-map name. " + err.getMessage(), "XTSE1590"); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
for (Object characterMapElement : characterMapElements) { |
| 188 |
((XSLCharacterMap) characterMapElement).checkCircularity(this); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
validated = true; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private void checkCircularity(XSLCharacterMap origin) throws XPathException { |
| 205 |
if (this == origin) { |
| 206 |
compileError("The definition of the character map is circular", "XTSE1600"); |
| 207 |
characterMapElements = null; |
| 208 |
} else { |
| 209 |
if (!validated) { |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
return; |
| 214 |
} |
| 215 |
if (characterMapElements != null) { |
| 216 |
for (Object characterMapElement : characterMapElements) { |
| 217 |
((XSLCharacterMap) characterMapElement).checkCircularity(origin); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public void assemble(IntHashMap<String> map) { |
| 231 |
if (characterMapElements != null) { |
| 232 |
for (XSLCharacterMap charmap : characterMapElements) { |
| 233 |
charmap.assemble(map); |
| 234 |
} |
| 235 |
} |
| 236 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 237 |
while (true) { |
| 238 |
Item child = kids.next(); |
| 239 |
if (child == null) { |
| 240 |
return; |
| 241 |
} |
| 242 |
XSLOutputCharacter oc = (XSLOutputCharacter) child; |
| 243 |
map.put(oc.getCodePoint(), oc.getReplacementString()); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) throws XPathException { |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
} |
| 252 |
|