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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /** Attributes for RtfText
  30. * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
  31. */
  32. public class RtfAttributes
  33. implements java.lang.Cloneable {
  34. private HashMap values = new HashMap();
  35. /**
  36. * Set attributes from another attributes object
  37. * @param attrs RtfAttributes object whose elements will be copied into this
  38. * instance
  39. * @return this object, for chaining calls
  40. */
  41. public RtfAttributes set (RtfAttributes attrs) {
  42. if (attrs != null) {
  43. Iterator it = attrs.nameIterator ();
  44. while (it.hasNext ()) {
  45. String name = (String) it.next ();
  46. if (attrs.getValue(name) instanceof Integer) {
  47. Integer value = (Integer)attrs.getValue (name);
  48. if (value == null) {
  49. set (name);
  50. } else {
  51. set (name, value.intValue ());
  52. }
  53. } else if (attrs.getValue(name) instanceof String) {
  54. String value = (String)attrs.getValue (name);
  55. if (value == null) {
  56. set (name);
  57. } else {
  58. set (name, value);
  59. }
  60. } else {
  61. set (name);
  62. }
  63. }
  64. // indicate the XSL attributes used to build the Rtf attributes
  65. setXslAttributes(attrs.getXslAttributes());
  66. }
  67. return this;
  68. }
  69. /**
  70. * set an attribute that has no value.
  71. * @param name name of attribute to set
  72. * @return this object, for chaining calls
  73. */
  74. public RtfAttributes set(String name) {
  75. values.put(name, null);
  76. return this;
  77. }
  78. /**
  79. * unset an attribute that has no value
  80. * @param name name of attribute to unset
  81. * @return this object, for chaining calls
  82. */
  83. public RtfAttributes unset(String name) {
  84. values.remove(name);
  85. return this;
  86. }
  87. /**
  88. * debugging log
  89. * @return String representation of object
  90. */
  91. public String toString() {
  92. return values.toString() + "(" + super.toString() + ")";
  93. }
  94. /**
  95. * implement cloning
  96. * @return cloned Object
  97. */
  98. public Object clone() {
  99. final RtfAttributes result = new RtfAttributes();
  100. result.values = (HashMap)values.clone();
  101. // Added by Normand Masse
  102. // indicate the XSL attributes used to build the Rtf attributes
  103. if (xslAttributes != null) {
  104. result.xslAttributes = new org.xml.sax.helpers.AttributesImpl(xslAttributes);
  105. }
  106. return result;
  107. }
  108. /**
  109. * Set an attribute that has an integer value
  110. * @param name name of attribute
  111. * @param value value of attribute
  112. * @return this (which now contains the new entry), for chaining calls
  113. */
  114. public RtfAttributes set(String name, int value) {
  115. values.put(name, new Integer(value));
  116. return this;
  117. }
  118. /**
  119. * Set an attribute that has a String value
  120. * @param name name of attribute
  121. * @param type value of attribute
  122. * @return this (which now contains the new entry)
  123. */
  124. public RtfAttributes set(String name, String type) {
  125. values.put(name, type);
  126. return this;
  127. }
  128. /**
  129. * Set an attribute that has nested attributes as value
  130. * @param name name of attribute
  131. * @param value value of the nested attributes
  132. * @return this (which now contains the new entry)
  133. */
  134. public RtfAttributes set(String name, RtfAttributes value) {
  135. values.put(name, value);
  136. return this;
  137. }
  138. /**
  139. * @param name String containing attribute name
  140. * @return the value of an attribute, null if not found
  141. */
  142. public Object getValue(String name) {
  143. return values.get(name);
  144. }
  145. /**
  146. * Returns a value as an Integer. The value is simply cast to an Integer.
  147. * @param name String containing attribute name
  148. * @return the value of an attribute, null if not found
  149. */
  150. public Integer getValueAsInteger(String name) {
  151. return (Integer)values.get(name);
  152. }
  153. /**
  154. * @param name String containing attribute name
  155. * @return true if given attribute is set
  156. */
  157. public boolean isSet(String name) {
  158. return values.containsKey(name);
  159. }
  160. /** @return an Iterator on all names that are set */
  161. public Iterator nameIterator() {
  162. return values.keySet().iterator();
  163. }
  164. private Attributes xslAttributes = null;
  165. /**
  166. * Added by Normand Masse
  167. * Used for attribute inheritance
  168. * @return Attributes
  169. */
  170. public Attributes getXslAttributes() {
  171. return xslAttributes;
  172. }
  173. /**
  174. * Added by Normand Masse
  175. * Used for attribute inheritance
  176. * @param pAttribs attributes
  177. */
  178. public void setXslAttributes(Attributes pAttribs) {
  179. if (pAttribs == null) {
  180. return;
  181. }
  182. // copy/replace the xsl attributes into the current attributes
  183. if (xslAttributes != null) {
  184. for (int i = 0; i < pAttribs.getLength(); i++) {
  185. String wKey = pAttribs.getQName(i);
  186. int wPos = xslAttributes.getIndex(wKey);
  187. if (wPos == -1) {
  188. ((AttributesImpl)xslAttributes).addAttribute(pAttribs.getURI(i),
  189. pAttribs.getLocalName(i), pAttribs.getQName(i),
  190. pAttribs.getType(i), pAttribs.getValue(i));
  191. } else {
  192. ((AttributesImpl)xslAttributes).setAttribute(wPos, pAttribs.getURI(i),
  193. pAttribs.getLocalName(i), pAttribs.getQName(i),
  194. pAttribs.getType(i), pAttribs.getValue(i));
  195. }
  196. }
  197. } else {
  198. xslAttributes = new org.xml.sax.helpers.AttributesImpl(pAttribs);
  199. }
  200. }
  201. /**
  202. * Add integer value <code>addValue</code> to attribute with name <code>name</code>.
  203. * If there is no such setted attribute, then value of this attribure is equal to
  204. * <code>addValue</code>.
  205. * @param addValue the increment of value
  206. * @param name the name of attribute
  207. */
  208. public void addIntegerValue(int addValue, String name) {
  209. Integer value = (Integer) getValue(name);
  210. int v = (value != null) ? value.intValue() : 0;
  211. set(name, v + addValue);
  212. }
  213. }