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.script;
028:
029: import org.databene.model.consumer.AbstractConsumer;
030: import org.databene.model.data.Entity;
031: import org.databene.script.ScriptedDocumentWriter;
032: import org.databene.commons.ConfigurationError;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.commons.logging.Log;
035:
036: import java.io.FileWriter;
037: import java.io.IOException;
038:
039: /**
040: * Script based entity exporter.
041: * Three scripts may be combined for formatting header, generated document part(s) and footer<br/>
042: * <br/>
043: * Created: 01.09.2007 18:05:04
044: * @author Volker Bergmann
045: */
046: public class ScriptedEntityExporter extends AbstractConsumer<Entity> {
047:
048: private static final Log logger = LogFactory
049: .getLog(ScriptedEntityExporter.class);
050:
051: private String uri;
052: private String headerScript;
053: private String partScript;
054: private String footerScript;
055:
056: private ScriptedDocumentWriter<Entity> writer;
057:
058: // constructors ----------------------------------------------------------------------------------------------------
059:
060: public ScriptedEntityExporter() {
061: this (null, null);
062: }
063:
064: public ScriptedEntityExporter(String uri, String partScript) {
065: this (uri, null, partScript, null);
066: }
067:
068: public ScriptedEntityExporter(String uri, String headerScript,
069: String partScript, String footerScript) {
070: this .uri = uri;
071: this .headerScript = headerScript;
072: this .partScript = partScript;
073: this .footerScript = footerScript;
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 getHeaderScript() {
087: return headerScript;
088: }
089:
090: public void setHeaderScript(String headerScript) {
091: this .headerScript = headerScript;
092: }
093:
094: public String getPartScript() {
095: return partScript;
096: }
097:
098: public void setPartScript(String partScript) {
099: this .partScript = partScript;
100: }
101:
102: public String getFooterScript() {
103: return footerScript;
104: }
105:
106: public void setFooterScript(String footerScript) {
107: this .footerScript = footerScript;
108: }
109:
110: // Consumer interface ----------------------------------------------------------------------------------------------
111:
112: public void startConsuming(Entity entity) {
113: try {
114: if (writer == null) {
115: writer = new ScriptedDocumentWriter<Entity>(
116: new FileWriter(uri), headerScript, partScript,
117: footerScript);
118: }
119: if (logger.isDebugEnabled())
120: logger.debug("Exporting " + entity);
121: writer.writeElement(entity);
122: } catch (IOException e) {
123: throw new ConfigurationError(e);
124: }
125: }
126:
127: public void flush() {
128: // ScriptedDocumentWriter does not support flushing
129: }
130:
131: public void close() {
132: if (writer != null) {
133: try {
134: writer.close();
135: } catch (IOException e) {
136: logger.error(e, e);
137: }
138: }
139: }
140:
141: }
|