| 1 | package com.hammurapi.render; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.FileWriter; |
| 5 | import java.io.IOException; |
| 6 | import java.io.InputStream; |
| 7 | import java.io.Writer; |
| 8 | import java.net.URL; |
| 9 | import java.util.HashMap; |
| 10 | import java.util.Locale; |
| 11 | import java.util.Map; |
| 12 | |
| 13 | import org.onemind.commons.java.util.FileUtils; |
| 14 | import org.onemind.jxp.CachedJxpPage; |
| 15 | import org.onemind.jxp.CachingPageSource; |
| 16 | import org.onemind.jxp.JxpContext; |
| 17 | import org.onemind.jxp.JxpPageSource; |
| 18 | import org.onemind.jxp.JxpProcessor; |
| 19 | |
| 20 | import com.hammurapi.common.ClassHierarchyVisitable; |
| 21 | import com.hammurapi.common.Context; |
| 22 | import com.hammurapi.common.IdentityManager; |
| 23 | import com.hammurapi.common.Visitor; |
| 24 | import com.hammurapi.convert.ConvertingService; |
| 25 | |
| 26 | /** |
| 27 | * JxpRenderer renders using JXP template engine and pages loaded by classloader. |
| 28 | * @author Pavel Vlasov |
| 29 | * |
| 30 | */ |
| 31 | public 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 | |