/** * This method iterates over each record in the file, yielding each item to the procedure function. * * @access public * @param callable $procedure the procedure function to be used * @throws Throwable\Parse\Exception indicates that an invalid record was * encountered */ public function each(callable $procedure) { $self = $this; IO\FileReader::read($this->file, function ($reader, $data, $index) use($self, $procedure) { $line = trim($data); if (strlen($line) > 0) { if ($index == 0 && $self->bom) { $line = preg_replace('/^' . pack('H*', 'EFBBBF') . '/', '', $line); } $query_string = parse_url($line, PHP_URL_QUERY); $properties = array(); parse_str($query_string, $properties); if (!empty($properties)) { $source_encoding = $self->encoder !== null ? call_user_func($self->encoder . "::getEncoding", $properties) : $self->encoding[0]; $target_encoding = $self->encoding[1]; foreach ($properties as $key => &$value) { $value = urldecode($value); $value = Core\Data\Charset::encode($value, $source_encoding, $target_encoding); $type = isset($self->schema[$key]) ? $self->schema[$key] : 'string'; $value = Core\Convert::changeType($value, $type); $record = new Common\Mutable\HashMap(); $record->putEntry($key, $value); $procedure($record); } } } }); }
/** * This method iterates over each record in the file, yielding each item to the procedure function. * * @access public * @param callable $procedure the procedure function to be used * @throws Throwable\Parse\Exception indicates that an invalid record was * encountered */ public function each(callable $procedure) { $self = $this; IO\FileReader::read($this->file, function ($reader, $data, $index) use($self, $procedure) { $line = trim($data); if (strlen($line) > 0) { if ($index == 0 && $self->bom) { $line = preg_replace('/^' . pack('H*', 'EFBBBF') . '/', '', $line); } if (!preg_match('/^\\s*#.*$/', $line)) { if (!preg_match('/^[^=]+=.+$/', $line)) { throw new Throwable\Parse\Exception('Unable to parse file. File ":uri" contains an invalid pattern on line :line.', array(':uri' => $self->file, ':line' => $index)); } $position = strpos($line, '='); $key = trim(substr($line, 0, $position)); $value = trim(substr($line, $position + 1)); $type = isset($self->schema[$key]) ? $self->schema[$key] : 'string'; $value = Core\Convert::changeType($value, $type); $record = new Common\Mutable\HashMap(); $record->putEntry($key, $value); $procedure($record); } } }); }
/** * This method sets the header with specified name. * * @access public * @param string $name the name of the header * @param string $value the value of the header */ public function setHeader($name, $value) { $name = strtolower($name); if (!in_array($name, array('content-length'))) { if ($value !== null) { $this->headers->putEntry($name, Core\Convert::toString($value)); } else { $this->headers->removeKey($name); } } }
/** * This method processes the models and returns the status. * * @access public * @param BT\Exchange $exchange the exchange given to process * @return integer the status code */ public function process(BT\Exchange $exchange) { $body = $exchange->getIn()->getBody(); if ($body instanceof IO\File) { try { $reader = new Config\JSON\Reader($body, array('assoc' => false)); $models = new Common\Mutable\HashMap(); $models->putEntry('Order', OrderCalc\Engine\Model\Marshaller::unmarshal($reader)); $exchange->getIn()->setBody($models); return BT\Task\Status::SUCCESS; } catch (Throwable\Runtime\Exception $ex) { $exchange->getOut()->setBody($ex); return BT\Task\Status::ERROR; } } return BT\Task\Status::FAILED; }
/** * This method registers a parser with the specified name. * * @access public * @param string $name the name to associate with the specified * parser * @param Spring\Object\Definition\Parser $parser the parser to be registered */ public function registerObjectDefinitionParser($name, Spring\Object\Definition\Parser $parser) { $this->parsers->putEntry($name, $parser); }
/** * This method parses a "dictionary" node. * * @access protected * @param \SimpleXMLElement &$node a reference to the "dictionary" node * @return \Unicity\Common\Mutable\IMap the value as a map * @throws \Unicity\Throwable\Parse\Exception indicates that an unrecognized child * node was encountered */ protected function parseDictElement(\SimpleXMLElement $node) { $element = new Common\Mutable\HashMap(); $children = $node->children(); $key = null; foreach ($children as $child) { $name = $child->getName(); if ($key !== null) { switch ($name) { case 'array': $element->putEntry($key, $this->parseArrayElement($child)); break; case 'data': $element->putEntry($key, $this->parseDataElement($child)); break; case 'date': $element->putEntry($key, $this->parseDateElement($child)); break; case 'dict': $element->putEntry($key, $this->parseDictElement($child)); break; case 'false': $element->putEntry($key, $this->parseFalseElement($child)); break; case 'integer': $element->putEntry($key, $this->parseIntegerElement($child)); break; case 'real': $element->putEntry($key, $this->parseRealElement($child)); break; case 'string': $element->putEntry($key, $this->parseStringElement($child)); break; case 'true': $element->putEntry($key, $this->parseTrueElement($child)); break; default: throw new Throwable\Parse\Exception('Invalid child node named ":name" detected.', array(':name' => $name)); break; } $key = null; } else { if ($name == 'key') { $key = $this->parseKeyElement($child); } else { throw new Throwable\Parse\Exception('Invalid child node named ":name" detected.', array(':name' => $name)); } } } if ($key !== null) { throw new Throwable\Parse\Exception('Failed to match the key ":key" with a value.', array(':key' => $key)); } return $element; }
/** * This method parses a "wddxPacket" node. * * @access protected * @param \DOMDocument $document a reference to the "wddxPacket" node * @return \Unicity\Common\String the value as a string */ protected function parseWDDXPacketElement(\DOMDocument $document) { $map = new Common\Mutable\HashMap(); $xpath = new \DOMXPath($document); $children = $xpath->query('/wddxPacket/header'); foreach ($children as $child) { $map->putEntry('header', $this->parseHeaderElement($child)); } $children = $xpath->query('/wddxPacket/data'); foreach ($children as $child) { $map->putEntry('data', $this->parseDataElement($child)); } return $map; }
/** * This method puts the key/value mapping to the collection. * * @access public * @override * @param mixed $key the key to be mapped * @param mixed $value the value to be mapped * @return boolean whether the key/value pair was set */ public function putEntry($key, $value) { if (Core\Data\Undefined::instance()->__equals($value)) { return $this->removeKey($key); } else { return parent::putEntry($key, $value); } }
/** * This method converts a collection to use collections. * * @access public * @static * @param mixed $data the data to be converted * @return mixed the converted data */ public static function useCollections($data) { if (is_object($data)) { if ($data instanceof Common\IList) { $ilist = $data instanceof Common\Mutable\IList ? get_class($data) : '\\Unicity\\Common\\Mutable\\ArrayList'; $buffer = new $ilist(); foreach ($data as $value) { $buffer->addValue(static::useCollections($value)); } return $buffer; } else { if ($data instanceof Common\ISet) { $iset = $data instanceof Common\Mutable\ISet ? get_class($data) : '\\Unicity\\Common\\Mutable\\HashSet'; $buffer = new $iset(); foreach ($data as $value) { $buffer->putValue(static::useCollections($value)); } return $buffer; } else { if ($data instanceof Common\IMap) { $imap = $data instanceof Common\Mutable\IMap ? get_class($data) : '\\Unicity\\Common\\Mutable\\HashMap'; $buffer = new $imap(); foreach ($data as $key => $value) { $buffer->putEntry($key, static::useCollections($value)); } return $buffer; } else { if ($data instanceof \stdClass) { $data = get_object_vars($data); $buffer = new Common\Mutable\HashMap(); foreach ($data as $key => $value) { $buffer->putEntry($key, static::useCollections($value)); } return $buffer; } } } } } if (is_array($data)) { if (static::isDictionary($data)) { $buffer = new Common\Mutable\HashMap(); foreach ($data as $key => $value) { $buffer->putEntry($key, static::useCollections($value)); } return $buffer; } else { $buffer = new Common\Mutable\ArrayList(); foreach ($data as $value) { $buffer->addValue(static::useCollections($value)); } return $buffer; } } return $data; }
/** * This method sets which nodes are to be treated as ArrayLists. * * @access public * @param Common\HashSet $properties the properties that will be * considered expandable */ public function setExpandableProperties(Common\HashSet $properties) { $this->directives->putEntry('expandableProperties', $properties); }
/** * This method parses a "property" node. * * @access protected * @param \SimpleXMLElement $root a reference to the root node * @param \SimpleXMLElement $node a reference to the "property" node * @return Common\Mutable\IMap a hash map * @throws Throwable\Parse\Exception indicates that problem occurred while * parsing */ protected function parsePropertyElement(\SimpleXMLElement $root, \SimpleXMLElement $node) { $map = new Common\Mutable\HashMap(); $attributes = $node->attributes(); if (!isset($attributes['name'])) { throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" is missing ":attribute" attribute', array(':tag' => 'property', ':attribute' => 'name')); } $name = Spring\Data\XML::valueOf($attributes['name']); if (!Spring\Data\XML\Syntax::isPropertyName($name)) { throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid property name, but got ":name".', array(':name' => $name)); } $value = null; $children = $node->children(); if (count($children) > 0) { foreach ($children as $child) { $child_name = $child->getName(); switch ($child_name) { case 'array': $value = $this->parseArrayElement($root, $child); break; case 'dictionary': $value = $this->parseDictionaryElement($root, $child); break; case 'expression': $value = $this->parseExpressionElement($root, $child); break; case 'idref': $value = $this->parseIdRefElement($root, $child); break; case 'list': $value = $this->parseListElement($root, $child); break; case 'map': $value = $this->parseMapElement($root, $child); break; case 'null': $value = $this->parseNullElement($root, $child); break; case 'object': $value = $this->parseInnerObjectElement($root, $child); break; case 'ref': $value = $this->parseRefElement($root, $child); break; case 'set': $value = $this->parseSetElement($root, $child); break; case 'undefined': $value = $this->parseUndefinedElement($root, $child); break; case 'value': $value = $this->parseValueElement($root, $child); break; default: throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" has invalid child node ":child".', array(':tag' => 'property', ':child' => $child_name)); break; } } } else { if (isset($attributes['expression'])) { $expression = Spring\Data\XML::valueOf($attributes['expression']); $value = null; @eval('$value = ' . $expression . ';'); if (isset($attributes['type'])) { $type = Spring\Data\XML::valueOf($attributes['type']); if (!Spring\Data\XML\Syntax::isPrimitiveType($type)) { throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid primitive type, but got ":type".', array(':type' => $type)); } if (!isset($value)) { $type = 'NULL'; $value = null; } $value = Core\Convert::changeType($value, $type); } } else { if (isset($attributes['ref'])) { $idref = Spring\Data\XML::valueOf($attributes['ref']); $nodes = $this->find($root, $idref); if (empty($nodes)) { throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid "ref" token, but got ":token".', array(':token' => $idref)); } $value = $this->parseOuterObjectElement($root, $nodes[0]); } else { if (isset($attributes['value'])) { $value = Spring\Data\XML::valueOf($attributes['value']); if (isset($attributes['type'])) { $type = Spring\Data\XML::valueOf($attributes['type']); if (!Spring\Data\XML\Syntax::isPrimitiveType($type)) { throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid primitive type, but got ":type".', array(':type' => $type)); } $value = Core\Convert::changeType($value, $type); } } else { throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" is missing ":attribute" attribute', array(':tag' => 'property', ':attribute' => 'value')); } } } } $map->putEntry($name, $value); return $map; }