001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.platform.dbunit;
028:
029: import java.io.IOException;
030: import java.io.PrintWriter;
031: import java.util.Map;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.databene.commons.IOUtil;
036: import org.databene.commons.SystemInfo;
037: import org.databene.commons.converter.AnyConverter;
038: import org.databene.model.consumer.AbstractConsumer;
039: import org.databene.model.data.Entity;
040:
041: /**
042: * Exports entities in DbUnit XML file format.
043: * @since 0.3.04
044: * @author Volker Bergmann
045: */
046: public class DbUnitEntityExporter extends AbstractConsumer<Entity> {
047:
048: // attributes ------------------------------------------------------------------------------------------------------
049:
050: private static final Log logger = LogFactory
051: .getLog(DbUnitEntityExporter.class);
052:
053: private static final String DEFAULT_FILE_ENCODING = "UTF-8";
054: private static final String DEFAULT_URI = "data.dbunit.xml";
055:
056: private String uri;
057: private String encoding;
058:
059: private PrintWriter printer;
060:
061: // constructors ----------------------------------------------------------------------------------------------------
062:
063: public DbUnitEntityExporter() {
064: this (DEFAULT_URI);
065: }
066:
067: public DbUnitEntityExporter(String uri) {
068: this (uri, DEFAULT_FILE_ENCODING);
069: }
070:
071: public DbUnitEntityExporter(String uri, String encoding) {
072: setUri(uri);
073: setEncoding(encoding);
074: }
075:
076: // properties ------------------------------------------------------------------------------------------------------
077:
078: public String getUri() {
079: return uri;
080: }
081:
082: public void setUri(String uri) {
083: this .uri = uri;
084: }
085:
086: public String getEncoding() {
087: return encoding;
088: }
089:
090: public void setEncoding(String encoding) {
091: this .encoding = (encoding != null ? encoding : SystemInfo
092: .fileEncoding());
093: if (this .encoding == null)
094: this .encoding = DEFAULT_FILE_ENCODING;
095: }
096:
097: // Consumer interface ----------------------------------------------------------------------------------------------
098:
099: public void startConsuming(Entity entity) {
100: try {
101: if (logger.isDebugEnabled())
102: logger.debug("exporting " + entity);
103: if (printer == null)
104: initPrinter();
105: printer.print(" <" + entity.getName());
106: for (Map.Entry<String, Object> entry : entity
107: .getComponents().entrySet()) {
108: Object value = entry.getValue();
109: if (value == null)
110: continue;
111: String s = AnyConverter.convert(value, String.class);
112: printer.print(' ' + entry.getKey() + "=\"" + s + '"');
113: }
114: printer.println("/>");
115: } catch (IOException e) {
116: throw new RuntimeException(e);
117: }
118: }
119:
120: public void flush() {
121: if (printer != null)
122: printer.flush();
123: }
124:
125: public void close() {
126: if (printer != null) {
127: printer.print("</dataset>");
128: printer.close();
129: }
130: }
131:
132: // java.lang.String overrides --------------------------------------------------------------------------------------
133:
134: private void initPrinter() throws IOException {
135: // create file and write header
136: printer = IOUtil.getPrinterForURI(uri, encoding);
137: printer.println("<?xml version=\"1.0\" encoding=\"" + encoding
138: + "\"?>");
139: printer.println("<dataset>");
140: }
141:
142: public String toString() {
143: return getClass().getSimpleName() + '[' + uri + ", " + encoding
144: + "]";
145: }
146: }
|