
patterntesting.tool.aspectj.XMLFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-tools
Show all versions of patterntesting-tools
PatternTesting Tools (patterntesting-tools) is the container for
tools around PatternTesting like the Ant extensions and Maven plugin.
The newest version!
/*
*========================================================================
*
* Copyright 2001-2004 Vincent Massol & Matt Smith.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*========================================================================
*/
package patterntesting.tool.aspectj;
import java.util.Enumeration;
/**
* Formats a list of {@link AjcFileResult} objects into an XML string.
*
* @author Vincent Massol
*
* @version $Id: XMLFormatter.java,v 1.6 2009/12/30 13:33:04 oboehm Exp $
*/
public class XMLFormatter implements ResultFormatter, XMLConstants {
/**
* @see patterntesting.tool.aspectj.ResultFormatter#format(java.util.Enumeration)
*/
public String format(Enumeration ajcFileResults) {
StringBuffer xml = new StringBuffer();
generateHeader(xml);
generatePatternTesting(xml, ajcFileResults);
generateFooter(xml);
return xml.toString();
}
/**
* @param ajcFileResults results to be formatted
* @param runtimeResults results to be formatted
* @return formatted result
*/
public String format(Enumeration ajcFileResults,
Enumeration runtimeResults) {
StringBuffer xml = new StringBuffer();
generateHeader(xml);
generatePatternTesting(xml, ajcFileResults);
generatePatternTestingRuntime(xml, runtimeResults);
generateFooter(xml);
return xml.toString();
}
private static void generateHeader(StringBuffer buffer) {
buffer.append("");
buffer.append("<" + ROOT + ">");
}
private static void generatePatternTesting(StringBuffer buffer,
Enumeration ajcFileResults) {
buffer.append("<" + PATTERNTESTING_REPORT + ">");
while (ajcFileResults.hasMoreElements()) {
AjcFileResult fileResult = ajcFileResults.nextElement();
generateFile(buffer, fileResult);
}
buffer.append("" + PATTERNTESTING_REPORT + ">");
}
private static void generatePatternTestingRuntime(StringBuffer buffer,
Enumeration ajcFileResults) {
buffer.append("<" + PATTERNTESTING_RUNTIME + ">");
while (ajcFileResults.hasMoreElements()) {
AjcFileResult fileResult = (AjcFileResult) ajcFileResults
.nextElement();
generateFile(buffer, fileResult);
}
buffer.append("" + PATTERNTESTING_RUNTIME + ">");
}
private static void generateFile(StringBuffer buffer,
AjcFileResult fileResult) {
buffer.append("<" + FILE + " " + ATTR_NAME + "=\""
+ fileResult.getFileName() + "\">");
Enumeration results = fileResult.getErrors();
while (results.hasMoreElements()) {
AjcResult result = results.nextElement();
generateError(buffer, result);
}
Enumeration warnings = fileResult.getWarnings();
while (warnings.hasMoreElements()) {
AjcResult result = warnings.nextElement();
generateWarning(buffer, result);
}
buffer.append("" + FILE + ">");
}
private static void generateWarning(StringBuffer buffer, AjcResult result) {
buffer.append("<" + WARNING + " " + ATTR_LINE + "=\""
+ result.getLine() + "\">");
buffer.append(xmlEncode(result.getErrorMessage()));
buffer.append("" + WARNING + ">");
}
private static void generateError(StringBuffer buffer, AjcResult result) {
buffer.append("<" + ERROR + " " + ATTR_LINE + "=\"" + result.getLine()
+ "\">");
buffer.append(xmlEncode(result.getErrorMessage()));
buffer.append("" + ERROR + ">");
}
private static void generateFooter(StringBuffer buffer) {
buffer.append("" + ROOT + ">");
}
/**
* Escapes reserved XML characters.
*
* @param theString
* the string to escape
* @return the escaped string
*/
private static String xmlEncode(String theString) {
String newString;
// It is important to replace the "&" first as the other replacements
// also introduces "&" chars ...
newString = replace(theString, '&', "&");
newString = replace(newString, '<', "<");
newString = replace(newString, '>', ">");
newString = replace(newString, '\"', """);
return newString;
}
/**
* Replaces a character in a string by a substring.
*
* @param theBaseString
* the base string in which to perform replacements
* @param theChar
* the char to look for
* @param theNewString
* the string with which to replace the char
* @return the string with replacements done or null if the input string was
* null
*/
public static String replace(String theBaseString, char theChar,
String theNewString) {
if (theBaseString == null) {
return null;
}
final int len = theBaseString.length() - 1;
int pos = -1;
while ((pos = theBaseString.indexOf(theChar, pos + 1)) > -1) {
if (pos == 0) {
final String after = theBaseString.substring(1);
theBaseString = theNewString + after;
} else if (pos == len) {
final String before = theBaseString.substring(0, pos);
theBaseString = before + theNewString;
} else {
final String before = theBaseString.substring(0, pos);
final String after = theBaseString.substring(pos + 1);
theBaseString = before + theNewString + after;
}
}
return theBaseString;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy