You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by st...@apache.org on 2012/10/21 15:42:34 UTC

svn commit: r1400646 - in /maven/shared/trunk/maven-shared-utils/src: main/java/org/apache/maven/shared/utils/ main/java/org/apache/maven/shared/utils/xml/ test/java/org/apache/maven/shared/utils/xml/

Author: struberg
Date: Sun Oct 21 13:42:33 2012
New Revision: 1400646

URL: http://svn.apache.org/viewvc?rev=1400646&view=rev
Log:
MSHARED-236 add PrettyPrintXmlWriter

Added:
    maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java   (with props)
    maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java   (with props)
    maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java   (with props)
    maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/
    maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java   (with props)
Modified:
    maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/Os.java

Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/Os.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/Os.java?rev=1400646&r1=1400645&r2=1400646&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/Os.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/Os.java Sun Oct 21 13:42:33 2012
@@ -52,6 +52,11 @@ public class Os
 
     public static final String PATH_SEP = System.getProperty( "path.separator" );
 
+    /**
+     * system line separator , e.g. "\n" on unixoid systems and "\r\n" on Windows
+     */
+    public static final String LINE_SEP = System.getProperty( "line.separator" );
+
     public static final String OS_FAMILY = getOsFamily();
 
     // store the valid families

