Source code: jbreport/core/Style.java
1 /*
2 * $Id: Style.java,v 1.1 2000/08/31 13:53:17 grantfin Exp $
3 *
4 * jbReport - A reporting library for Java
5 * Copyright (C) 2000 Grant Finnemore <grantfin@users.sourceforge.net>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package jbreport.core;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import org.xml.sax.Attributes;
27
28 import jbreport.Constants;
29 import jbreport.ReportElement;
30 import jbreport.ReportException;
31 import jbreport.ReportStylesheet;
32
33 /**
34 * This represents either a style or a set of styles - known as a style-sheet
35 * within a repository. It includes special methods for locating inherited
36 * data within a stylesheet.
37 *
38 * @author Grant Finnemore
39 * @version $Revision: 1.1 $
40 */
41 public
42 class Style extends ReportCompositeImpl
43 implements ReportStylesheet, XMLParsingElement {
44
45 /** The named styles within a stylesheet */
46 private Map namedStyles;
47
48 /** The parameters known to this style element */
49 private Map params;
50
51 /** The name of the style that this inherits from, if any */
52 private String inheritName;
53
54 /** The name of the object type that this style applies to, if any */
55 private String objectType;
56
57 //
58 // Constructors
59 //
60
61 //
62 // Implementation of the ReportStylesheet interface
63 //
64
65 public String getStyleValue(String objectType, String className,
66 String paramType) throws ReportException {
67 assertNotNull(objectType, "objectType");
68 assertNotNull(paramType, "paramType");
69 assert("stylesheet".equals(getType()),
70 "the getStyleValue() method can only be used with a stylesheet");
71 String result = null;
72 // First check for a className match - if possible
73 if(className != null) {
74 List children = getList(Constants.RE_CHILDREN);
75 for(int i = 0; i < children.size(); i++) {
76 Style child = (Style)children.get(i);
77 if(className.equals(child.name)) {
78 // Search the inheritance tree for a match
79 // System.out.println("HERE cn:" + className + ", ccn:"
80 // + child.name + ", pt:" + paramType);
81 result = search(child, paramType);
82 // System.out.println("HERE 3 - " + result);
83 // If we found one, stop looking
84 if(result != null) break;
85 }
86 }
87 }
88 // Look for an object type match
89 if(result == null) {
90 List children = getList(Constants.RE_CHILDREN);
91 for(int i = 0; i < children.size(); i++) {
92 Style child = (Style)children.get(i);
93 if(objectType.equals(child.objectType)) {
94 // Search the inheritance tree for a match
95 result = search(child, paramType);
96 // If we found one, stop looking
97 if(result != null) break;
98 }
99 }
100 }
101 // Look for a match in the superclass of this stylesheet, if possible
102 if(result == null && inheritName != null) {
103 ReportStylesheet stylesheet =
104 getRepository().fetchStylesheet(inheritName);
105 result = stylesheet.getStyleValue(objectType, className, paramType);
106 }
107 return result;
108 }
109
110 //
111 // Methods overloaded from AbstractReportElement
112 //
113
114 public void accept(ReportVisitor visitor, ReportVisitorState state)
115 throws ReportException {
116
117 if("stylesheet".equals(getType())) {
118 visitor.visitStylesheet(this, state);
119 }
120 else if("style".equals(getType())) {
121 visitor.visitStyle(this, state);
122 }
123 }
124
125 public void xmlInitialize(String localName, Attributes attributes)
126 throws ReportException {
127
128 super.xmlInitialize(localName, attributes);
129 inheritName = attributes.getValue("inherits");
130 objectType = attributes.getValue("object");
131 }
132
133 public void xmlEndChild(ReportElement elem) throws ReportException {
134 if("style".equals(elem.getType())) {
135 addElement(elem);
136 // If the style is named, add it to the named styles in the stylesheet
137 Style style = (Style)elem;
138 if(style.name != null) {
139 if(namedStyles == null) {
140 namedStyles = new HashMap();
141 }
142 namedStyles.put(style.name, style);
143 }
144 }
145 else {
146 super.xmlEndChild(elem);
147 }
148 }
149
150 //
151 // Implementation of the XMLParsingElement interface
152 //
153
154 public boolean xmlCanParse() {
155 return "style".equals(getType());
156 }
157
158 public void xmlParse(XMLHandler handler, String localName,
159 Attributes attributes) {
160 if("param".equals(localName)) {
161 addParameter(attributes.getValue("type"),
162 attributes.getValue("value"));
163 }
164 else {
165 throw new IllegalArgumentException("Unknown token " + localName);
166 }
167 }
168
169 public void xmlCData(XMLHandler handler, String cdata) {
170
171 }
172
173 //
174 // Implementation methods
175 //
176
177 private void addParameter(String type, String value) {
178 assertNotNull(type, "type");
179 assertNotNull(value, "value");
180 if(params == null) {
181 params = new HashMap();
182 }
183 params.put(type, value);
184 }
185
186 private static String search(Style top, String paramType) {
187 String result = null;
188 for(Style elem = top; elem != null; elem = findInherited(elem)) {
189 // if(elem.params != null) {
190 // for(java.util.Iterator it = elem.params.entrySet().iterator(); it.hasNext();){
191 // Map.Entry entry = (Map.Entry)it.next();
192 // System.out.println("key: " + entry.getKey()
193 // + ", value:" + entry.getValue());
194 // }
195 // }
196 if(elem.params != null && elem.params.containsKey(paramType)) {
197 // System.out.println("HERE 2");
198 result = (String)elem.params.get(paramType);
199 // System.out.println("HERE 4 - " + result);
200 break;
201 }
202 }
203 return result;
204 }
205
206 private static Style findInherited(Style style) {
207 // Find the parent which is the stylesheet
208 Style stylesheet = (Style)style.getParent();
209 Style result = null;
210 // If the style declares an inheritance, search for it
211 if(style.inheritName != null && stylesheet.namedStyles != null) {
212 result = (Style)stylesheet.namedStyles.get(style.inheritName);
213 }
214 return result;
215 }
216 }
217
218
219
220