Hi, > > If I am not wrong interpreting your suggestion, then > > PrettyPrinter/OutputManager need to be rewritten in order to preserve > > the DOM document in order to check namespace,etc. I think something like > > DOMUtilities.writeNode() with PrettyPrinter formating rule. What is your > > idea? any suggestion? > > Yah, I guess that is what my suggestion would boil down to. > Looking at the code I really have to wonder a bit at why it wasn't > written that way to start with (it has it's own XML Parser 'lite' > built in). One thing is that with a normal XML Parser you lose > all information about spaces between the attributes of an element, > it may be that they want to preserve this from the source document.
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. Regards Tonny Kohar -- Kiyut http://www.kiyut.com SVG Editor
/* ============================================================================ 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("<"); break; case '>': result.append(">"); break; case '&': result.append("&"); break; case '"': result.append("""); break; case '\'': result.append("'"); 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(); boolean newLine = false; if (str.length() + column > getDocumentWidth()) { printNewline(); printString(margin.toString()); for(int i=0; i< getTabulationWidth(); i++) { printCharacter(' '); } newLine = true; } printString(str); if (newLine == false && 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("?>"); } }
/* ============================================================================ 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.Writer; import org.apache.batik.dom.util.DOMUtilities; import org.apache.batik.transcoder.AbstractTranscoder; import org.apache.batik.transcoder.ErrorHandler; import org.apache.batik.transcoder.TranscoderException; import org.apache.batik.transcoder.TranscoderInput; import org.apache.batik.transcoder.TranscoderOutput; import org.apache.batik.transcoder.TranscodingHints; import org.apache.batik.transcoder.keys.BooleanKey; import org.apache.batik.transcoder.keys.IntegerKey; import org.apache.batik.transcoder.keys.StringKey; import org.w3c.dom.Document; /** This class represents an SVGDOM to source files pretty-printer. * Most of the code here is adapted from batik svg2svg Transcoder * and DOMUtilities. This class delegate the writing to DOMPrettyPrinter * * Things left to do * - DOCTYPE stuff, batik SVGDOM doesn't return DOCTYPE * - Attribute Delimiter customization " (double quote) or ' (single quote), currently it is use " (double quote) * - Error handler, currently it is just throw IOException & Transcoder Exception * * @author Tonny Kohar */ public class DOMTranscoder extends AbstractTranscoder { /** * The default error handler. */ public final static ErrorHandler DEFAULT_ERROR_HANDLER = new ErrorHandler() { public void error(TranscoderException ex) throws TranscoderException { throw ex; } public void fatalError(TranscoderException ex) throws TranscoderException { throw ex; } public void warning(TranscoderException ex) throws TranscoderException { // Do nothing } }; /** * The key to specify the newline character sequence. */ public final static TranscodingHints.Key KEY_NEWLINE = new NewlineKey(); /** * The "\r" newline value. */ public final static NewlineValue VALUE_NEWLINE_CR = new NewlineValue("\r"); /** * The "\r\n" newline value. */ public final static NewlineValue VALUE_NEWLINE_CR_LF = new NewlineValue("\r\n"); /** * The "\n" newline value. */ public final static NewlineValue VALUE_NEWLINE_LF = new NewlineValue("\n"); /** * The key to specify whether to format the input. */ public final static TranscodingHints.Key KEY_FORMAT = new BooleanKey(); /** * The value to turn on formatting. */ public final static Boolean VALUE_FORMAT_ON = Boolean.TRUE; /** * The value to turn off formatting. */ public final static Boolean VALUE_FORMAT_OFF = Boolean.FALSE; /** * The key to specify the tabulation width. */ public final static TranscodingHints.Key KEY_TABULATION_WIDTH = new IntegerKey(); /** * The key to specify the document width. */ public final static TranscodingHints.Key KEY_DOCUMENT_WIDTH = new IntegerKey(); /** * The key to specify the XML declaration option. */ public final static TranscodingHints.Key KEY_XML_DECLARATION = new StringKey(); /** Creates a new instance of DOMTranscoder */ public DOMTranscoder() { setErrorHandler(DEFAULT_ERROR_HANDLER); } public void transcode(TranscoderInput input, TranscoderOutput output) throws TranscoderException { Document d = input.getDocument(); if (d == null) { throw new Error("Document expected"); } Writer w = output.getWriter(); if (w == null) { throw new Error("Writer expected"); } prettyPrint(d, w); } /** * Pretty print the given reader. */ protected void prettyPrint(Document doc, Writer out) throws TranscoderException { try { DOMPrettyPrinter pp = new DOMPrettyPrinter(); NewlineValue nlv = (NewlineValue)hints.get(KEY_NEWLINE); if (nlv != null) { pp.setNewline(nlv.getValue()); } Boolean b = (Boolean)hints.get(KEY_FORMAT); if (b != null) { pp.setFormat(b.booleanValue()); } Integer i = (Integer)hints.get(KEY_TABULATION_WIDTH); if (i != null) { pp.setTabulationWidth(i.intValue()); } i = (Integer)hints.get(KEY_DOCUMENT_WIDTH); if (i != null) { pp.setDocumentWidth(i.intValue()); } String s = (String)hints.get(KEY_XML_DECLARATION); if (s != null) { pp.setXMLDeclaration(s); } pp.print(doc, out); out.flush(); } catch (IOException e) { getErrorHandler().fatalError(new TranscoderException(e.getMessage())); } } /** * To represent a newline key. */ protected static class NewlineKey extends TranscodingHints.Key { public boolean isCompatibleValue(Object v) { return v instanceof NewlineValue; } } /** * To represent a newline value. */ protected static class NewlineValue { protected String value; public NewlineValue(String val) { value = val; } public String getValue() { return value; } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]