FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
XmlImportSource.php
1 <?php
2 /*
3  * Copyright (C) 2017, Siemens AG
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 namespace Fossology\ReportImport;
20 
21 
23 require_once 'ImportSource.php';
24 
25 class XmlImportSource implements ImportSource
26 {
28  private $filename;
30  private $xml;
31 
33  private $datas = array();
34 
39  function __construct($filename)
40  {
41  $this->filename = $filename;
42  }
43 
47  public function parse()
48  {
49  $this->xml = simplexml_load_file($this->filename, null, LIBXML_NOCDATA);
50 
52  $licenses = $this->xml->xpath("License");
54  $copyrights = $this->xml->xpath("Copyright");
55 
56  $this->parseLicenseInformation($licenses);
57  $this->parseCopyrightInformation($copyrights);
58 
59  return true;
60  }
61 
66  private function splitFilesList($filesNode)
67  {
68  $files = array();
69 
70  $separator = "\r\n";
71  $line = strtok((string) $filesNode, $separator);
72 
73  while ($line !== false) {
74  if(! array_key_exists($line, $this->datas))
75  {
76  $this->datas[$line] = new ReportImportData();
77  }
78  $files[] = $line;
79  $line = strtok($separator);
80  }
81 
82  return $files;
83  }
84 
88  private function parseLicenseInformation($licenses)
89  {
90  foreach ($licenses as $licenseNode)
91  {
92  $attributes = $licenseNode->attributes();
93  $licenseName = (string) $attributes["name"];
94  $licenseId = $attributes["spdxidentifier"] !== NULL ? (string) $attributes["spdxidentifier"] : $licenseName;
95  $licenseText = (string) $licenseNode->xpath("Content")[0];
96  $item = new ReportImportDataItem($licenseId);
97  $item->setLicenseCandidate($licenseName, $licenseText, false);
98  $item->setCustomText((string) $licenseNode);
99 
100  foreach ($licenseNode->xpath("Files") as $filesNode)
101  {
102  $files = $this->splitFilesList($filesNode);
103  foreach ($files as $file)
104  {
105  $this->datas[$file]->addLicenseInfoInFile($item);
106  }
107  }
108  }
109  }
110 
114  private function parseCopyrightInformation($copyrights)
115  {
116  foreach ($copyrights as $copyrightNode)
117  {
118  foreach ($copyrightNode->xpath("Files") as $filesNode)
119  {
120  $files = $this->splitFilesList($filesNode);
121  foreach ($files as $file) {
122  foreach ($copyrightNode->xpath("Content") as $content) {
123  $this->datas[$file]->addCopyrightText((string)$content);
124  }
125  }
126  }
127  }
128  }
129 
133  public function getAllFiles()
134  {
135  $allFiles = array();
136  foreach ($this->datas as $fileName => $data)
137  {
138  $allFiles[$fileName] = $fileName;
139  }
140  return $allFiles;
141  }
142 
147  public function getHashesMap($fileid)
148  {
149  return array();
150  }
151 
156  public function getDataForFile($fileid)
157  {
158  return $this->datas[$fileid];
159  }
160 }