Java Source Code: com.engroom.util.xml.PrintXML


   1: /*
   2:  * Created on 27-Oct-2005
   3:  *
   4:  *
   5:  *  Copyright (c) Engine Room Software Ltd. 1997-2006
   6:  *  
   7:  *  This file is part of MayFly Version 1.01 Build 1 build date @DATE@
   8:  *  
   9:  *  MayFly is free software; you can redistribute it and/or modify
  10:  *  it under the terms of the GNU General Public License as published by
  11:  *  the Free Software Foundation; either version 2 of the License, or
  12:  *  (at your option) any later version.
  13:  *  
  14:  *  MayFly is distributed in the hope that it will be useful,
  15:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17:  *  GNU General Public License for more details.
  18:  *  
  19:  *  You should have received a copy of the GNU General Public License
  20:  *  along with MayFly; if not, write to the Free Software
  21:  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  22:  *  
  23:  *  The GNU General Public License should be found in the COPYING
  24:  *  file in the root of this distribution
  25:  *
  26:  */
  27: package com.engroom.util.xml;
  28: 
  29: import java.io.IOException;
  30: import java.io.StringWriter;
  31: import java.io.Writer;
  32: 
  33: import javax.xml.parsers.ParserConfigurationException;
  34: 
  35: import org.apache.xml.serialize.OutputFormat;
  36: import org.apache.xml.serialize.XMLSerializer;
  37: import org.w3c.dom.Document;
  38: import org.w3c.dom.Element;
  39: import org.w3c.dom.Node;
  40: 
  41: import com.engroom.util.XMLUtil;
  42: 
  43: /**
  44:  * @author Mitch Britton
  45:  *
  46:  */
  47: public class PrintXML
  48:	  {
  49:    private final static int DEF_LINE_WIDTH = -1;
  50:    private Document doc;
  51:    private int lineWidth;
  52:    private Writer out;
  53:    
  54:    public PrintXML()
  55:	      {
  56:        this(null,null, DEF_LINE_WIDTH);
  57:    }
  58:
  59:    public PrintXML(Document doc)
  60:	      {
  61:        this(doc,null, DEF_LINE_WIDTH);
  62:    }
  63:
  64:    public PrintXML(Document doc, Writer out, int lineWidth)
  65:	      {
  66:        this.doc = doc;
  67:        this.out = out;
  68:        this.lineWidth = lineWidth;
  69:    }
  70:    
  71:    public PrintXML(Document doc, Writer out)
  72:	      {
  73:        this(doc,out,DEF_LINE_WIDTH);
  74:    }
  75:    
  76:    public PrintXML(Element el)
  77:	      {
  78:        this(null,null, DEF_LINE_WIDTH);
  79:        setDocument(el);
  80:    }
  81:
  82:    public void print() throws IOException
  83:	      {
  84:        XMLSerializer ser = new XMLSerializer(getOutput(), getOutputFormat(lineWidth));
  85:        ser.serialize(getDocument());
  86:    }
  87:    
  88:    public String toString()
  89:	      {
  90:        try
  91:	          {
  92:            print();
  93:        }
  94:        catch (IOException e)
  95:	          {
  96:            return e.getMessage();
  97:        }
  98:        return getOutput().toString();
  99:    }
 100:    
 101:    private static OutputFormat getOutputFormat(int lineWidth)
 102:	      {
 103:        OutputFormat opf = new OutputFormat();
 104:
 105:        if (lineWidth == -1)
 106:	          {
 107:            opf.setPreserveSpace(false);
 108:            opf.setLineWidth(100);
 109:            opf.setIndenting(true);
 110:            opf.setIndent(4);
 111:        }
 112:        else
 113:	          {
 114:            opf.setPreserveSpace(true);
 115:            opf.setLineWidth(lineWidth);
 116:            opf.setIndenting(false);
 117:        }
 118:        return opf;
 119:    }
 120:
 121:    /**
 122:     * @return Returns the doc.
 123:     */
 124:    public Document getDocument()
 125:	      {
 126:        return doc;
 127:    }
 128:    
 129:    /**
 130:     * @param doc The doc to set.
 131:     */
 132:    public void setDocument(Document doc)
 133:	      {
 134:        this.doc = doc;
 135:    }
 136:    
 137:    /**
 138:     * @param el The element to set.
 139:     */
 140:    public void setDocument(Element el)
 141:	      {
 142:        try
 143:	          {
 144:            Document doc = XMLUtil.getNewDocument();
 145:            Node n = doc.importNode(el, true);
 146:            doc.appendChild(n);
 147:            setDocument(doc);
 148:        }
 149:        catch (ParserConfigurationException e)
 150:	          {
 151:        }
 152:    }
 153:    
 154:    /**
 155:     * @return Returns the lineWidth.
 156:     */
 157:    public int getLineWidth()
 158:	      {
 159:        return lineWidth;
 160:    }
 161:    
 162:    /**
 163:     * @param lineWidth The lineWidth to set.
 164:     */
 165:    public void setLineWidth(int lineWidth)
 166:	      {
 167:        this.lineWidth = lineWidth;
 168:    }
 169:    
 170:    /**
 171:     * @return Returns the out.
 172:     */
 173:    public Writer getOutput()
 174:	      {
 175:        if ( out == null )
 176:            out = new StringWriter();
 177:        return out;
 178:    }
 179:    
 180:    /**
 181:     * @param out The out to set.
 182:     */
 183:    public void setOutput(Writer out)
 184:	      {
 185:        this.out = out;
 186:    }
 187:}