xmlresultaggregator.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 378 行 · 第 1/2 页
JAVA
378 行
Enumeration enum = transformers.elements();
while (enum.hasMoreElements()) {
AggregateTransformer transformer =
(AggregateTransformer) enum.nextElement();
transformer.setXmlDocument(rootElement.getOwnerDocument());
transformer.transform();
}
// After finishing making reports, check if we should halt build.
if (haltUnlessSuccess && errorFlag[0]) {
throw new BuildException("Did not pass all tests");
}
}
/**
* Get the full destination file where to write the result. It is made of
* the <tt>todir</tt> and <tt>tofile</tt> attributes.
* @return the destination file where should be written the result file.
*/
protected File getDestinationFile(){
if (toFile == null){
toFile = DEFAULT_FILENAME;
}
if (toDir == null){
toDir = project.resolveFile(DEFAULT_DIR);
}
return new File(toDir, toFile);
}
/**
* Get all <code>.xml</code> files in the fileset.
*
* @return all files in the fileset that end with a '.xml'.
*/
protected File[] getFiles() {
Vector v = new Vector();
final int size = filesets.size();
for (int i = 0; i < size; i++) {
FileSet fs = (FileSet) filesets.elementAt(i);
DirectoryScanner ds = fs.getDirectoryScanner(project);
ds.scan();
String[] f = ds.getIncludedFiles();
for (int j = 0; j < f.length; j++) {
String pathname = f[j];
if (pathname.endsWith(".xml")) {
File file = new File(ds.getBasedir(), pathname);
file = project.resolveFile(file.getPath());
v.addElement(file);
}
}
}
File[] files = new File[v.size()];
v.copyInto(files);
return files;
}
//----- from now, the methods are all related to DOM tree manipulation
/**
* Write the DOM tree to a file.
* @param doc the XML document to dump to disk.
* @param file the filename to write the document to. Should obviouslly be a .xml file.
* @throws IOException thrown if there is an error while writing the content.
*/
protected void writeDOMTree(Document doc, File file) throws IOException {
OutputStream out = null;
PrintWriter wri = null;
try {
out = new FileOutputStream(file);
wri = new PrintWriter(new OutputStreamWriter(out, "UTF8"));
wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
(new DOMElementWriter()).write(doc.getDocumentElement(), wri, 0, " ");
wri.flush();
// writers do not throw exceptions, so check for them.
if (wri.checkError()){
throw new IOException("Error while writing DOM content");
}
} finally {
if (wri != null) {
wri.close();
out = null;
}
if (out != null) {
out.close();
}
}
}
/**
* <p> Create a DOM tree.
* Has 'testsuites' as firstchild and aggregates all
* testsuite results that exists in the base directory.
* @param errorFlag a one element array, mark it true if any test fails or hits
* an error
* @return the root element of DOM tree that aggregates all testsuites.
*/
protected Element createDocument(boolean[] errorFlag) {
// create the dom tree
DocumentBuilder builder = getDocumentBuilder();
Document doc = builder.newDocument();
Element rootElement = doc.createElement(TESTSUITES);
doc.appendChild(rootElement);
// get all files and add them to the document
File[] files = getFiles();
for (int i = 0; i < files.length; i++) {
try {
log("Parsing file: '" + files[i] + "'", Project.MSG_VERBOSE);
//XXX there seems to be a bug in xerces 1.3.0 that doesn't like file object
// will investigate later. It does not use the given directory but
// the vm dir instead ? Works fine with crimson.
Document testsuiteDoc
= builder.parse("file:///" + files[i].getAbsolutePath());
Element elem = testsuiteDoc.getDocumentElement();
// make sure that this is REALLY a testsuite.
if (TESTSUITE.equals(elem.getNodeName())) {
// If we hit an error or failure, set errorFlag;
int failures = -1;
int errors = -1;
try {
failures = Integer.parseInt(elem.getAttribute("failures"));
errors = Integer.parseInt(elem.getAttribute("errors"));
}
catch (RuntimeException e) {
log("Could not parse errors and failures in " + files[i],Project.MSG_ERR);
errorFlag[0] = true;
}
if (failures != 0 || errors != 0) {
errorFlag[0] = true;
}
addTestSuite(rootElement, elem);
} else {
// issue a warning.
log("the file " + files[i] + " is not a valid testsuite XML document", Project.MSG_WARN);
}
} catch (SAXException e){
// a testcase might have failed and write a zero-length document,
// It has already failed, but hey.... mm. just put a warning
log("The file " + files[i] + " is not a valid XML document. It is possibly corrupted.", Project.MSG_WARN);
log(StringUtils.getStackTrace(e), Project.MSG_DEBUG);
} catch (IOException e){
log("Error while accessing file " + files[i] + ": " + e.getMessage(), Project.MSG_ERR);
}
}
return rootElement;
}
/**
* <p> Add a new testsuite node to the document.
* The main difference is that it
* split the previous fully qualified name into a package and a name.
* <p> For example: <tt>org.apache.Whatever</tt> will be split into
* <tt>org.apache</tt> and <tt>Whatever</tt>.
* @param root the root element to which the <tt>testsuite</tt> node should
* be appended.
* @param testsuite the element to append to the given root. It will slightly
* modify the original node to change the name attribute and add
* a package one.
*/
protected void addTestSuite(Element root, Element testsuite){
String fullclassname = testsuite.getAttribute(ATTR_NAME);
int pos = fullclassname.lastIndexOf('.');
// a missing . might imply no package at all. Don't get fooled.
String pkgName = (pos == -1) ? "" : fullclassname.substring(0, pos);
String classname = (pos == -1) ? fullclassname : fullclassname.substring(pos + 1);
Element copy = (Element) DOMUtil.importNode(root, testsuite);
// modify the name attribute and set the package
copy.setAttribute(ATTR_NAME, classname);
copy.setAttribute(ATTR_PACKAGE, pkgName);
}
/**
* Create a new document builder. Will issue an <tt>ExceptionInitializerError</tt>
* if something is going wrong. It is fatal anyway.
* @todo factorize this somewhere else. It is duplicated code.
* @return a new document builder to create a DOM
*/
private static DocumentBuilder getDocumentBuilder() {
try {
return DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (Exception exc) {
throw new ExceptionInInitializerError(exc);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?