-
-
Notifications
You must be signed in to change notification settings - Fork 542
/
XDOMOfficeDocument.java
193 lines (177 loc) · 6.01 KB
/
XDOMOfficeDocument.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.officeimporter.document;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.officeimporter.converter.OfficeConverterResult;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.block.HeaderBlock;
import org.xwiki.rendering.block.SectionBlock;
import org.xwiki.rendering.block.XDOM;
import org.xwiki.rendering.renderer.BlockRenderer;
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
import org.xwiki.rendering.renderer.printer.WikiPrinter;
import org.xwiki.stability.Unstable;
/**
* An {@link OfficeDocument} backed by an {@link XDOM} document.
*
* @version $Id$
* @since 2.1M1
*/
public class XDOMOfficeDocument implements OfficeDocument
{
/**
* {@link XDOM} corresponding to office document content.
*/
private XDOM xdom;
/**
* Artifacts for this office document.
*/
private Set<File> artifactFiles;
/**
* {@link ComponentManager} used to lookup for various renderers.
*/
private ComponentManager componentManager;
private OfficeConverterResult converterResult;
/**
* Creates a new {@link XDOMOfficeDocument}.
*
* @param xdom {@link XDOM} corresponding to office document content.
* @param artifactFiles artifacts for this office document.
* @param componentManager {@link ComponentManager} used to lookup for various renderers.
* @param converterResult the {@link OfficeConverterResult} used to build that object.
* @since 13.1RC1
*/
@Unstable
public XDOMOfficeDocument(XDOM xdom, Set<File> artifactFiles, ComponentManager componentManager,
OfficeConverterResult converterResult)
{
this.xdom = xdom;
this.artifactFiles = artifactFiles;
this.componentManager = componentManager;
this.converterResult = converterResult;
}
@Override
public XDOM getContentDocument()
{
return this.xdom;
}
@Override
public String getContentAsString()
{
return getContentAsString("xwiki/2.0");
}
/**
* Renders the XDOM encapsulated by this document into the given syntax.
*
* @param syntaxId string identifier of the syntax.
* @return content of this document in the given syntax or null if the syntax is invalid.
*/
public String getContentAsString(String syntaxId)
{
try {
WikiPrinter printer = new DefaultWikiPrinter();
BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, syntaxId);
renderer.render(this.xdom, printer);
return printer.toString();
} catch (ComponentLookupException ex) {
// Nothing to do here.
}
return null;
}
@Override
public Set<File> getArtifactsFiles()
{
return this.artifactFiles;
}
/**
* Tries to extract a title suitable for this document. This is done by navigating the internal {@link XDOM} and
* finding a matching header block.
*
* @return a title suitable for this document or null if no title can be found.
*/
public String getTitle()
{
String title = getTitle(this.xdom);
if (null != title) {
// Strip line-feed and new-line characters if present.
title = title.replaceAll("[\n\r]", "");
// Truncate long titles.
if (title.length() > 255) {
title = title.substring(0, 255);
}
}
return title;
}
/**
* Utility method for recursively traversing the XDOM and extracting a title.
*
* @param parent parent block.
* @return a title if found or null.
*/
private String getTitle(Block parent)
{
for (Block block : parent.getChildren()) {
if (block instanceof HeaderBlock) {
String title = renderTitle((HeaderBlock) block);
if (!StringUtils.isBlank(title)) {
return title;
}
} else if (block instanceof SectionBlock) {
return getTitle(block);
}
}
return null;
}
/**
* Utility method for rendering a title.
*
* @param header header block which contains the title.
* @return header block content rendered as a string.
*/
private String renderTitle(HeaderBlock header)
{
try {
WikiPrinter printer = new DefaultWikiPrinter();
BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, "plain/1.0");
renderer.render(header, printer);
return printer.toString();
} catch (ComponentLookupException ex) {
// Ignore.
}
return null;
}
@Override
public void close() throws IOException
{
if (this.converterResult != null) {
this.converterResult.close();
}
}
@Override
public OfficeConverterResult getConverterResult()
{
return this.converterResult;
}
}