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

File MacroBlockDumper.java

 
testIllegalArgumentException: Unsupported block [org.xwiki.rendering.block.WordBlock].
 

Coverage histogram

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

Code metrics

10
32
5
1
124
80
11
0.34
6.4
5
2.2

Classes

Class Line # Actions
MacroBlockDumper 48 32 0% 11 3
0.936170293.6%
 

Contributing tests

No tests hitting this source file were found.

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.rendering.signature.internal;
21   
22    import java.io.ByteArrayOutputStream;
23    import java.io.IOException;
24    import java.io.OutputStream;
25    import java.io.UnsupportedEncodingException;
26    import java.util.Map;
27   
28    import javax.inject.Named;
29    import javax.inject.Singleton;
30   
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.rendering.block.Block;
33    import org.xwiki.rendering.block.MacroBlock;
34    import org.xwiki.rendering.block.MacroMarkerBlock;
35    import org.xwiki.rendering.block.MetaDataBlock;
36    import org.xwiki.rendering.block.match.MetadataBlockMatcher;
37    import org.xwiki.rendering.listener.MetaData;
38   
39    /**
40    * Dump a {@link MacroBlock} or a {@link MacroMarkerBlock} into a binary stream.
41    *
42    * @version $Id: 6a43be035b6d24fa5197c9baefb5763073afd951 $
43    * @since 6.1M2
44    */
45    @Component
46    @Named("macro")
47    @Singleton
 
48    public class MacroBlockDumper implements BlockDumper
49    {
50    /**
51    * {@inheritDoc}
52    *
53    * The dump contains all the macro parameters and the content of the macro.
54    * The source MetaData is also included if available.
55    *
56    */
 
57  24 toggle @Override
58    public void dump(OutputStream out, Block block) throws IOException
59    {
60  24 if (block instanceof MacroBlock) {
61  20 MacroBlock b = (MacroBlock) block;
62  20 dump(out, b.getId(), b.getParameters(), b.getContent());
63  3 } else if (block instanceof MacroMarkerBlock) {
64  3 MacroMarkerBlock b = (MacroMarkerBlock) block;
65  3 dump(out, b.getId(), b.getParameters(), b.getContent());
66    } else {
67  0 Test failure here throw new IllegalArgumentException("Unsupported block [" + block.getClass().getName() + "].");
68    }
69   
70  23 String source = getSourceReference(block);
71  23 if (source != null) {
72  5 out.write(toBytes(source));
73    }
74  23 out.write(0x00);
75    }
76   
 
77  24 toggle @Override
78    public byte[] dump(Block block) throws IOException
79    {
80  24 ByteArrayOutputStream out = new ByteArrayOutputStream();
81  24 Test failure here dump(out, block);
82  23 return out.toByteArray();
83    }
84   
 
85  23 toggle private static void dump(OutputStream out, String macroId, Map<String, String> parameters, String content)
86    throws IOException
87    {
88  23 out.write(toBytes(macroId));
89  23 out.write(0x00);
90   
91  23 for (Map.Entry<String, String> param : parameters.entrySet()) {
92  10 out.write(toBytes(param.getKey()));
93  10 out.write(0x00);
94  10 out.write(toBytes(param.getValue()));
95  10 out.write(0x00);
96    }
97  23 out.write(0x00);
98   
99  23 if (content != null) {
100  22 out.write(toBytes(content));
101    }
102  23 out.write(0x00);
103    }
104   
 
105  23 toggle private String getSourceReference(Block block)
106    {
107  23 MetaDataBlock metaDataBlock =
108    block.getFirstBlock(new MetadataBlockMatcher(MetaData.SOURCE), Block.Axes.ANCESTOR);
109  23 if (metaDataBlock != null) {
110  6 return (String) metaDataBlock.getMetaData().getMetaData(MetaData.SOURCE);
111    }
112  17 return null;
113    }
114   
 
115  70 toggle private static byte[] toBytes(String s)
116    {
117  70 try {
118  70 return s.getBytes("UTF-8");
119    } catch (UnsupportedEncodingException ignored) {
120    // Should never happen since UTF-8 is a requirement for any JVM.
121    }
122  0 return s.getBytes();
123    }
124    }