/* * Created on May 5, 2004 * * This file is part of Thingamablog. ( http://thingamablog.sf.net ) * * Copyright (c) 2004, Bob Tantlinger All Rights Reserved. * * 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. * * ************************************************************************** * Some of the code in this class comes from RSS OWL @ http://rssowl.sf.net ************************************************************************** */ package net.sf.thingamablog.xml; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import org.jdom.Comment; import org.jdom.Document; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; /** * Static methods for preparing and writing an XML file */ public class XMLUtils { public static final String SEP = System.getProperty("file.separator"); public static final String TEMP = System.getProperty("user.home") + SEP + "tbtemp.xml"; //public static String CHARSET = "UTF-8"; /* * Write the document as XML file to the given path * * @param document The XML Document * @param filePath The file where to write the XML contents * @param newLines Set TRUE to write XML with \n after each line */ public static void writeXML(Document document, String filePath, boolean newLines) throws IOException { XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat()); FileOutputStream fout = new FileOutputStream(new File(filePath)); outp.output(document, fout); fout.close(); } /** * Initialize a document * @param rootOpen Open tag for the root element * @param rootClose Close tag for the root element * @return A document * @throws IOException * @throws JDOMException */ public static Document initDocument(String rootOpen, String rootClose) throws IOException, JDOMException { return initDocument(rootOpen, rootClose, new File(TEMP)); } /** * Init the document for the export to a file * * @param rootOpen Open tag for the root element * @param rootClose Close tag for the root element * @param file File that should be initialized * @return Document The initalized document */ public static Document initDocument(String rootOpen, String rootClose, File file) throws IOException, JDOMException { SAXBuilder builder = new SAXBuilder(false); builder.setEntityResolver(new TBEntityResolver()); Document document = null; FileWriter fw = null; /** Default "user.xml" template */ try { fw = new FileWriter(file); fw.write( "<?xml version=\"1.0\" encoding=\"" + "UTF-8" + "\"?>\n"); fw.write("<" + rootOpen + ">"); fw.write("</" + rootClose + ">"); //fw.close(); } catch (IOException e) { throw e; } finally { /** Try to close the stream in any case */ try { if (fw != null) fw.close(); } catch (IOException e) { } } document = builder.build(file); /** Comment */ Comment comment = new Comment("Generated by Thingamablog. DO NOT EDIT THIS FILE"); document.getContent().add(0, comment); return document; } }