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

File XDOMBuilder.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

4
14
5
1
105
49
10
0.71
2.8
5
2

Classes

Class Line # Actions
XDOMBuilder 38 14 0% 10 6
0.7391304473.9%
 

Contributing tests

This file is covered by 1163 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   
21    package org.xwiki.rendering.internal.parser;
22   
23    import java.util.ArrayDeque;
24    import java.util.ArrayList;
25    import java.util.Deque;
26    import java.util.List;
27    import java.util.NoSuchElementException;
28   
29    import org.xwiki.rendering.block.Block;
30    import org.xwiki.rendering.block.XDOM;
31   
32    /**
33    * Helper class to build a {@link XDOM} from listener events.
34    *
35    * @version $Id: 5edba782ad0156ca3939554b429aac55ab5e69c4 $
36    * @since 6.0M1
37    */
 
38    public class XDOMBuilder
39    {
40    private Deque<List<Block>> stack = new ArrayDeque<List<Block>>();
41   
42    /**
43    * Default constructor.
44    */
 
45  54324 toggle public XDOMBuilder()
46    {
47  54327 startBlockList();
48    }
49   
50    /**
51    * @return the resulting {@link XDOM}.
52    */
 
53  54317 toggle public XDOM getXDOM()
54    {
55  54316 List<Block> blocks = endBlockList();
56   
57  54322 if (!this.stack.isEmpty()) {
58  0 throw new IllegalStateException("Unbalanced begin/end Block events, missing " + this.stack.size()
59    + " calls to endBlockList().");
60    }
61   
62    // support even events without begin/endDocument for partial content
63  54324 if (!blocks.isEmpty() && blocks.get(0) instanceof XDOM) {
64  54321 return (XDOM) blocks.get(0);
65    } else {
66  0 return new XDOM(blocks);
67    }
68    }
69   
70    /**
71    * Start a new container element.
72    */
 
73  168595 toggle public void startBlockList()
74    {
75  168594 this.stack.push(new ArrayList<Block>());
76    }
77   
78    /**
79    * End a container element.
80    *
81    * @return the list of blocks in that container.
82    */
 
83  168588 toggle public List<Block> endBlockList()
84    {
85  168588 try {
86  168596 return this.stack.pop();
87    } catch (NoSuchElementException e) {
88  0 throw new IllegalStateException("Unbalanced begin/end Block events, too many calls to endBlockList().");
89    }
90    }
91   
92    /**
93    * Add a block to the current block container.
94    *
95    * @param block the block to be added.
96    */
 
97  1755570 toggle public void addBlock(Block block)
98    {
99  1755573 try {
100  1755576 this.stack.getFirst().add(block);
101    } catch (NoSuchElementException e) {
102  0 throw new IllegalStateException("All container blocks are closed, too many calls to endBlockList().");
103    }
104    }
105    }