1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wysiwyg.server.internal.plugin.importer

File OfficeMacroImporter.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

2
25
3
1
156
70
6
0.24
8.33
3
2

Classes

Class Line # Actions
OfficeMacroImporter 50 25 0% 6 11
0.633333363.3%
 

Contributing tests

This file is covered by 1 test. .

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.wysiwyg.server.internal.plugin.importer;
21   
22    import java.util.Collections;
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import org.xwiki.bridge.DocumentAccessBridge;
27    import org.xwiki.bridge.DocumentModelBridge;
28    import org.xwiki.component.manager.ComponentLookupException;
29    import org.xwiki.component.manager.ComponentManager;
30    import org.xwiki.model.reference.AttachmentReference;
31    import org.xwiki.model.reference.EntityReferenceSerializer;
32    import org.xwiki.rendering.block.Block;
33    import org.xwiki.rendering.block.MacroBlock;
34    import org.xwiki.rendering.block.XDOM;
35    import org.xwiki.rendering.internal.transformation.MutableRenderingContext;
36    import org.xwiki.rendering.listener.MetaData;
37    import org.xwiki.rendering.renderer.BlockRenderer;
38    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
39    import org.xwiki.rendering.renderer.printer.WikiPrinter;
40    import org.xwiki.rendering.transformation.RenderingContext;
41    import org.xwiki.rendering.transformation.Transformation;
42    import org.xwiki.rendering.transformation.TransformationContext;
43   
44    /**
45    * Used to import an office document using the office macro (rather than converting and including the content of the
46    * office file).
47    *
48    * @version $Id: 220627ef282174eaf41b8441594c896e14115fa8 $
49    */
 
50    public class OfficeMacroImporter
51    {
52    /**
53    * Used to update the rendering context.
54    */
55    private final RenderingContext renderingContext;
56   
57    /**
58    * The component used to execute the XDOM macro transformations before rendering to XHTML.
59    * <p>
60    * NOTE: We execute only macro transformations because they are the only transformations protected by the WYSIWYG
61    * editor. We should use the transformation manager once generic transformation markers are implemented in the
62    * rendering module and the WYSIWYG editor supports them.
63    *
64    * @see <a href="http://jira.xwiki.org/browse/XRENDERING-78">XWIKI-3260: Add markers to modified XDOM by
65    * Transformations/Macros</a>
66    */
67    private final Transformation macroTransformation;
68   
69    /**
70    * The component used to render a XDOM to XHTML.
71    */
72    private final BlockRenderer xhtmlRenderer;
73   
74    /**
75    * Used to serialize the reference of the document that has the office file attachment.
76    */
77    private final EntityReferenceSerializer<String> entityReferenceSerializer;
78   
79    /**
80    * Used to find out the Syntax of the document containing the attachment to render.
81    */
82    private final DocumentAccessBridge documentAccessBridge;
83   
84    /**
85    * Creates a new instance.
86    *
87    * @param componentManager the component manager
88    */
 
89  1 toggle public OfficeMacroImporter(ComponentManager componentManager)
90    {
91  1 try {
92  1 renderingContext = componentManager.getInstance(RenderingContext.class);
93  1 macroTransformation = componentManager.getInstance(Transformation.class, "macro");
94  1 xhtmlRenderer = componentManager.getInstance(BlockRenderer.class, "annotatedxhtml/1.0");
95  1 entityReferenceSerializer = componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING);
96  1 documentAccessBridge = componentManager.getInstance(DocumentAccessBridge.class);
97    } catch (ComponentLookupException e) {
98  0 throw new RuntimeException("Failed to initialize the office importer based on office macro.", e);
99    }
100    }
101   
102    /**
103    * Builds an XDOM with a single block, the office macro.
104    *
105    * @param attachmentReference the office file to be viewed
106    * @param filterStyles whether to filter text styles such as font, color, alignment, margins, etc.
107    * @return the XDOM that can be rendered to view the office file
108    */
 
109  1 toggle public XDOM buildXDOM(AttachmentReference attachmentReference, boolean filterStyles)
110    {
111  1 Map<String, String> macroParams = new HashMap<String, String>();
112  1 macroParams.put("attachment", attachmentReference.getName());
113  1 if (!filterStyles) {
114  0 macroParams.put("filterStyles", "false");
115    }
116  1 MacroBlock officeMacro = new MacroBlock("office", macroParams, false);
117   
118  1 XDOM xdom = new XDOM(Collections.<Block> singletonList(officeMacro));
119   
120    // Since we're generating an XDOM block we need to set up the required MetaData information
121   
122    // Set the BASE MetaData
123  1 xdom.getMetaData().addMetaData(MetaData.BASE,
124    entityReferenceSerializer.serialize(attachmentReference.getDocumentReference()));
125   
126    // Set the SYNTAX MetaData
127  1 try {
128  1 DocumentModelBridge document = documentAccessBridge.getDocument(attachmentReference.getDocumentReference());
129  1 xdom.getMetaData().addMetaData(MetaData.SYNTAX, document.getSyntax());
130    } catch (Exception e) {
131  0 throw new RuntimeException(String.format(
132    "Failed to compute Syntax for the document containing attachment [%s]", attachmentReference), e);
133    }
134   
135  1 return xdom;
136    }
137   
138    /**
139    * Renders the given XDOM to the annotated XHTML syntax.
140    *
141    * @param xdom the XDOM to be rendered
142    * @return the result of rendering the given XDOM to annotated XHTML syntax
143    * @throws Exception if the rendering process fails
144    */
 
145  0 toggle public String render(XDOM xdom) throws Exception
146    {
147  0 TransformationContext txContext = new TransformationContext();
148  0 txContext.setXDOM(xdom);
149  0 ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, txContext, xdom);
150   
151  0 WikiPrinter printer = new DefaultWikiPrinter();
152  0 xhtmlRenderer.render(xdom, printer);
153   
154  0 return printer.toString();
155    }
156    }