2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package org
.libreoffice
.report
.pentaho
.output
;
20 import org
.libreoffice
.report
.pentaho
.OfficeNamespaces
;
21 import org
.libreoffice
.report
.pentaho
.model
.DataStyle
;
22 import org
.libreoffice
.report
.pentaho
.model
.FontFaceDeclsSection
;
23 import org
.libreoffice
.report
.pentaho
.model
.FontFaceElement
;
24 import org
.libreoffice
.report
.pentaho
.model
.OfficeMasterPage
;
25 import org
.libreoffice
.report
.pentaho
.model
.OfficeMasterStyles
;
26 import org
.libreoffice
.report
.pentaho
.model
.OfficeStyle
;
27 import org
.libreoffice
.report
.pentaho
.model
.OfficeStyles
;
28 import org
.libreoffice
.report
.pentaho
.model
.OfficeStylesCollection
;
29 import org
.libreoffice
.report
.pentaho
.model
.PageLayout
;
30 import org
.libreoffice
.report
.pentaho
.model
.RawText
;
32 import java
.io
.IOException
;
33 import java
.io
.Writer
;
35 import java
.util
.Iterator
;
37 import java
.util
.TreeMap
;
39 import org
.jfree
.layouting
.namespace
.Namespaces
;
40 import org
.jfree
.layouting
.util
.AttributeMap
;
41 import org
.jfree
.report
.JFreeReportBoot
;
42 import org
.jfree
.report
.JFreeReportInfo
;
43 import org
.jfree
.report
.structure
.Element
;
44 import org
.jfree
.report
.structure
.Node
;
45 import org
.jfree
.report
.structure
.Section
;
46 import org
.jfree
.report
.structure
.StaticText
;
48 import org
.pentaho
.reporting
.libraries
.xmlns
.common
.AttributeList
;
49 import org
.pentaho
.reporting
.libraries
.xmlns
.writer
.DefaultTagDescription
;
50 import org
.pentaho
.reporting
.libraries
.xmlns
.writer
.XmlWriter
;
51 import org
.pentaho
.reporting
.libraries
.xmlns
.writer
.XmlWriterSupport
;
55 * This class writes the style collection into a 'styles.xml' document.
59 public class StylesWriter
62 private final XmlWriter xmlWriter
;
63 private final boolean writeOpeningTag
;
65 public StylesWriter(final Writer writer
)
67 final DefaultTagDescription tagDescription
= new DefaultTagDescription();
68 tagDescription
.configure(JFreeReportBoot
.getInstance().getGlobalConfig(),
69 OfficeDocumentReportTarget
.TAG_DEF_PREFIX
);
71 this.xmlWriter
= new XmlWriter(writer
, tagDescription
);
72 this.xmlWriter
.setAlwaysAddNamespace(true);
73 this.writeOpeningTag
= true;
76 public StylesWriter(final XmlWriter xmlWriter
)
78 this.xmlWriter
= xmlWriter
;
79 this.writeOpeningTag
= false;
82 public void writeContentStyles(
83 final OfficeStylesCollection globals
)
86 writeFontFaces(new OfficeStylesCollection
[]
90 writeAutomaticStylesSection(new OfficeStylesCollection
[]
96 public void writeGlobalStyles(
97 final OfficeStylesCollection globals
)
102 performWriteRootTag();
105 writeFontFaces(new OfficeStylesCollection
[]
109 writeCommonStylesSection(new OfficeStylesCollection
[]
113 writeAutomaticStylesSection(new OfficeStylesCollection
[]
117 writeMasterStylesSection(new OfficeStylesCollection
[]
124 xmlWriter
.writeCloseTag();
128 private void writeMasterStylesSection(final OfficeStylesCollection
[] osc
)
131 xmlWriter
.writeTag(OfficeNamespaces
.OFFICE_NS
, "master-styles", XmlWriterSupport
.OPEN
);
132 for (int sci
= 0; sci
< osc
.length
; sci
++)
134 final OfficeStylesCollection collection
= osc
[sci
];
135 final OfficeMasterStyles officeStyles
= collection
.getMasterStyles();
136 final OfficeMasterPage
[] officeMasterPages
= officeStyles
.getAllMasterPages();
137 for (int i
= 0; i
< officeMasterPages
.length
; i
++)
139 final OfficeMasterPage masterPage
= officeMasterPages
[i
];
140 writeSection(masterPage
);
143 writeSectionChildren(officeStyles
.getOtherNodes().getNodeArray());
146 xmlWriter
.writeCloseTag();
149 private void writeCommonStylesSection(final OfficeStylesCollection
[] osc
)
152 xmlWriter
.writeTag(OfficeNamespaces
.OFFICE_NS
, "styles", XmlWriterSupport
.OPEN
);
154 for (int sci
= 0; sci
< osc
.length
; sci
++)
156 final OfficeStylesCollection collection
= osc
[sci
];
157 final OfficeStyles officeStyles
= collection
.getCommonStyles();
158 writeStyles(officeStyles
);
161 xmlWriter
.writeCloseTag();
164 private void writeAutomaticStylesSection(final OfficeStylesCollection
[] osc
)
167 xmlWriter
.writeTag(OfficeNamespaces
.OFFICE_NS
, "automatic-styles", XmlWriterSupport
.OPEN
);
168 for (int sci
= 0; sci
< osc
.length
; sci
++)
170 final OfficeStylesCollection collection
= osc
[sci
];
171 final OfficeStyles officeStyles
= collection
.getAutomaticStyles();
172 writeStyles(officeStyles
);
175 xmlWriter
.writeCloseTag();
178 private void writeFontFaces(final OfficeStylesCollection
[] osc
)
181 xmlWriter
.writeTag(OfficeNamespaces
.OFFICE_NS
, "font-face-decls", XmlWriterSupport
.OPEN
);
183 final TreeMap
<String
,FontFaceElement
> fontFaces
= new TreeMap
<String
,FontFaceElement
>();
184 for (int sci
= 0; sci
< osc
.length
; sci
++)
186 final OfficeStylesCollection collection
= osc
[sci
];
187 final FontFaceDeclsSection fontFaceDecls
= collection
.getFontFaceDecls();
188 final FontFaceElement
[] fontFaceElements
= fontFaceDecls
.getAllFontFaces();
189 for (int i
= 0; i
< fontFaceElements
.length
; i
++)
191 final FontFaceElement element
= fontFaceElements
[i
];
192 fontFaces
.put(element
.getStyleName(), element
);
196 final Iterator
<FontFaceElement
> values
= fontFaces
.values().iterator();
197 while (values
.hasNext())
199 final FontFaceElement element
= values
.next();
200 writeElement(element
);
203 xmlWriter
.writeCloseTag();
206 private void writeStyles(final OfficeStyles styles
)
209 final OfficeStyle
[] allStyles
= styles
.getAllStyles();
210 for (int i
= 0; i
< allStyles
.length
; i
++)
212 final OfficeStyle style
= allStyles
[i
];
216 final DataStyle
[] allDataStyles
= styles
.getAllDataStyles();
217 for (int i
= 0; i
< allDataStyles
.length
; i
++)
219 final DataStyle style
= allDataStyles
[i
];
223 final PageLayout
[] allPageStyles
= styles
.getAllPageStyles();
224 for (int i
= 0; i
< allPageStyles
.length
; i
++)
226 final PageLayout style
= allPageStyles
[i
];
230 writeSectionChildren(styles
.getOtherStyles());
233 private void writeElement(final Element element
)
236 final String type
= element
.getType();
237 final String namespace
= element
.getNamespace();
238 final AttributeList attrList
= buildAttributeList(element
.getAttributeMap());
239 xmlWriter
.writeTag(namespace
, type
, attrList
, XmlWriterSupport
.CLOSE
);
242 private void writeSection(final Section section
)
245 final String type
= section
.getType();
246 final String namespace
= section
.getNamespace();
247 final AttributeList attrList
= buildAttributeList(section
.getAttributeMap());
248 if (section
.getNodeCount() == 0)
250 xmlWriter
.writeTag(namespace
, type
, attrList
, XmlWriterSupport
.CLOSE
);
254 xmlWriter
.writeTag(namespace
, type
, attrList
, XmlWriterSupport
.OPEN
);
255 writeSectionChildren(section
.getNodeArray());
257 xmlWriter
.writeCloseTag();
260 private void writeSectionChildren(final Node
[] nodes
)
263 for (int i
= 0; i
< nodes
.length
; i
++)
265 final Node node
= nodes
[i
];
266 if (node
instanceof Section
)
268 writeSection((Section
) node
);
270 else if (node
instanceof Element
)
272 writeElement((Element
) node
);
274 else if (node
instanceof RawText
)
276 final RawText text
= (RawText
) node
;
277 xmlWriter
.writeText(text
.getText());
279 else if (node
instanceof StaticText
)
281 final StaticText text
= (StaticText
) node
;
282 xmlWriter
.writeTextNormalized(text
.getText(), false);
287 private AttributeList
buildAttributeList(final AttributeMap attrs
)
289 final AttributeList attrList
= new AttributeList();
290 final String
[] namespaces
= attrs
.getNameSpaces();
291 for (int i
= 0; i
< namespaces
.length
; i
++)
293 final String attrNamespace
= namespaces
[i
];
294 if (isFilteredNamespace(attrNamespace
))
299 final Map localAttributes
= attrs
.getAttributes(attrNamespace
);
300 final Iterator entries
= localAttributes
.entrySet().iterator();
301 while (entries
.hasNext())
303 final Map
.Entry entry
= (Map
.Entry
) entries
.next();
304 final String key
= String
.valueOf(entry
.getKey());
305 attrList
.setAttribute(attrNamespace
, key
, String
.valueOf(entry
.getValue()));
311 private boolean isFilteredNamespace(final String namespace
)
313 if (Namespaces
.LIBLAYOUT_NAMESPACE
.equals(namespace
))
317 if (JFreeReportInfo
.REPORT_NAMESPACE
.equals(namespace
))
321 if (JFreeReportInfo
.COMPATIBILITY_NAMESPACE
.equals(namespace
))
325 if (OfficeNamespaces
.OOREPORT_NS
.equals(namespace
))
332 private void performWriteRootTag()
335 final AttributeList rootAttributes
= new AttributeList();
336 rootAttributes
.addNamespaceDeclaration("office",
337 OfficeNamespaces
.OFFICE_NS
);
338 rootAttributes
.addNamespaceDeclaration("style", OfficeNamespaces
.STYLE_NS
);
339 rootAttributes
.addNamespaceDeclaration("text", OfficeNamespaces
.TEXT_NS
);
340 rootAttributes
.addNamespaceDeclaration("table", OfficeNamespaces
.TABLE_NS
);
341 rootAttributes
.addNamespaceDeclaration("draw", OfficeNamespaces
.DRAWING_NS
);
342 rootAttributes
.addNamespaceDeclaration("fo", OfficeNamespaces
.FO_NS
);
343 rootAttributes
.addNamespaceDeclaration("xlink", OfficeNamespaces
.XLINK_NS
);
344 rootAttributes
.addNamespaceDeclaration("dc", OfficeNamespaces
.PURL_NS
);
345 rootAttributes
.addNamespaceDeclaration("meta", OfficeNamespaces
.META_NS
);
346 rootAttributes
.addNamespaceDeclaration("number",
347 OfficeNamespaces
.DATASTYLE_NS
);
348 rootAttributes
.addNamespaceDeclaration("svg", OfficeNamespaces
.SVG_NS
);
349 rootAttributes
.addNamespaceDeclaration("chart", OfficeNamespaces
.CHART_NS
);
350 rootAttributes
.addNamespaceDeclaration("chartooo", OfficeNamespaces
.CHARTOOO_NS
);
351 rootAttributes
.addNamespaceDeclaration("dr3d", OfficeNamespaces
.DR3D_NS
);
352 rootAttributes
.addNamespaceDeclaration("math", OfficeNamespaces
.MATHML_NS
);
353 rootAttributes
.addNamespaceDeclaration("form", OfficeNamespaces
.FORM_NS
);
354 rootAttributes
.addNamespaceDeclaration("script",
355 OfficeNamespaces
.SCRIPT_NS
);
356 rootAttributes
.addNamespaceDeclaration("ooo", OfficeNamespaces
.OO2004_NS
);
357 rootAttributes
.addNamespaceDeclaration("ooow", OfficeNamespaces
.OOW2004_NS
);
358 rootAttributes
.addNamespaceDeclaration("oooc", OfficeNamespaces
.OOC2004_NS
);
359 rootAttributes
.addNamespaceDeclaration("dom",
360 OfficeNamespaces
.XML_EVENT_NS
);
361 rootAttributes
.addNamespaceDeclaration("xforms",
362 OfficeNamespaces
.XFORMS_NS
);
363 rootAttributes
.addNamespaceDeclaration("xsd", OfficeNamespaces
.XSD_NS
);
364 rootAttributes
.addNamespaceDeclaration("xsi", OfficeNamespaces
.XSI_NS
);
365 rootAttributes
.addNamespaceDeclaration("grddl", OfficeNamespaces
.GRDDL_NS
);
366 rootAttributes
.setAttribute(OfficeNamespaces
.OFFICE_NS
, "version",
367 OfficeDocumentReportTarget
.ODF_VERSION
);
369 this.xmlWriter
.writeXmlDeclaration("UTF-8");
370 this.xmlWriter
.writeTag(OfficeNamespaces
.OFFICE_NS
,
371 "document-styles", rootAttributes
, XmlWriterSupport
.OPEN
);