EMMA Coverage Report (generated Thu Jan 20 11:39:44 EST 2011)
[all classes][com.hammurapi.render]

COVERAGE SUMMARY FOR SOURCE FILE [JxpRenderer.java]

nameclass, %method, %block, %line, %
JxpRenderer.java0%   (0/4)0%   (0/14)0%   (0/479)0%   (0/80)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class JxpRenderer0%   (0/1)0%   (0/6)0%   (0/263)0%   (0/46)
JxpRenderer (Object): void 0%   (0/1)0%   (0/12)0%   (0/4)
access$0 (JxpRenderer): Object 0%   (0/1)0%   (0/3)0%   (0/1)
access$1 (JxpRenderer): JxpPageSource 0%   (0/1)0%   (0/3)0%   (0/1)
postRender (Map, Context, String, Locale, File): void 0%   (0/1)0%   (0/105)0%   (0/14)
render (File, Map, Context, String, Locale): boolean 0%   (0/1)0%   (0/54)0%   (0/12)
render (Writer, Map, Context, String, Locale, File): boolean 0%   (0/1)0%   (0/86)0%   (0/15)
     
class JxpRenderer$10%   (0/1)0%   (0/5)0%   (0/39)0%   (0/8)
JxpRenderer$1 (JxpRenderer): void 0%   (0/1)0%   (0/6)0%   (0/2)
getStreamName (String): String 0%   (0/1)0%   (0/4)0%   (0/1)
hasStream (String): boolean 0%   (0/1)0%   (0/17)0%   (0/3)
isExpired (CachedJxpPage): boolean 0%   (0/1)0%   (0/2)0%   (0/1)
loadStream (CachedJxpPage): InputStream 0%   (0/1)0%   (0/10)0%   (0/1)
     
class JxpRenderer$1SearchResult0%   (0/1)0%   (0/1)0%   (0/12)0%   (0/4)
JxpRenderer$1SearchResult (JxpRenderer, String, JxpPageSource): void 0%   (0/1)0%   (0/12)0%   (0/4)
     
