Inspector | Message | Severity | Location |
---|---|---|---|
Java Inspector 048 | Copyrights information should be present in each file. | 1 | |
Java Inspector 049 | Use a Collection instead of arrays Object[] | 2 | 151:18 |
Java Inspector 081 | Avoid static collections, they can grow in size over time. | 2 | 98:5 |
Java Inspector 089 | Type is not documented | 2 | 58:1 |
Java Inspector 089 | Undocumented constructor | 2 | 63:9 |
Java Inspector 089 | Undocumented method | 2 | 69:9 |
Java Inspector 089 | Undocumented method | 2 | 89:9 |
Java Inspector 089 | Undocumented field | 2 | 100:5 |
Java Inspector 089 | Undocumented field | 2 | 101:5 |
Java Inspector 089 | Undocumented field | 2 | 102:5 |
Java Inspector 089 | Undocumented constructor | 2 | 104:5 |
Java Inspector 089 | Undocumented constructor | 2 | 108:5 |
Java Inspector 089 | Parameter clazz is not documented | 2 | 120:5 |
Java Inspector 089 | Method return value is not properly documented | 2 | 120:5 |
Java Inspector 089 | Undocumented method | 2 | 168:5 |
Java Inspector 089 | Undocumented method | 2 | 172:5 |
Java Inspector 089 | Undocumented field | 2 | 220:5 |
Java Inspector 089 | Undocumented method | 2 | 222:5 |
Java Inspector 089 | Undocumented field | 2 | 226:5 |
Java Inspector 089 | Undocumented method | 2 | 228:5 |
Java Inspector 089 | Parameter name is not documented | 2 | 240:9 |
Java Inspector 089 | Parameter text is not documented | 2 | 240:9 |
Java Inspector 089 | Parameter owner is not documented | 2 | 240:9 |
Java Inspector 089 | @return tag is not applicable to void methods | 2 | 240:9 |
Java Inspector 024 | Avoid hardwired character literals | 3 | 141:65 |
Java Inspector 024 | Avoid hardwired character literals | 3 | 141:69 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 141:91 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 141:123 |
Java Inspector 040 | Parameter name useEmbeddedStyle clashes with field name in AbstractRenderer | 3 | 222:34 |
Java Inspector 051 | It is good practice to call in any case super() in a constructor. | 3 | 104:5 |
Java Inspector 051 | It is good practice to call in any case super() in a constructor. | 3 | 108:5 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 70:13 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 72:20 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 75:21 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 77:28 |
1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23
24package biz.hammurapi.render.dom;
25
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.OutputStream;
29import java.io.UnsupportedEncodingException;
30import java.util.HashMap;
31import java.util.Iterator;
32import java.util.Map;
33
34import javax.xml.parsers.DocumentBuilder;
35import javax.xml.parsers.DocumentBuilderFactory;
36import javax.xml.parsers.ParserConfigurationException;
37import javax.xml.transform.Templates;
38import javax.xml.transform.Transformer;
39import javax.xml.transform.TransformerConfigurationException;
40import javax.xml.transform.TransformerException;
41import javax.xml.transform.TransformerFactory;
42import javax.xml.transform.dom.DOMSource;
43import javax.xml.transform.stream.StreamResult;
44import javax.xml.transform.stream.StreamSource;
45
46import org.w3c.dom.Element;
47
48import biz.hammurapi.config.Parameterizable;
49import biz.hammurapi.render.RenderRequest;
50import biz.hammurapi.render.RenderingException;
51
52
53/**
54 *
55 * @author Pavel Vlasov
56 * @version $Revision: 1.5 $
57 */
58public abstract class AbstractRenderer implements DomRenderer, Parameterizable {
59 private static class StyleKey {
60 Class clazz;
61 String profile;
62
63 public StyleKey(Class clazz, String profile) {
64 super();
65 this.clazz = clazz;
66 this.profile = profile;
67 }
68
69 public boolean equals(Object obj) {
70 if (obj==this) {
71 return true;
72 } else if (obj instanceof StyleKey) {
73 StyleKey otherKey=(StyleKey) obj;
74 if (clazz.equals(otherKey.clazz)) {
75 if (profile==null) {
76 return otherKey.profile==null;
77 } else if (otherKey.profile==null) {
78 return false;
79 } else {
80 return profile.equals(otherKey.profile);
81 }
82 }
83 return false;
84 } else {
85 return super.equals(obj);
86 }
87 }
88
89 public int hashCode() {
90 int ret=clazz.hashCode();
91 if (profile!=null) {
92 ret^=profile.hashCode();
93 }
94 return ret;
95 }
96 }
97
98 private static Map styleMap=new HashMap();
99
100 protected RenderRequest request;
101 protected String profile;
102 public static final String PROFILE_SEPARATOR="!";
103
104 protected AbstractRenderer(RenderRequest request) {
105 this.request=request;
106 }
107
108 protected AbstractRenderer(RenderRequest request, String profile) {
109 this.request=request;
110 this.profile=profile;
111 }
112
113 /**
114 * Searches for class name first, then for all interfaces and then for
115 * superclass.
116 * @param clazz
117 * @return
118 * @throws TransformerConfigurationException
119 */
120 public synchronized Transformer getEmbeddedStyle(Class clazz) throws TransformerConfigurationException {
121 StyleKey key=new StyleKey(clazz, profile);
122 Templates templates=(Templates) styleMap.get(key);
123 if (templates==null) {
124 String transletClass=clazz.getName()+StyleCompiler.POSTFIX;
125 if (profile!=null) {
126 transletClass+=StyleCompiler.TRANSLET_PROFILE_SEPARATOR+profile;
127 }
128
129// try {
130// getClass().getClassLoader().loadClass(transletClass);
131// int idx=transletClass.lastIndexOf('.');
132// TransformerFactory tf=new TransformerFactoryImpl();
133// tf.setAttribute("use-classpath", Boolean.TRUE);
134// if (idx!=-1) {
135// tf.setAttribute("package-name", transletClass.substring(0, idx));
136// }
137// templates=tf.newTemplates(new StreamSource(idx==-1 ? transletClass : transletClass.substring(idx+1)));
138// styleMap.put(key, templates);
139// return templates.newTransformer();
140// } catch (ClassNotFoundException e) {
141 String resourceName=clazz.getName().replace('.','/')+(profile==null ? "" : PROFILE_SEPARATOR+profile)+".xsl";
142 InputStream rets=getClass().getClassLoader().getResourceAsStream(resourceName);
143 if (rets!=null) {
144 TransformerFactory tf=TransformerFactory.newInstance();
145 templates=tf.newTemplates(new StreamSource(rets));
146 styleMap.put(key, templates);
147 return templates.newTransformer();
148 }
149// }
150
151 Class[] interfaces = clazz.getInterfaces();
152 for (int i=0; i<interfaces.length; i++) {
153 Transformer rett=getEmbeddedStyle(interfaces[i]);
154 if (rett!=null) {
155 return rett;
156 }
157 }
158
159 if (clazz.getSuperclass()==null) {
160 styleMap.put(key, null);
161 return null;
162 }
163 return getEmbeddedStyle(clazz.getSuperclass());
164 }
165 return templates.newTransformer();
166 }
167
168 public RenderRequest render(OutputStream out) throws RenderingException {
169 return render(null, out);
170 }
171
172 public RenderRequest render(InputStream style, OutputStream out) throws RenderingException {
173 try {
174 Transformer transformer=null;
175
176 if (style==null && useEmbeddedStyle) {
177 transformer=getEmbeddedStyle(request.getRenderee().getClass());
178 }
179
180 if (transformer==null) {
181 TransformerFactory tFactory = TransformerFactory.newInstance();
182 transformer =
183 style==null ?
184 tFactory.newTransformer() :
185 tFactory.newTransformer(new StreamSource(style));
186 }
187
188 Iterator pit=params.entrySet().iterator();
189 while (pit.hasNext()) {
190 Map.Entry entry=(Map.Entry) pit.next();
191 transformer.setParameter((String) entry.getKey(), entry.getValue());
192 }
193
194 DocumentBuilder builder=null;
195
196 if (request.getDocument()==null) {
197 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
198 builder=factory.newDocumentBuilder();
199 request.setDocument(builder.newDocument());
200 request.getDocument().appendChild(render(request.getDocument()));
201 }
202
203 DOMSource domSource = new DOMSource(request.getDocument());
204 transformer.transform(domSource, new StreamResult(out));
205 out.close();
206 return request;
207 } catch (TransformerConfigurationException e) {
208 throw new RenderingException(e.toString(), e);
209 } catch (ParserConfigurationException e) {
210 throw new RenderingException(e.toString(), e);
211 } catch (TransformerException e) {
212 throw new RenderingException(e.toString(), e);
213 } catch (UnsupportedEncodingException e) {
214 throw new RenderingException(e.toString(), e);
215 } catch (IOException e) {
216 throw new RenderingException(e.toString(), e);
217 }
218 }
219
220 protected boolean useEmbeddedStyle=true;
221
222 public void setEmbeddedStyle(boolean useEmbeddedStyle) {
223 this.useEmbeddedStyle=useEmbeddedStyle;
224 }
225
226 protected Map params=new HashMap();
227
228 public boolean setParameter(String name, Object value) {
229 params.put(name, value);
230 return true;
231 }
232
233 /**
234 * Creates and add element with text if text!=null && name!=null
235 * @param name
236 * @param text
237 * @param owner
238 * @return
239 */
240 public static void appendTextElement(String name, String text, Element owner) {
241 if (name!=null && text!=null) {
242 Element e=owner.getOwnerDocument().createElement(name);
243 owner.appendChild(e);
244 e.appendChild(owner.getOwnerDocument().createTextNode(text));
245 }
246 }
247}
248