src/snac/util/EADParser.php
/**
     * Parse a zip file
     *
     * Uses SAXON to parse all the XML in a zip file and returns the contents of a
     * zip file containing the TSV files produced by SAXON's parsing.
     *
     * @param $zipcontents string the contents of the zip file to be parsed
     * @return string The contents of the result zip
     */
Parse a zip file
Uses SAXON to parse all the XML in a zip file and returns the contents of a
zip file containing the TSV files produced by SAXON's parsing.

@param $zipcontents string the contents of the zip file to be parsed
@return string The contents of the result zip
Parse a zip file Uses SAXON to parse all the XML in a zip file and returns the contents of a zip file containing the TSV files produced by SAXON ' s parsing . @param $zipcontents string the contents of the zip file to be parsed @return string The contents of the result zip
public function parseZip($zipcontents) {
        $tmpdir = $this->unzip($zipcontents);
        $outfile = $tmpdir."/output.zip";
        $eaddir = $tmpdir."/ead/";
        $outputdir = $tmpdir."/output";
        $toReturn = false;
        try {
            $errors = $this->validateDirectory($eaddir, false); 
            if (!empty($errors) || $this->eadVersion === false) {
                return $errors;
            }
            $xmlfile = \snac\Config::$EAD_PARSER_DIR."/ead_parse_driver.xml";
            $xslfile = \snac\Config::$EAD_PARSER_DIR."/{$this->eadVersion}ToOR.xsl";
            $tmpoutfile = $tmpdir."/tmpoutput.xml";
            $descriptorspec = array(
                0 => array("pipe", "r"),  
                1 => array("pipe", "w"),  
                2 => array("pipe", "a")
            );
            $pipes = array();
            $process = proc_open("java -cp ".\snac\Config::$SAXON_JARFILE." net.sf.saxon.Transform -s:$xmlfile -xsl:$xslfile -o:$tmpoutfile sourceFolderPath=$eaddir outputFolderPath=$outputdir 2>&1", $descriptorspec, $pipes);
            $procOutput = "";
            if (is_resource($process)) {
                fclose($pipes[0]);
                $procOutput = stream_get_contents($pipes[1]);
                fclose($pipes[1]);
                fclose($pipes[2]);
                $return_value = proc_close($process);
            }
            if ($return_value != 0) {
                $this->logger->addDebug("Error occurred while running SAXON: $procOutput");
                throw new \Exception("Error in SAXON\n$procOutput");
            }
            $this->logger->addDebug("Done Running SAXON");
            $this->postProcess($outputdir);
            $this->logger->addDebug("Creating output ZIP file");
            $zip = new \ZipArchive();
            if ($zip->open($outfile, \ZipArchive::CREATE) !== true) {
                throw new \Exception("Could not create output Zip file");
            }
            $this->logger->addDebug("Adding Zip Content");
            $zip->addFile($outputdir."/Join-Table.tsv", "Join-Table.tsv");
            $zip->addFile($outputdir."/CPF-Table.tsv", "CPF-Table.tsv");
            $zip->addFile($outputdir."/RD-Table.tsv", "RD-Table.tsv");
            $this->logger->addDebug("Done writing Zip");
            $zip->close();
            $this->logger->addDebug("Loading Content of Zip file to return");
            $toReturn = file_get_contents($outfile);
        } catch (\Exception $e) {
            $this->cleanup($tmpdir);
            throw new \snac\exceptions\SNACEADParserException($e);
        }
        $this->logger->addDebug("Cleaning up");
        $this->cleanup($tmpdir);
        $this->logger->addDebug("Returning Zip file");
        return $toReturn;
    }
public function parseZip ( $ zipcontents ) { $ tmpdir = $ this -> unzip ( $ zipcontents ) ; $ outfile = $ tmpdir . " /output.zip " ; $ eaddir = $ tmpdir . " /ead/ " ; $ outputdir = $ tmpdir . " /output " ; $ toReturn = false ; try { $ errors = $ this -> validateDirectory ( $ eaddir , false ) ; if ( ! empty ( $ errors ) || $ this -> eadVersion === false ) { return $ errors ; } $ xmlfile = \ snac \ Config :: $ EAD_PARSER_DIR . " /ead_parse_driver.xml " ; $ xslfile = \ snac \ Config :: $ EAD_PARSER_DIR . " / { $ this -> eadVersion } ToOR.xsl " ; $ tmpoutfile = $ tmpdir . " /tmpoutput.xml " ; $ descriptorspec = array ( 0 => array ( " pipe " , " r " ) , 1 => array ( " pipe " , " w " ) , 2 => array ( " pipe " , " a " ) ) ; $ pipes = array ( ) ; $ process = proc_open ( " java -cp  " . \ snac \ Config :: $ SAXON_JARFILE . "  net.sf.saxon.Transform -s: $ xmlfile  -xsl: $ xslfile  -o: $ tmpoutfile  sourceFolderPath= $ eaddir  outputFolderPath= $ outputdir  2>&1 " , $ descriptorspec , $ pipes ) ; $ procOutput = " " ; if ( is_resource ( $ process ) ) { fclose ( $ pipes [ 0 ] ) ; $ procOutput = stream_get_contents ( $ pipes [ 1 ] ) ; fclose ( $ pipes [ 1 ] ) ; fclose ( $ pipes [ 2 ] ) ; $ return_value = proc_close ( $ process ) ; } if ( $ return_value != 0 ) { $ this -> logger -> addDebug ( " Error occurred while running SAXON:  $ procOutput " ) ; throw new \ Exception ( " Error in SAXON \n $ procOutput " ) ; } $ this -> logger -> addDebug ( " Done Running SAXON " ) ; $ this -> postProcess ( $ outputdir ) ; $ this -> logger -> addDebug ( " Creating output ZIP file " ) ; $ zip = new \ ZipArchive ( ) ; if ( $ zip -> open ( $ outfile , \ ZipArchive :: CREATE ) !== true ) { throw new \ Exception ( " Could not create output Zip file " ) ; } $ this -> logger -> addDebug ( " Adding Zip Content " ) ; $ zip -> addFile ( $ outputdir . " /Join-Table.tsv " , " Join-Table.tsv " ) ; $ zip -> addFile ( $ outputdir . " /CPF-Table.tsv " , " CPF-Table.tsv " ) ; $ zip -> addFile ( $ outputdir . " /RD-Table.tsv " , " RD-Table.tsv " ) ; $ this -> logger -> addDebug ( " Done writing Zip " ) ; $ zip -> close ( ) ; $ this -> logger -> addDebug ( " Loading Content of Zip file to return " ) ; $ toReturn = file_get_contents ( $ outfile ) ; } catch ( \ Exception $ e ) { $ this -> cleanup ( $ tmpdir ) ; throw new \ snac \ exceptions \ SNACEADParserException ( $ e ) ; } $ this -> logger -> addDebug ( " Cleaning up " ) ; $ this -> cleanup ( $ tmpdir ) ; $ this -> logger -> addDebug ( " Returning Zip file " ) ; return $ toReturn ; }
Parse a zip file
 Uses SAXON to parse all the XML in a zip file and returns the contents of a
 zip file containing the TSV files produced by SAXON's parsing.
Parse a zip file Uses SAXON to parse all the XML in a zip file and returns the contents of a zip file containing the TSV files produced by SAXON ' s parsing .
// Validate the XML // only check for well-formedness // Set up the SAXON environment  // Run SAXON // stdin is a pipe that the child will read from // stdout is a pipe that the child will write to // Post-process the SAXON output // Put the files into an exported ZIP file // close zip for downloading // show ZIP file