class JxpRenderer$20%   (0/1)0%   (0/2)0%   (0/165)0%   (0/25)
JxpRenderer$2 (JxpRenderer, Locale, String, JxpPageSource, JxpRenderer$1Searc... 0%   (0/1)0%   (0/22)0%   (0/3)
visit (Class): boolean 0%   (0/1)0%   (0/143)0%   (0/22)

1package com.hammurapi.render;
2 
3import java.io.File;
4import java.io.FileWriter;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.Writer;
8import java.net.URL;
9import java.util.HashMap;
10import java.util.Locale;
11import java.util.Map;
12 
13import org.onemind.commons.java.util.FileUtils;
14import org.onemind.jxp.CachedJxpPage;
15import org.onemind.jxp.CachingPageSource;
16import org.onemind.jxp.JxpContext;
17import org.onemind.jxp.JxpPageSource;
18import org.onemind.jxp.JxpProcessor;
19 
20import com.hammurapi.common.ClassHierarchyVisitable;
21import com.hammurapi.common.Context;
22import com.hammurapi.common.IdentityManager;
23import com.hammurapi.common.Visitor;
24import com.hammurapi.convert.ConvertingService;
25 
26/**
27 * JxpRenderer renders using JXP template engine and pages loaded by classloader.
28 * @author Pavel Vlasov
29 *
30 */
31public class JxpRenderer implements WriterRenderer, FileRenderer, RenderingConstants {
32 
33        /**
34         * Loads rendering resource form toRender class loader.
35         * @author Pavel Vlasov
36         *
37         */
38        private JxpPageSource pageSource = new CachingPageSource() {
39 
40                /**
41                 * {@inheritDoc}
42                 */
43                private String getStreamName(String pageName) {
44                        return FileUtils.concatFilePath(DefaultRenderingPageSource.PATH_PREFIX, pageName);
45                }
46 
47                /**
48                 * {@inheritDoc}
49                 */
50 
51                protected boolean isExpired(CachedJxpPage page) {
52                        return false;
53                }
54 
55                /**
56                 * {@inheritDoc}
57                 */
58                protected boolean hasStream(String pageName) {
59                        String streamName = getStreamName(pageName);
60                        URL resource = toRender.getClass().getResource(streamName);
61                        return resource != null;
62                }
63 
64                /**
65                 * {@inheritDoc}
66                 */
67                protected InputStream loadStream(CachedJxpPage page) throws IOException {
68                        return toRender.getClass().getResourceAsStream(getStreamName(page.getName()));
69                }
70        };        
71        
72        private Object toRender;
73 
74        public JxpRenderer(Object obj) {
75                this.toRender = obj;
76        }
77 
78        /**
79         * Renders to a file. 
80         * This method does not overwrite existing files if they were created after rendering has started.
81         * It prevents infinite loops in rendering.
82         */
83        @Override
84        public boolean render(File out, Map<String, Object> environment, Context context, String profile, Locale locale) throws RenderingException {
85                if (environment!=null) {
86                        Object renderStart = environment.get(RENDER_START);
87                        if (renderStart instanceof Long) {
88                                if (out.exists() && out.isFile() && out.lastModified()>=((Long) renderStart)) {
89                                        return false;
90                                }
91                        }
92                }
93                
94                try {
95                        Writer writer = new FileWriter(out);
96                        try {
97                                return render(writer, environment, context, profile, locale, out.getParentFile());
98                        } finally {
99                                writer.close();
100                        }
101                } catch (Exception e) {
102                        throw new RenderingException(e);
103                }
104        }
105                
106        @Override
107        public boolean render(Writer out, Map<String, Object> environment, Context context, final String profile, final Locale locale, File outputDir)        throws RenderingException {
108                final JxpPageSource ps = context.lookup(JxpPageSource.class);
109                
110                class SearchResult {
111                        String pageName;
112                        JxpPageSource pageSource;
113                        
114                        SearchResult(String pageName, JxpPageSource pageSource) {
115                                super();
116                                this.pageName = pageName;
117                                this.pageSource = pageSource;
118                        }                                                
119                }
120                
121                final SearchResult[] sr = {null};
122                
123                ClassHierarchyVisitable chv = new ClassHierarchyVisitable(toRender.getClass());
124                
125                chv.accept(new Visitor<Class<?>>() {
126 
127                        Locale actualLocale = locale==null ? Locale.getDefault() : locale;
128                        
129                        @Override
130                        public boolean visit(Class<?> target) {
131                                for (int i=0; i<4; i++) {
132                                        String variant=target.getName().replace('.','/');
133                                        
134                                        if (profile!=null) {
135                                                variant+="!"+profile;
136                                        }
137                                        
138                                        switch (i) {
139                                                case 0:
140                                                        variant+="_"+actualLocale;
141                                                        break;
142                                                case 1:
143                                                        variant+="_"+actualLocale.getLanguage();
144                                                        if (actualLocale.getCountry().length()!=0) {
145                                                                variant+="_"+actualLocale.getCountry();
146                                                        }
147                                                        break;
148                                                case 2:
149                                                        variant+="_"+actualLocale.getLanguage();
150                                                        break;
151                                                case 3:
152                                                        break;                                                        
153                                        }
154                                        
155                                        variant+=".jxp";
156                                        
157                                        if (ps!=null && ps.hasJxpPage(variant)) {
158                                                sr[0] = new SearchResult(variant, ps);
159                                                return false;
160                                        }
161                                        
162                                        if (pageSource.hasJxpPage(variant)) {
163                                                sr[0] = new SearchResult(variant, pageSource);
164                                                return false;                                                
165                                        }
166                                }
167                                
168                                return true;
169                        }
170                        
171                });
172                
173                if (sr[0]==null) {
174                        return false;
175                }
176                
177                JxpContext jxpContext = new JxpContext(sr[0].pageSource);
178                JxpProcessor processor = new JxpProcessor(jxpContext);
179                
180                try {
181                        Map<String, Object> env = environment==null ? new HashMap<String, Object>() : new HashMap<String, Object>(environment);
182                        env.put(TO_RENDER, toRender);
183//                        env.remove(TO_RENDER_TYPE_NAME); // Clean up.
184                        processor.process(sr[0].pageName, out, env);                
185                        postRender(env, context, profile, locale, outputDir);
186                        return true;
187                } catch (Exception e) {
188                        throw new RenderingException(e);
189                }
190        }
191 
192        /**
193         * This method is invoked after successful rendering. This implementation is tailored for report generator -
194         * it renders details and contents if outputDir environment entry is instance of File and profile is outline or
195         * outline_http
196         * @param environment
197         * @param context
198         * @param profile
199         * @param locale
200         * @throws RenderingException
201         */
202        protected void postRender(Map<String, Object> environment, Context context, final String profile, final Locale locale, File outputDir) throws RenderingException {
203                if (outputDir!=null && (OUTLINE.equals(profile) || OUTLINE_HTTP.equals(profile))) {
204                        IdentityManager<?> identityManager = context.lookup(IdentityManager.class);
205                        Object id = identityManager.getIdentity(toRender);
206                        File detailsOut = new File(outputDir, "e"+id+".html");
207                        FileRenderer fileRenderer = ConvertingService.convert(toRender, FileRenderer.class);
208                        if (fileRenderer==null) {
209                                render(detailsOut, environment, context, null, locale);
210                        } else {
211                                fileRenderer.render(detailsOut, environment, context, null, locale);
212                        }
213                        
214                        boolean http = OUTLINE_HTTP.equals(profile);
215                        File contentsOut = new File(outputDir, "e"+id+"_contents.html");
216                        if (fileRenderer==null) {
217                                render(contentsOut, environment, context, http ? CONTENTS_HTTP : CONTENTS, locale);
218                        } else {
219                                fileRenderer.render(contentsOut, environment, context, http ? CONTENTS_HTTP : CONTENTS, locale);                                
220                        }
221                }                                
222        }
223}
224 

[all classes][com.hammurapi.render]
EMMA 2.0.5312 EclEmma Fix 2 (C) Vladimir Roubtsov