Example #1
0
 /**
  * Main
  *
  * Exitcodes used:
  * <ul>
  *   <li>127: Archive referenced in -xar [...] does not exist</li>
  *   <li>126: No manifest or manifest does not have a main-class</li>
  * </ul>
  *
  * @see     http://tldp.org/LDP/abs/html/exitcodes.html
  * @param   string[] args
  * @return  int
  */
 public static function main(array $args)
 {
     // Open archive
     $f = new File(array_shift($args));
     if (!$f->exists()) {
         Console::$err->writeLine('*** Cannot find archive ' . $f->getURI());
         return 127;
     }
     // Register class loader
     $cl = ClassLoader::registerLoader(new ArchiveClassLoader(new Archive($f)));
     if (!$cl->providesResource(self::MANIFEST)) {
         Console::$err->writeLine('*** Archive ' . $f->getURI() . ' does not have a manifest');
         return 126;
     }
     // Load manifest
     $pr = Properties::fromString($cl->getResource(self::MANIFEST));
     if (NULL === ($class = $pr->readString('archive', 'main-class', NULL))) {
         Console::$err->writeLine('*** Archive ' . $f->getURI() . '\'s manifest does not have a main class');
         return 126;
     }
     // Run main()
     try {
         return XPClass::forName($class, $cl)->getMethod('main')->invoke(NULL, array($args));
     } catch (TargetInvocationException $e) {
         throw $e->getCause();
     }
 }
 /**
  * Constructor
  *
  * @param   io.File file
  * @throws  lang.IllegalArgumentException if the given file does not exist
  */
 public function __construct(File $file)
 {
     if (!$file->exists()) {
         throw new IllegalArgumentException('File "' . $file->getURI() . '" does not exist!');
     }
     $this->file = $file;
 }
 /**
  * Read from a file
  *
  * @deprecated  Use img.io.MetaDataReader instead
  * @param   io.File file
  * @param   var default default void what should be returned in case no data is found
  * @return  img.util.IptcData
  * @throws  lang.FormatException in case malformed meta data is encountered
  * @throws  lang.ElementNotFoundException in case no meta data is available
  * @throws  img.ImagingException in case reading meta data fails
  */
 public static function fromFile(File $file)
 {
     if (FALSE === getimagesize($file->getURI(), $info)) {
         $e = new ImagingException('Cannot read image information from ' . $file->getURI());
         xp::gc(__FILE__);
         throw $e;
     }
     if (!isset($info['APP13'])) {
         if (func_num_args() > 1) {
             return func_get_arg(1);
         }
         throw new ElementNotFoundException('Cannot get IPTC information from ' . $file->getURI() . ' (no APP13 marker)');
     }
     if (!($iptc = iptcparse($info['APP13']))) {
         throw new FormatException('Cannot parse IPTC information from ' . $file->getURI());
     }
     // Parse creation date
     if (3 == sscanf(@$iptc['2#055'][0], '%4d%2d%d', $year, $month, $day)) {
         $created = Date::create($year, $month, $day, 0, 0, 0);
     } else {
         $created = NULL;
     }
     with($i = new self());
     $i->setTitle(@$iptc['2#005'][0]);
     $i->setUrgency(@$iptc['2#010'][0]);
     $i->setCategory(@$iptc['2#015'][0]);
     $i->setSupplementalCategories(@$iptc['2#020']);
     $i->setKeywords(@$iptc['2#025']);
     $i->setSpecialInstructions(@$iptc['2#040'][0]);
     $i->setDateCreated($created);
     $i->setAuthor(@$iptc['2#080'][0]);
     $i->setAuthorPosition(@$iptc['2#085'][0]);
     $i->setCity(@$iptc['2#090'][0]);
     $i->setState(@$iptc['2#095'][0]);
     $i->setCountry(@$iptc['2#101'][0]);
     $i->setOriginalTransmissionReference(@$iptc['2#103'][0]);
     $i->setHeadline(@$iptc['2#105'][0]);
     $i->setCredit(@$iptc['2#110'][0]);
     $i->setSource(@$iptc['2#115'][0]);
     $i->setCopyrightNotice(@$iptc['2#116'][0]);
     $i->setCaption(@$iptc['2#120'][0]);
     $i->setWriter(@$iptc['2#122'][0]);
     return $i;
 }
