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

File DocumentSplitterUtils.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

6
13
3
1
97
46
6
0.46
4.33
3
2

Classes

Class Line # Actions
DocumentSplitterUtils 44 13 0% 6 13
0.409090940.9%
 

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.splitter;
21   
22    import java.util.HashMap;
23    import java.util.List;
24    import java.util.Map;
25   
26    import org.xwiki.bridge.DocumentAccessBridge;
27    import org.xwiki.officeimporter.OfficeImporterException;
28    import org.xwiki.officeimporter.document.XDOMOfficeDocument;
29    import org.xwiki.refactoring.WikiDocument;
30    import org.xwiki.refactoring.splitter.criterion.naming.HeadingNameNamingCriterion;
31    import org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion;
32    import org.xwiki.refactoring.splitter.criterion.naming.PageIndexNamingCriterion;
33    import org.xwiki.rendering.block.ImageBlock;
34    import org.xwiki.rendering.block.Block.Axes;
35    import org.xwiki.rendering.block.match.ClassBlockMatcher;
36    import org.xwiki.rendering.renderer.BlockRenderer;
37   
38    /**
39    * Utility class for the {@link org.xwiki.refactoring.splitter.DocumentSplitter} implementations.
40    *
41    * @version $Id: 9d58c13ad07defbf6786f5181a8775a449336e8a $
42    * @since 4.1M1
43    */
 
44    public final class DocumentSplitterUtils
45    {
46    /** Private constructor preventing the instantiation of this utility class. */
 
47  0 toggle private DocumentSplitterUtils()
48    {
49    // Utility class, should not be instantiated
50    }
51   
52    /**
53    * Utility method for building a {@link NamingCriterion} based on the parameters provided.
54    *
55    * @param namingCriterionId naming criterion identifier.
56    * @param baseDocument reference document name to be used when generating names.
57    * @param docBridge bridge needed by the actual {@link NamingCriterion} implementations
58    * @param plainTextRenderer renderer needed by the actual {@link NamingCriterion} implementations
59    * @return a {@link NamingCriterion} based on the parameters provided.
60    * @throws OfficeImporterException if there is no naming criterion matching the given naming criterion id.
61    */
 
62  1 toggle public static NamingCriterion getNamingCriterion(String namingCriterionId, String baseDocument,
63    DocumentAccessBridge docBridge, BlockRenderer plainTextRenderer)
64    throws OfficeImporterException
65    {
66    // TODO: This code needs to be refactored along with the xwiki-refactoring module code.
67  1 if (namingCriterionId.equals("headingNames")) {
68  1 return new HeadingNameNamingCriterion(baseDocument, docBridge, plainTextRenderer, false);
69  0 } else if (namingCriterionId.equals("mainPageNameAndHeading")) {
70  0 return new HeadingNameNamingCriterion(baseDocument, docBridge, plainTextRenderer, true);
71  0 } else if (namingCriterionId.equals("mainPageNameAndNumbering")) {
72  0 return new PageIndexNamingCriterion(baseDocument, docBridge);
73    } else {
74  0 throw new OfficeImporterException("The specified naming criterion is not implemented yet.");
75    }
76    }
77   
78    /**
79    * Move artifacts (i.e. embedded images) from the original office document to a specific wiki document corresponding
80    * to a section. Only the artifacts from that section are moved.
81    *
82    * @param sectionDoc the newly created wiki document corresponding to a section of the original office document
83    * @param officeDocument the office document being splitted into wiki documents
84    * @return the relocated artifacts
85    */
 
86  5 toggle public static Map<String, byte[]> relocateArtifacts(WikiDocument sectionDoc, XDOMOfficeDocument officeDocument)
87    {
88  5 Map<String, byte[]> artifacts = new HashMap<String, byte[]>();
89  5 List<ImageBlock> imageBlocks =
90    sectionDoc.getXdom().getBlocks(new ClassBlockMatcher(ImageBlock.class), Axes.DESCENDANT);
91  5 for (ImageBlock imageBlock : imageBlocks) {
92  0 String imageReference = imageBlock.getReference().getReference();
93  0 artifacts.put(imageReference, officeDocument.getArtifacts().remove(imageReference));
94    }
95  5 return artifacts;
96    }
97    }