<commit_before><?php
/* This file is part of the XP framework's examples
 *
 * $Id$
 */
  require('lang.base.php');
  xp::sapi('cli');
  uses(
    'com.sun.webstart.jnlp.JnlpDocument', 
    'peer.http.HttpConnection',
    'lang.System',
    'lang.Process',
    'io.File',
    'io.Folder'
  );
  
  // {{{ main
  $p= &new ParamString();
  if (!$p->exists(1)) {
    Console::writeLinef('Usage: %s <url_to_jnlp_file> [--java=<java_executable>]', $p->value(0));
    exit(1);
  }
  
  Console::writeLine('===> Downloading webstart URL ', $p->value(1));
  try(); {
    $c= &new HttpConnection($p->value(1));
    $response= &$c->get();
    $document= '';
    while (FALSE !== ($buf= $response->readData())) {
      $document.= preg_replace('/&(?!(amp;))/', '&amp;', $buf);
    }
    delete($c);
  } if (catch('Exception', $e)) {
    $e->printStackTrace();
    exit(-1);
  }
  
  try(); {
    $j= &JnlpDocument::fromString($document);
  } if (catch('Exception', $e)) {
    $e->printStackTrace();
    exit(-1);
  }
  
  // Print out information provided by the JNLP information section
  $inf= &$j->getInformation();
  Console::writeLinef(
    "---> Application is %s (%s)\n     Vendor %s (see %s)", 
    $inf->getTitle(),
    $inf->getDescription(JNLP_DESCR_SHORT),
    $inf->getVendor(),
    $inf->getHomepage()
  );
  
  // Create an application directory
  $folder= &new Folder(basename($j->getCodebase()));
  try(); {
    if (!$folder->exists()) $folder->create();
  } if (catch('Exception', $e)) {
    $e->printStackTrace();
    exit(-1);
  }
  
  // Download all JAR files, adding them to the classpath as we go
  $classpath= System::getEnv('CLASSPATH');
  $properties= '';
  Console::writeLinef('---> Processing resources from codebase %s', $j->getCodebase());
  foreach ($j->getResources() as $resource) {
    switch (xp::typeOf($resource)) {

      // A JAR file, download it
      case 'com.sun.webstart.jnlp.JnlpJarResource':
        $href= $resource->getHref();
        $classpath.= ':'.rtrim($folder->getURI(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$href;
        try(); {
          $download= TRUE;

          // Create a new file instance
          $jar= &new File($folder->getURI().DIRECTORY_SEPARATOR.$href);
          if ($jar->exists()) {
            Console::writef('     >> Have %s... ', $jar->getURI());
            
            // TBI: Check if remote file is newer
            $download= FALSE;
          }

          if (!$download) {
            Console::writeLine('(cached)');
            break;
          }
          
          // Download it
          $c= &new HttpConnection($j->getCodebase().'/'.$href);
          Console::writef('     >> Downloading %s... ', $href);
          $response= &$c->get();
          Console::writef(
            'Status %d, Length %d of %s... ', 
            $response->getStatusCode(), 
            $response->getHeader('Content-length'), 
            $response->getHeader('Content-type')
          );

          // Check if this file resided in a subdirectory. If so, create this
          // subdirectory if necessary
          $f= &new Folder(dirname($jar->getURI()));
          if (!$f->exists()) $f->create();

          $jar->open(FILE_MODE_WRITE);
          while (FALSE !== ($buf= $response->readData(0x2000, $binary= TRUE))) {
            $jar->write($buf);
          }
          $jar->close();
        } if (catch('Exception', $e)) {
          Console::writeLine('FAIL');
          $e->printStackTrace();
          exit(-1);
        }
        Console::writeLine('OK');
        break;

      // A property, add it to the Java command line
      case 'com.sun.webstart.jnlp.JnlpPropertyResource':
        Console::writeLine('     >> Have property ', $resource->getName(), '=', $resource->getValue());
        $properties.= ' -D'.$resource->getName().'='.$resource->getValue();
        break;
      
      // Ignore anything else
      default:
        Console::writeLine('     >> Ignoring ['.$resource->getClassName().']');
    }
  }
  
  // 
  
  // Execute Java
  $app= &$j->getApplicationDesc();<commit_msg>- Make cached jars distinguishable<commit_after><?php
/* This file is part of the XP framework's examples
 *
 * $Id$
 */
  require('lang.base.php');
  xp::sapi('cli');
  uses(
    'com.sun.webstart.jnlp.JnlpDocument', 
    'peer.http.HttpConnection',
    'lang.System',
    'lang.Process',
    'io.File',
    'io.Folder'
  );
  
  // {{{ main
  $p= &new ParamString();
  if (!$p->exists(1)) {
    Console::writeLinef('Usage: %s <url_to_jnlp_file> [--java=<java_executable>]', $p->value(0));
    exit(1);
  }
  
  Console::writeLine('===> Downloading webstart URL ', $p->value(1));
  try(); {
    $c= &new HttpConnection($p->value(1));
    $response= &$c->get();
    $document= '';
    while (FALSE !== ($buf= $response->readData())) {
      $document.= preg_replace('/&(?!(amp;))/', '&amp;', $buf);
    }
    delete($c);
  } if (catch('Exception', $e)) {
    $e->printStackTrace();
    exit(-1);
  }
  
  try(); {
    $j= &JnlpDocument::fromString($document);
  } if (catch('Exception', $e)) {
    $e->printStackTrace();
    exit(-1);
  }
  
  // Print out information provided by the JNLP information section
  $inf= &$j->getInformation();
  Console::writeLinef(
    "---> Application is %s (%s)\n     Vendor %s (see %s)", 
    $inf->getTitle(),
    $inf->getDescription(JNLP_DESCR_SHORT),
    $inf->getVendor(),
    $inf->getHomepage()
  );
  
  // Create an application directory
  $folder= &new Folder(basename($j->getCodebase()));
  try(); {
    if (!$folder->exists()) $folder->create();
  } if (catch('Exception', $e)) {
    $e->printStackTrace();
    exit(-1);
  }
  
  // Download all JAR files, adding them to the classpath as we go
  $classpath= System::getEnv('CLASSPATH');
  $properties= '';
  Console::writeLinef('---> Processing resources from codebase %s', $j->getCodebase());
  foreach ($j->getResources() as $resource) {
    switch (xp::typeOf($resource)) {

      // A JAR file, download it
      case 'com.sun.webstart.jnlp.JnlpJarResource':
        $href= $resource->getHref();
        $classpath.= ':'.rtrim($folder->getURI(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$href;
        try(); {
          $download= TRUE;

          // Create a new file instance
          $jar= &new File($folder->getURI().DIRECTORY_SEPARATOR.$href);
          if ($jar->exists()) {
            Console::writef('     >> Have %s... ', $jar->getURI());
            
            // TBI: Check if remote file is newer
            $download= FALSE;
          }

          if (!$download) {
            Console::writeLine('(cached)');
            break;
          }
          
          // Download it
          $c= &new HttpConnection($j->getCodebase().'/'.$href);
          Console::writef('     >> Downloading %s... ', $href);
          $response= &$c->get();
          Console::writef(
            'Status %d, Length %d of %s... ', 
            $response->getStatusCode(), 
            $response->getHeader('Content-length'), 
            $response->getHeader('Content-type')
          );

          // Check if this file resided in a subdirectory. If so, create this
          // subdirectory if necessary
          $f= &new Folder(dirname($jar->getURI()));
          if (!$f->exists()) $f->create();

          $jar->open(FILE_MODE_WRITE);
          while (FALSE !== ($buf= $response->readData(0x2000, $binary= TRUE))) {
            $jar->write($buf);
          }
          $jar->close();
        } if (catch('Exception', $e)) {
          Console::writeLine('FAIL');
          $e->printStackTrace();
          exit(-1);
        }
        Console::writeLine('OK');
        break;

      // A property, add it to the Java command line
      case 'com.sun.webstart.jnlp.JnlpPropertyResource':
        Console::writeLine('     >> Have property ', $resource->getName(), '=', $resource->getValue());
        $properties.= ' -D'.$resource->getName().'=\''.$resource->getValue().'\'';
        break;
      
      // Ignore anything else
      default:
        Console::writeLine('     >> Ignoring ['.$resource->getClassName().']');
    }
  }
  
  // 
  
  // Execute Java
  $app= &$j->getApplicationDesc();
{'fraction_non_alphanumeric': 0.19867873746023978, 'fraction_numerical': 0.0034254954734524103, 'mean_word_length': 2.308377175232699, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6682758', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2574, 'n_words': 533}