1 37 package net.sourceforge.cruisecontrol.buildloggers; 38 39 import net.sourceforge.cruisecontrol.BuildLogger; 40 import net.sourceforge.cruisecontrol.CruiseControlException; 41 import net.sourceforge.cruisecontrol.util.PruneElementFilter; 42 import net.sourceforge.cruisecontrol.util.ValidationHelper; 43 44 import org.jdom.Element; 45 import org.jdom.JDOMException; 46 import org.jdom.input.SAXBuilder; 47 import org.apache.log4j.Logger; 48 import org.apache.oro.io.GlobFilenameFilter; 49 import org.apache.oro.text.MalformedCachePatternException; 50 51 import java.io.File ; 52 import java.io.FileFilter ; 53 import java.io.IOException ; 54 import java.util.Arrays ; 55 56 63 public class MergeLogger implements BuildLogger { 64 65 private static final Logger LOG = Logger.getLogger(MergeLogger.class); 66 67 private String file; 68 private String dir; 69 private String pattern = "*.xml"; 70 private GlobFilenameFilter fileNameFilter; 71 private boolean removeProperties = true; 72 73 public void setFile(String file) { 74 this.file = file; 75 } 76 77 public void setDir(String dir) { 78 this.dir = dir; 79 } 80 81 87 public void setPattern(String pattern) { 88 this.pattern = pattern; 89 this.fileNameFilter = null; 90 } 91 92 public void validate() throws CruiseControlException { 93 ValidationHelper.assertFalse(file == null && dir == null, 94 "one of file or dir are required attributes"); 95 ValidationHelper.assertFalse(file != null && dir != null, 96 "only one of file or dir may be specified"); 97 98 ValidationHelper.assertFalse(dir != null && pattern == null, 99 "no file pattern was specified"); 100 compilePattern(); 101 } 102 103 108 private void compilePattern() throws CruiseControlException { 109 if (fileNameFilter == null && pattern != null) { 110 try { 111 fileNameFilter = new GlobFilenameFilter(pattern); 112 } catch (MalformedCachePatternException e) { 113 ValidationHelper.fail("Invalid filename pattern " + pattern, e); 114 } 115 } 116 } 117 118 121 public void log(Element buildLog) throws CruiseControlException { 122 String nextLogFilename = ((file != null) ? file : dir); 123 mergeFile(new File (nextLogFilename), buildLog); 124 } 125 126 131 private void mergeFile(File nextLogFile, Element buildLog) { 132 133 if (!nextLogFile.exists()) { 134 LOG.info(nextLogFile.toString() + " does not exist. Skipping ..."); 135 } else if (nextLogFile.isDirectory()) { 136 File [] children = nextLogFile.listFiles(new FileFilter () { 137 public boolean accept(File file) { 138 return file.isDirectory() 139 || fileNameFilter.accept(file); 140 } 141 }); 142 Arrays.sort(children); 143 for (int j = 0; j < children.length; j++) { 144 mergeFile(children[j], buildLog); 145 } 146 } else { 147 Element auxLogElement = getElement(nextLogFile); 148 if (auxLogElement != null) { 149 buildLog.addContent(auxLogElement.detach()); 150 } 151 } 152 } 153 154 160 Element getElement(File xmlFile) { 161 try { 162 SAXBuilder builder = 163 new SAXBuilder("org.apache.xerces.parsers.SAXParser"); 164 if (removeProperties) { 165 builder.setXMLFilter(new PruneElementFilter("properties")); 166 } 167 return builder.build(xmlFile).getRootElement(); 168 } catch (JDOMException e) { 169 LOG.warn("Could not read log: " + xmlFile + ". Skipping...", e); 170 } catch (IOException e) { 171 LOG.warn("Could not read log: " + xmlFile + ". Skipping...", e); 172 } 173 174 return null; 175 } 176 177 public void setRemoveProperties(boolean b) { 178 removeProperties = b; 179 } 180 } 181 | Popular Tags |