You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by ju...@apache.org on 2001/05/04 16:52:00 UTC

cvs commit: jakarta-slide/src/webdav/server/org/apache/slide/webdav/logger XMLTestCaseGenerator.java

juergen     01/05/04 07:51:59

  Modified:    src/webdav/server/org/apache/slide/webdav/logger
                        XMLTestCaseGenerator.java
  Log:
  building the output is now Jdom based (instead of strings). This was necessary caused by nested cdata sections
  
  Revision  Changes    Path
  1.3       +71 -153   jakarta-slide/src/webdav/server/org/apache/slide/webdav/logger/XMLTestCaseGenerator.java
  
  Index: XMLTestCaseGenerator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/logger/XMLTestCaseGenerator.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- XMLTestCaseGenerator.java	2001/04/20 17:51:14	1.2
  +++ XMLTestCaseGenerator.java	2001/05/04 14:51:45	1.3
  @@ -57,29 +57,20 @@
    *
    */
   
  -
  + 
   package org.apache.slide.webdav.logger;
   
   import java.util.Enumeration;
  -import java.io.StringReader;
  +import java.util.Iterator;
  +import java.util.Vector;
   import java.io.IOException;
  -import java.io.StringWriter;
  -
  -import org.w3c.dom.Document;
  -import org.xml.sax.InputSource;
  -import org.xml.sax.SAXException;
   
  -import javax.xml.parsers.DocumentBuilderFactory;
  -import javax.xml.parsers.DocumentBuilder;
  -import javax.xml.parsers.ParserConfigurationException;
  -
   import org.apache.util.WebdavStatus;
  -import org.apache.util.DOMWriter;
   
   import org.jdom.Element;
   import org.jdom.JDOMException;
  -import org.jdom.input.SAXBuilder;
   import org.jdom.output.XMLOutputter;
  +import org.jdom.Document;
   
    
   /**
  @@ -92,18 +83,16 @@
   
   public class XMLTestCaseGenerator {
       
  -    /**
  -     * constants
  -     */
  -    private static final String           CRLF = System.getProperty("line.separator");
  -    private static final String XML_MASK_BEGIN = "<![CDATA[";
  -    private static final String   XML_MASK_END = "]]>";
  -    private static final String SEPARATOR_LINE = "CRLF+<! --------------------------- END OF COMMAND ------------------------ >+CRLF";
  +
  +    /** The XML outputter */
  +    private XMLOutputter xmlOut = new XMLOutputter( "  ", true ); // indent: 2 spaces, newlines=true
   
       
       private XHttpServletRequestFacade request;
       private XHttpServletResponseFacade response;
       
  +    private Element root = new Element( "test" );
  +    private Document doc = new Document( root );
       
           
       /*
  @@ -117,20 +106,8 @@
       public String getThreadName() {
           return threadName;
       }
  -    
  -    
  -    /**
  -     * Use JAXP
  -     */
  -    private boolean useJAXP = false;
  -    
  -    public void setUseJAXP(boolean useJAXP) {
  -        this.useJAXP = useJAXP;
  -    }
  -    public boolean isUseJAXP() {
  -        return useJAXP;
  -    }
       
  +        
       
       /*
        * Constructs an XMLTestCaseGenerator object.
  @@ -144,163 +121,107 @@
        * this method writes the data as XML.
        */
       public String toString() {
  -        String result = new String(printXMLbody());
  -        if (useJAXP) {
  -            try {
  -                DocumentBuilder documentBuilder =
  -                    DocumentBuilderFactory.newInstance().newDocumentBuilder();
  -                Document document =
  -                    documentBuilder.parse(new InputSource
  -                        (new StringReader(result)));
  -                StringWriter writer = new StringWriter();
  -                (new DOMWriter(writer, true)).print(document);
  -                result = writer.toString();
  -            } catch(ParserConfigurationException e) {
  -                e.printStackTrace();
  -            } catch (SAXException e) {
  -                e.printStackTrace();
  -            } catch (IOException e) {
  -                e.printStackTrace();
  -            }
  -        } else {
  -            try {
  -                Element elem = 
  -                    (new SAXBuilder(false).build(new StringReader(result))
  -                        .getRootElement());
  -                result = new XMLOutputter(" ", true).outputString(elem);
  -            } catch (JDOMException e) {
  -                e.printStackTrace();
  -            } catch (Exception e) {
  -                e.printStackTrace();
  -            }
  +        String result = "";
  +            
  +        root.addContent( printXMLstep() );
  +        try {
  +            result = xmlOut.outputString( doc.getRootElement().getChild("step") );
  +        } catch ( IOException e ) {
  +            e.printStackTrace();
           }
           return result;
       }
   
       /*
  -     * this method prints the XML test attribute.
  -     */
  -    private String printXMLbody() {
  -        String result;
  -        result = printXMLstep();
  -        return result;
  -    }
  -
  -    /*
        * this method prints the XML step attribute.
        */
  -    private String printXMLstep() {
  -        String result = new String("<step executeIn=\"" + getThreadName() + "\">");
  -        result = result + printXMLrequest();
  -        result = result + printXMLresponse();
  -        result = result + "</step>";
  -        return result;
  +    private Element printXMLstep() {
  +        Element stepElem = new Element( "step" );
  +        stepElem.addAttribute( "executeIn", getThreadName() );
  +        stepElem.addContent( printXMLrequest() );
  +        stepElem.addContent( printXMLresponse() );
  +        return stepElem;
       }
       /*
        * this method prints the XML request attribute.
        */
  -    private String printXMLrequest() {
  -        String result = new String("<request>");
  -        result = result + printXMLrequestCommand();
  -        result = result + printXMLrequestHeaders();
  -        result = result + printXMLrequestBody();
  -        result = result + "</request>";
  -        return result;
  +    private Element printXMLrequest() {
  +        Element stepElem = new Element( "request" );
  +        stepElem.addContent( printXMLrequestCommand() );
  +        Iterator it = printXMLrequestHeaders();
  +        while ( it.hasNext() ) {
  +            stepElem.addContent( (Element)it.next() );
  +        }
  +        stepElem.addContent( printXMLrequestBody() );
  +        return stepElem;
       }
                                     
       /*
        * this method prints the XML request attribute.
        */
  -    private String printXMLresponse() {
  -        String result = new String("<response>");
  -        result = result + printXMLresponseCommand();
  -        result = result + printXMLresponseHeaders();
  -        result = result + printXMLresponseBody();
  -        result = result + "</response>";
  -        return result;
  +    private Element printXMLresponse() {
  +        Element respElem = new Element( "response" );
  +        respElem.addContent( printXMLresponseCommand() );
  +        Iterator it = printXMLresponseHeaders();
  +        while ( it.hasNext() ) {
  +            respElem.addContent( (Element)it.next() );
  +        }
  +        respElem.addContent( printXMLresponseBody() );
  +        return respElem;
       }
                                     
       /*
        * this method prints the XML request command attribute.
        */
  -    private String printXMLrequestCommand() {
  -        String result = new String("<command>");
  -        result = result + request.getMethod() + " " + request.getRequestURI() + " " + getProtocol();
  -        result = result + "</command>";
  -        return result;
  +    private Element printXMLrequestCommand() {
  +        Element reqComElem = new Element( "command" );
  +        reqComElem.addContent( request.getMethod() + " " + request.getRequestURI() + " " + getProtocol() );
  +        return reqComElem;
       }
                                     
                                     
       /*
        * this method prints the XML request header attributes.
        */
  -    private String printXMLrequestHeaders() {
  -        String result = new String();
  +    private Iterator printXMLrequestHeaders() {
  +        Vector vector = new Vector();
           Enumeration e = request.getHeaderNames();
           if ( e != null ) {
               while ( e.hasMoreElements() ) {
                   String headerName = (String)e.nextElement();
                   String headerValue = request.getHeader(headerName);
  -                result = result + "<header>" + headerName + ": " + escapeXMLcharacters(headerValue) + "</header>";
  +                Element elem = new Element( "header" );
  +                elem.addContent( headerName + ": " + headerValue );
  +                vector.add( elem );
               }
  -        }
  -        return result;
  -    }
  -                                  
  -    /*
  -     * this method converts a string containing XML characters
  -     * to a string containing the equvivalent escape characters
  -     */
  -    private String escapeXMLcharacters(String input){
  -        String result = input;
  -        result = escapeXMLcharacters(result, "<", "&lt;" );
  -        result = escapeXMLcharacters(result, ">", "&gt;" );
  -        return result;
  -    }
  -                                  
  -    /*
  -     * this method converts a string containing XML characters
  -     * to a string containing the equvivalent escape characters
  -     */
  -    private String escapeXMLcharacters(String input, String pattern, String replacement){
  -        String result = input;
  -        int position = result.indexOf(pattern);
  -        while (position != (-1)) {
  -            result = result.substring(0, position) +
  -                     replacement +
  -                     result.substring(position+pattern.length(), result.length());
  -            position = result.indexOf(pattern);
           }
  -        return result;
  +        return vector.iterator();
       }
                                     
       /*
        * this method prints the XML request body attribute.
        */
  -    private String printXMLrequestBody(){
  -        String result = new String("<body>");
  -        result = result + XML_MASK_BEGIN;
  +    private Element printXMLrequestBody(){
  +        Element bodyElem = new Element( "body" );
           try {
  -            result = result + request.getRequestBody();
  +            bodyElem.addContent( request.getRequestBody() );
           }
           catch (IOException e) {
               e.printStackTrace();
           }
  -        result = result + XML_MASK_END;
  -        result = result + "</body>";
  -        return result;
  +        return bodyElem;
       }
   
       /*
        * this method prints the XML response command attribute.
        */
  -    private String printXMLresponseCommand() {
  -        String result = new String("<command>");
  -        result = result + getProtocol() + " " +
  +    private Element printXMLresponseCommand() {
  +        Element respComElem = new Element( "command" );
  +        respComElem.addContent(
  +            getProtocol() + " " +
               response.getStatus() + " " +
  -            WebdavStatus.getStatusText(response.getStatus());
  -        result = result + "</command>";
  -        return result;
  +            WebdavStatus.getStatusText(response.getStatus()));
  +        return respComElem;
       }
           
       /*
  @@ -317,34 +238,31 @@
       /*
        * this method prints the XML response header attributes.
        */
  -    private String printXMLresponseHeaders() {
  +    private Iterator printXMLresponseHeaders() {
  +        Vector vector = new Vector();
           XResponseHeader respHeader;
  -        String result = new String();
           Enumeration e = response.getResponseHeaders();
           if ( e != null ) {
               while ( e.hasMoreElements() ) {
  -                result = result + "<header>";
  -                result = result + ((XResponseHeader)e.nextElement()).toString();
  -                result = result + "</header>";
  +                Element elem = new Element( "header" );
  +                elem.addContent( ((XResponseHeader)e.nextElement()).toString() );
  +                vector.add( elem );
               }
           }
  -        return result;
  +        return vector.iterator();
       }
                                     
       /*
        * this method prints the XML response body attribute.
        */
  -    private String printXMLresponseBody() {
  -        String result = new String("<body>");
  -        result = result + XML_MASK_BEGIN;
  +    private Element printXMLresponseBody() {
  +        Element bodyElem = new Element( "body" );
           try {
  -            result = result + response.getResponseBody();
  +            bodyElem.addContent( response.getResponseBody() );
           } catch ( IOException e ) {
               e.printStackTrace();
           }
  -        result = result + XML_MASK_END;
  -        result = result + "</body>";
  -        return result;
  +        return bodyElem;
       }
   
   }