Hi,

> I would like to submit the DOM to source file transcoder with Pretty
> Printer formating rule.  This time no cvs diff -u because it is
> completely new file. See the attachment. Almost all the code is copy and paste from 
> DOMUtilitis and svg2svg transcoder with modification.
> 
> I put it under the package org.apache.batik.transcoder.dom, because I
> think it is different from org.apache.batik.transcoder.svg2svg (Source
> to Source) and this one is DOM to source.
> 

Last time I send the file got bug, here is the updated version of
DOMPrettyPrinter


Regards
Tonny Kohar
-- 
Sketsa 
SVG Graphics Editor
http://www.kiyut.com

/*
============================================================================
                   The Apache Software License, Version 1.1
 ============================================================================
 
 Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modifica-
 tion, are permitted provided that the following conditions are met:
 
 1. Redistributions of  source code must  retain the above copyright  notice,
    this list of conditions and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
 
 3. The end-user documentation included with the redistribution, if any, must
    include  the following  acknowledgment:  "This product includes  software
    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
    Alternately, this  acknowledgment may  appear in the software itself,  if
    and wherever such third-party acknowledgments normally appear.
 
 4. The names "Batik" and  "Apache Software Foundation" must  not  be
    used to  endorse or promote  products derived from  this software without
    prior written permission. For written permission, please contact
    [EMAIL PROTECTED]
 
 5. Products  derived from this software may not  be called "Apache", nor may
    "Apache" appear  in their name,  without prior written permission  of the
    Apache Software Foundation.
 
 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
 APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
 DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
 OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
 ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
 (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 This software  consists of voluntary contributions made  by many individuals
 on  behalf of the Apache Software  Foundation. For more  information on the
 Apache Software Foundation, please see <http://www.apache.org/>.
 
 */

package org.apache.batik.transcoder.dom;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import org.w3c.dom.*;
import org.w3c.dom.svg.*;

import org.apache.batik.transcoder.ErrorHandler;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.util.SVGConstants;
import org.apache.batik.xml.XMLUtilities;

/** This class represents an SVGDOM to source files pretty-printer.
 * Most of the code here is adapted from batik svg2svg Transcoder
 * and DOMUtilities.
 *
 * @author  Tonny Kohar
 */
public class DOMPrettyPrinter {
    /**
     * The writer used to output the document.
     */
    protected Writer writer;
    
    /**
     * The newline characters.
     */
    protected String newline = "\n";
    
    /**
     * Whether the output must be formatted.
     */
    protected boolean format = true;
    
    /**
     * The tabulation width.
     */
    protected int tabulationWidth = 4;
    
    /**
     * The document width.
     */
    protected int documentWidth = 80;
    
    /**
     * The public id.
     */
    protected String publicId;
    
    /**
     * The system id.
     */
    protected String systemId;
    
    /**
     * The XML declaration.
     */
    protected String xmlDeclaration;
    
    /**
     * The type of the current lexical unit.
     */
    protected int type;
    
    /**
     * The indentation level.
     */
    protected int level;
    
    /**
     * The margin.
     */
    protected StringBuffer margin = new StringBuffer();
    
    /**
     * The current line.
     */
    protected int line = 1;
    
    /**
     * The current column.
     */
    protected int column;
    
    /**
     * Whether the next markup can be indented.
     */
    protected boolean canIndent = true;
    
    
    /** Creates a new instance of DOMPrettyPrinter */
    public DOMPrettyPrinter() {
    }
    
    /**
     * Sets the XML declaration text.
     */
    public void setXMLDeclaration(String s) {
        xmlDeclaration = s;
    }
    
    /**
     * Sets the public ID.
     */
    public void setPublicId(String s) {
        publicId = s;
    }
    
    /**
     * Sets the system ID.
     */
    public void setSystemId(String s) {
        systemId = s;
    }
    
    /**
     * Sets the newline characters.
     */
    public void setNewline(String s) {
        newline = s;
    }
    
    /**
     * Returns the newline characters.
     */
    public String getNewline() {
        return newline;
    }
    
    /**
     * Sets the format attribute.
     */
    public void setFormat(boolean b) {
        format = b;
    }
    
    /**
     * Returns whether the output must be formatted.
     */
    public boolean getFormat() {
        return format;
    }
    
    /**
     * Sets the tabulation width.
     */
    public void setTabulationWidth(int i) {
        tabulationWidth = Math.max(i, 0);
    }
    
    /**
     * Returns whether the tabulation width.
     */
    public int getTabulationWidth() {
        return tabulationWidth;
    }
    
    /**
     * Sets the document width.
     */
    public void setDocumentWidth(int i) {
        documentWidth = Math.max(i, 0);
    }
    
    /**
     * Returns whether the document width.
     */
    public int getDocumentWidth() {
        return documentWidth;
    }
    
