All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tuwien.auto.calimero.xml.def.DefaultXMLWriter Maven / Gradle / Ivy

There is a newer version: 2.6-rc1
Show newest version
/*
    Calimero 2 - A library for KNX network access
    Copyright (c) 2006, 2011 B. Malinowsky

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Linking this library statically or dynamically with other modules is
    making a combined work based on this library. Thus, the terms and
    conditions of the GNU General Public License cover the whole
    combination.

    As a special exception, the copyright holders of this library give you
    permission to link this library with independent modules to produce an
    executable, regardless of the license terms of these independent
    modules, and to copy and distribute the resulting executable under terms
    of your choice, provided that you also meet, for each linked independent
    module, the terms and conditions of the license of that module. An
    independent module is a module which is not derived from or based on
    this library. If you modify this library, you may extend this exception
    to your version of the library, but you are not obligated to do so. If
    you do not wish to do so, delete this exception statement from your
    version.
*/

package tuwien.auto.calimero.xml.def;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

import tuwien.auto.calimero.exception.KNXIllegalStateException;
import tuwien.auto.calimero.xml.Attribute;
import tuwien.auto.calimero.xml.KNXMLException;
import tuwien.auto.calimero.xml.XMLWriter;

/**
 * Default XML writer implementation of the XMLWriter interface.
 * 

* Does not add any feature not already documented in the implemented interface.
* This writer is not thread safe. * * @author B. Malinowsky */ public class DefaultXMLWriter implements XMLWriter { // default indentation private static final int indentWidth = 4; private static final String quote = "\""; private BufferedWriter w; private boolean closeWriter; // xml layout stack private Stack layout; // current layout indentation private int indent; // controls indentation for new tags private boolean newTag; /** * Creates a new XML writer. *

*/ public DefaultXMLWriter() {} /** * Creates a new XML writer with output w. *

* The writer is buffered by this XML writer. * * @param w the output {@link Writer} * @param close true to close w if XML writer is closed, * false otherwise * @see XMLWriter#setOutput(Writer, boolean) */ public DefaultXMLWriter(final Writer w, final boolean close) { setOutput(w, close); }; /* (non-Javadoc) * @see tuwien.auto.calimero.xml.XMLWriter#setOutput(java.io.Writer, boolean) */ public void setOutput(final Writer output, final boolean close) { reset(); w = new BufferedWriter(output); closeWriter = close; } /* (non-Javadoc) * @see tuwien.auto.calimero.xml.XMLWriter#writeDeclaration(boolean, java.lang.String) */ public void writeDeclaration(final boolean standalone, final String encoding) throws KNXMLException { try { w.write(" 0) w.write(" encoding=" + quote + encoding + quote); w.write("?>"); w.newLine(); } catch (final IOException e) { throw new KNXMLException(e.getMessage()); } } /* (non-Javadoc) * @see tuwien.auto.calimero.xml.XMLWriter#writeElement * (java.lang.String, java.util.List, java.lang.String) */ public void writeElement(final String name, final List att, final String content) throws KNXMLException { try { final Tag tag = new Tag(name, att, content, false); layout.push(tag); } catch (final IOException e) { throw new KNXMLException(e.getMessage()); } } /* (non-Javadoc) * @see tuwien.auto.calimero.xml.XMLWriter#writeEmptyElement * (java.lang.String, java.util.List) */ public void writeEmptyElement(final String name, final List att) throws KNXMLException { try { final Tag tag = new Tag(name, att, null, true); tag.endTag(); } catch (final IOException e) { throw new KNXMLException(e.getMessage()); } } /* (non-Javadoc) * @see tuwien.auto.calimero.xml.XMLWriter#writeCharData(java.lang.String, boolean) */ public void writeCharData(final String text, final boolean isCDATASection) throws KNXMLException { try { if (isCDATASection) { w.write(""); } else w.write(References.replace(text, true)); } catch (final IOException e) { throw new KNXMLException(e.getMessage()); } } /* (non-Javadoc) * @see tuwien.auto.calimero.xml.XMLWriter#writeComment(java.lang.String) */ public void writeComment(final String comment) throws KNXMLException { try { if (newTag) w.newLine(); indent(); w.write(""); if (!newTag) w.newLine(); } catch (final IOException e) { throw new KNXMLException(e.getMessage()); } } /* (non-Javadoc) * @see tuwien.auto.calimero.xml.XMLWriter#endElement() */ public void endElement() throws KNXMLException { if (layout.empty()) throw new KNXIllegalStateException("no elements to end"); try { ((Tag) layout.pop()).endTag(); if (layout.empty()) w.flush(); } catch (final IOException e) { throw new KNXMLException(e.getMessage()); } } /* (non-Javadoc) * @see tuwien.auto.calimero.xml.XMLWriter#endAllElements() */ public void endAllElements() throws KNXMLException { try { while (!layout.empty()) ((Tag) layout.pop()).endTag(); w.flush(); } catch (final IOException e) { throw new KNXMLException(e.getMessage()); } } /* (non-Javadoc) * @see tuwien.auto.calimero.xml.XMLWriter#close() */ public void close() throws KNXMLException { if (!layout.isEmpty()) endAllElements(); if (closeWriter) try { w.close(); } catch (final IOException e) { throw new KNXMLException(e.getMessage()); } } private BufferedWriter indent() throws IOException { for (int i = 0; i < indent; ++i) w.write(' '); return w; } private void reset() { layout = new Stack(); indent = 0; newTag = false; } // helper class for tag writing private final class Tag { private static final String lt = "<"; private static final String gt = ">"; private static final String equal = "="; private static final String slash = "/"; private static final String space = " "; private String name; Tag(final String name, final List att, final String cnt, final boolean empty) throws IOException { if (newTag) w.newLine(); indent().write(lt + name); if (att != null) for (final Iterator i = att.iterator(); i.hasNext();) { final Attribute a = (Attribute) i.next(); w.write(space + a.getName() + equal + quote + References.replace(a.getValue(), true) + quote); } if (empty) { w.write(space + slash + gt); w.newLine(); newTag = false; } else { newTag = true; if (cnt == null || cnt.length() == 0) w.write(gt); else w.write(gt + References.replace(cnt, true)); this.name = name; indent += indentWidth; } } void endTag() throws IOException { if (name != null) { indent -= indentWidth; if (!newTag) indent(); newTag = false; w.write(lt + slash + name + gt); w.newLine(); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy