001: /**
002: * ========================================
003: * JFreeReport : a free Java report library
004: * ========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * $Id: XmlPrintReportTarget.java 3525 2007-10-16 11:43:48Z tmorgner $
027: * ------------
028: * (C) Copyright 2000-2005, by Object Refinery Limited.
029: * (C) Copyright 2005-2007, by Pentaho Corporation.
030: */package org.jfree.report.flow.raw;
031:
032: import java.awt.Image;
033: import java.io.IOException;
034: import java.util.Iterator;
035: import java.util.Map;
036:
037: import org.jfree.layouting.namespace.NamespaceDefinition;
038: import org.jfree.layouting.namespace.Namespaces;
039: import org.jfree.layouting.namespace.NamespaceCollection;
040: import org.jfree.layouting.namespace.DefaultNamespaceCollection;
041: import org.jfree.layouting.util.AttributeMap;
042: import org.jfree.layouting.LibLayoutBoot;
043: import org.jfree.report.DataFlags;
044: import org.jfree.report.DataSourceException;
045: import org.jfree.report.JFreeReportInfo;
046: import org.jfree.report.ReportProcessingException;
047: import org.jfree.report.util.AttributeNameGenerator;
048: import org.jfree.report.flow.ReportJob;
049: import org.jfree.report.flow.ReportStructureRoot;
050: import org.jfree.report.flow.ReportTarget;
051: import org.jfree.report.flow.ReportTargetUtil;
052: import org.jfree.xmlns.common.AttributeList;
053: import org.jfree.xmlns.writer.XmlWriter;
054: import org.jfree.xmlns.writer.XmlWriterSupport;
055:
056: /**
057: * Todo: Document me!
058: *
059: * @author Thomas Morgner
060: * @since 20.03.2007
061: */
062: public class XmlPrintReportTarget implements ReportTarget {
063: private ReportJob reportJob;
064: private XmlWriter writer;
065: private NamespaceCollection namespaces;
066: private AttributeNameGenerator namespacePrefixGenerator;
067:
068: public XmlPrintReportTarget(final ReportJob job,
069: final XmlWriter writer) {
070: this .reportJob = job;
071: this .writer = writer;
072:
073: final NamespaceDefinition[] reportNamespaces = Namespaces
074: .createFromConfig(reportJob.getConfiguration(),
075: "org.jfree.report.namespaces.", null);
076: final NamespaceDefinition[] layoutNamespaces = Namespaces
077: .createFromConfig(LibLayoutBoot.getInstance()
078: .getGlobalConfig(),
079: "org.jfree.layouting.namespaces.", null);
080: final DefaultNamespaceCollection namespaces = new DefaultNamespaceCollection();
081: namespaces.addDefinitions(reportNamespaces);
082: namespaces.addDefinitions(layoutNamespaces);
083: this .namespaces = namespaces;
084: this .namespacePrefixGenerator = new AttributeNameGenerator();
085: }
086:
087: public ReportJob getReportJob() {
088: return reportJob;
089: }
090:
091: public void startReport(final ReportStructureRoot report)
092: throws DataSourceException, ReportProcessingException {
093: try {
094: writer.writeComment("starting report");
095: } catch (IOException e) {
096: throw new ReportProcessingException("IOError", e);
097: }
098: }
099:
100: public void startElement(final AttributeMap attrs)
101: throws DataSourceException, ReportProcessingException {
102: final String namespace = ReportTargetUtil
103: .getNamespaceFromAttribute(attrs);
104: final String elementType = ReportTargetUtil
105: .getElemenTypeFromAttribute(attrs);
106:
107: try {
108: final AttributeList attrList = buildAttributeList(attrs);
109: validateNamespace(namespace, attrList);
110: writer.writeTag(namespace, elementType, attrList,
111: XmlWriterSupport.OPEN);
112: } catch (IOException e) {
113: throw new ReportProcessingException("IOError", e);
114: }
115: }
116:
117: public void processContent(final DataFlags value)
118: throws DataSourceException, ReportProcessingException {
119: final Object rawvalue = value.getValue();
120: if (rawvalue == null) {
121: return;
122: }
123:
124: // special handler for image (possibly also for URL ..)
125: if (rawvalue instanceof Image) {
126: // do nothing yet. We should define something for that later ..
127: return;
128: }
129:
130: try {
131: final String text = String.valueOf(rawvalue);
132: writer.writeText(XmlWriterSupport.normalize(text, false));
133: } catch (IOException e) {
134: throw new ReportProcessingException("Failed", e);
135: }
136: }
137:
138: public void endElement(final AttributeMap attrs)
139: throws DataSourceException, ReportProcessingException {
140: try {
141: writer.writeCloseTag();
142: } catch (IOException e) {
143: throw new ReportProcessingException("IOError", e);
144: }
145: }
146:
147: public void endReport(final ReportStructureRoot report)
148: throws DataSourceException, ReportProcessingException {
149: try {
150: writer.writeComment("starting report");
151: } catch (IOException e) {
152: throw new ReportProcessingException("IOError", e);
153: }
154: }
155:
156: public NamespaceDefinition getNamespaceByUri(final String uri) {
157: return namespaces.getDefinition(uri);
158: }
159:
160: public void processText(final String text)
161: throws DataSourceException, ReportProcessingException {
162: try {
163: writer.writeText(XmlWriterSupport.normalize(text, false));
164: } catch (IOException e) {
165: throw new ReportProcessingException("IOError", e);
166: }
167: }
168:
169: public void commit() throws ReportProcessingException {
170: try {
171: writer.flush();
172: } catch (IOException e) {
173: throw new ReportProcessingException("Failed to flush", e);
174: }
175: }
176:
177: public String getExportDescriptor() {
178: return "raw/text+xml";
179: }
180:
181: protected AttributeList buildAttributeList(final AttributeMap attrs) {
182: final AttributeList attrList = new AttributeList();
183: final String[] namespaces = attrs.getNameSpaces();
184: for (int i = 0; i < namespaces.length; i++) {
185: final String attrNamespace = namespaces[i];
186: if (isInternalNamespace(attrNamespace)) {
187: continue;
188: }
189:
190: final Map localAttributes = attrs
191: .getAttributes(attrNamespace);
192: final Iterator entries = localAttributes.entrySet()
193: .iterator();
194: while (entries.hasNext()) {
195: final Map.Entry entry = (Map.Entry) entries.next();
196: final String key = String.valueOf(entry.getKey());
197: validateNamespace(attrNamespace, attrList);
198: attrList.setAttribute(attrNamespace, key, String
199: .valueOf(entry.getValue()));
200: }
201: }
202: return attrList;
203: }
204:
205: private void validateNamespace(final String uri,
206: final AttributeList list) {
207: if (writer.isNamespaceDefined(uri)) {
208: return;
209: }
210:
211: if (list.isNamespaceUriDefined(uri)) {
212: return;
213: }
214:
215: final NamespaceDefinition def = getNamespaceByUri(uri);
216: if (def != null) {
217: final String prefix = def.getPreferredPrefix();
218: if (writer.isNamespacePrefixDefined(prefix) == false
219: && list.isNamespacePrefixDefined(prefix) == false) {
220: list.addNamespaceDeclaration(prefix, uri);
221: } else {
222: list.addNamespaceDeclaration(namespacePrefixGenerator
223: .generateName(prefix), uri);
224: }
225: } else {
226: list.addNamespaceDeclaration(namespacePrefixGenerator
227: .generateName("auto"), uri);
228: }
229: }
230:
231: private boolean isInternalNamespace(final String namespace) {
232: return JFreeReportInfo.REPORT_NAMESPACE.equals(namespace);
233: }
234:
235: }
|