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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  19. /*
  20. * This file is part of the RTF library of the FOP project, which was originally
  21. * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
  22. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  23. * the FOP project.
  24. */
  25. import java.util.HashMap;
  26. import java.util.Iterator;
  27. import org.xml.sax.Attributes;
  28. import org.xml.sax.helpers.AttributesImpl;
  29. /**
  30. * <p>Attributes for RtfText.</p>
  31. *
  32. * <p>This work was authored by Bertrand Delacretaz (bdelacretaz@codeconsult.ch).</p>
  33. */
  34. public class RtfAttributes implements Cloneable {
  35. private HashMap values = new HashMap();
  36. /**
  37. * Set attributes from another attributes object
  38. * @param attrs RtfAttributes object whose elements will be copied into this
  39. * instance
  40. * @return this object, for chaining calls
  41. */
  42. public RtfAttributes set(RtfAttributes attrs) {
  43. if (attrs != null) {
  44. Iterator it = attrs.nameIterator();
  45. while (it.hasNext()) {
  46. String name = (String) it.next();
  47. if (attrs.getValue(name) instanceof Integer) {
  48. Integer value = (Integer)attrs.getValue(name);
  49. if (value == null) {
  50. set(name);
  51. } else {
  52. set(name, value);
  53. }
  54. } else if (attrs.getValue(name) instanceof String) {
  55. String value = (String)attrs.getValue(name);
  56. if (value == null) {
  57. set(name);
  58. } else {
  59. set(name, value);
  60. }
  61. } else {
  62. set(name);
  63. }
  64. }
  65. // indicate the XSL attributes used to build the Rtf attributes
  66. setXslAttributes(attrs.getXslAttributes());
  67. }
  68. return this;
  69. }
  70. /**
  71. * set an attribute that has no value.
  72. * @param name name of attribute to set
  73. * @return this object, for chaining calls
  74. */
  75. public RtfAttributes set(String name) {
  76. values.put(name, null);
  77. return this;
  78. }
  79. /**
  80. * unset an attribute that has no value
  81. * @param name name of attribute to unset
  82. * @return this object, for chaining calls
  83. */
  84. public RtfAttributes unset(String name) {
  85. values.remove(name);
  86. return this;
  87. }
  88. /**
  89. * debugging log
  90. * @return String representation of object
  91. */
  92. public String toString() {
  93. return values.toString() + "(" + super.toString() + ")";
  94. }
  95. /** {@inheritDoc} */
  96. public Object clone() throws CloneNotSupportedException {
  97. RtfAttributes result = (RtfAttributes) super.clone();
  98. result.values = (HashMap)values.clone();
  99. // Added by Normand Masse
  100. // indicate the XSL attributes used to build the Rtf attributes
  101. if (xslAttributes != null) {
  102. result.xslAttributes = new org.xml.sax.helpers.AttributesImpl(xslAttributes);
  103. }
  104. return result;
  105. }
  106. /**
  107. * Set an attribute that has an integer value
  108. * @param name name of attribute
  109. * @param value value of attribute
  110. * @return this (which now contains the new entry), for chaining calls
  111. */
  112. public RtfAttributes set(String name, int value) {
  113. values.put(name, value);
  114. return this;
  115. }
  116. /**
  117. * Set an attribute that has a String value
  118. * @param name name of attribute
  119. * @param type value of attribute
  120. * @return this (which now contains the new entry)
  121. */
  122. public RtfAttributes set(String name, String type) {
  123. values.put(name, type);
  124. return this;
  125. }
  126. /**
  127. * Set an attribute that has nested attributes as value
  128. * @param name name of attribute
  129. * @param value value of the nested attributes
  130. * @return this (which now contains the new entry)
  131. */
  132. public RtfAttributes set(String name, RtfAttributes value) {
  133. values.put(name, value);
  134. return this;
  135. }
  136. /**
  137. * @param name String containing attribute name
  138. * @return the value of an attribute, null if not found
  139. */
  140. public Object getValue(String name) {
  141. return values.get(name);
  142. }
  143. /**
  144. * Returns a value as an Integer. The value is simply cast to an Integer.
  145. * @param name String containing attribute name
  146. * @return the value of an attribute, null if not found
  147. */
  148. public Integer getValueAsInteger(String name) {
  149. return (Integer)values.get(name);
  150. }
  151. /**
  152. * @param name String containing attribute name
  153. * @return true if given attribute is set
  154. */
  155. public boolean isSet(String name) {
  156. return values.containsKey(name);
  157. }
  158. /** @return an Iterator on all names that are set */
  159. public Iterator nameIterator() {
  160. return values.keySet().iterator();
  161. }
  162. private Attributes xslAttributes;
  163. /**
  164. * Added by Normand Masse
  165. * Used for attribute inheritance
  166. * @return Attributes
  167. */
  168. public Attributes getXslAttributes() {
  169. return xslAttributes;
  170. }
  171. /**
  172. * Added by Normand Masse
  173. * Used for attribute inheritance
  174. * @param pAttribs attributes
  175. */
  176. public void setXslAttributes(Attributes pAttribs) {
  177. if (pAttribs == null) {
  178. return;
  179. }
  180. // copy/replace the xsl attributes into the current attributes
  181. if (xslAttributes != null) {
  182. for (int i = 0; i < pAttribs.getLength(); i++) {
  183. String wKey = pAttribs.getQName(i);
  184. int wPos = xslAttributes.getIndex(wKey);
  185. if (wPos == -1) {
  186. ((AttributesImpl)xslAttributes).addAttribute(pAttribs.getURI(i),
  187. pAttribs.getLocalName(i), pAttribs.getQName(i),
  188. pAttribs.getType(i), pAttribs.getValue(i));
  189. } else {
  190. ((AttributesImpl)xslAttributes).setAttribute(wPos, pAttribs.getURI(i),
  191. pAttribs.getLocalName(i), pAttribs.getQName(i),
  192. pAttribs.getType(i), pAttribs.getValue(i));
  193. }
  194. }
  195. } else {
  196. xslAttributes = new org.xml.sax.helpers.AttributesImpl(pAttribs);
  197. }
  198. }
  199. /**
  200. * Add integer value <code>addValue</code> to attribute with name <code>name</code>.
  201. * If there is no such setted attribute, then value of this attribure is equal to
  202. * <code>addValue</code>.
  203. * @param addValue the increment of value
  204. * @param name the name of attribute
  205. */
  206. public void addIntegerValue(int addValue, String name) {
  207. Integer value = (Integer) getValue(name);
  208. int v = (value != null) ? value : 0;
  209. set(name, v + addValue);
  210. }
  211. }