FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
HighlightState.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, 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 
27 {
28  const PLACEHOLDER = " # ";
29 
31  private $elementStack;
33  private $highlightRenderer;
35  private $anchorDrawn;
37  private $insertBacklink;
38 
39  public function __construct(HighlightRenderer $highlightRenderer, $insertBacklink = false)
40  {
41  $this->highlightRenderer = $highlightRenderer;
42  $this->insertBacklink = $insertBacklink;
43  $this->elementStack = array();
44  }
45 
49  public function push(SplitPosition $splitPosition)
50  {
51  array_push($this->elementStack, $splitPosition);
52  }
53 
57  public function pop()
58  {
59  return array_pop($this->elementStack);
60  }
61 
65  public function getElementStack()
66  {
67  return $this->elementStack;
68  }
69 
73  public function processSplitEntries($entries)
74  {
75  foreach ($entries as $entry) {
76  switch ($entry->getAction()) {
77  case SplitPosition::START:
78  $this->push($entry);
79  $this->checkForAnchor($entry);
80  break;
81  case SplitPosition::END:
82  $this->pop();
83  break;
84  }
85  }
86  }
87 
92  public function insertElements($entries, PagedResult $result)
93  {
94  foreach ($entries as $entry) {
95  switch ($entry->getAction()) {
96  case SplitPosition::START:
97  $this->push($entry);
98  $result->appendMetaText($this->startSpan($entry));
99  break;
100  case SplitPosition::ATOM:
101  $result->appendMetaText($this->startSpan($entry));
102  $result->appendMetaText(
103  self::PLACEHOLDER . $this->highlightRenderer->createSpanEnd($entry));
104  break;
105 
106  case SplitPosition::END:
107  $this->pop();
108  $result->appendMetaText(
109  $this->highlightRenderer->createSpanEnd($entry));
110  break;
111  }
112  }
113  }
114 
118  public function closeOpenElements(PagedResult $result)
119  {
120  foreach ($this->elementStack as $splitPosition) {
121  $result->appendMetaText(
122  $this->highlightRenderer->createSpanEnd($splitPosition));
123  }
124  }
125 
129  public function openExistingElements(PagedResult $result)
130  {
131  foreach ($this->elementStack as $entry) {
132  $result->appendMetaText($this->highlightRenderer->createSpanStart($entry));
133  }
134  }
135 
140  protected function startSpan(SplitPosition $entry)
141  {
142  $anchorText = $this->checkForAnchor($entry)
143  ? "<a id=\"highlight\"" . ($this->insertBacklink ? " href=\"#top\">&nbsp;&#8593;&nbsp;" : ">") . "</a>"
144  : "";
145 
146  return $anchorText . $this->highlightRenderer->createSpanStart($entry);
147  }
148 
153  protected function checkForAnchor(SplitPosition $entry)
154  {
155  $shouldShowAnchor = !$this->anchorDrawn && $entry->getHighlight()->getType() != Highlight::KEYWORD;
156  if ($shouldShowAnchor) {
157  $this->anchorDrawn = true;
158  }
159  return $shouldShowAnchor;
160  }
161 }
insertElements($entries, PagedResult $result)
closeOpenElements(PagedResult $result)
checkForAnchor(SplitPosition $entry)
openExistingElements(PagedResult $result)
push(SplitPosition $splitPosition)