KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > rtfdoc > RtfAttributes


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
18 /* $Id: RtfAttributes.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.rtfdoc;
21
22 /*
23  * This file is part of the RTF library of the FOP project, which was originally
24  * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
25  * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
26  * the FOP project.
27  */

28
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.helpers.AttributesImpl JavaDoc;
33
34
35 /** Attributes for RtfText
36  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
37  */

38
39 public class RtfAttributes
40 implements java.lang.Cloneable JavaDoc {
41     private HashMap JavaDoc values = new HashMap JavaDoc();
42
43     /**
44      * Set attributes from another attributes object
45      * @param attrs RtfAttributes object whose elements will be copied into this
46      * instance
47      * @return this object, for chaining calls
48      */

49     public RtfAttributes set (RtfAttributes attrs) {
50         if (attrs != null) {
51             Iterator JavaDoc it = attrs.nameIterator ();
52             while (it.hasNext ()) {
53                 String JavaDoc name = (String JavaDoc) it.next ();
54                 if (attrs.getValue(name) instanceof Integer JavaDoc) {
55                     Integer JavaDoc value = (Integer JavaDoc)attrs.getValue (name);
56                     if (value == null) {
57                         set (name);
58                     } else {
59                         set (name, value.intValue ());
60                     }
61                 } else if (attrs.getValue(name) instanceof String JavaDoc) {
62                     String JavaDoc value = (String JavaDoc)attrs.getValue (name);
63                     if (value == null) {
64                         set (name);
65                     } else {
66                         set (name, value);
67                     }
68                 } else {
69                         set (name);
70                 }
71
72
73             }
74             // indicate the XSL attributes used to build the Rtf attributes
75
setXslAttributes(attrs.getXslAttributes());
76         }
77         return this;
78     }
79
80     /**
81      * set an attribute that has no value.
82      * @param name name of attribute to set
83      * @return this object, for chaining calls
84      */

85     public RtfAttributes set(String JavaDoc name) {
86         values.put(name, null);
87         return this;
88     }
89
90     /**
91      * unset an attribute that has no value
92      * @param name name of attribute to unset
93      * @return this object, for chaining calls
94      */

95     public RtfAttributes unset(String JavaDoc name) {
96         values.remove(name);
97         return this;
98     }
99
100     /**
101      * debugging log
102      * @return String representation of object
103      */

104     public String JavaDoc toString() {
105         return values.toString() + "(" + super.toString() + ")";
106     }
107
108     /**
109      * implement cloning
110      * @return cloned Object
111      */

112     public Object JavaDoc clone() {
113         final RtfAttributes result = new RtfAttributes();
114         result.values = (HashMap JavaDoc)values.clone();
115
116         // Added by Normand Masse
117
// indicate the XSL attributes used to build the Rtf attributes
118
if (xslAttributes != null) {
119             result.xslAttributes = new org.xml.sax.helpers.AttributesImpl JavaDoc(xslAttributes);
120         }
121
122         return result;
123     }
124
125     /**
126      * Set an attribute that has an integer value
127      * @param name name of attribute
128      * @param value value of attribute
129      * @return this (which now contains the new entry), for chaining calls
130      */

131     public RtfAttributes set(String JavaDoc name, int value) {
132         values.put(name, new Integer JavaDoc(value));
133         return this;
134     }
135
136     /**
137      * Set an attribute that has a String value
138      * @param name name of attribute
139      * @param type value of attribute
140      * @return this (which now contains the new entry)
141      */

142     public RtfAttributes set(String JavaDoc name, String JavaDoc type) {
143         values.put(name, type);
144         return this;
145     }
146
147     /**
148      * Set an attribute that has nested attributes as value
149      * @param name name of attribute
150      * @param value value of the nested attributes
151      * @return this (which now contains the new entry)
152      */

153     public RtfAttributes set(String JavaDoc name, RtfAttributes value) {
154         values.put(name, value);
155         return this;
156     }
157
158     /**
159      * @param name String containing attribute name
160      * @return the value of an attribute, null if not found
161      */

162     public Object JavaDoc getValue(String JavaDoc name) {
163         return values.get(name);
164     }
165
166     /**
167      * @param name String containing attribute name
168      * @return true if given attribute is set
169      */

170     public boolean isSet(String JavaDoc name) {
171         return values.containsKey(name);
172     }
173
174     /** @return an Iterator on all names that are set */
175     public Iterator JavaDoc nameIterator() {
176         return values.keySet().iterator();
177     }
178
179     private Attributes JavaDoc xslAttributes = null;
180
181     /**
182      * Added by Normand Masse
183      * Used for attribute inheritance
184      * @return Attributes
185      */

186     public Attributes JavaDoc getXslAttributes() {
187         return xslAttributes;
188     }
189
190     /**
191      * Added by Normand Masse
192      * Used for attribute inheritance
193      * @param pAttribs attributes
194      */

195     public void setXslAttributes(Attributes JavaDoc pAttribs) {
196         if (pAttribs == null) {
197             return;
198         }
199         // copy/replace the xsl attributes into the current attributes
200
if (xslAttributes != null) {
201             for (int i = 0; i < pAttribs.getLength(); i++) {
202                 String JavaDoc wKey = pAttribs.getQName(i);
203                 int wPos = xslAttributes.getIndex(wKey);
204                 if (wPos == -1) {
205                     ((AttributesImpl JavaDoc)xslAttributes).addAttribute(pAttribs.getURI(i),
206                             pAttribs.getLocalName(i), pAttribs.getQName(i),
207                             pAttribs.getType(i), pAttribs.getValue(i));
208                 } else {
209                     ((AttributesImpl JavaDoc)xslAttributes).setAttribute(wPos, pAttribs.getURI(i),
210                             pAttribs.getLocalName(i), pAttribs.getQName(i),
211                             pAttribs.getType(i), pAttribs.getValue(i));
212                 }
213             }
214         } else {
215             xslAttributes = new org.xml.sax.helpers.AttributesImpl JavaDoc(pAttribs);
216         }
217     }
218     
219     /**
220      * Add integer value <code>addValue</code> to attribute with name <code>name</code>.
221      * If there is no such setted attribute, then value of this attribure is equal to
222      * <code>addValue</code>.
223      * @param addValue the increment of value
224      * @param name the name of attribute
225      */

226     public void addIntegerValue(int addValue, String JavaDoc name) {
227         Integer JavaDoc value = (Integer JavaDoc) getValue(name);
228         int v = (value != null) ? value.intValue() : 0;
229         set(name, v + addValue);
230     }
231 }
232
Popular Tags