You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RtfAttributes.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. /*
  52. * This file is part of the RTF library of the FOP project, which was originally
  53. * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
  54. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  55. * the FOP project.
  56. */
  57. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  58. import java.util.HashMap;
  59. import java.util.Iterator;
  60. import org.xml.sax.Attributes;
  61. import org.xml.sax.helpers.AttributesImpl;
  62. /** Attributes for RtfText
  63. * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
  64. */
  65. public class RtfAttributes
  66. implements java.lang.Cloneable {
  67. private HashMap values = new HashMap();
  68. /**
  69. * Set attributes from another attributes object
  70. * @param attrs RtfAttributes object whose elements will be copied into this
  71. * instance
  72. * @return this object, for chaining calls
  73. */
  74. public RtfAttributes set (RtfAttributes attrs) {
  75. if (attrs != null) {
  76. Iterator it = attrs.nameIterator ();
  77. while (it.hasNext ()) {
  78. String name = (String) it.next ();
  79. if (attrs.getValue(name) instanceof Integer) {
  80. Integer value = (Integer)attrs.getValue (name);
  81. if (value == null) {
  82. set (name);
  83. } else {
  84. set (name, value.intValue ());
  85. }
  86. } else if (attrs.getValue(name) instanceof String) {
  87. String value = (String)attrs.getValue (name);
  88. if (value == null) {
  89. set (name);
  90. } else {
  91. set (name, value);
  92. }
  93. } else {
  94. set (name);
  95. }
  96. }
  97. // indicate the XSL attributes used to build the Rtf attributes
  98. setXslAttributes(attrs.getXslAttributes());
  99. }
  100. return this;
  101. }
  102. /**
  103. * set an attribute that has no value.
  104. * @param name name of attribute to set
  105. * @return this object, for chaining calls
  106. */
  107. public RtfAttributes set(String name) {
  108. values.put(name, null);
  109. return this;
  110. }
  111. /**
  112. * unset an attribute that has no value
  113. * @param name name of attribute to unset
  114. * @return this object, for chaining calls
  115. */
  116. public RtfAttributes unset(String name) {
  117. values.remove(name);
  118. return this;
  119. }
  120. /**
  121. * debugging log
  122. * @return String representation of object
  123. */
  124. public String toString() {
  125. return values.toString() + "(" + super.toString() + ")";
  126. }
  127. /**
  128. * implement cloning
  129. * @return cloned Object
  130. */
  131. public Object clone() {
  132. final RtfAttributes result = new RtfAttributes();
  133. result.values = (HashMap)values.clone();
  134. // Added by Normand Masse
  135. // indicate the XSL attributes used to build the Rtf attributes
  136. if (xslAttributes != null) {
  137. result.xslAttributes = new org.xml.sax.helpers.AttributesImpl(xslAttributes);
  138. }
  139. return result;
  140. }
  141. /**
  142. * Set an attribute that has an integer value
  143. * @param name name of attribute
  144. * @param value value of attribute
  145. * @return this (which now contains the new entry), for chaining calls
  146. */
  147. public RtfAttributes set(String name, int value) {
  148. values.put(name, new Integer(value));
  149. return this;
  150. }
  151. /**
  152. * Set an attribute that has a String value
  153. * @param name name of attribute
  154. * @param type value of attribute
  155. * @return this (which now contains the new entry)
  156. */
  157. public RtfAttributes set(String name, String type) {
  158. values.put(name, type);
  159. return this;
  160. }
  161. /**
  162. * @param name String containing attribute name
  163. * @return the value of an attribute, null if not found
  164. */
  165. public Object getValue(String name) {
  166. return values.get(name);
  167. }
  168. /**
  169. * @param name String containing attribute name
  170. * @return true if given attribute is set
  171. */
  172. public boolean isSet(String name) {
  173. return values.containsKey(name);
  174. }
  175. /** @return an Iterator on all names that are set */
  176. public Iterator nameIterator() {
  177. return values.keySet().iterator();
  178. }
  179. private Attributes xslAttributes = null;
  180. /**
  181. * Added by Normand Masse
  182. * Used for attribute inheritance
  183. * @return Attributes
  184. */
  185. public Attributes getXslAttributes() {
  186. return xslAttributes;
  187. }
  188. /**
  189. * Added by Normand Masse
  190. * Used for attribute inheritance
  191. * @param pAttribs attributes
  192. */
  193. public void setXslAttributes(Attributes pAttribs) {
  194. if (pAttribs == null) {
  195. return;
  196. }
  197. // copy/replace the xsl attributes into the current attributes
  198. if (xslAttributes != null) {
  199. for (int i = 0; i < pAttribs.getLength(); i++) {
  200. String wKey = pAttribs.getQName(i);
  201. int wPos = xslAttributes.getIndex(wKey);
  202. if (wPos == -1) {
  203. ((AttributesImpl)xslAttributes).addAttribute(pAttribs.getURI(i),
  204. pAttribs.getLocalName(i), pAttribs.getQName(i),
  205. pAttribs.getType(i), pAttribs.getValue(i));
  206. } else {
  207. ((AttributesImpl)xslAttributes).setAttribute(wPos, pAttribs.getURI(i),
  208. pAttribs.getLocalName(i), pAttribs.getQName(i),
  209. pAttribs.getType(i), pAttribs.getValue(i));
  210. }
  211. }
  212. } else {
  213. xslAttributes = new org.xml.sax.helpers.AttributesImpl(pAttribs);
  214. }
  215. }
  216. }