1 package net.sf.saxon.event; 2 import net.sf.saxon.Err; 3 import net.sf.saxon.om.XMLChar; 4 import net.sf.saxon.trans.DynamicError; 5 6 import javax.xml.transform.OutputKeys ; 7 import java.util.StringTokenizer ; 8 9 19 20 public class SaxonOutputKeys { 21 22 25 26 private SaxonOutputKeys() {} 27 28 33 34 public static final String INDENT_SPACES = "{http://saxon.sf.net/}indent-spaces"; 35 36 40 41 public static final String STYLESHEET_VERSION = "{http://saxon.sf.net/}stylesheet-version"; 42 43 49 50 public static final String USE_CHARACTER_MAPS = "use-character-maps"; 51 52 53 58 59 public static final String INCLUDE_CONTENT_TYPE = "include-content-type"; 60 61 66 67 public static final String UNDECLARE_PREFIXES = "undeclare-prefixes"; 68 69 74 75 public static final String ESCAPE_URI_ATTRIBUTES = "escape-uri-attibutes"; 76 77 84 public static final String CHARACTER_REPRESENTATION = "{http://saxon.sf.net/}character-representation"; 85 86 93 public static final String NEXT_IN_CHAIN = "{http://saxon.sf.net/}next-in-chain"; 94 public static final String NEXT_IN_CHAIN_BASE_URI = "{http://saxon.sf.net/}next-in-chain-base-uri"; 95 96 102 103 public static final String BYTE_ORDER_MARK = "byte-order-mark"; 104 105 110 111 public static final String NORMALIZATION_FORM = "normalization-form"; 112 113 114 121 122 public static final String REQUIRE_WELL_FORMED = "{http://saxon.sf.net/}require-well-formed"; 123 124 132 133 public static final String WRAP = "{http://saxon.sf.net/}require-well-formed"; 134 135 141 142 public static final void checkOutputProperty(String key, String value) throws DynamicError { 143 if (!key.startsWith("{") || key.startsWith("{http://saxon.sf.net/}" )) { 144 if (key.equals(OutputKeys.CDATA_SECTION_ELEMENTS)) { 145 if (value != null) { 146 checkListOfClarkNames(key, value); 147 } 148 } else if (key.equals(OutputKeys.DOCTYPE_PUBLIC)) { 149 } else if (key.equals(OutputKeys.DOCTYPE_SYSTEM)) { 151 } else if (key.equals(OutputKeys.ENCODING)) { 153 } else if (key.equals(OutputKeys.INDENT)) { 155 if (value != null) { 156 checkYesOrNo(key, value); 157 } 158 } else if (key.equals(OutputKeys.MEDIA_TYPE)) { 159 } else if (key.equals(OutputKeys.METHOD)) { 161 if (value != null) { 162 checkMethod(value); 163 } 164 } else if (key.equals(OutputKeys.OMIT_XML_DECLARATION)) { 165 if (value != null) { 166 checkYesOrNo(key, value); 167 } 168 } else if (key.equals(OutputKeys.STANDALONE)) { 169 if (value != null && !value.equals("omit")) { 170 checkYesOrNo(key, value); 171 } 172 } else if (key.equals(OutputKeys.VERSION)) { 173 } else if (key.equals(STYLESHEET_VERSION)) { 175 } else if (key.equals(INDENT_SPACES)) { 177 if (value != null) { 178 checkNonNegativeInteger(key, value); 179 } 180 } else if (key.equals(INCLUDE_CONTENT_TYPE)) { 181 if (value != null) { 182 checkYesOrNo(key, value); 183 } 184 } else if (key.equals(ESCAPE_URI_ATTRIBUTES)) { 185 if (value != null) { 186 checkYesOrNo(key, value); 187 } 188 } else if (key.equals(CHARACTER_REPRESENTATION)) { 189 } else if (key.equals(NEXT_IN_CHAIN)) { 191 } else if (key.equals(NEXT_IN_CHAIN_BASE_URI)) { 193 } else if (key.equals(UNDECLARE_PREFIXES)) { 195 if (value != null) { 196 checkYesOrNo(key, value); 197 } 198 } else if (key.equals(USE_CHARACTER_MAPS)) { 199 if (value != null) { 200 checkListOfClarkNames(key, value); 201 } 202 } else if (key.equals(REQUIRE_WELL_FORMED)) { 203 if (value != null) { 204 checkYesOrNo(key, value); 205 } 206 } else if (key.equals(BYTE_ORDER_MARK)) { 207 if (value != null) { 208 checkYesOrNo(key, value); 209 } 210 } else if (key.equals(WRAP)) { 211 if (value != null) { 212 checkYesOrNo(key, value); 213 } 214 } else { 215 throw new DynamicError("Unknown serialization parameter " + Err.wrap(key)); 216 } 217 } else { 218 return; 219 } 220 } 221 222 private static void checkYesOrNo(String key, String value) throws DynamicError { 223 if ("yes".equals(value) || "no".equals(value)) { 224 } else { 226 throw new DynamicError("Serialization parameter " + Err.wrap(key) + " must have the value yes or no"); 227 } 228 } 229 230 private static void checkMethod(String value) throws DynamicError { 231 if ("xml".equals(value)) return; 232 if ("html".equals(value)) return; 233 if ("xhtml".equals(value)) return; 234 if ("text".equals(value)) return; 235 if (isValidClarkName(value)) return; 236 throw new DynamicError("Invalid value for serialization method: " + 237 "must be xml, html, xhtml, text, or a QName in '{uri}local' form"); 238 239 } 240 241 private static boolean isValidClarkName(String value) { 242 if (value.charAt(0) != '{') return false; 243 int closer = value.indexOf('}'); 244 if (closer < 2) return false; 245 if (closer == value.length()-1) return false; 246 if (!XMLChar.isValidNCName(value.substring(closer+1))) return false; 247 return true; 248 } 249 250 private static void checkNonNegativeInteger(String key, String value) throws DynamicError { 251 try { 252 int n = Integer.parseInt(value); 253 if (n < 0) { 254 throw new DynamicError("Value of " + Err.wrap(key) + " must be a non-negative integer"); 255 } 256 } catch (NumberFormatException err) { 257 throw new DynamicError("Value of " + Err.wrap(key) + " must be a non-negative integer"); 258 } 259 } 260 261 private static void checkListOfClarkNames(String key, String value) throws DynamicError { 262 StringTokenizer tok = new StringTokenizer (value); 263 while (tok.hasMoreTokens()) { 264 String s = tok.nextToken(); 265 if (isValidClarkName(s) || XMLChar.isValidNCName(s)) { 266 } else { 268 throw new DynamicError("Value of " + Err.wrap(key) + 269 " must be a list of QNames in '{uri}local' notation"); 270 } 271 } 272 } 273 274 } 275 276 | Popular Tags |