Added: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java?rev=1400646&view=auto
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java (added)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java Sun Oct 21 13:42:33 2012
@@ -0,0 +1,331 @@
+package org.apache.maven.shared.utils.xml;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import org.apache.maven.shared.utils.Os;
+
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.util.LinkedList;
+
+
+/**
+ * XMLWriter with nice indention
+ */
+public class PrettyPrintXMLWriter implements XMLWriter
+{
+    private PrintWriter writer;
+
+    private LinkedList elementStack = new LinkedList();
+
+    private boolean processingElement = false;
+    private boolean documentStarted = false;
+    private boolean endOnSameLine = false;
+
+    private int depth = 0;
+
+    private String lineIndent;
+
+    private String lineSeparator;
+
+    private String encoding;
+
+    private String docType;
+
+
+
+    /**
+     * @param writer not null
+     * @param lineIndent could be null, but the normal way is some spaces.
+     */
+    public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent )
+    {
+        this( writer, lineIndent, null, null );
+    }
+
+    /**
+     * @param writer not null
+     * @param lineIndent could be null, but the normal way is some spaces.
+     */
+    public PrettyPrintXMLWriter( Writer writer, String lineIndent )
+    {
+        this( new PrintWriter( writer ), lineIndent );
+    }
+
+    /**
+     * @param writer not null
+     */
+    public PrettyPrintXMLWriter( PrintWriter writer )
+    {
+        this( writer, null, null );
+    }
+
+    /**
+     * @param writer not null
+     */
+    public PrettyPrintXMLWriter( Writer writer )
+    {
+        this( new PrintWriter( writer ) );
+    }
+
+    /**
+     * @param writer not null
+     * @param lineIndent could be null, but the normal way is some spaces.
+     * @param encoding could be null or invalid.
+     * @param doctype could be null.
+     */
+    public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent, String encoding, String doctype )
+    {
+        this( writer, lineIndent, Os.LINE_SEP, encoding, doctype );
+    }
+
+    /**
+     * @param writer not null
+     * @param lineIndent could be null, but the normal way is some spaces.
+     * @param encoding could be null or invalid.
+     * @param doctype could be null.
+     */
+    public PrettyPrintXMLWriter( Writer writer, String lineIndent, String encoding, String doctype )
+    {
+        this( new PrintWriter( writer ), lineIndent, encoding, doctype );
+    }
+
+    /**
+     * @param writer not null
+     * @param encoding could be null or invalid.
+     * @param doctype could be null.
+     */
+    public PrettyPrintXMLWriter( PrintWriter writer, String encoding, String doctype )
+    {
+        this( writer, "  ", encoding, doctype );
+    }
+
+    /**
+     * @param writer not null
+     * @param encoding could be null or invalid.
+     * @param doctype could be null.
+     */
+    public PrettyPrintXMLWriter( Writer writer, String encoding, String doctype )
+    {
+        this( new PrintWriter( writer ), encoding, doctype );
+    }
+
+    /**
+     * @param writer not null
+     * @param lineIndent could be null, but the normal way is some spaces.
+     * @param lineSeparator could be null, but the normal way is valid line separator
+     * @param encoding could be null or the encoding to use.
+     * @param doctype could be null.
+     */
+    public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent, String lineSeparator, String encoding, String doctype )
+    {
+        this.writer = writer;
+        this.lineIndent = lineIndent;
+        this.lineSeparator = lineSeparator;
+        this.encoding = encoding;
+        this.docType = doctype;
+
+        depth = 0;
+    }
+
+    public void addAttribute( String key, String value )
+    {
+        if ( !processingElement )
+        {
+            throw new IllegalStateException( "currently processing no element" );
+        }
+
+        writer.write( " " );
+        writer.write( key );
+        writer.write( "=" );
+        writer.write( XMLEncode.xmlEncodeTextForAttribute( value, '"' ) );
+    }
+
+    public void setEncoding( String encoding )
+    {
+        if (documentStarted)
+        {
+            throw new IllegalStateException( "Document headers already written!" );
+        }
+
+        this.encoding = encoding;
+    }
+
+    public void setDocType( String docType )
+    {
+        if (documentStarted)
+        {
+            throw new IllegalStateException( "Document headers already written!" );
+        }
+
+        this.docType = docType;
+    }
+
+    public void setLineSeparator( String lineSeparator )
+    {
+        if (documentStarted)
+        {
+            throw new IllegalStateException( "Document headers already written!" );
+        }
+
+        this.lineSeparator = lineSeparator;
+    }
+
+    public void setLineIndenter( String lineIndent )
+    {
+        if (documentStarted)
+        {
+            throw new IllegalStateException( "Document headers already written!" );
+        }
+
+        this.lineIndent = lineIndent;
+    }
+
+
+    public void startElement( String elementName )
+    {
+        boolean firstLine = ensureDocumentStarted();
+
+        completePreviouslyOpenedElement();
+
+        if ( !firstLine)
+        {
+            newLine();
+        }
+
+        writer.write( "<" );
+        writer.write( elementName );
+
+        processingElement = true;
+        depth++;
+
+        elementStack.addLast( elementName );
+    }
+
+
+    public void writeText( String text )
+    {
+        ensureDocumentStarted();
+
+        completePreviouslyOpenedElement();
+
+        writer.write( XMLEncode.xmlEncodeText( text ) );
+
+        endOnSameLine = true;
+    }
+
+    public void writeMarkup( String markup )
+    {
+        ensureDocumentStarted();
+
+        completePreviouslyOpenedElement();
+
+        newLine();
+
+        writer.write( markup );
+    }
+
+    public void endElement()
+    {
+        depth--;
+
+        if ( processingElement )
+        {
+            // this means we don't have any content yet so we just add a />
+            writer.write( "/>" );
+
+            elementStack.removeLast();
+            processingElement = false;
+        }
+        else
+        {
+            if ( !endOnSameLine )
+            {
+                newLine();
+            }
+
+            // otherwise we need a full closing tag for that element
+            writer.write( "</" + elementStack.removeLast() + ">" );
+        }
+
+        endOnSameLine = false;
+    }
+
+    /**
+     * Write the documents if not already done.
+     * @return <code>true</code> if the document headers have freshly been written.
+     */
+    private boolean ensureDocumentStarted()
+    {
+        if (!documentStarted)
+        {
+            if ( docType != null || encoding != null )
+            {
+                writeDocumentHeader();
+            }
+
+            documentStarted = true;
+
+            return true;
+        }
+
+        return false;
+    }
+
+    private void writeDocumentHeader()
+    {
+        writer.write( "<?xml version=\"1.0\"" );
+
+        if ( encoding != null )
+        {
+            writer.write( " encoding=\"" + encoding + "\"" );
+        }
+
+        writer.write( "?>" );
+
+        if ( docType != null )
+        {
+            newLine();
+            writer.write( "<!DOCTYPE " + docType + ">" );
+        }
+    }
+
+    private void newLine()
+    {
+        writer.write( lineSeparator );
+
+        for ( int i = 0; i < depth; i++ )
+        {
+            writer.write( lineIndent );
+        }
+    }
+
+    private void completePreviouslyOpenedElement()
+    {
+        if ( processingElement )
+        {
+            writer.write( ">" );
+            processingElement = false;
+        }
+    }
+
+
+}