Example #4
0
 /**
  * Main method
  *
  * @param   util.cmd.ParamString params
  * @return  int
  */
 public function run(ParamString $params)
 {
     // No arguments given - show our own usage
     if ($params->count < 1) {
         self::$err->writeLine(self::textOf(XPClass::forName(xp::nameOf(__CLASS__))->getComment()));
         return 1;
     }
     // Configure properties
     $pm = PropertyManager::getInstance();
     // Separate runner options from class options
     for ($offset = 0, $i = 0; $i < $params->count; $i++) {
         switch ($params->list[$i]) {
             case '-c':
                 if (0 == strncmp('res://', $params->list[$i + 1], 6)) {
                     $pm->appendSource(new ResourcePropertySource(substr($params->list[$i + 1], 6)));
                 } else {
                     $pm->appendSource(new FilesystemPropertySource($params->list[$i + 1]));
                 }
                 $offset += 2;
                 $i++;
                 break;
             case '-cp':
                 ClassLoader::registerPath($params->list[$i + 1], NULL);
                 $offset += 2;
                 $i++;
                 break;
             case '-v':
                 $this->verbose = TRUE;
                 $offset += 1;
                 $i++;
                 break;
             default:
                 break 2;
         }
     }
     // Sanity check
     if (!$params->exists($offset)) {
         self::$err->writeLine('*** Missing classname');
         return 1;
     }
     // Use default path for PropertyManager if no sources set
     if (!$pm->getSources()) {
         $pm->configure(self::DEFAULT_CONFIG_PATH);
     }
     unset($params->list[-1]);
     $classname = $params->value($offset);
     $classparams = new ParamString(array_slice($params->list, $offset + 1));
     // Class file or class name
     if (strstr($classname, xp::CLASS_FILE_EXT)) {
         $file = new File($classname);
         if (!$file->exists()) {
             self::$err->writeLine('*** Cannot load class from non-existant file ', $classname);
             return 1;
         }
         $uri = $file->getURI();
         $path = dirname($uri);
         $paths = array_flip(array_map('realpath', xp::$classpath));
         $class = NULL;
         while (FALSE !== ($pos = strrpos($path, DIRECTORY_SEPARATOR))) {
             if (isset($paths[$path])) {
                 $class = XPClass::forName(strtr(substr($uri, strlen($path) + 1, -10), DIRECTORY_SEPARATOR, '.'));
                 break;
             }
             $path = substr($path, 0, $pos);
         }
         if (!$class) {
             self::$err->writeLine('*** Cannot load class from ', $file);
             return 1;
         }
     } else {
         try {
             $class = XPClass::forName($classname);
         } catch (ClassNotFoundException $e) {
             self::$err->writeLine('*** ', $this->verbose ? $e : $e->getMessage());
             return 1;
         }
     }
     // Check whether class is runnable
     if (!$class->isSubclassOf('lang.Runnable')) {
         self::$err->writeLine('*** ', $class->getName(), ' is not runnable');
         return 1;
     }
     // Usage
     if ($classparams->exists('help', '?')) {
         self::showUsage($class);
         return 0;
     }
     // Load, instantiate and initialize
     $l = Logger::getInstance();
     $pm->hasProperties('log') && $l->configure($pm->getProperties('log'));
     $cm = ConnectionManager::getInstance();
     $pm->hasProperties('database') && $cm->configure($pm->getProperties('database'));
     // Setup logger context for all registered log categories
     foreach (Logger::getInstance()->getCategories() as $category) {
         if (NULL === ($context = $category->getContext()) || !$context instanceof EnvironmentAware) {
             continue;
         }
         $context->setHostname(System::getProperty('host.name'));
         $context->setRunner($this->getClassName());
         $context->setInstance($class->getName());
         $context->setResource(NULL);
         $context->setParams($params->string);
     }
     $instance = $class->newInstance();
     $instance->in = self::$in;
     $instance->out = self::$out;
     $instance->err = self::$err;
     $methods = $class->getMethods();
     // Injection
     foreach ($methods as $method) {
         if (!$method->hasAnnotation('inject')) {
             continue;
         }
         $inject = $method->getAnnotation('inject');
         if (isset($inject['type'])) {
             $type = $inject['type'];
         } else {
             if ($restriction = $method->getParameter(0)->getTypeRestriction()) {
                 $type = $restriction->getName();
             } else {
                 $type = $method->getParameter(0)->getType()->getName();
             }
         }
         try {
             switch ($type) {
                 case 'rdbms.DBConnection':
                     $args = array($cm->getByHost($inject['name'], 0));
                     break;
                 case 'util.Properties':
                     $p = $pm->getProperties($inject['name']);
                     // If a PropertyAccess is retrieved which is not a util.Properties,
                     // then, for BC sake, convert it into a util.Properties
                     if ($p instanceof PropertyAccess && !$p instanceof Properties) {
                         $convert = Properties::fromString('');
                         $section = $p->getFirstSection();
                         while ($section) {
                             // HACK: Properties::writeSection() would first attempts to
                             // read the whole file, we cannot make use of it.
                             $convert->_data[$section] = $p->readSection($section);
                             $section = $p->getNextSection();
                         }
                         $args = array($convert);
                     } else {
                         $args = array($p);
                     }
                     break;
                 case 'util.log.LogCategory':
                     $args = array($l->getCategory($inject['name']));
                     break;
                 default:
                     self::$err->writeLine('*** Unknown injection type "' . $type . '" at method "' . $method->getName() . '"');
                     return 2;
             }
             $method->invoke($instance, $args);
         } catch (TargetInvocationException $e) {
             self::$err->writeLine('*** Error injecting ' . $type . ' ' . $inject['name'] . ': ' . $e->getCause()->compoundMessage());
             return 2;
         } catch (Throwable $e) {
             self::$err->writeLine('*** Error injecting ' . $type . ' ' . $inject['name'] . ': ' . $e->compoundMessage());
             return 2;
         }
     }
     // Arguments
     foreach ($methods as $method) {
         if ($method->hasAnnotation('args')) {
             // Pass all arguments
             if (!$method->hasAnnotation('args', 'select')) {
                 $begin = 0;
                 $end = $classparams->count;
                 $pass = array_slice($classparams->list, 0, $end);
             } else {
                 $pass = array();
                 foreach (preg_split('/, ?/', $method->getAnnotation('args', 'select')) as $def) {
                     if (is_numeric($def) || '-' == $def[0]) {
                         $pass[] = $classparams->value((int) $def);
                     } else {
                         sscanf($def, '[%d..%d]', $begin, $end);
                         isset($begin) || ($begin = 0);
                         isset($end) || ($end = $classparams->count - 1);
                         while ($begin <= $end) {
                             $pass[] = $classparams->value($begin++);
                         }
                     }
                 }
             }
             try {
                 $method->invoke($instance, array($pass));
             } catch (Throwable $e) {
                 self::$err->writeLine('*** Error for arguments ' . $begin . '..' . $end . ': ', $this->verbose ? $e : $e->getMessage());
                 return 2;
             }
         } else {
             if ($method->hasAnnotation('arg')) {
                 // Pass arguments
                 $arg = $method->getAnnotation('arg');
                 if (isset($arg['position'])) {
                     $name = '#' . ($arg['position'] + 1);
                     $select = intval($arg['position']);
                     $short = NULL;
                 } else {
                     if (isset($arg['name'])) {
                         $name = $select = $arg['name'];
                         $short = isset($arg['short']) ? $arg['short'] : NULL;
                     } else {
                         $name = $select = strtolower(preg_replace('/^set/', '', $method->getName()));
                         $short = isset($arg['short']) ? $arg['short'] : NULL;
                     }
                 }
                 if (0 == $method->numParameters()) {
                     if (!$classparams->exists($select, $short)) {
                         continue;
                     }
                     $args = array();
                 } else {
                     if (!$classparams->exists($select, $short)) {
                         list($first, ) = $method->getParameters();
                         if (!$first->isOptional()) {
                             self::$err->writeLine('*** Argument ' . $name . ' does not exist!');
                             return 2;
                         }
                         $args = array();
                     } else {
                         $args = array($classparams->value($select, $short));
                     }
                 }
                 try {
                     $method->invoke($instance, $args);
                 } catch (TargetInvocationException $e) {
                     self::$err->writeLine('*** Error for argument ' . $name . ': ', $this->verbose ? $e->getCause() : $e->getCause()->compoundMessage());
                     return 2;
                 }
             }
         }
     }
     try {
         $instance->run();
     } catch (Throwable $t) {
         self::$err->writeLine('*** ', $t->toString());
         return 70;
         // EX_SOFTWARE according to sysexits.h
     }
     return 0;
 }
