b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | # |
| 2 | # All Rights Reserved |
| 3 | # |
| 4 | # MARVELL CONFIDENTIAL |
| 5 | # Copyright 2012 Marvell International Ltd All Rights Reserved. |
| 6 | # The source code contained or described herein and all documents related to |
| 7 | # the source code ("Material") are owned by Marvell International Ltd or its |
| 8 | # suppliers or licensors. Title to the Material remains with Marvell International Ltd |
| 9 | # or its suppliers and licensors. The Material contains trade secrets and |
| 10 | # proprietary and confidential information of Marvell or its suppliers and |
| 11 | # licensors. The Material is protected by worldwide copyright and trade secret |
| 12 | # laws and treaty provisions. No part of the Material may be used, copied, |
| 13 | # reproduced, modified, published, uploaded, posted, transmitted, distributed, |
| 14 | # or disclosed in any way without Marvell's prior express written permission. |
| 15 | # |
| 16 | # No license under any patent, copyright, trade secret or other intellectual |
| 17 | # property right is granted to or conferred upon you by disclosure or delivery |
| 18 | # of the Materials, either expressly, by implication, inducement, estoppel or |
| 19 | # otherwise. Any license under such intellectual property rights must be |
| 20 | # express and approved by Marvell in writing. |
| 21 | # |
| 22 | #!/usr/bin/perl -w |
| 23 | |
| 24 | use strict; |
| 25 | use Spreadsheet::ParseExcel; |
| 26 | use XML::DOM; |
| 27 | use XML::Twig; |
| 28 | use IO::File; |
| 29 | |
| 30 | my ($inputFile, $outputFile) = @ARGV; |
| 31 | my $file_type = substr($inputFile, -4, 5); |
| 32 | die "Input file type error $inputFile" unless $file_type eq ".xls"; |
| 33 | die "Cen't open $inputFile" unless -r $inputFile; |
| 34 | |
| 35 | # define node names |
| 36 | my %headline = ('Operation'=>0, 'ReferencePath'=>1, 'Component'=>2, 'ControlProtocol'=>3, 'Comment'=>4, 'Register'=>5, 'Value'=>6, 'Misc'=>7); |
| 37 | my %str_headline = (0=>'Operation', 1=>'ReferencePath', 2=>'Component', 3=>'ControlProtocol', 4=>'Comment', 5=>'Register', 6=>'Value', 7=>'Misc'); |
| 38 | my %row_type = ('Operation'=>0, 'Component'=>1, 'Comment'=>2, 'Register'=>3); |
| 39 | |
| 40 | #open input excel |
| 41 | my $parser_excel = Spreadsheet::ParseExcel->new(); |
| 42 | my $workbook = $parser_excel ->parse($inputFile); |
| 43 | die $parser_excel->error() unless defined $workbook; |
| 44 | |
| 45 | #open parser for output xml |
| 46 | my $xml = "<MarvellAudioPathConfiguration/>"; |
| 47 | my $parser_xml = new XML::DOM::Parser; |
| 48 | my $dom = $parser_xml->parse($xml); |
| 49 | die $parser_xml->error() unless defined $dom; |
| 50 | |
| 51 | # Write <?xml version="1.0"?> and <MarvellAudioPathConfiguration> element |
| 52 | my $Decl = $dom->createXMLDecl("1.0"); |
| 53 | $dom->setXMLDecl($Decl); |
| 54 | |
| 55 | for my $worksheet ( $workbook->worksheets() ) { |
| 56 | |
| 57 | if ($worksheet->get_name() eq 'PathToAliasList') { |
| 58 | my ( $row_min, $row_max ) = $worksheet->row_range(); |
| 59 | for my $row ( $row_min+1 .. $row_max ) { |
| 60 | my $AudioPath = $dom->createElement("AudioPath"); |
| 61 | my $path_name = $worksheet->get_cell($row, 1)->value; |
| 62 | $AudioPath->setAttribute("identifier", $path_name); |
| 63 | my $alias_name = $worksheet->get_cell($row, 0)->value; |
| 64 | $AudioPath->setAttribute("alias", $alias_name); |
| 65 | $dom->getDocumentElement->appendChild($AudioPath); |
| 66 | } |
| 67 | next; |
| 68 | } |
| 69 | |
| 70 | # Generate AudioPath node. |
| 71 | my $AudioPath = $dom->createElement("AudioPath"); |
| 72 | |
| 73 | # The first row is the audio path name. |
| 74 | my $path_name = $worksheet->get_cell(0, 0)->value; |
| 75 | $AudioPath->setAttribute("identifier", $path_name); |
| 76 | |
| 77 | my $Operation; |
| 78 | my $Component; |
| 79 | my $Comment; |
| 80 | my $Register; |
| 81 | my $str_Component_cur; |
| 82 | |
| 83 | my ( $row_min, $row_max ) = $worksheet->row_range(); |
| 84 | my ( $col_min, $col_max ) = $worksheet->col_range(); |
| 85 | |
| 86 | #ingore second row since it is the headline |
| 87 | for my $row ( $row_min+2 .. $row_max ) { |
| 88 | my $str_Operation = undef; |
| 89 | my $str_Refpath = undef; |
| 90 | my $str_Component = undef; |
| 91 | my $str_CtlProtocol = undef; |
| 92 | my $str_Comment = undef; |
| 93 | my $str_Register = undef; |
| 94 | my $str_Value = undef; |
| 95 | my $str_Misc = undef; |
| 96 | my $row_type = undef; |
| 97 | |
| 98 | #parse each row |
| 99 | for my $col ( $col_min .. $col_max ) { |
| 100 | my $cell = $worksheet->get_cell( $row, $col ); |
| 101 | next unless $cell; |
| 102 | |
| 103 | if ($str_headline{$col} eq 'Operation'){ |
| 104 | #Operation |
| 105 | $row_type = $row_type{'Operation'}; |
| 106 | $str_Operation = $cell->value(); |
| 107 | }elsif($str_headline{$col} eq 'ReferencePath'){ |
| 108 | #ReferencePath |
| 109 | $str_Refpath = $cell->value(); |
| 110 | }elsif($str_headline{$col} eq 'Component'){ |
| 111 | #Component |
| 112 | $row_type = $row_type{'Component'}; |
| 113 | $str_Component = $cell->value(); |
| 114 | }elsif($str_headline{$col} eq 'ControlProtocol'){ |
| 115 | #ControlProtocol |
| 116 | $str_CtlProtocol = $cell->value(); |
| 117 | }elsif($str_headline{$col} eq 'Comment'){ |
| 118 | #Comment |
| 119 | $row_type = $row_type{'Comment'}; |
| 120 | $str_Comment = $cell->value(); |
| 121 | }elsif($str_headline{$col} eq 'Register'){ |
| 122 | #Register |
| 123 | $row_type = $row_type{'Register'}; |
| 124 | $str_Register = $cell->value(); |
| 125 | }elsif($str_headline{$col} eq 'Value'){ |
| 126 | #Value |
| 127 | $str_Value = $cell->value(); |
| 128 | }elsif($str_headline{$col} eq 'Misc'){ |
| 129 | #Misc |
| 130 | $str_Misc = $cell->value(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if ($row_type == $row_type{'Operation'}){ |
| 135 | $Operation->appendChild($Component) if defined $Component && defined $Operation; #append the last Component Node |
| 136 | $Component = undef; |
| 137 | $AudioPath->appendChild($Operation) if defined $Operation; #append the old Operation Node |
| 138 | $Operation = $dom->createElement('Operation'); #create new Operation node |
| 139 | $Operation->setAttribute("identifier", $str_Operation) if $str_Operation; |
| 140 | $Operation->setAttribute("ref_path_identifier", $str_Refpath) if $str_Refpath; |
| 141 | }elsif($row_type == $row_type{'Component'}){ |
| 142 | $Operation->appendChild($Component) if defined $Component; #append the old Component Node |
| 143 | $Component = $dom->createElement('AudioComponent'); #create new Component node |
| 144 | $Component->setAttribute("identifier", $str_Component) if $str_Component; |
| 145 | $str_Component_cur = $str_Component; |
| 146 | $Component->setAttribute("control_protocol", $str_CtlProtocol) if $str_CtlProtocol; |
| 147 | }elsif($row_type == $row_type{'Comment'}){ |
| 148 | $Comment = $dom->createComment($str_Comment); #create new Comment node |
| 149 | $Component->appendChild($Comment) if defined $Component; #append the old Comment Node |
| 150 | }elsif($row_type == $row_type{'Register'}){ |
| 151 | if($str_Component_cur eq 'DELAY'){ |
| 152 | $Register = $dom->createElement('Delay'); #create new Delay node |
| 153 | $Register->setAttribute("value", $str_Register) if defined $str_Register; |
| 154 | }elsif($str_Component_cur eq 'GPIO_PORT'){ |
| 155 | $Register = $dom->createElement('GPIO'); #create new GPIO node |
| 156 | $Register->setAttribute("name", $str_Register) if defined $str_Register; |
| 157 | $Register->setAttribute("port", $str_Value) if defined $str_Value; |
| 158 | $Register->setAttribute("value", $str_Misc) if defined $str_Misc; |
| 159 | }else{ |
| 160 | $Register = $dom->createElement('Register'); #create new Register node |
| 161 | $Register->setAttribute("field", $str_Register) if defined $str_Register; |
| 162 | $Register->setAttribute("value", $str_Value) if defined $str_Value; |
| 163 | $Register->setAttribute("misc", $str_Misc) if defined $str_Misc; |
| 164 | } |
| 165 | $Component->appendChild($Register); #append the old Register Node |
| 166 | } |
| 167 | $row_type = undef; |
| 168 | } |
| 169 | #append the last Component Node |
| 170 | $Operation->appendChild($Component) if defined $Component && defined $Operation; |
| 171 | #append the last Operation Node |
| 172 | $AudioPath->appendChild($Operation) if defined $Operation; |
| 173 | #append AudioPath |
| 174 | $dom->getDocumentElement->appendChild($AudioPath); |
| 175 | } |
| 176 | |
| 177 | # end of parsing, flush to output file |
| 178 | open OUTPUT, ">$outputFile"; |
| 179 | select OUTPUT; |
| 180 | my $twig = new XML::Twig; |
| 181 | $twig->set_indent(" "x4); |
| 182 | $twig->parse($dom->toString); |
| 183 | $twig->set_pretty_print("indented"); |
| 184 | print $twig->sprint; |
| 185 | select STDOUT; |
| 186 | close OUTPUT |