001/* 002 * Copyright 2006 - 2013 003 * Stefan Balev <stefan.balev@graphstream-project.org> 004 * Julien Baudry <julien.baudry@graphstream-project.org> 005 * Antoine Dutot <antoine.dutot@graphstream-project.org> 006 * Yoann Pigné <yoann.pigne@graphstream-project.org> 007 * Guilhelm Savin <guilhelm.savin@graphstream-project.org> 008 * 009 * GraphStream is a library whose purpose is to handle static or dynamic 010 * graph, create them from scratch, file or any source and display them. 011 * 012 * This program is free software distributed under the terms of two licenses, the 013 * CeCILL-C license that fits European law, and the GNU Lesser General Public 014 * License. You can use, modify and/ or redistribute the software under the terms 015 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following 016 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by 017 * the Free Software Foundation, either version 3 of the License, or (at your 018 * option) any later version. 019 * 020 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 021 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 022 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 023 * 024 * You should have received a copy of the GNU Lesser General Public License 025 * along with this program. If not, see <http://www.gnu.org/licenses/>. 026 * 027 * The fact that you are presently reading this means that you have had 028 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms. 029 */ 030package org.graphstream.stream.file.gexf; 031 032import java.io.Writer; 033import java.util.LinkedList; 034 035import javax.xml.stream.FactoryConfigurationError; 036import javax.xml.stream.XMLOutputFactory; 037import javax.xml.stream.XMLStreamException; 038import javax.xml.stream.XMLStreamWriter; 039 040public class SmartXMLWriter { 041 public final XMLStreamWriter stream; 042 043 boolean smart; 044 int depth; 045 LinkedList<Integer> childrenCount; 046 047 public SmartXMLWriter(Writer output, boolean smart) 048 throws XMLStreamException, FactoryConfigurationError { 049 stream = XMLOutputFactory.newFactory().createXMLStreamWriter(output); 050 stream.writeStartDocument("UTF-8", "1.0"); 051 052 this.smart = smart; 053 this.depth = 0; 054 this.childrenCount = new LinkedList<Integer>(); 055 this.childrenCount.add(0); 056 } 057 058 public void startElement(String name) throws XMLStreamException { 059 if (smart) { 060 stream.writeCharacters("\n"); 061 062 for (int i = 0; i < depth; i++) 063 stream.writeCharacters(" "); 064 } 065 066 childrenCount.set(0, childrenCount.get(0) + 1); 067 childrenCount.addFirst(0); 068 069 stream.writeStartElement(name); 070 depth++; 071 } 072 073 public void endElement() throws XMLStreamException { 074 depth--; 075 076 boolean leaf = (childrenCount.pop() == 0); 077 078 if (smart && !leaf) { 079 stream.writeCharacters("\n"); 080 081 for (int i = 0; i < depth; i++) 082 stream.writeCharacters(" "); 083 } 084 085 stream.writeEndElement(); 086 } 087 088 public void leafWithText(String name, String content) 089 throws XMLStreamException { 090 startElement(name); 091 stream.writeCharacters(content); 092 endElement(); 093 } 094 095 public void flush() { 096 try { 097 stream.flush(); 098 } catch (XMLStreamException e) { 099 // Ignored 100 } 101 } 102 103 public void close() throws XMLStreamException { 104 stream.writeEndDocument(); 105 stream.flush(); 106 107 stream.close(); 108 } 109}