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

File DefaultXDOMOfficeDocumentBuilder.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
10
2
1
105
58
3
0.3
5
2
1.5

Classes

Class Line # Actions
DefaultXDOMOfficeDocumentBuilder 53 10 0% 3 1
0.916666791.7%
 

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.officeimporter.internal.builder;
21   
22    import java.io.InputStream;
23    import java.io.StringReader;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.w3c.dom.Document;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.component.manager.ComponentManager;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.model.reference.EntityReferenceSerializer;
34    import org.xwiki.officeimporter.OfficeImporterException;
35    import org.xwiki.officeimporter.builder.XDOMOfficeDocumentBuilder;
36    import org.xwiki.officeimporter.builder.XHTMLOfficeDocumentBuilder;
37    import org.xwiki.officeimporter.document.XDOMOfficeDocument;
38    import org.xwiki.officeimporter.document.XHTMLOfficeDocument;
39    import org.xwiki.rendering.block.XDOM;
40    import org.xwiki.rendering.listener.MetaData;
41    import org.xwiki.rendering.parser.ParseException;
42    import org.xwiki.rendering.parser.Parser;
43    import org.xwiki.xml.html.HTMLUtils;
44   
45    /**
46    * Default implementation of {@link XDOMOfficeDocumentBuilder}.
47    *
48    * @version $Id: ffaf768a6703b526128987cd1e6d4caf67a4cef8 $
49    * @since 2.1M1
50    */
51    @Component
52    @Singleton
 
53    public class DefaultXDOMOfficeDocumentBuilder implements XDOMOfficeDocumentBuilder
54    {
55    /**
56    * Xhtml office document builder used internally.
57    */
58    @Inject
59    private XHTMLOfficeDocumentBuilder xhtmlOfficeDocumentBuilder;
60   
61    /**
62    * XHTML/1.0 syntax parser used to build an XDOM from an XHTML input.
63    */
64    @Inject
65    @Named("xhtml/1.0")
66    private Parser xHtmlParser;
67   
68    /**
69    * Component manager to be passed into XDOMOfficeDocument.
70    */
71    @Inject
72    private ComponentManager componentManager;
73   
74    /**
75    * Used to serialize the target document reference.
76    */
77    @Inject
78    private EntityReferenceSerializer<String> entityReferenceSerializer;
79   
 
80  1 toggle @Override
81    public XDOMOfficeDocument build(InputStream officeFileStream, String officeFileName, DocumentReference reference,
82    boolean filterStyles) throws OfficeImporterException
83    {
84  1 XDOMOfficeDocument xdomOfficeDocument =
85    build(this.xhtmlOfficeDocumentBuilder.build(officeFileStream, officeFileName, reference, filterStyles));
86    // Make sure references are resolved relative to the target document reference.
87  1 xdomOfficeDocument.getContentDocument().getMetaData()
88    .addMetaData(MetaData.BASE, entityReferenceSerializer.serialize(reference));
89  1 return xdomOfficeDocument;
90    }
91   
 
92  1 toggle @Override
93    public XDOMOfficeDocument build(XHTMLOfficeDocument xhtmlOfficeDocument) throws OfficeImporterException
94    {
95  1 Document xhtmlDoc = xhtmlOfficeDocument.getContentDocument();
96  1 HTMLUtils.stripHTMLEnvelope(xhtmlDoc);
97  1 XDOM xdom = null;
98  1 try {
99  1 xdom = this.xHtmlParser.parse(new StringReader(HTMLUtils.toString(xhtmlDoc)));
100    } catch (ParseException ex) {
101  0 throw new OfficeImporterException("Error: Could not parse xhtml office content.", ex);
102    }
103  1 return new XDOMOfficeDocument(xdom, xhtmlOfficeDocument.getArtifacts(), this.componentManager);
104    }
105    }