    /**
     * Returns the number of newlines in the given char array.
     */
    protected int newlines(char[] text) {
        int result = 0;
        for (int i = 0; i < text.length; i++) {
            if (text[i] == 10) {
                result++;
            }
        }
        return result;
    }
    
    /**
     * Prints a single character.
     */
    protected void printCharacter(char c) throws IOException {
        if (c == 10) {
            printNewline();
        } else {
            column++;
            writer.write(c);
        }
    }
    
    /**
     * Prints a newline.
     */
    protected void printNewline() throws IOException {
        String nl = getNewline();
        for (int i = 0; i < nl.length(); i++) {
            writer.write(nl.charAt(i));
        }
        column = 0;
        line++;
    }
    
    /**
     * Prints a string.
     */
    protected void printString(String s) throws IOException {
        for (int i = 0; i < s.length(); i++) {
            printCharacter(s.charAt(i));
        }
    }
    
    /**
     * Prints a char array.
     */
    protected void printCharacters(char[] ca) throws IOException {
        for (int i = 0; i < ca.length; i++) {
            printCharacter(ca[i]);
        }
    }
    
    /**
     * Prints white spaces.
     * @param text The space text.
     * @param opt whether the space is optional.
     */
    protected void printSpaces(char[] text, boolean opt) throws IOException {
        if (getFormat()) {
            if (!opt) {
                printCharacter(' ');
            }
        } else {
            printCharacters(text);
        }
    }
    
    /**
     * Prints top level white spaces.
     * @param text The space text.
     * @param opt whether the space is optional.
     */
    protected void printTopSpaces(char[] text) throws IOException {
        if (getFormat()) {
            int nl = newlines(text);
            for (int i = 0; i < nl; i++) {
                printNewline();
            }
        } else {
            printCharacters(text);
        }
    }
    
    protected void marginAppend() {
        for (int i = 0; i < getTabulationWidth(); i++) {
            margin.append(' ');
        }
    }
    
    protected void marginDelete() {
        for (int i = 0; i < getTabulationWidth(); i++) {
            margin.deleteCharAt(0);
        }
    }
    
    /**
     * Returns the given content value transformed to replace invalid
     * characters with entities.
     */
    protected String contentToString(String s) {
        StringBuffer result = new StringBuffer();
        
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            
            switch (c) {
                case '<':
                    result.append("&lt;");
                    break;
                case '>':
                    result.append("&gt;");
                    break;
                case '&':
                    result.append("&amp;");
                    break;
                case '"':
                    result.append("&quot;");
                    break;
                case '\'':
                    result.append("&apos;");
                    break;
                default:
                    result.append(c);
            }
        }
        
