1 23 package org.enhydra.kelp.common.deployer; 24 25 import org.enhydra.kelp.common.AbstractEchoBuilder; 27 import org.enhydra.kelp.common.PathUtil; 28 import org.enhydra.kelp.common.event.WriteListener; 29 import org.enhydra.kelp.common.node.OtterTemplateNode; 30 import org.enhydra.kelp.common.node.OtterFileNode; 31 import org.enhydra.kelp.common.node.OtterNode; 32 import org.enhydra.kelp.common.node.OtterNodeFactory; 33 34 import org.enhydra.tool.common.FileUtil; 36 import org.enhydra.tool.common.PathHandle; 37 import org.enhydra.tool.common.Template; 38 import org.enhydra.tool.common.ToolException; 39 import org.enhydra.tool.configure.ConfigTool; 40 41 import java.awt.Component ; 43 import java.io.File ; 44 import java.util.ArrayList ; 45 import java.util.Arrays ; 46 import java.util.ResourceBundle ; 47 48 public class InputBuilder extends AbstractEchoBuilder { 50 static ResourceBundle res = 51 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); private File [] builtTemplates = new File [0]; 53 private File [] builtPassthrough = new File [0]; 54 private OtterNode[] nodes = null; 55 private Component owner = null; 56 57 public InputBuilder(WriteListener listener) { 58 super(listener); 59 } 60 61 public File [] getBuiltTemplates() { 62 return builtTemplates; 63 } 64 65 public File [] getBuiltPassthrough() { 66 return builtPassthrough; 67 } 68 69 public void setNodes(OtterNode[] n) { 70 nodes = n; 71 } 72 73 public OtterNode[] getNodes() { 74 OtterNode[] n = new OtterTemplateNode[0]; 75 76 if (nodes == null) { 77 n = getProject().getAllInput(); 78 } else { 79 n = nodes; 80 } 81 return n; 82 } 83 84 protected void buildImpl() throws ToolException { 85 File [] out = new File [0]; 86 ArrayList outList = new ArrayList (); 87 88 if (isInputEnabled()) { 89 builtTemplates = makeTemplates(); 90 if (!getProject().isDeployInputFilter()) { 91 builtPassthrough = makePassthrough(); 92 } 93 outList.addAll(Arrays.asList(builtTemplates)); 94 outList.addAll(Arrays.asList(builtPassthrough)); 95 out = (File []) outList.toArray(out); 96 outList.clear(); 97 addFilesToProject(out); 98 } 99 } 100 101 public void setOwner(Component c) { 102 owner = c; 103 } 104 105 public Component getOwner() { 106 return owner; 107 } 108 109 private boolean isInputEnabled() { 111 boolean enabled = false; 112 113 if (getNodes().length > 0) { 114 enabled = getNodes()[0].getProject().isDeployInput(); 115 } 116 return enabled; 117 } 118 119 private void addFilesToProject(File [] files) { 120 OtterNodeFactory factory = null; 121 122 factory = getProject().getNodeFactory(); 123 for (int i = 0; i < files.length; i++) { 124 if (!isFresh()) { 125 break; 126 } 127 factory.importFile(getProject(), files[i]); 128 } 129 130 } 131 132 private File [] makePassthrough() throws ToolException { 133 File [] out = new File [0]; 134 OtterFileNode[] pass = new OtterFileNode[0]; 135 PathHandle outPath = null; 136 File inFile = null; 137 138 pass = PathUtil.getInputPassthrough(getProject()); 139 for (int i = 0; i < pass.length; i++) { 140 if (pass[i].isSelected()) { 141 outPath = getPassthroughPath(pass[i]); 142 inFile = new File (pass[i].getFilePath()); 143 try { 144 FileUtil.copy(inFile, outPath.getFile()); 145 echo(res.getString("Copying_input_to") 146 + outPath.getPath()); 147 } catch (ToolException e) { 148 echo(e); 149 e.printStackTrace(); 150 } 151 } 152 } 153 return out; 154 } 155 156 private File [] makeTemplates() throws ToolException { 157 File [] output = new File [0]; 158 ConfigTool tool = new ConfigTool(); 159 160 tool.setOwner(getOwner()); 161 tool.setEcho(isEcho()); 162 tool.setEchoWriter(getEchoWriter()); 163 tool.setTransformPath(true); 164 tool.setTemplates(getTemplates()); 165 tool.setOverwrite(getProject().isDeployOverwrite()); 166 tool.initReplacements(PathUtil.expandReplacementTable(getProject(), 167 getProject().getReplacementTable())); 168 output = tool.createOutput(); 169 return output; 170 } 171 172 private Template[] getTemplates() { 173 Template[] templates = new Template[0]; 174 ArrayList list = new ArrayList (); 175 OtterTemplateNode[] templateNodes = new OtterTemplateNode[0]; 176 177 templateNodes = PathUtil.getInputTemplates(getProject()); 178 for (int i = 0; i < templateNodes.length; i++) { 179 OtterTemplateNode cursor = null; 180 Template temp = null; 181 File outFile = null; 182 String outPath = new String (); 183 184 cursor = templateNodes[i]; 185 if (cursor.isSelected()) { 186 temp = new Template(new File (cursor.getFilePath()), 187 getProject().getDeployInputPath()); 188 outPath = cursor.getOutputPath(); 189 outFile = new File (outPath); 190 temp.setOutput(outFile); 191 list.add(temp); 192 } 193 if (! isFresh()) { 194 break; 195 } 196 } 197 list.trimToSize(); 198 templates = new Template[list.size()]; 199 templates = (Template[]) list.toArray(templates); 200 list.clear(); 201 return templates; 202 } 203 204 private PathHandle getPassthroughPath(OtterFileNode node) { 205 PathHandle inputPath = null; 206 PathHandle filePath = null; 207 PathHandle rootPath = null; 208 PathHandle passPath = null; 209 StringBuffer buf = new StringBuffer (); 210 String suffix = new String (); 211 212 inputPath = 213 PathHandle.createPathHandle(getProject().getDeployInputPath()); 214 filePath = PathHandle.createPathHandle(node.getFilePath()); 215 rootPath = 216 PathHandle.createPathHandle(getProject().getDeployRootPath()); 217 buf.append(rootPath.getPath()); 218 if (inputPath.parentOf(filePath)) { 219 suffix = 220 filePath.getPath().substring(inputPath.getPath().length()); 221 } else { 222 suffix = filePath.getFile().getName(); 223 } 224 if ((suffix.length() == 0) || (suffix.charAt(0) != '/')) { 225 buf.append('/'); 226 } 227 buf.append(suffix); 228 passPath = PathHandle.createPathHandle(buf.toString()); 229 return passPath; 230 } 231 232 } 233 | Popular Tags |