1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.officeimporter.document

File XDOMOfficeDocument.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

10
32
8
1
168
86
15
0.47
4
8
1.88

Classes

Class Line # Actions
XDOMOfficeDocument 41 32 0% 15 7
0.8686%
 

Contributing tests

This file is covered by 7 tests. .

Source view

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