1:
31:
32: package ;
33:
34: import ;
35: import ;
36: import ;
37: import ;
38:
39: import ;
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57:
58:
64: public class XmlPrintReportTarget implements ReportTarget
65: {
66: private ReportJob reportJob;
67: private XmlWriter writer;
68: private NamespaceCollection namespaces;
69: private AttributeNameGenerator namespacePrefixGenerator;
70:
71: public XmlPrintReportTarget(final ReportJob job,
72: final XmlWriter writer)
73: {
74: this.reportJob = job;
75: this.writer = writer;
76:
77: final NamespaceDefinition[] reportNamespaces = Namespaces.createFromConfig
78: (reportJob.getConfiguration(), "org.jfree.report.namespaces.", null);
79: final NamespaceDefinition[] layoutNamespaces = Namespaces.createFromConfig
80: (LibLayoutBoot.getInstance().getGlobalConfig(),
81: "org.jfree.layouting.namespaces.", null);
82: final DefaultNamespaceCollection namespaces = new DefaultNamespaceCollection();
83: namespaces.addDefinitions(reportNamespaces);
84: namespaces.addDefinitions(layoutNamespaces);
85: this.namespaces = namespaces;
86: this.namespacePrefixGenerator = new AttributeNameGenerator();
87: }
88:
89: public ReportJob getReportJob()
90: {
91: return reportJob;
92: }
93:
94: public void startReport(final ReportStructureRoot report)
95: throws DataSourceException, ReportProcessingException
96: {
97: try
98: {
99: writer.writeComment("starting report");
100: }
101: catch (IOException e)
102: {
103: throw new ReportProcessingException("IOError", e);
104: }
105: }
106:
107: public void startElement(final AttributeMap attrs)
108: throws DataSourceException, ReportProcessingException
109: {
110: final String namespace = ReportTargetUtil.getNamespaceFromAttribute(attrs);
111: final String elementType =
112: ReportTargetUtil.getElemenTypeFromAttribute(attrs);
113:
114: try
115: {
116: final AttributeList attrList = buildAttributeList(attrs);
117: validateNamespace(namespace, attrList);
118: writer.writeTag(namespace, elementType, attrList,
119: XmlWriterSupport.OPEN);
120: }
121: catch (IOException e)
122: {
123: throw new ReportProcessingException("IOError", e);
124: }
125: }
126:
127: public void processContent(final DataFlags value)
128: throws DataSourceException, ReportProcessingException
129: {
130: final Object rawvalue = value.getValue();
131: if (rawvalue == null)
132: {
133: return;
134: }
135:
136:
137: if (rawvalue instanceof Image)
138: {
139:
140: return;
141: }
142:
143: try
144: {
145: final String text = String.valueOf(rawvalue);
146: writer.writeText(XmlWriterSupport.normalize(text, false));
147: }
148: catch (IOException e)
149: {
150: throw new ReportProcessingException("Failed", e);
151: }
152: }
153:
154: public void endElement(final AttributeMap attrs)
155: throws DataSourceException, ReportProcessingException
156: {
157: try
158: {
159: writer.writeCloseTag();
160: }
161: catch (IOException e)
162: {
163: throw new ReportProcessingException("IOError", e);
164: }
165: }
166:
167: public void endReport(final ReportStructureRoot report)
168: throws DataSourceException, ReportProcessingException
169: {
170: try
171: {
172: writer.writeComment("starting report");
173: }
174: catch (IOException e)
175: {
176: throw new ReportProcessingException("IOError", e);
177: }
178: }
179:
180: public NamespaceDefinition getNamespaceByUri(final String uri)
181: {
182: return namespaces.getDefinition(uri);
183: }
184:
185: public void processText(final String text)
186: throws DataSourceException, ReportProcessingException
187: {
188: try
189: {
190: writer.writeText(XmlWriterSupport.normalize(text, false));
191: }
192: catch (IOException e)
193: {
194: throw new ReportProcessingException("IOError", e);
195: }
196: }
197:
198:
199: public void commit()
200: throws ReportProcessingException
201: {
202: try
203: {
204: writer.flush();
205: }
206: catch (IOException e)
207: {
208: throw new ReportProcessingException("Failed to flush", e);
209: }
210: }
211:
212: public String getExportDescriptor()
213: {
214: return "raw/text+xml";
215: }
216:
217: protected AttributeList buildAttributeList(final AttributeMap attrs)
218: {
219: final AttributeList attrList = new AttributeList();
220: final String[] namespaces = attrs.getNameSpaces();
221: for (int i = 0; i < namespaces.length; i++)
222: {
223: final String attrNamespace = namespaces[i];
224: if (isInternalNamespace(attrNamespace))
225: {
226: continue;
227: }
228:
229: final Map localAttributes = attrs.getAttributes(attrNamespace);
230: final Iterator entries = localAttributes.entrySet().iterator();
231: while (entries.hasNext())
232: {
233: final Map.Entry entry = (Map.Entry) entries.next();
234: final String key = String.valueOf(entry.getKey());
235: validateNamespace(attrNamespace, attrList);
236: attrList.setAttribute(attrNamespace, key,
237: String.valueOf(entry.getValue()));
238: }
239: }
240: return attrList;
241: }
242:
243: private void validateNamespace(final String uri, final AttributeList list)
244: {
245: if (writer.isNamespaceDefined(uri))
246: {
247: return;
248: }
249:
250: if (list.isNamespaceUriDefined(uri))
251: {
252: return;
253: }
254:
255: final NamespaceDefinition def = getNamespaceByUri(uri);
256: if (def != null)
257: {
258: final String prefix = def.getPreferredPrefix();
259: if (writer.isNamespacePrefixDefined(prefix) == false &&
260: list.isNamespacePrefixDefined(prefix) == false)
261: {
262: list.addNamespaceDeclaration (prefix, uri);
263: }
264: else
265: {
266: list.addNamespaceDeclaration
267: (namespacePrefixGenerator.generateName(prefix), uri);
268: }
269: }
270: else
271: {
272: list.addNamespaceDeclaration
273: (namespacePrefixGenerator.generateName("auto"), uri);
274: }
275: }
276:
277: private boolean isInternalNamespace(final String namespace)
278: {
279: return JFreeReportInfo.REPORT_NAMESPACE.equals(namespace);
280: }
281:
282: }