1 16 19 package com.sun.org.apache.xml.internal.serializer; 20 21 import java.io.BufferedInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 import java.util.Enumeration ; 27 import java.util.Properties ; 28 import javax.xml.transform.OutputKeys ; 29 30 import com.sun.org.apache.xml.internal.res.XMLErrorResources; 31 import com.sun.org.apache.xml.internal.res.XMLMessages; 32 import com.sun.org.apache.xml.internal.utils.Constants; 33 import com.sun.org.apache.xml.internal.utils.WrappedRuntimeException; 34 35 39 public class OutputPropertiesFactory 40 { 41 48 public static final String S_BUILTIN_EXTENSIONS_UNIVERSAL = 49 "{" + Constants.S_BUILTIN_EXTENSIONS_URL + "}"; 50 51 53 54 public static final String S_KEY_INDENT_AMOUNT = 55 S_BUILTIN_EXTENSIONS_UNIVERSAL + "indent-amount"; 56 57 62 public static final String S_KEY_CONTENT_HANDLER = 63 S_BUILTIN_EXTENSIONS_UNIVERSAL + "content-handler"; 64 65 66 public static final String S_KEY_ENTITIES = 67 S_BUILTIN_EXTENSIONS_UNIVERSAL + "entities"; 68 69 71 public static final String S_USE_URL_ESCAPING = 72 S_BUILTIN_EXTENSIONS_UNIVERSAL + "use-url-escaping"; 73 74 77 public static final String S_OMIT_META_TAG = 78 S_BUILTIN_EXTENSIONS_UNIVERSAL + "omit-meta-tag"; 79 80 83 public static final String S_BUILTIN_OLD_EXTENSIONS_UNIVERSAL = 84 "{" + Constants.S_BUILTIN_OLD_EXTENSIONS_URL + "}"; 85 86 89 public static final int S_BUILTIN_OLD_EXTENSIONS_UNIVERSAL_LEN = 90 S_BUILTIN_OLD_EXTENSIONS_UNIVERSAL.length(); 91 92 96 private static final String S_XSLT_PREFIX = "xslt.output."; 97 private static final int S_XSLT_PREFIX_LEN = S_XSLT_PREFIX.length(); 98 private static final String S_XALAN_PREFIX = "org.apache.xslt."; 99 private static final int S_XALAN_PREFIX_LEN = S_XALAN_PREFIX.length(); 100 101 102 private static Integer m_synch_object = new Integer (1); 103 104 105 private static final String PROP_DIR = "com/sun/org/apache/xml/internal/serializer/"; 106 107 private static final String PROP_FILE_XML = "output_xml.properties"; 108 109 private static final String PROP_FILE_TEXT = "output_text.properties"; 110 111 private static final String PROP_FILE_HTML = "output_html.properties"; 112 113 private static final String PROP_FILE_UNKNOWN = "output_unknown.properties"; 114 115 119 120 private static Properties m_xml_properties = null; 121 122 123 private static Properties m_html_properties = null; 124 125 126 private static Properties m_text_properties = null; 127 128 129 private static Properties m_unknown_properties = null; 130 131 private static final Class 132 ACCESS_CONTROLLER_CLASS = findAccessControllerClass(); 133 134 private static Class findAccessControllerClass() { 135 try 136 { 137 144 return Class.forName("java.security.AccessController"); 145 } 146 catch (Exception e) 147 { 148 } 151 152 return null; 153 } 154 155 167 static public Properties getDefaultMethodProperties(String method) 168 { 169 String fileName = null; 170 Properties defaultProperties = null; 171 try 174 { 175 synchronized (m_synch_object) 176 { 177 if (null == m_xml_properties) { 179 fileName = PROP_FILE_XML; 180 m_xml_properties = loadPropertiesFile(fileName, null); 181 } 182 } 183 184 if (method.equals(Method.XML)) 185 { 186 defaultProperties = m_xml_properties; 187 } 188 else if (method.equals(Method.HTML)) 189 { 190 if (null == m_html_properties) { 192 fileName = PROP_FILE_HTML; 193 m_html_properties = 194 loadPropertiesFile(fileName, m_xml_properties); 195 } 196 197 defaultProperties = m_html_properties; 198 } 199 else if (method.equals(Method.TEXT)) 200 { 201 if (null == m_text_properties) { 203 fileName = PROP_FILE_TEXT; 204 m_text_properties = 205 loadPropertiesFile(fileName, m_xml_properties); 206 if (null 207 == m_text_properties.getProperty(OutputKeys.ENCODING)) 208 { 209 String mimeEncoding = Encodings.getMimeEncoding(null); 210 m_text_properties.put( 211 OutputKeys.ENCODING, 212 mimeEncoding); 213 } 214 } 215 216 defaultProperties = m_text_properties; 217 } 218 else if (method.equals(com.sun.org.apache.xml.internal.serializer.Method.UNKNOWN)) 219 { 220 if (null == m_unknown_properties) { 222 fileName = PROP_FILE_UNKNOWN; 223 m_unknown_properties = 224 loadPropertiesFile(fileName, m_xml_properties); 225 } 226 227 defaultProperties = m_unknown_properties; 228 } 229 else 230 { 231 defaultProperties = m_xml_properties; 233 } 234 } 235 catch (IOException ioe) 236 { 237 throw new WrappedRuntimeException( 238 XMLMessages.createXMLMessage( 239 XMLErrorResources.ER_COULD_NOT_LOAD_METHOD_PROPERTY, 240 new Object [] { fileName, method }), 241 ioe); 242 } 243 244 return new Properties (defaultProperties); 245 } 246 247 259 static private Properties loadPropertiesFile( 260 final String resourceName, 261 Properties defaults) 262 throws IOException 263 { 264 265 269 Properties props = new Properties (defaults); 270 271 InputStream is = null; 272 BufferedInputStream bis = null; 273 274 try 275 { 276 if (ACCESS_CONTROLLER_CLASS != null) 277 { 278 is = (InputStream ) AccessController 279 .doPrivileged(new PrivilegedAction () { 280 public Object run() 281 { 282 return OutputPropertiesFactory.class 283 .getResourceAsStream(resourceName); 284 } 285 }); 286 } 287 else 288 { 289 is = OutputPropertiesFactory.class 291 .getResourceAsStream(resourceName); 292 } 293 294 bis = new BufferedInputStream (is); 295 props.load(bis); 296 } 297 catch (IOException ioe) 298 { 299 if (defaults == null) 300 { 301 throw ioe; 302 } 303 else 304 { 305 throw new WrappedRuntimeException( 306 XMLMessages.createXMLMessage( 307 XMLErrorResources.ER_COULD_NOT_LOAD_RESOURCE, 308 new Object [] { resourceName }), 309 ioe); 310 } 312 } 313 catch (SecurityException se) 314 { 315 if (defaults == null) 317 { 318 throw se; 319 } 320 else 321 { 322 throw new WrappedRuntimeException( 323 XMLMessages.createXMLMessage( 324 XMLErrorResources.ER_COULD_NOT_LOAD_RESOURCE, 325 new Object [] { resourceName }), 326 se); 327 } 329 } 330 finally 331 { 332 if (bis != null) 333 { 334 bis.close(); 335 } 336 if (is != null) 337 { 338 is.close(); 339 } 340 } 341 342 356 Enumeration keys = ((Properties ) props.clone()).keys(); 357 while (keys.hasMoreElements()) 358 { 359 String key = (String ) keys.nextElement(); 360 String value = null; 364 try 365 { 366 value = System.getProperty(key); 367 } 368 catch (SecurityException se) 369 { 370 } 372 if (value == null) 373 value = (String ) props.get(key); 374 375 String newKey = fixupPropertyString(key, true); 376 String newValue = null; 377 try 378 { 379 newValue = System.getProperty(newKey); 380 } 381 catch (SecurityException se) 382 { 383 } 385 if (newValue == null) 386 newValue = fixupPropertyString(value, false); 387 else 388 newValue = fixupPropertyString(newValue, false); 389 390 if (key != newKey || value != newValue) 391 { 392 props.remove(key); 393 props.put(newKey, newValue); 394 } 395 396 } 397 398 return props; 399 } 400 401 408 static private String fixupPropertyString(String s, boolean doClipping) 409 { 410 int index; 411 if (doClipping && s.startsWith(S_XSLT_PREFIX)) 412 { 413 s = s.substring(S_XSLT_PREFIX_LEN); 414 } 415 if (s.startsWith(S_XALAN_PREFIX)) 416 { 417 s = 418 S_BUILTIN_EXTENSIONS_UNIVERSAL 419 + s.substring(S_XALAN_PREFIX_LEN); 420 } 421 if ((index = s.indexOf("\\u003a")) > 0) 422 { 423 String temp = s.substring(index + 6); 424 s = s.substring(0, index) + ":" + temp; 425 426 } 427 return s; 428 } 429 430 } 431 | Popular Tags |