All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.plugin.aspectj.XMLFormatter Maven / Gradle / Ivy

Go to download

PatternTesting is a testing framework that allows to automatically verify that Architecture/Design recommendations are implemented correctly in the code. It uses AOP and AspectJ to perform this feat.

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.plugin.aspectj;

import java.util.Enumeration;

import patterntesting.plugin.aspectj.XMLConstants;

/**
 * Formats a list of {@link AjcFileResult} objects into an XML string.
 *
 * @author Vincent Massol
 *
 * @version $Id: XMLFormatter.java,v 1.1 2007/05/19 22:32:59 oboehm Exp $
 */
public class XMLFormatter implements ResultFormatter, XMLConstants
{
    public String format(Enumeration ajcFileResults)
    {
        StringBuffer xml = new StringBuffer();
        
        generateHeader(xml);
        generatePatternTesting(xml, ajcFileResults);
        generateFooter(xml);
        
        return xml.toString();
    }    
    
    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 = 
               (AjcFileResult) ajcFileResults.nextElement();
           generateFile(buffer, fileResult);
       }
       buffer.append("");
    }

    
    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("");
    }
    private static void generateFile(StringBuffer buffer, AjcFileResult fileResult)
    {
        buffer.append("<" + FILE + " " + ATTR_NAME + "=\"" 
            + fileResult.getFileName() + "\">");
        Enumeration results = fileResult.getErrors();
        while (results.hasMoreElements())
        {
            AjcResult result = (AjcResult) results.nextElement();
            generateError(buffer, result);
        }
        Enumeration warnings = fileResult.getWarnings();
        while(warnings.hasMoreElements())
        {
           AjcResult result = (AjcResult) warnings.nextElement();
           generateWarning(buffer, result);
        }
        buffer.append("");
    }
    
    private static void generateWarning(StringBuffer buffer, AjcResult result)
    {
       buffer.append("<" + WARNING + " " + ATTR_LINE + "=\""
             + result.getLine() + "\">");
       buffer.append(xmlEncode(result.getErrorMessage()));
       buffer.append("");
    }
    private static void generateError(StringBuffer buffer, AjcResult result)
    {
        buffer.append("<" + ERROR + " " + ATTR_LINE + "=\"" 
            + result.getLine() + "\">");
        buffer.append(xmlEncode(result.getErrorMessage()));
        buffer.append("");
    }
    
    private static void generateFooter(StringBuffer buffer)
    {
        buffer.append("");
    }

    /**
     * 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