Example #5
0
 public function rooted_file()
 {
     $rooted = new File('/rooted.ext');
     $this->assertEquals($rooted->getURI(), (new Path($rooted))->toString());
 }
Example #6
0
 /**
  * Read from a file
  *
  * @deprecated  Use img.io.MetaDataReader instead
  * @param   io.File file
  * @param   var default default void what should be returned in case no data is found
  * @return  img.util.ExifData
  * @throws  lang.FormatException in case malformed meta data is encountered
  * @throws  lang.ElementNotFoundException in case no meta data is available
  * @throws  img.ImagingException in case reading meta data fails
  */
 public static function fromFile(File $file)
 {
     if (FALSE === getimagesize($file->getURI(), $info)) {
         $e = new ImagingException('Cannot read image information from ' . $file->getURI());
         xp::gc(__FILE__);
         throw $e;
     }
     if (!isset($info['APP1'])) {
         if (func_num_args() > 1) {
             return func_get_arg(1);
         }
         throw new ElementNotFoundException('Cannot get EXIF information from ' . $file->getURI() . ' (no APP1 marker)');
     }
     if (!($info = exif_read_data($file->getURI(), 'COMPUTED,FILE,IFD0,EXIF,COMMENT,MAKERNOTE', TRUE, FALSE))) {
         throw new FormatException('Cannot get EXIF information from ' . $file->getURI());
     }
     // Change key case for lookups
     foreach ($info as &$val) {
         $val = array_change_key_case($val, CASE_LOWER);
     }
     with($e = new self());
     // COMPUTED info
     $e->setWidth(self::lookup($info['COMPUTED'], 'width'));
     $e->setHeight(self::lookup($info['COMPUTED'], 'height'));
     $e->setApertureFNumber(self::lookup($info['COMPUTED'], 'aperturefnumber'));
     // IFD0 info
     $e->setMake(trim(self::lookup($info['IFD0'], 'make')));
     $e->setModel(trim(self::lookup($info['IFD0'], 'model')));
     $e->setSoftware(self::lookup($info['IFD0'], 'software'));
     if (NULL !== ($o = self::lookup($info['IFD0'], 'orientation'))) {
         $e->setOrientation($o);
     } else {
         $e->setOrientation($e->width / $e->height > 1.0 ? 1 : 5);
     }
     // FILE info
     $e->setFileName(self::lookup($info['FILE'], 'filename'));
     $e->setFileSize(self::lookup($info['FILE'], 'filesize'));
     $e->setMimeType(self::lookup($info['FILE'], 'mimetype'));
     // EXIF info
     $e->setExposureTime(self::lookup($info['EXIF'], 'exposuretime'));
     $e->setExposureProgram(self::lookup($info['EXIF'], 'exposureprogram'));
     $e->setMeteringMode(self::lookup($info['EXIF'], 'meteringmode'));
     $e->setIsoSpeedRatings(self::lookup($info['EXIF'], 'isospeedratings'));
     // Sometimes white balance is in MAKERNOTE - e.g. FUJIFILM's Finepix
     if (NULL !== ($w = self::lookup($info['EXIF'], 'whitebalance'))) {
         $e->setWhiteBalance($w);
     } else {
         if (isset($info['MAKERNOTE']) && NULL !== ($w = self::lookup($info['MAKERNOTE'], 'whitebalance'))) {
             $e->setWhiteBalance($w);
         } else {
             $e->setWhiteBalance(NULL);
         }
     }
     // Extract focal length. Some models store "80" as "80/1", rip off
     // the divisor "1" in this case.
     if (NULL !== ($l = self::lookup($info['EXIF'], 'focallength'))) {
         sscanf($l, '%d/%d', $n, $frac);
         $e->setFocalLength(1 == $frac ? $n : $n . '/' . $frac);
     } else {
         $e->setFocalLength(NULL);
     }
     // Check for Flash and flashUsed keys
     if (NULL !== ($f = self::lookup($info['EXIF'], 'flash'))) {
         $e->setFlash($f);
     } else {
         $e->setFlash(NULL);
     }
     if (NULL !== ($date = self::lookup($info['EXIF'], 'datetimeoriginal', 'datetimedigitized'))) {
         $t = sscanf($date, '%4d:%2d:%2d %2d:%2d:%2d');
         $e->setDateTime(new Date(mktime($t[3], $t[4], $t[5], $t[1], $t[2], $t[0])));
     }
     return $e;
 }
 public function moving()
 {
     with($data = 'Hello World');
     $this->writeData($this->file, $data);
     $target = new File($this->file->getURI() . '.moved');
     $this->file->move($target->getURI());
     $read = $this->readData($target);
     $target->unlink();
     $this->assertEquals($data, $read);
     // FIXME I don't think io.File should be updating its URI when
     // move() is called. Because it does, this assertion fails!
     // $this->assertFalse($this->file->exists());
 }
 /**
  * Create a property file from an io.File object
  *
  * @deprecated  Use load() method instead
  * @param   io.File file
  * @return  util.Properties
  * @throws  io.IOException in case the file given does not exist
  */
 public static function fromFile(File $file)
 {
     $self = new self($file->getURI());
     $self->load($file->getInputStream());
     return $self;
 }