1 16 19 package org.apache.xalan.templates; 20 21 import java.util.Enumeration ; 22 import java.util.Properties ; 23 import java.util.Vector ; 24 25 import javax.xml.transform.OutputKeys ; 26 import javax.xml.transform.TransformerException ; 27 28 import org.apache.xalan.res.XSLMessages; 29 import org.apache.xalan.res.XSLTErrorResources; 30 import org.apache.xml.serializer.OutputPropertiesFactory; 31 import org.apache.xml.serializer.OutputPropertyUtils; 32 import org.apache.xml.utils.FastStringBuffer; 33 import org.apache.xml.utils.QName; 34 35 47 public class OutputProperties extends ElemTemplateElement 48 implements Cloneable 49 { 50 51 54 public OutputProperties() 55 { 56 this(org.apache.xml.serializer.Method.XML); 57 } 58 59 64 public OutputProperties(Properties defaults) 65 { 66 m_properties = new Properties (defaults); 67 } 68 69 79 public OutputProperties(String method) 80 { 81 m_properties = new Properties ( 82 OutputPropertiesFactory.getDefaultMethodProperties(method)); 83 } 84 85 92 public Object clone() 93 { 94 95 try 96 { 97 OutputProperties cloned = (OutputProperties) super.clone(); 98 99 cloned.m_properties = (Properties ) cloned.m_properties.clone(); 100 101 return cloned; 102 } 103 catch (CloneNotSupportedException e) 104 { 105 return null; 106 } 107 } 108 109 116 public void setProperty(QName key, String value) 117 { 118 setProperty(key.toNamespacedString(), value); 119 } 120 121 128 public void setProperty(String key, String value) 129 { 130 if(key.equals(OutputKeys.METHOD)) 131 { 132 setMethodDefaults(value); 133 } 134 135 if (key.startsWith(OutputPropertiesFactory.S_BUILTIN_OLD_EXTENSIONS_UNIVERSAL)) 136 key = OutputPropertiesFactory.S_BUILTIN_EXTENSIONS_UNIVERSAL 137 + key.substring(OutputPropertiesFactory.S_BUILTIN_OLD_EXTENSIONS_UNIVERSAL_LEN); 138 139 m_properties.put(key, value); 140 } 141 142 151 public String getProperty(QName key) 152 { 153 return m_properties.getProperty(key.toNamespacedString()); 154 } 155 156 165 public String getProperty(String key) 166 { 167 if (key.startsWith(OutputPropertiesFactory.S_BUILTIN_OLD_EXTENSIONS_UNIVERSAL)) 168 key = OutputPropertiesFactory.S_BUILTIN_EXTENSIONS_UNIVERSAL 169 + key.substring(OutputPropertiesFactory.S_BUILTIN_OLD_EXTENSIONS_UNIVERSAL_LEN); 170 return m_properties.getProperty(key); 171 } 172 173 180 public void setBooleanProperty(QName key, boolean value) 181 { 182 m_properties.put(key.toNamespacedString(), value ? "yes" : "no"); 183 } 184 185 192 public void setBooleanProperty(String key, boolean value) 193 { 194 m_properties.put(key, value ? "yes" : "no"); 195 } 196 197 208 public boolean getBooleanProperty(QName key) 209 { 210 return getBooleanProperty(key.toNamespacedString()); 211 } 212 213 224 public boolean getBooleanProperty(String key) 225 { 226 return OutputPropertyUtils.getBooleanProperty(key, m_properties); 227 } 228 229 236 public void setIntProperty(QName key, int value) 237 { 238 setIntProperty(key.toNamespacedString(), value); 239 } 240 241 248 public void setIntProperty(String key, int value) 249 { 250 m_properties.put(key, Integer.toString(value)); 251 } 252 253 264 public int getIntProperty(QName key) 265 { 266 return getIntProperty(key.toNamespacedString()); 267 } 268 269 280 public int getIntProperty(String key) 281 { 282 return OutputPropertyUtils.getIntProperty(key, m_properties); 283 } 284 285 286 294 public void setQNameProperty(QName key, QName value) 295 { 296 setQNameProperty(key.toNamespacedString(), value); 297 } 298 299 305 public void setMethodDefaults(String method) 306 { 307 String defaultMethod = m_properties.getProperty(OutputKeys.METHOD); 308 309 if((null == defaultMethod) || !defaultMethod.equals(method) 310 || defaultMethod.equals("xml") 318 ) 319 { 320 Properties savedProps = m_properties; 321 Properties newDefaults = 322 OutputPropertiesFactory.getDefaultMethodProperties(method); 323 m_properties = new Properties (newDefaults); 324 copyFrom(savedProps, false); 325 } 326 } 327 328 329 337 public void setQNameProperty(String key, QName value) 338 { 339 setProperty(key, value.toNamespacedString()); 340 } 341 342 352 public QName getQNameProperty(QName key) 353 { 354 return getQNameProperty(key.toNamespacedString()); 355 } 356 357 367 public QName getQNameProperty(String key) 368 { 369 return getQNameProperty(key, m_properties); 370 } 371 372 383 public static QName getQNameProperty(String key, Properties props) 384 { 385 386 String s = props.getProperty(key); 387 388 if (null != s) 389 return QName.getQNameFromString(s); 390 else 391 return null; 392 } 393 394 402 public void setQNameProperties(QName key, Vector v) 403 { 404 setQNameProperties(key.toNamespacedString(), v); 405 } 406 407 415 public void setQNameProperties(String key, Vector v) 416 { 417 418 int s = v.size(); 419 420 FastStringBuffer fsb = new FastStringBuffer(9,9); 422 423 for (int i = 0; i < s; i++) 424 { 425 QName qname = (QName) v.elementAt(i); 426 427 fsb.append(qname.toNamespacedString()); 428 if (i < s-1) 430 fsb.append(' '); 431 } 432 433 m_properties.put(key, fsb.toString()); 434 } 435 436 447 public Vector getQNameProperties(QName key) 448 { 449 return getQNameProperties(key.toNamespacedString()); 450 } 451 452 463 public Vector getQNameProperties(String key) 464 { 465 return getQNameProperties(key, m_properties); 466 } 467 468 480 public static Vector getQNameProperties(String key, Properties props) 481 { 482 483 String s = props.getProperty(key); 484 485 if (null != s) 486 { 487 Vector v = new Vector (); 488 int l = s.length(); 489 boolean inCurly = false; 490 FastStringBuffer buf = new FastStringBuffer(); 491 492 for (int i = 0; i < l; i++) 496 { 497 char c = s.charAt(i); 498 499 if (Character.isWhitespace(c)) 500 { 501 if (!inCurly) 502 { 503 if (buf.length() > 0) 504 { 505 QName qname = QName.getQNameFromString(buf.toString()); 506 v.addElement(qname); 507 buf.reset(); 508 } 509 continue; 510 } 511 } 512 else if ('{' == c) 513 inCurly = true; 514 else if ('}' == c) 515 inCurly = false; 516 517 buf.append(c); 518 } 519 520 if (buf.length() > 0) 521 { 522 QName qname = QName.getQNameFromString(buf.toString()); 523 v.addElement(qname); 524 buf.reset(); 525 } 526 527 return v; 528 } 529 else 530 return null; 531 } 532 533 538 public void recompose(StylesheetRoot root) 539 throws TransformerException 540 { 541 root.recomposeOutput(this); 542 } 543 544 550 public void compose(StylesheetRoot sroot) throws TransformerException 551 { 552 553 super.compose(sroot); 554 555 } 556 557 562 public Properties getProperties() 563 { 564 return m_properties; 565 } 566 567 575 public void copyFrom(Properties src) 576 { 577 copyFrom(src, true); 578 } 579 580 590 public void copyFrom(Properties src, boolean shouldResetDefaults) 591 { 592 593 Enumeration keys = src.keys(); 594 595 while (keys.hasMoreElements()) 596 { 597 String key = (String ) keys.nextElement(); 598 599 if (!isLegalPropertyKey(key)) 600 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_OUTPUT_PROPERTY_NOT_RECOGNIZED, new Object []{key})); 602 Object oldValue = m_properties.get(key); 603 if (null == oldValue) 604 { 605 String val = (String ) src.get(key); 606 607 if(shouldResetDefaults && key.equals(OutputKeys.METHOD)) 608 { 609 setMethodDefaults(val); 610 } 611 612 m_properties.put(key, val); 613 } 614 else if (key.equals(OutputKeys.CDATA_SECTION_ELEMENTS)) 615 { 616 m_properties.put(key, (String ) oldValue + " " + (String ) src.get(key)); 617 } 618 } 619 } 620 621 629 public void copyFrom(OutputProperties opsrc) 630 throws TransformerException 631 { 632 copyFrom(opsrc.getProperties()); 635 } 636 637 644 public static boolean isLegalPropertyKey(String key) 645 { 646 647 return (key.equals(OutputKeys.CDATA_SECTION_ELEMENTS) 648 || key.equals(OutputKeys.DOCTYPE_PUBLIC) 649 || key.equals(OutputKeys.DOCTYPE_SYSTEM) 650 || key.equals(OutputKeys.ENCODING) 651 || key.equals(OutputKeys.INDENT) 652 || key.equals(OutputKeys.MEDIA_TYPE) 653 || key.equals(OutputKeys.METHOD) 654 || key.equals(OutputKeys.OMIT_XML_DECLARATION) 655 || key.equals(OutputKeys.STANDALONE) 656 || key.equals(OutputKeys.VERSION) 657 || (key.length() > 0) 658 && (key.charAt(0) == '{') 659 && (key.lastIndexOf('{') == 0) 660 && (key.indexOf('}') > 0) 661 && (key.lastIndexOf('}') == key.indexOf('}'))); 662 } 663 664 666 private Properties m_properties = null; 667 668 683 static public Properties getDefaultMethodProperties(String method) 684 { 685 return org.apache.xml.serializer.OutputPropertiesFactory.getDefaultMethodProperties(method); 686 } 687 } 688 | Popular Tags |