1 package net.sf.invicta.dumper; 2 3 import java.util.Iterator ; 4 import java.util.List ; 5 6 import net.sf.invicta.InvictaConstants; 7 import net.sf.invicta.InvictaException; 8 import net.sf.invicta.api.InvictaComponent; 9 import net.sf.invicta.api.Product; 10 import net.sf.invicta.api.DependItem; 11 import net.sf.invicta.template.TemplateHelper; 12 import org.apache.commons.lang.StringUtils; 13 14 15 20 public class ExportedProjectDefinitionDumper extends InvictaBasicDumper { 21 protected static final String DEFAULT_EXPORT = "false"; 22 protected static final String DIST_PRODUCT_DIR_PROPERTY = 23 InvictaConstants.DIST_PRODUCT_DIR_PROPERTY; 24 25 protected static final String PROJECT_DEF_HEADER_TEMPLATE = 26 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 27 "<!DOCTYPE invictaProject PUBLIC \"Invicta Project Definition 1.0\" \"InvictaProject.dtd\">\n" + 28 "<invictaProject>\n" + 29 "\t<component name=\"%{projectName}\" type=\"" + 30 InvictaConstants.DEFAULT_LIBRARIES_TYPE +"\" dir=\"${project.%{projectName}.home}\">\n"; 31 32 private static final String PROJECT_DEF_FOOTER_TEMPLATE = 33 "\n\t</component>\n" + 34 "\n</invictaProject>\n"; 35 36 37 40 public ExportedProjectDefinitionDumper() { 41 super(); 42 } 43 44 47 public String getName() { 48 return "exportedProjectDefinition"; 49 } 50 51 54 public String getDumpContent() throws InvictaException { 55 56 StringBuffer dump = new StringBuffer (); 57 58 TemplateHelper.format(PROJECT_DEF_HEADER_TEMPLATE, getProject(), dump); 59 60 for (Iterator iter = getProject().getComponents().iterator(); iter.hasNext();) { 63 InvictaComponent component = (InvictaComponent) iter.next(); 64 65 String name = getName(component.getName()); 66 String dependProducts = getDependProducts(component); 67 boolean foundPrimary = false; 68 String exportedSelfProducts = ""; 69 for (Iterator prodIter = component.getSelfProducts().iterator(); prodIter.hasNext();) { 71 Product product = (Product) prodIter.next(); 72 String productFile = canonizeFile(product.getFile(), component.getDir()); 73 if (product.getName() == null) 74 foundPrimary = true; 75 76 if (product.getExport()) { 77 if (exportedSelfProducts.length() != 0) 78 exportedSelfProducts += ","; 79 exportedSelfProducts += getName(product.getFullName()); 80 } 81 82 String actualDependProduct; 83 if (product.getDependNames().size() > 0) 84 actualDependProduct = getProductDependProducts(component.getName(), product); 85 else 86 actualDependProduct = dependProducts; 87 88 dump.append( 89 getProductDefinition(product, productFile, actualDependProduct)); 90 } 91 92 if (!foundPrimary && 95 (exportedSelfProducts.length() > 0)) { 96 dump.append( 97 getDefaultProductDefinition(component.getName(), exportedSelfProducts)); 98 99 } else if (component.getSelfProducts().size() == 0) { 100 dump.append( 101 getDefaultProductDefinition(component.getName(), dependProducts)); 102 } 103 } 104 105 dump.append(PROJECT_DEF_FOOTER_TEMPLATE); 106 107 return dump.toString(); 108 } 109 110 116 protected String getDependProducts(InvictaComponent component) { 117 String dependProducts = ""; 118 List dependItems = component.getDependItems(); 119 for (Iterator iter = dependItems.iterator(); iter.hasNext();) { 121 DependItem dependItem = (DependItem) iter.next(); 122 123 InvictaComponent dependComponent = getProject().getComponent(dependItem.getName()); 125 126 if (dependItem.getProductsList().size() > 0) { 129 List productList = dependItem.getProductsList(); 130 for (Iterator iterator = dependItem.getProductsList().iterator();iterator.hasNext();) { 131 String productName = (String ) iterator.next(); 132 if (dependProducts.length() != 0) 133 dependProducts += ","; 134 dependProducts += getName(dependItem.getName()) + "." + productName; 135 } 136 } else { 137 138 for (Iterator iterator = dependComponent.getExportedProducts().iterator();iterator.hasNext();) { 141 Product product = (Product) iterator.next(); 142 if (dependProducts.length() != 0) 143 dependProducts += ","; 144 dependProducts += getName(product.getFullName()); 145 } 146 } 147 } 148 return dependProducts; 149 } 150 151 154 protected String getProductDependProducts(String componentName, Product product) { 155 String dependProducts = ""; 156 for (Iterator iter = product.getDependNames().iterator(); iter.hasNext();) { 158 String producName = (String ) iter.next(); 159 160 if (dependProducts.length() != 0) 161 dependProducts += ","; 162 dependProducts += getName(componentName) + "." + producName; 163 } 164 return dependProducts; 165 } 166 167 172 protected String canonizeFile(String file, String distDir){ 173 if (file == null) 174 return null; 175 176 String doubleSeparator = InvictaConstants.FILE_SEPARATOR + InvictaConstants.FILE_SEPARATOR; 177 String specialDoubleSeparator = InvictaConstants.FILE_SEPARATOR + "." + InvictaConstants.FILE_SEPARATOR; 178 String path = InvictaConstants.FILE_SEPARATOR 179 + getProject().getProjectPropertyValue(DIST_PRODUCT_DIR_PROPERTY) 180 + InvictaConstants.FILE_SEPARATOR 181 + distDir + InvictaConstants.FILE_SEPARATOR + file; 182 path = StringUtils.replace(path, doubleSeparator, InvictaConstants.FILE_SEPARATOR); 183 path = StringUtils.replace(path, specialDoubleSeparator, InvictaConstants.FILE_SEPARATOR); 184 return path; 185 } 186 187 188 195 protected String getName(String oldName) { 196 String projectPrefix = getProject().getName() + "."; 197 198 String newName = oldName; 199 200 int dotIndex = newName.indexOf(projectPrefix); 202 if (dotIndex != -1) 203 newName = newName.substring(dotIndex + projectPrefix.length()); 204 205 return newName; 206 } 207 208 212 protected String getProductDefinition(Product product, String productFile, 213 String dependProducts) { 214 String name = getName(product.getFullName()); 215 String productDef = "\t\t<product name=\"" + name + "\""; 217 218 productDef += " static=\"true\""; 219 productDef += " export=\"" + DEFAULT_EXPORT + "\" "; 220 221 if (product.getFile() != null) { 223 productDef += "file=\"" + productFile + "\" "; 224 } 225 226 if (product.getType() != null) 227 productDef += " type=\"" + product.getType() + "\""; 228 229 if ((dependProducts != null) && (dependProducts.length() != 0)) 230 productDef += "\n\t\t depends=\"" + dependProducts + "\""; 231 232 if (product.getAppName() != null) 233 productDef += " appName=\"" + product.getAppName() + "\""; 234 235 productDef +="/>\n"; 236 237 return productDef; 238 } 239 240 243 protected String getDefaultProductDefinition(String name, String dependProducts) { 244 String productDef = "\t\t<product name=\"" + getName(name) + "\""; 245 246 productDef += " static=\"true\""; 247 productDef += " export=\"" + DEFAULT_EXPORT + "\" "; 248 249 if ((dependProducts != null) && (dependProducts.length() != 0)) 250 productDef += "\n\t\t depends=\"" + dependProducts + "\""; 251 252 productDef +="/>\n"; 253 254 return productDef; 255 } 256 257 } 258 | Popular Tags |