        return result.toString();
    }
    
    /** Return the normalizeAttributeValue
     * First it pass String param to contentToString(s) then
     * It also replace multiple space into single space
     * and cariage return/linefeed into single space
     */
    protected String normalizeAttributeValue(String s) {
        String str = contentToString(s);
        StringBuffer sb = new StringBuffer();
        
        /* since batik using jdk1.3 no regex is use, use StringTokenizer instead */
        java.util.StringTokenizer st = new java.util.StringTokenizer(str);
        while (st.hasMoreTokens()) {
            sb.append(st.nextToken() + " ");
        }
        sb.deleteCharAt(sb.length()-1);
        
        return sb.toString();
    }
    
    /** write the String into allowable document width and margin.
     * The line break is done on word boundary
     */
    protected void formatText(String text) throws IOException {
        java.util.StringTokenizer st = new java.util.StringTokenizer(text);
        while (st.hasMoreTokens()) {
            String str = st.nextToken();
            if (str.length() + column > getDocumentWidth()) {
                printNewline();
                printString(margin.toString());
                for(int i=0; i< getTabulationWidth(); i++) {
                    printCharacter(' ');
                }
            }
            printString(str);
            if (st.hasMoreTokens()==true) {
                printCharacter(' ');
            }
        }
    }
    
    /**
     * Tells whether the given character represents white spaces.
     */
    protected boolean isWhiteSpace(char[] text) {
        for (int i = 0; i < text.length; i++) {
            if (!XMLUtilities.isXMLSpace(text[i])) {
                return false;
            }
        }
        return true;
    }
    
    /** Return wheter the specified Attr attributeValue is line breakable on word boundary
     [EMAIL PROTECTED] true if line breakable otherwise false
     */
    protected boolean isAttributeValueLineBreakable(Attr attr) {
        boolean breakable = false;
        
        Element elt = attr.getOwnerElement();
        String name = attr.getName();
        if (name.equalsIgnoreCase("points")) {
            if (elt instanceof SVGPolygonElement || elt instanceof SVGPolylineElement) {
                breakable = true;
            }
        } else if (name.equalsIgnoreCase("d")) {
            if (elt instanceof SVGPathElement) {
                breakable = true;
            }
        } else if (name.equalsIgnoreCase("style")) {
            if (elt instanceof SVGStylable) {
                breakable = true;
            }
        }
        
        return breakable;
    }
    
    /**
     * Prints an SVG document to the given writer.
     */
    public void print(Document doc, Writer w) throws TranscoderException, IOException {
        writer = w;
        
        printXMLDecl();
        
        for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling()) {
            writeNode(n);
        }
    }
    
    /**
     * Prints the XML declaration.
     */
    protected void printXMLDecl() throws TranscoderException, IOException {
        if (xmlDeclaration != null) {
            printString(xmlDeclaration);
            printNewline();
        }
    }
    
    /**
     * Writes a node using the given writer.
     */
    protected void writeNode(Node n) throws IOException {
        switch (n.getNodeType()) {
            case Node.ELEMENT_NODE:
                writeElement(n);
                break;
            case Node.TEXT_NODE:
                writeCharacterData(n);
                break;
            case Node.CDATA_SECTION_NODE:
                writeCDATASection(n);
                break;
            case Node.ENTITY_REFERENCE_NODE:
                writeEntityReference(n);
                break;
            case Node.PROCESSING_INSTRUCTION_NODE:
                writePI(n);
                break;
            case Node.COMMENT_NODE:
                writeComment(n);
                break;
            case Node.DOCUMENT_TYPE_NODE:
                break;
            default:
                throw new Error("Internal error (" + n.getNodeType() + ")");
        }
    }
    
    /** write Element node */
    protected void writeElement(Node n) throws IOException {
        ////////////////////////
        // begining of element
        if (getFormat()) {
            if (canIndent) {
                printNewline();
                printString(margin.toString());
            }
        }
        
        int startLine = line;
        
        printCharacter('<');
        printString(n.getNodeName());
        
        ////////////////////
        // attribute stuff
        if (n.hasAttributes()) {
            NamedNodeMap attr = n.getAttributes();
            int len = attr.getLength();
            for (int i = 0; i < len; i++) {
                Attr a = (Attr)attr.item(i);
                
                // attr name stuff
                String attrName = " " + a.getNodeName() + "=";
                if (attrName.length() + column > getDocumentWidth()) {
                    printNewline();
                    printString(margin.toString());
                    for(int j=0; j< getTabulationWidth(); j++) {
                        printCharacter(' ');
                    }
                }
                printString(attrName);
                
                // attr value stuff
                String attrValue = "\"" + normalizeAttributeValue(a.getNodeValue()) + "\"";
                if (isAttributeValueLineBreakable(a) == true) {
                    formatText(attrValue);
                } else {
                    printString(attrValue);
                }
            }
        }
        
        
        ////////////////////
        // end of element
        Node c = n.getFirstChild();
        if (c != null) {
            printCharacter('>');
            level++;
            marginAppend();
            canIndent = true;
            for (; c != null;c = c.getNextSibling()) {
                writeNode(c);
            }
            level--;
            marginDelete();
            if (getFormat()) {
                if (column + n.getNodeName().length() + 3 >= getDocumentWidth() || startLine != line) {
                    printNewline();
                    printString(margin.toString());
                }
            }
            printString("</");
            printString(n.getNodeName());
            printString(">");
        } else {
            printString("/>");
        }
    }
    
    /** write comment node */
    protected void writeComment(Node n) throws IOException {
        String str = n.getNodeValue();
        
        if (getFormat()) {
            if (canIndent) {
                printNewline();
                printString(margin.toString());
            }
            printString("<!-- ");
            str = contentToString(str);
            formatText(str);
            if (column + 3 > getDocumentWidth()) {
                printNewline();
                printString(margin.toString());
            }
            printString(" -->");
        } else {
            printString("<!--");
            printString(str);
            printString("-->");
        }
    }
    
    /** write CharacterData node */
    protected void writeCharacterData(Node n) throws IOException {
        String text = contentToString(n.getNodeValue());
        if (getFormat()) {
            canIndent = true;
            char[] data = text.toCharArray();
            if (isWhiteSpace(data)) {
                int nl = newlines(data);
                for (int i = 0; i < nl - 1; i++) {
                    printNewline();
                }
            } else {
                formatText(text);
            }
        } else {
            printString(text);
        }
    }
    
    /** write EntityReference node */
    protected void writeEntityReference(Node n) throws IOException {
        printCharacter('&');
        printString(n.getNodeName());
        printCharacter(';');
    }
    
    /** write CDATASection node */
    protected void writeCDATASection(Node n) throws IOException {
        printString("<![CDATA[");
        printString(n.getNodeValue());
        printString("]]>");
    }
    
    /** write ProcessingInstruction node */
    public void writePI(Node n) throws IOException {
        if (getFormat()) {
            if (canIndent) {
                printNewline();
                printString(margin.toString());
            }
        }
        printString("<?");
        printString(n.getNodeName());
        printString(" ");
        printString(n.getNodeValue());
        printString("?>");
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to