FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
TextRenderer.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 Author: Andreas Würl
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program 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
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\View;
21 
22 
25 
26 if (! defined('ENT_SUBSTITUTE')) {
27  define('ENT_SUBSTITUTE', 0);
28 }
29 
31 {
35  private $highlightRenderer;
36 
37  public function __construct(HighlightRenderer $highlightRenderer)
38  {
39  $this->highlightRenderer = $highlightRenderer;
40  }
41 
48  public function renderText(TextFragment $fragment, $splitPositions = array(), $insertBacklink = false)
49  {
50  $state = new HighlightState($this->highlightRenderer, $insertBacklink);
51  $result = $this->render($fragment, $state, new PagedTextResult($fragment->getStartOffset()), $splitPositions);
52  return $result->getText();
53  }
54 
60  public function renderHex(TextFragment $fragment, $splitPositions = array())
61  {
62  $state = new HighlightState($this->highlightRenderer);
63  $result = $this->render($fragment, $state, new PagedHexResult($fragment->getStartOffset(), $state), $splitPositions);
64  return $result->getText();
65  }
66 
74  public function render(TextFragment $fragment, HighlightState $state, PagedResult $result, $splitPositions = array())
75  {
76  foreach ($splitPositions as $actionPosition => $entries) {
77  $isBeforeVisibleRange = $actionPosition < $fragment->getStartOffset();
78  $isAfterVisibleRange = $actionPosition >= $fragment->getEndOffset();
79  if ($isBeforeVisibleRange || $isAfterVisibleRange) {
80  $this->processEntriesOutsideVisibleRange($fragment, $state, $result, $entries, $isAfterVisibleRange);
81  } else {
82  $this->processEntriesWithinVisibleRange($fragment, $state, $result, $actionPosition, $entries);
83  }
84  }
85  $this->finalizeContentText($fragment, $state, $result);
86 
87  return $result;
88  }
89 
97  protected function processEntriesOutsideVisibleRange(TextFragment $fragment, HighlightState $state, PagedResult $result, $entries, $isAfterVisibleRange)
98  {
99  if ($isAfterVisibleRange) {
100  $this->finalizeContentText($fragment, $state, $result);
101  }
102  $state->processSplitEntries($entries, $result);
103  }
104 
112  protected function processEntriesWithinVisibleRange(TextFragment $fragment, HighlightState $state, PagedResult $result, $actionPosition, $entries)
113  {
114  if ($result->isEmpty()) {
115  $state->openExistingElements($result);
116  }
117  $result->appendContentText($fragment->getSlice($result->getCurrentOffset(), $actionPosition));
118  $state->insertElements($entries, $result);
119  assert($result->getCurrentOffset() == $actionPosition);
120  }
121 
127  protected function finalizeContentText(TextFragment $fragment, HighlightState $state, PagedResult $result)
128  {
129  if ($result->getCurrentOffset() < $fragment->getEndOffset()) {
130  if ($result->isEmpty()) {
131  $state->openExistingElements($result);
132  }
133  $result->appendContentText($fragment->getSlice($result->getCurrentOffset()));
134  $state->closeOpenElements($result);
135  }
136  }
137 }
render(TextFragment $fragment, HighlightState $state, PagedResult $result, $splitPositions=array())
processEntriesWithinVisibleRange(TextFragment $fragment, HighlightState $state, PagedResult $result, $actionPosition, $entries)
insertElements($entries, PagedResult $result)
closeOpenElements(PagedResult $result)
finalizeContentText(TextFragment $fragment, HighlightState $state, PagedResult $result)
processEntriesOutsideVisibleRange(TextFragment $fragment, HighlightState $state, PagedResult $result, $entries, $isAfterVisibleRange)
openExistingElements(PagedResult $result)
renderHex(TextFragment $fragment, $splitPositions=array())
renderText(TextFragment $fragment, $splitPositions=array(), $insertBacklink=false)