| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.functions.ResolveQName; |
| 11 |
import net.sf.saxon.lib.NamespaceConstant; |
| 12 |
import net.sf.saxon.lib.SaxonOutputKeys; |
| 13 |
import net.sf.saxon.lib.SerializerFactory; |
| 14 |
import net.sf.saxon.om.*; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.value.Whitespace; |
| 17 |
|
| 18 |
import javax.xml.transform.OutputKeys; |
| 19 |
import java.util.HashMap; |
| 20 |
import java.util.Map; |
| 21 |
import java.util.Properties; |
| 22 |
import java.util.StringTokenizer; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class XSLOutput extends StyleElement { |
| 29 |
|
| 30 |
private StructuredQName outputFormatName; |
| 31 |
private String method = null; |
| 32 |
private String outputVersion = null; |
| 33 |
|
| 34 |
private String useCharacterMaps = null; |
| 35 |
private Map<String, String> serializationAttributes = new HashMap<String, String>(10); |
| 36 |
|
| 37 |
private HashMap<String, String> userAttributes = null; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
@Override |
| 47 |
public boolean isDeclaration() { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
public void prepareAttributes() throws XPathException { |
| 52 |
AttributeCollection atts = getAttributeList(); |
| 53 |
String nameAtt = null; |
| 54 |
for (int a = 0; a < atts.getLength(); a++) { |
| 55 |
String f = atts.getNodeName(a).getStructuredQName().getClarkName(); |
| 56 |
if (f.equals("name")) { |
| 57 |
nameAtt = Whitespace.trim(atts.getValue(a)); |
| 58 |
} else if (f.equals("version")) { |
| 59 |
String outputVersion = Whitespace.trim(atts.getValue(a)); |
| 60 |
serializationAttributes.put(f, outputVersion); |
| 61 |
} else if (f.equals("use-character-maps")) { |
| 62 |
useCharacterMaps = atts.getValue(a); |
| 63 |
} else if (f.equals("parameter-document")) { |
| 64 |
String val = Whitespace.trim(atts.getValue(a)); |
| 65 |
serializationAttributes.put(f, val); |
| 66 |
serializationAttributes.put(SaxonOutputKeys.PARAMETER_DOCUMENT_BASE_URI, getBaseURI()); |
| 67 |
} else if (XSLResultDocument.fans.contains(f) && !f.equals("output-version")) { |
| 68 |
String val = atts.getValue(a); |
| 69 |
if (!f.equals(SaxonOutputKeys.ITEM_SEPARATOR) && !f.equals(SaxonOutputKeys.NEWLINE)) { |
| 70 |
val = Whitespace.trim(val); |
| 71 |
} |
| 72 |
serializationAttributes.put(f, val); |
| 73 |
} else { |
| 74 |
String attributeURI = atts.getURI(a); |
| 75 |
if ("".equals(attributeURI) || |
| 76 |
NamespaceConstant.XSLT.equals(attributeURI) || |
| 77 |
NamespaceConstant.SAXON.equals(attributeURI)) { |
| 78 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 79 |
} else { |
| 80 |
String name = '{' + attributeURI + '}' + atts.getLocalName(a); |
| 81 |
if (userAttributes == null) { |
| 82 |
userAttributes = new HashMap<String, String>(5); |
| 83 |
} |
| 84 |
userAttributes.put(name, atts.getValue(a)); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
if (nameAtt != null) { |
| 89 |
try { |
| 90 |
outputFormatName = makeQName(nameAtt); |
| 91 |
} catch (XPathException err) { |
| 92 |
compileError(err.getMessage(), "XTSE1570"); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public StructuredQName getFormatQName() { |
| 104 |
return outputFormatName; |
| 105 |
} |
| 106 |
|
| 107 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 108 |
checkTopLevel("XTSE0010", false); |
| 109 |
checkEmpty(); |
| 110 |
} |
| 111 |
|
| 112 |
public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) { |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
@Override |
| 122 |
protected void processVersionAttribute(String ns) { |
| 123 |
version = ((StyleElement)getParent()).getEffectiveVersion(); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
protected void gatherOutputProperties(Properties details, HashMap<String, Integer> precedences, int thisPrecedence) |
| 140 |
throws XPathException { |
| 141 |
SerializerFactory sf = getConfiguration().getSerializerFactory(); |
| 142 |
if (method != null) { |
| 143 |
if ("xml".equals(method) || "html".equals(method) || |
| 144 |
"text".equals(method) || "xhtml".equals(method) || |
| 145 |
"json".equals(method) || "adaptive".equals(method)) { |
| 146 |
checkAndPut(sf, OutputKeys.METHOD, method, details, precedences, thisPrecedence); |
| 147 |
|
| 148 |
} else { |
| 149 |
String[] parts; |
| 150 |
try { |
| 151 |
parts = NameChecker.getQNameParts(method); |
| 152 |
String prefix = parts[0]; |
| 153 |
if (prefix.isEmpty()) { |
| 154 |
compileError("method must be xml, html, xhtml, text, json, adaptive, or a prefixed name", "XTSE1570"); |
| 155 |
} else { |
| 156 |
String uri = getURIForPrefix(prefix, false); |
| 157 |
if (uri == null) { |
| 158 |
undeclaredNamespaceError(prefix, "XTSE0280", "method"); |
| 159 |
} |
| 160 |
checkAndPut(sf, OutputKeys.METHOD, '{' + uri + '}' + parts[1], details, precedences, thisPrecedence); |
| 161 |
|
| 162 |
} |
| 163 |
} catch (QNameException e) { |
| 164 |
compileError("Invalid method name. " + e.getMessage(), "XTSE1570"); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
for (Map.Entry<String, String> entry : serializationAttributes.entrySet()) { |
| 170 |
checkAndPut(sf, entry.getKey(), entry.getValue(), details, precedences, thisPrecedence); |
| 171 |
} |
| 172 |
|
| 173 |
if (serializationAttributes.containsKey(SaxonOutputKeys.NEXT_IN_CHAIN)) { |
| 174 |
checkAndPut(sf, SaxonOutputKeys.NEXT_IN_CHAIN_BASE_URI, getSystemId(), details, precedences, thisPrecedence); |
| 175 |
} |
| 176 |
|
| 177 |
if (useCharacterMaps != null) { |
| 178 |
String s = prepareCharacterMaps(this, useCharacterMaps, details); |
| 179 |
details.setProperty(SaxonOutputKeys.USE_CHARACTER_MAPS, s); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
if (userAttributes != null) { |
| 185 |
for (Map.Entry<String, String> e : userAttributes.entrySet()) { |
| 186 |
details.setProperty(e.getKey(), e.getValue()); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private void checkAndPut(SerializerFactory sf, String property, String value, Properties props, |
| 206 |
HashMap<String, Integer> precedences, int thisPrecedence) { |
| 207 |
try { |
| 208 |
if (isListOfNames(property)) { |
| 209 |
boolean useDefaultNS = !property.equals(SaxonOutputKeys.ATTRIBUTE_ORDER); |
| 210 |
boolean allowStar = property.equals(SaxonOutputKeys.ATTRIBUTE_ORDER); |
| 211 |
value = SaxonOutputKeys.parseListOfNodeNames( |
| 212 |
value, this, useDefaultNS, false, allowStar, "XTSE0280"); |
| 213 |
} |
| 214 |
if (isQName(property) && value.contains(":")) { |
| 215 |
value = ResolveQName.resolveQName(value, this).getClarkName(); |
| 216 |
} |
| 217 |
value = sf.checkOutputProperty(property, value); |
| 218 |
} catch (XPathException err) { |
| 219 |
String code = property.equals("method") ? "XTSE1570" : "XTSE0020"; |
| 220 |
if (property.contains("{")) { |
| 221 |
compileError(err.getMessage(), code); |
| 222 |
} else { |
| 223 |
compileErrorInAttribute(err.getMessage(), code, property); |
| 224 |
} |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
String old = props.getProperty(property); |
| 229 |
if (old == null) { |
| 230 |
props.setProperty(property, value); |
| 231 |
precedences.put(property, thisPrecedence); |
| 232 |
} else if (old.equals(value)) { |
| 233 |
|
| 234 |
} else if (isListOfNames(property)) { |
| 235 |
props.setProperty(property, old + " " + value); |
| 236 |
precedences.put(property, thisPrecedence); |
| 237 |
} else { |
| 238 |
Integer oldPrec = precedences.get(property); |
| 239 |
if (oldPrec == null) { |
| 240 |
return; |
| 241 |
} |
| 242 |
if (oldPrec > thisPrecedence) { |
| 243 |
|
| 244 |
} else if (oldPrec == thisPrecedence) { |
| 245 |
compileError("Conflicting values for output property " + property, "XTSE1560"); |
| 246 |
} else { |
| 247 |
|
| 248 |
throw new IllegalStateException("Output properties must be processed in decreasing precedence order"); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
private static boolean isListOfNames(String property) { |
| 254 |
return property.equals(OutputKeys.CDATA_SECTION_ELEMENTS) || |
| 255 |
property.equals(SaxonOutputKeys.SUPPRESS_INDENTATION) || |
| 256 |
property.equals(SaxonOutputKeys.ATTRIBUTE_ORDER) || |
| 257 |
property.equals(SaxonOutputKeys.DOUBLE_SPACE); |
| 258 |
} |
| 259 |
|
| 260 |
private static boolean isQName(String property) { |
| 261 |
return property.equals(OutputKeys.METHOD) || |
| 262 |
property.equals(SaxonOutputKeys.JSON_NODE_OUTPUT_METHOD); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public static String prepareCharacterMaps(StyleElement element, |
| 275 |
String useCharacterMaps, |
| 276 |
Properties details) |
| 277 |
throws XPathException { |
| 278 |
PrincipalStylesheetModule psm = element.getPrincipalStylesheetModule(); |
| 279 |
String existing = details.getProperty(SaxonOutputKeys.USE_CHARACTER_MAPS); |
| 280 |
if (existing == null) { |
| 281 |
existing = ""; |
| 282 |
} |
| 283 |
String s = ""; |
| 284 |
StringTokenizer st = new StringTokenizer(useCharacterMaps, " \t\n\r", false); |
| 285 |
while (st.hasMoreTokens()) { |
| 286 |
String displayname = st.nextToken(); |
| 287 |
StructuredQName qName = element.makeQName(displayname); |
| 288 |
ComponentDeclaration decl = psm.getCharacterMap(qName); |
| 289 |
if (decl == null) { |
| 290 |
element.compileErrorInAttribute( |
| 291 |
"No character-map named '" + displayname + "' has been defined", "XTSE1590", "use-character-maps"); |
| 292 |
} |
| 293 |
s += " " + qName.getClarkName(); |
| 294 |
} |
| 295 |
existing = s + existing; |
| 296 |
return existing; |
| 297 |
} |
| 298 |
|
| 299 |
} |
| 300 |
|