001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: RtfAttributes.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.render.rtf.rtflib.rtfdoc;
021:
022: /*
023: * This file is part of the RTF library of the FOP project, which was originally
024: * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
025: * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
026: * the FOP project.
027: */
028:
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import org.xml.sax.Attributes;
032: import org.xml.sax.helpers.AttributesImpl;
033:
034: /** Attributes for RtfText
035: * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
036: */
037:
038: public class RtfAttributes implements java.lang.Cloneable {
039: private HashMap values = new HashMap();
040:
041: /**
042: * Set attributes from another attributes object
043: * @param attrs RtfAttributes object whose elements will be copied into this
044: * instance
045: * @return this object, for chaining calls
046: */
047: public RtfAttributes set(RtfAttributes attrs) {
048: if (attrs != null) {
049: Iterator it = attrs.nameIterator();
050: while (it.hasNext()) {
051: String name = (String) it.next();
052: if (attrs.getValue(name) instanceof Integer) {
053: Integer value = (Integer) attrs.getValue(name);
054: if (value == null) {
055: set(name);
056: } else {
057: set(name, value.intValue());
058: }
059: } else if (attrs.getValue(name) instanceof String) {
060: String value = (String) attrs.getValue(name);
061: if (value == null) {
062: set(name);
063: } else {
064: set(name, value);
065: }
066: } else {
067: set(name);
068: }
069:
070: }
071: // indicate the XSL attributes used to build the Rtf attributes
072: setXslAttributes(attrs.getXslAttributes());
073: }
074: return this ;
075: }
076:
077: /**
078: * set an attribute that has no value.
079: * @param name name of attribute to set
080: * @return this object, for chaining calls
081: */
082: public RtfAttributes set(String name) {
083: values.put(name, null);
084: return this ;
085: }
086:
087: /**
088: * unset an attribute that has no value
089: * @param name name of attribute to unset
090: * @return this object, for chaining calls
091: */
092: public RtfAttributes unset(String name) {
093: values.remove(name);
094: return this ;
095: }
096:
097: /**
098: * debugging log
099: * @return String representation of object
100: */
101: public String toString() {
102: return values.toString() + "(" + super .toString() + ")";
103: }
104:
105: /**
106: * implement cloning
107: * @return cloned Object
108: */
109: public Object clone() {
110: final RtfAttributes result = new RtfAttributes();
111: result.values = (HashMap) values.clone();
112:
113: // Added by Normand Masse
114: // indicate the XSL attributes used to build the Rtf attributes
115: if (xslAttributes != null) {
116: result.xslAttributes = new org.xml.sax.helpers.AttributesImpl(
117: xslAttributes);
118: }
119:
120: return result;
121: }
122:
123: /**
124: * Set an attribute that has an integer value
125: * @param name name of attribute
126: * @param value value of attribute
127: * @return this (which now contains the new entry), for chaining calls
128: */
129: public RtfAttributes set(String name, int value) {
130: values.put(name, new Integer(value));
131: return this ;
132: }
133:
134: /**
135: * Set an attribute that has a String value
136: * @param name name of attribute
137: * @param type value of attribute
138: * @return this (which now contains the new entry)
139: */
140: public RtfAttributes set(String name, String type) {
141: values.put(name, type);
142: return this ;
143: }
144:
145: /**
146: * Set an attribute that has nested attributes as value
147: * @param name name of attribute
148: * @param value value of the nested attributes
149: * @return this (which now contains the new entry)
150: */
151: public RtfAttributes set(String name, RtfAttributes value) {
152: values.put(name, value);
153: return this ;
154: }
155:
156: /**
157: * @param name String containing attribute name
158: * @return the value of an attribute, null if not found
159: */
160: public Object getValue(String name) {
161: return values.get(name);
162: }
163:
164: /**
165: * @param name String containing attribute name
166: * @return true if given attribute is set
167: */
168: public boolean isSet(String name) {
169: return values.containsKey(name);
170: }
171:
172: /** @return an Iterator on all names that are set */
173: public Iterator nameIterator() {
174: return values.keySet().iterator();
175: }
176:
177: private Attributes xslAttributes = null;
178:
179: /**
180: * Added by Normand Masse
181: * Used for attribute inheritance
182: * @return Attributes
183: */
184: public Attributes getXslAttributes() {
185: return xslAttributes;
186: }
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(
204: pAttribs.getURI(i), pAttribs
205: .getLocalName(i), pAttribs
206: .getQName(i), pAttribs.getType(i),
207: pAttribs.getValue(i));
208: } else {
209: ((AttributesImpl) xslAttributes).setAttribute(wPos,
210: pAttribs.getURI(i), pAttribs
211: .getLocalName(i), pAttribs
212: .getQName(i), pAttribs.getType(i),
213: pAttribs.getValue(i));
214: }
215: }
216: } else {
217: xslAttributes = new org.xml.sax.helpers.AttributesImpl(
218: pAttribs);
219: }
220: }
221:
222: /**
223: * Add integer value <code>addValue</code> to attribute with name <code>name</code>.
224: * If there is no such setted attribute, then value of this attribure is equal to
225: * <code>addValue</code>.
226: * @param addValue the increment of value
227: * @param name the name of attribute
228: */
229: public void addIntegerValue(int addValue, String name) {
230: Integer value = (Integer) getValue(name);
231: int v = (value != null) ? value.intValue() : 0;
232: set(name, v + addValue);
233: }
234: }
|