Propchange: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java?rev=1400646&view=auto
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java (added)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java Sun Oct 21 13:42:33 2012
@@ -0,0 +1,365 @@
+package org.apache.maven.shared.utils.xml;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+/**
+ * Collection of XML encoding/decoding helpers. <br>
+ * This is all about the special characters &amp; and &lt;, and for attributes
+ * &quot; and &apos;. These must be encoded/decoded from/to XML.
+ */
+public final class XMLEncode
+{
+
+    private final static int CDATA_BLOCK_THRESHOLD_LENGTH = 12;
+    private final static char DEFAULT_QUOTE_CHAR = '"';
+
+    /**
+     * Checks if this text purely consists of the white space characters
+     * ' ',  TAB, NEWLINE.
+     */
+    public final static boolean isWhiteSpace( String text )
+    {
+        for( int i = 0; i < text.length(); i++ )
+        {
+            char c = text.charAt( i );
+            if( Character.isWhitespace( c ) )
+            {
+                continue;
+            }
+            else
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Makes any text fit into XML attributes.
+     */
+    public final static String xmlEncodeTextForAttribute( String text, char quoteChar )
+    {
+        if( text == null )
+        {
+            return null;
+        }
+        return xmlEncodeTextAsPCDATA( text, true, quoteChar );
+    }
+
+    /**
+     * Encodes text as XML in the most suitable way, either CDATA block or PCDATA.
+     */
+    public final static String xmlEncodeText( String text )
+    {
+        if( text == null )
+        {
+            return null;
+        }
+        if( !needsEncoding( text ) )
+        {
+            return text;
+        }
+        else
+        {
+            // only encode as cdata if is is longer than CDATA block overhead:
+            if( text.length() > CDATA_BLOCK_THRESHOLD_LENGTH )
+            {
+                String cdata = xmlEncodeTextAsCDATABlock( text );
+                if( cdata != null )
+                {
+                    return cdata;
+                }
+            }
+        }
+        // if every thing else fails, do it the save way...
+        return xmlEncodeTextAsPCDATA( text );
+    }
+
+    /**
+     * Encodes any text as PCDATA.
+     */
+    public final static String xmlEncodeTextAsPCDATA( String text )
+    {
+        if( text == null )
+        {
+            return null;
+        }
+        return xmlEncodeTextAsPCDATA( text, false );
+    }
+
+    /**
+     * Encodes any text as PCDATA.
+     *
+     * @param forAttribute if you want
+     *                     quotes and apostrophes specially treated for attributes
+     */
+    public final static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute )
+    {
+        return xmlEncodeTextAsPCDATA( text, forAttribute, DEFAULT_QUOTE_CHAR );
+    }
+
+    /**
+     * Encodes any text as PCDATA.
+     *
+     * @param forAttribute if you want
+     *                     quotes and apostrophes specially treated for attributes
+     * @param quoteChar    if this is for attributes this <code>char</code> is used to quote the attribute value
+     */
+    public final static String xmlEncodeTextAsPCDATA( String text, boolean forAttribute, char quoteChar )
+    {
+        if( text == null )
+        {
+            return null;
+        }
+        char c;
+        StringBuffer n = new StringBuffer( text.length() * 2 );
+        for( int i = 0; i < text.length(); i++ )
+        {
+            c = text.charAt( i );
+            switch( c )
+            {
+                case '&':
+                    n.append( "&amp;" );
+                    break;
+                case '<':
+                    n.append( "&lt;" );
+                    break;
+                case '>': // FIX for sourceforge bug #802520 ("]]>" needs encoding)
+                    n.append( "&gt;" );
+                    break;
+                case '"':
+                    if( forAttribute )
+                    {
+                        n.append( "&quot;" );
+                    }
+                    else
+                    {
+                        n.append( c );
+                    }
+                    break;
+                case '\'':
+                    if( forAttribute )
+                    {
+                        n.append( "&apos;" );
+                    }
+                    else
+                    {
+                        n.append( c );
+                    }
+                    break;
+                case '\r':
+                    if ( forAttribute )
+                    {
+                        if ( i == text.length() || text.charAt( i + 1 ) != '\n')
+                        {
+                            n.append( "&#13;" );
+                        }
+                    }
+                    else
+                    {
+                        n.append( c );
+                    }
+                    // but skip the \r in \r\n
+
+
+                    break;
+                case '\n':
+                    if ( forAttribute )
+                    {
+                        n.append( "&#10;" );
+                    }
+                    break;
+
+                default:
+                {
+                    n.append( c );
+                    break;
+                }
+            }
+        }
+
+        if( forAttribute )
+        {
+            n.append( quoteChar );
+            n.insert( 0, quoteChar );
+        }
+
+        return n.toString();
+    }
+
+    /**
+     * Returns string as CDATA block if possible, otherwise null.
+     */
+    public final static String xmlEncodeTextAsCDATABlock( String text )
+    {
+        if( text == null )
+        {
+            return null;
+        }
+        if( isCompatibleWithCDATABlock( text ) )
+        {
+            return "<![CDATA[" + text + "]]>";
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+    /**
+     * Checks if this text needs encoding in order to be represented in XML.
+     */
+    public final static boolean needsEncoding( String text )
+    {
+        return needsEncoding( text, false );
+    }
+
+    /**
+     * Checks if this text needs encoding in order to be represented in XML.
+     * <p/>
+     * Set <code>checkForAttr</code> if you want to check for storability in
+     * an attribute.
+     */
+    public final static boolean needsEncoding( String data, boolean checkForAttr )
+    {
+        if( data == null )
+        {
+            return false;
+        }
+        char c;
+        for( int i = 0; i < data.length(); i++ )
+        {
+            c = data.charAt( i );
+            if( c == '&' || c == '<' || (checkForAttr && (c == '"' || c == '\'')) )
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Can this text be stored into a CDATA block?
+     */
+    public final static boolean isCompatibleWithCDATABlock( String text )
+    {
+        if( text == null )
+        {
+            return false;
+        }
+        return (text.indexOf( "]]>" ) == -1);
+    }
+
+    /**
+     * Make CDATA out of possibly encoded PCDATA. <br>
+     * E.g. make '&amp;' out of '&amp;amp;'
+     */
+    public final static String xmlDecodeTextToCDATA( String pcdata )
+    {
+        if( pcdata == null )
+        {
+            return null;
+        }
+        char c, c1, c2, c3, c4, c5;
+        StringBuffer n = new StringBuffer( pcdata.length() );
+        for( int i = 0; i < pcdata.length(); i++ )
+        {
+            c = pcdata.charAt( i );
+            if( c == '&' )
+            {
+                c1 = lookAhead( 1, i, pcdata );
+                c2 = lookAhead( 2, i, pcdata );
+                c3 = lookAhead( 3, i, pcdata );
+                c4 = lookAhead( 4, i, pcdata );
+                c5 = lookAhead( 5, i, pcdata );
+
+                if( c1 == 'a' && c2 == 'm' && c3 == 'p' && c4 == ';' )
+                {
+                    n.append( "&" );
+                    i += 4;
+                }
+                else if( c1 == 'l' && c2 == 't' && c3 == ';' )
+                {
+                    n.append( "<" );
+                    i += 3;
+                }
+                else if( c1 == 'g' && c2 == 't' && c3 == ';' )
+                {
+                    n.append( ">" );
+                    i += 3;
+                }
+                else if( c1 == 'q' && c2 == 'u' && c3 == 'o' && c4 == 't' && c5 == ';' )
+                {
+                    n.append( "\"" );
+                    i += 5;
+                }
+                else if( c1 == 'a' && c2 == 'p' && c3 == 'o' && c4 == 's' && c5 == ';' )
+                {
+                    n.append( "'" );
+                    i += 5;
+                }
+                else
+                {
+                    n.append( "&" );
+                }
+            }
+            else
+            {
+                n.append( c );
+            }
+        }
+        return n.toString();
+    }
+
+    private final static char lookAhead( int la, int offset, String data )
+    {
+        try
+        {
+            return data.charAt( offset + la );
+        }
+        catch( StringIndexOutOfBoundsException e )
+        {
+            return 0x0;
+        }
+    }
+
+    // combine multiple checks in one methods for speed
+    private final static boolean contains( String text, char[] chars )
+    {
+        if( text == null || chars == null || chars.length == 0 )
+        {
+            return false;
+        }
+        for( int i = 0; i < text.length(); i++ )
+        {
+            char c = text.charAt( i );
+            for( int j = 0; j < chars.length; j++ )
+            {
+                if( chars[j] == c )
+                {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+}

Propchange: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLEncode.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java?rev=1400646&view=auto
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java (added)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java Sun Oct 21 13:42:33 2012
@@ -0,0 +1,84 @@
+package org.apache.maven.shared.utils.xml;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+/**
+ * Interface for tools writing XML files.
+ * XMLWriters are not thread safe and must not be accessed concurrently.
+ */
+public interface XMLWriter
+{
+
+    /**
+     * Sets the encoding of the document.
+     * If not set, UTF-8 is being used
+     *
+     * @param encoding the encoding
+     * @throws IllegalStateException if the generation of the document has already started
+     */
+    void setEncoding( String encoding );
+
+    /**
+     * Sets the docType of the document.
+     *
+     * @param docType the docType
+     * @throws IllegalStateException if the generation of the document has already started
+     */
+    void setDocType( String docType );
+
+
+    /**
+     * Start an XML Element tag.
+     * @param name
+     */
+    void startElement( String name );
+
+
+    /**
+     * Add a XML attribute to the current XML Element.
+     * This method must get called immediately after {@link #startElement(String)}
+     * @param key
+     * @param value
+     * @throws IllegalStateException if no element tag is currently in process
+     */
+    void addAttribute( String key, String value );
+
+    /**
+     * Add a value text to the current element tag
+     * This will perform XML escaping to guarantee valid content
+     * @param text
+     * @throws IllegalStateException if no element tag got started yet
+     */
+    void writeText( String text );
+
+    /**
+     * Add a preformatted markup to the current element tag
+     * @param text
+     * @throws IllegalStateException if no element tag got started yet
+     */
+    void writeMarkup( String text );
+
+    /**
+     * End the previously opened element.
+     * @see #startElement(String)
+     */
+    void endElement();
+}

Propchange: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java?rev=1400646&view=auto
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java (added)
+++ maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java Sun Oct 21 13:42:33 2012
@@ -0,0 +1,201 @@
+package org.apache.maven.shared.utils.xml;
+
+import javax.swing.text.html.HTML;
+import java.io.StringWriter;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+
+import org.apache.maven.shared.utils.Os;
+import org.apache.maven.shared.utils.StringUtils;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test of {@link PrettyPrintXMLWriter}
+ *
+ * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
+ * @version $Id$
+ */
+public class PrettyPrintXmlWriterTest
+{
+    StringWriter w;
+
+    PrettyPrintXMLWriter writer;
+
+    @Before
+    public void before()
+            throws Exception
+    {
+        w = new StringWriter();
+        writer = new PrettyPrintXMLWriter( w );
+    }
+
+    @After
+    public void after()
+            throws Exception
+    {
+        writer = null;
+        w = null;
+    }
+
+    @Test
+    public void testDefaultPrettyPrintXMLWriter()
+    {
+        writer.startElement( HTML.Tag.HTML.toString() );
+
+        writeXhtmlHead( writer );
+
+        writeXhtmlBody( writer );
+
+        writer.endElement(); // Tag.HTML
+
+        Assert.assertEquals( expectedResult( Os.LINE_SEP ), w.toString() );
+    }
+
+    @Test
+    public void testPrettyPrintXMLWriterWithGivenLineSeparator()
+    {
+        writer.setLineSeparator( "\n" );
+
+        writer.startElement( HTML.Tag.HTML.toString() );
+
+        writeXhtmlHead( writer );
+
+        writeXhtmlBody( writer );
+
+        writer.endElement(); // Tag.HTML
+
+        Assert.assertEquals( expectedResult( "\n" ), w.toString() );
+    }
+
+    @Test
+    public void testPrettyPrintXMLWriterWithGivenLineIndenter()
+    {
+        writer.setLineIndenter( "    " );
+
+        writer.startElement( HTML.Tag.HTML.toString() );
+
+        writeXhtmlHead( writer );
+
+        writeXhtmlBody( writer );
+
+        writer.endElement(); // Tag.HTML
+
+        Assert.assertEquals( expectedResult( "    ", Os.LINE_SEP ), w.toString() );
+    }
+
+    @Test
+    public void testEscapeXmlAttributeWindows()
+    {
+        // Windows
+        writer.startElement( HTML.Tag.DIV.toString() );
+        writer.addAttribute( "class", "sect\r\nion" );
+        writer.endElement(); // Tag.DIV
+        Assert.assertEquals( "<div class=\"sect&#10;ion\"/>", w.toString() );
+    }
+
+    @Test
+    public void testEscapeXmlAttributeMac()
+    {
+        // Mac
+        writer.startElement( HTML.Tag.DIV.toString() );
+        writer.addAttribute( "class", "sect\rion" );
+        writer.endElement(); // Tag.DIV
+        Assert.assertEquals( "<div class=\"sect&#13;ion\"/>", w.toString() );
+    }
+
+    @Test
+    public void testEscapeXmlAttributeUnix()
+    {
+        // Unix
+        writer.startElement( HTML.Tag.DIV.toString() );
+        writer.addAttribute( "class", "sect\nion" );
+        writer.endElement(); // Tag.DIV
+        Assert.assertEquals( "<div class=\"sect&#10;ion\"/>", w.toString() );
+    }
+
+    private void writeXhtmlHead( XMLWriter writer )
+    {
+        writer.startElement( HTML.Tag.HEAD.toString() );
+        writer.startElement( HTML.Tag.TITLE.toString() );
+        writer.writeText( "title" );
+        writer.endElement(); // Tag.TITLE
+        writer.startElement( HTML.Tag.META.toString() );
+        writer.addAttribute( "name", "author" );
+        writer.addAttribute( "content", "Author" );
+        writer.endElement(); // Tag.META
+        writer.startElement( HTML.Tag.META.toString() );
+        writer.addAttribute( "name", "date" );
+        writer.addAttribute( "content", "Date" );
+        writer.endElement(); // Tag.META
+        writer.endElement(); // Tag.HEAD
+    }
+
+    private void writeXhtmlBody( XMLWriter writer )
+    {
+        writer.startElement( HTML.Tag.BODY.toString() );
+        writer.startElement( HTML.Tag.P.toString() );
+        writer.writeText( "Paragraph 1, line 1. Paragraph 1, line 2." );
+        writer.endElement(); // Tag.P
+        writer.startElement( HTML.Tag.DIV.toString() );
+        writer.addAttribute( "class", "section" );
+        writer.startElement( HTML.Tag.H2.toString() );
+        writer.writeText( "Section title" );
+        writer.endElement(); // Tag.H2
+        writer.endElement(); // Tag.DIV
+        writer.endElement(); // Tag.BODY
+    }
+
+    private String expectedResult( String lineSeparator )
+    {
+        return expectedResult( "  ", lineSeparator );
+    }
+
+    private String expectedResult( String lineIndenter, String lineSeparator )
+    {
+        StringBuffer expected = new StringBuffer();
+
+        expected.append( "<html>" ).append( lineSeparator );
+        expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "<head>" ).append( lineSeparator );
+        expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "<title>title</title>" )
+                .append( lineSeparator );
+        expected.append( StringUtils.repeat( lineIndenter, 2 ) )
+                .append( "<meta name=\"author\" content=\"Author\"/>" ).append( lineSeparator );
+        expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "<meta name=\"date\" content=\"Date\"/>" )
+                .append( lineSeparator );
+        expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "</head>" ).append( lineSeparator );
+        expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "<body>" ).append( lineSeparator );
+        expected.append( StringUtils.repeat( lineIndenter, 2 ) )
+                .append( "<p>Paragraph 1, line 1. Paragraph 1, line 2.</p>" ).append( lineSeparator );
+        expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "<div class=\"section\">" )
+                .append( lineSeparator );
+        expected.append( StringUtils.repeat( lineIndenter, 3 ) ).append( "<h2>Section title</h2>" )
+                .append( lineSeparator );
+        expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "</div>" ).append( lineSeparator );
+        expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "</body>" ).append( lineSeparator );
+        expected.append( "</html>" );
+
+        return expected.toString();
+    }
+}

Propchange: maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native