| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.taskdefs; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.util.Locale; |
| 37 | | | |
| 38 | | | import org.apache.fop.tools.anttasks.Fop; |
| 39 | | | import org.jcoderz.commons.taskdefs.XtremeDocs.FormatterInfoData; |
| 40 | | | |
| 41 | | | |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | | | @author |
| 46 | | | |
| 47 | | | public abstract class Formatter |
| 48 | | | { |
| 49 | | | private final FormatterInfoData mInfoData; |
| 50 | | | |
| 51 | | | private Formatter (FormatterInfoData i) |
| 52 | 0 | | { |
| 53 | 0 | | mInfoData = i; |
| 54 | 0 | | } |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | | @param |
| 59 | | | @return |
| 60 | | | |
| 61 | | | public static Formatter getInstance (FormatterInfoData f) |
| 62 | | | { |
| 63 | 0 | | if (f.getType() == null) |
| 64 | | | { |
| 65 | 0 | (1) | throw new IllegalArgumentException("formatter type cannot be null"); |
| 66 | | | } |
| 67 | 0 | | final String typeAsLowercase = f.getType().toLowerCase(Locale.US); |
| 68 | | | final Formatter result; |
| 69 | 0 | (2)(3) | if (typeAsLowercase.equals("html")) |
| 70 | | | { |
| 71 | 0 | | result = new HtmlFormatter(f); |
| 72 | | | } |
| 73 | 0 | (4)(5) | else if (typeAsLowercase.equals("pdf")) |
| 74 | | | { |
| 75 | 0 | | result = new PdfFormatter(f); |
| 76 | | | } |
| 77 | | | else |
| 78 | | | { |
| 79 | 0 | | throw new NoSuchMethodError("Unsupported type " + f.getType()); |
| 80 | | | } |
| 81 | 0 | | return result; |
| 82 | | | } |
| 83 | | | |
| 84 | | | |
| 85 | | | |
| 86 | | | <tt></tt> |
| 87 | | | @param |
| 88 | | | @param |
| 89 | | | @param |
| 90 | | | |
| 91 | | | public abstract void transform (XtremeDocs parent, File in, File out); |
| 92 | | | |
| 93 | | | |
| 94 | | | |
| 95 | | | <tt></tt> |
| 96 | | | @return |
| 97 | | | |
| 98 | | | public abstract String getFileExtension (); |
| 99 | | | |
| 100 | | | |
| 101 | | | |
| 102 | | | |
| 103 | | | @return |
| 104 | | | |
| 105 | | | public FormatterInfoData getInfoData () |
| 106 | | | { |
| 107 | 0 | | return mInfoData; |
| 108 | | | } |
| 109 | | | |
| 110 | | | protected void executeSaxon (XtremeDocs parent, File in, final File out) |
| 111 | | | { |
| 112 | 0 | | final SaxonTask task = new SaxonTask(); |
| 113 | 0 | | task.setProject(parent.getProject()); |
| 114 | 0 | | task.setTaskName("saxon"); |
| 115 | 0 | | task.setCss(getInfoData().getCascadingStyleSheet()); |
| 116 | 0 | | task.setXsl(getInfoData().getStyleSheet()); |
| 117 | 0 | | task.setIn(in); |
| 118 | 0 | | task.setOut(out); |
| 119 | 0 | | task.setDir(in.getParentFile()); |
| 120 | 0 | | task.setFailonerror(parent.failOnError()); |
| 121 | 0 | | task.setClasspath(parent.getClassPath()); |
| 122 | 0 | | task.setHaveRenderX(haveRenderX(parent)); |
| 123 | 0 | | task.execute(); |
| 124 | 0 | | } |
| 125 | | | |
| 126 | | | protected boolean haveRenderX (XtremeDocs parent) |
| 127 | | | { |
| 128 | 0 | | return parent.getXepHome() != null && parent.getXepHome().isDirectory(); |
| 129 | | | } |
| 130 | | | |
| 131 | | | |
| 132 | | | |
| 133 | | | |
| 134 | | | @author |
| 135 | | | |
| 136 | | | private static class HtmlFormatter |
| 137 | | | extends Formatter |
| 138 | | | { |
| 139 | | | HtmlFormatter (FormatterInfoData i) |
| 140 | | | { |
| 141 | 0 | | super(i); |
| 142 | 0 | | } |
| 143 | | | |
| 144 | | | {@inheritDoc} |
| 145 | | | public void transform (XtremeDocs parent, File in, File out) |
| 146 | | | { |
| 147 | 0 | | executeSaxon(parent, in, out); |
| 148 | 0 | | parent.log("Transformed " + in + " successfully to " + out); |
| 149 | 0 | | } |
| 150 | | | |
| 151 | | | {@inheritDoc} |
| 152 | | | public String getFileExtension () |
| 153 | | | { |
| 154 | 0 | | return "html"; |
| 155 | | | } |
| 156 | | | } |
| 157 | | | |
| 158 | | | |
| 159 | | | |
| 160 | | | |
| 161 | | | @author |
| 162 | | | |
| 163 | | | private static class PdfFormatter |
| 164 | | | extends Formatter |
| 165 | | | { |
| 166 | | | |
| 167 | | | |
| 168 | | | |
| 169 | | | @param |
| 170 | | | |
| 171 | | | PdfFormatter (FormatterInfoData i) |
| 172 | | | { |
| 173 | 0 | | super(i); |
| 174 | 0 | | } |
| 175 | | | |
| 176 | | | {@inheritDoc} |
| 177 | | | public void transform (XtremeDocs parent, File in, File out) |
| 178 | | | { |
| 179 | 0 | | final File tmp = new File(out.getParentFile(), out.getName() + ".fo"); |
| 180 | 0 | | executeSaxon(parent, in, tmp); |
| 181 | | | |
| 182 | 0 | | if (haveRenderX(parent)) |
| 183 | | | { |
| 184 | 0 | | executeXep(parent, in, out, tmp); |
| 185 | | | } |
| 186 | | | else |
| 187 | | | { |
| 188 | 0 | | executeFop(parent, in, out, tmp); |
| 189 | | | } |
| 190 | 0 | | parent.log("Transformed " + tmp + " successfully to " + out); |
| 191 | 0 | | } |
| 192 | | | |
| 193 | | | private void executeXep (XtremeDocs parent, File in, File out, File tmp) |
| 194 | | | { |
| 195 | 0 | | final XepTask xep = new XepTask(); |
| 196 | 0 | | xep.setProject(parent.getProject()); |
| 197 | 0 | | xep.setTaskName("xep"); |
| 198 | 0 | | xep.setFo(tmp); |
| 199 | 0 | | xep.setOut(out); |
| 200 | 0 | | xep.setXephome(parent.getXepHome()); |
| 201 | 0 | | xep.execute(); |
| 202 | 0 | | } |
| 203 | | | |
| 204 | | | private void executeFop (XtremeDocs parent, File in, File out, File tmp) |
| 205 | | | { |
| 206 | 0 | | final Fop fop = new Fop(); |
| 207 | 0 | | fop.setProject(parent.getProject()); |
| 208 | 0 | | fop.setTaskName("fop"); |
| 209 | 0 | | fop.setFofile(tmp); |
| 210 | 0 | | fop.setOutfile(out); |
| 211 | 0 | | fop.setFormat("application/pdf"); |
| 212 | 0 | | fop.setMessagelevel("info"); |
| 213 | 0 | | fop.setBasedir(in.getParentFile()); |
| 214 | 0 | | fop.execute(); |
| 215 | 0 | | } |
| 216 | | | |
| 217 | | | {@inheritDoc} |
| 218 | | | public String getFileExtension () |
| 219 | | | { |
| 220 | 0 | | return "pdf"; |
| 221 | | | } |
| 222 | | | |
| 223 | | | } |
| 224 | | | |
| 225 | | | } |