FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
xml2html.php
1 #!/usr/bin/php
2 <?php
3 /*
4  Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
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 
29 /*
30  * xml2html
31  *
32  * xml2html [- h] -f <file> [-o <file>] -x <file>
33  *
34  * -h usage
35  * -f file the xml input file
36  * -o optional output file, will overwrite if it exists
37  * -x xsl file to use in the transformation
38  *
39  */
40 
41 $usage = "{$argv[0]} [-h] -f <file> [-o <file>] -x <file>\n" .
42  "Options:\n" .
43  "-h: usage\n" .
44  "-f <file>: the xml input file\n" .
45  "-o <file>: optional output filepath, overwritten if exists. StdOut Default\n" .
46  "-x <file>: the xsl style sheet file to use in the transformation\n";
47 
48 // process options
49 $options = getopt('hf:o:x:');
50 
51 if(array_key_exists('h',$options))
52 {
53  echo "$usage\n";
54  exit(0);
55 }
56 
57 if(array_key_exists('f',$options))
58 {
59  // make sure it exists and readable
60  if(is_readable($options['f']))
61  {
62  $xmlFile = $options['f'];
63  }
64  else
65  {
66  echo "FATAL: xml file {$options['f']} does not exist or cannot be read\n";
67  exit(1);
68  }
69 }
70 
71 if(array_key_exists('o',$options))
72 {
73  $outputFile = $options['o'];
74 }
75 
76 if(array_key_exists('x',$options))
77 {
78  // make sure it exists and readable
79  if(is_readable($options['x']))
80  {
81  $xslFile = $options['x'];
82  }
83  else
84  {
85  echo "FATAL: xsl file {$options['x']} does not exist or cannot be read\n";
86  exit(1);
87  }
88 }
89 
90 /* Debug
91 echo "XML2J: after parameter processing\n";
92 echo "infile:$xmlFile,\nout:$outputFile,\nxsl:$xslFile\n";
93 echo "XMLJ2: we are operating at:" . getcwd() . "\n";
94 */
95 
96 $xsl = new XSLTProcessor();
97 $xsldoc = new DOMDocument();
98 $xsldoc->load($xslFile);
99 $xsl->importStyleSheet($xsldoc);
100 
101 $xmldoc = new DOMDocument();
102 @$xmldoc->load($xmlFile);
103 @$transformed = $xsl->transformToXML($xmldoc);
104 
105 if($outputFile)
106 {
107  // open file and write output
108  $OF = fopen($outputFile, 'w') or
109  die("Fatal cannot open output file $outputFile\n");
110  $wrote = fwrite($OF, $transformed);
111  fclose($OF);
112 }
113 else
114 {
115  // no output file, echo to stdout
116  echo $transformed;
117 }
118 ?>
if(!$Test &&$OptionQ) if($stdin_flag) if($Verbose) else
Definition: cp2foss.php:551
Fatal($msg, $filenm, $lineno)
Write message to stdout and die.
Definition: common-ui.php:75