001package org.apache.archiva.xml; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.w3c.dom.Document; 023 024import javax.xml.transform.OutputKeys; 025import javax.xml.transform.Transformer; 026import javax.xml.transform.TransformerException; 027import javax.xml.transform.TransformerFactory; 028import javax.xml.transform.dom.DOMSource; 029import javax.xml.transform.stream.StreamResult; 030import java.io.IOException; 031import java.io.Writer; 032 033/** 034 * XMLWriter - Making writing XML files easier. 035 */ 036public class XMLWriter { 037 /** 038 * Write the Document to the provided Writer, leaving the Writer open. 039 * 040 * @param doc the document to write. 041 * @param writer the writer to write to. 042 * @throws XMLException if there was a problem writing the xml to the writer. 043 */ 044 public static void write(Document doc, Writer writer) 045 throws XMLException { 046 write(doc, writer, false); 047 } 048 049 /** 050 * Write the Document to the provided Writer, with an option to close the writer upon completion. 051 * 052 * @param doc the document to write. 053 * @param writer the writer to write to. 054 * @param close true to close the writer on completion. 055 * @throws XMLException if there was a problem writing the xml to the writer. 056 */ 057 public static void write(Document doc, Writer writer, boolean close) 058 throws XMLException { 059 060 try { 061 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 062 Transformer transformer = transformerFactory.newTransformer(); 063 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 064 try { 065 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 066 } catch (IllegalArgumentException ex) { 067 // Indent not supported 068 } 069 // Writing the XML declaration, because the JDK implementation does not create a newline 070 writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 071 StreamResult result = new StreamResult(writer); 072 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 073 DOMSource source = new DOMSource(doc); 074 transformer.transform(source, result); 075 076 077 } catch (TransformerException e) { 078 throw new XMLException("Could not create the xml transformer: " + e.getMessage(), e); 079 } catch (IOException e) { 080 throw new XMLException("Could not write to xml output: " + e.getMessage(), e); 081 } finally { 082 if (writer!=null) { 083 if (close) { 084 try { 085 writer.flush(); 086 } catch (IOException e) { 087 /* quietly ignore */ 088 } 089 try { 090 writer.close(); 091 } catch (IOException e) { 092 /* quietly ignore */ 093 } 094 } 095 } 096 } 097 } 098}