Example #1
0
 public function toNodeList()
 {
     $nodes = new NodeList([]);
     foreach ($this->buckets as $id => $bucket) {
         if ($bucket !== NULL) {
             $nodes->addNodeList($bucket->toNodeList());
         }
     }
     return $nodes;
 }
Example #2
0
 /**
  * @param NodeList $nodelist An XPath query reesult
  * @param boolean $clean Clean field or not, defaults to true
  * @return string field
  */
 public function get_text_content($nodelist, $clean = true)
 {
     $field = '';
     if ($nodelist->length) {
         $field = $nodelist->item(0)->textContent;
         if ($clean) {
             $field = $this->clean_field($field);
         }
     }
     return $field;
 }
Example #3
0
 public static function convertToNodes($array, $depth = 0)
 {
     $nodelist = new NodeList();
     foreach ($array as $var) {
         $current = Node::autoCreate($var);
         if (isset($var->children)) {
             $current->setChildren(self::convertToNodes($var->children));
         }
         $nodelist->append($current);
     }
     return $nodelist;
 }
Example #4
0
 public function perform($results)
 {
     $node_list = new NodeList([]);
     foreach ($results as $nodes) {
         $node_list->addNodeArray($nodes);
     }
     if ($node_list->containsNodeId($this->settings->own_node_id)) {
         $this->settings->own_node_id = Node::randomNodeId();
         $this->find_node_task = new Bootstrap($this->settings, $this->bootstrap_nodes);
         $this->find_node_task->enqueue()->done([$this, 'emitSuccess']);
     } else {
         $this->emitSuccess($this->settings->own_node_id);
     }
 }
 /**
  * Find list of nodes with a CSS selector
  *
  * @param string $selector
  * @param int    $idx
  *
  * @return NodeList|Element|null
  */
 public function find($selector, $idx = null)
 {
     $elements = new NodeList();
     foreach ($this as $node) {
         foreach ($node->find($selector) as $res) {
             $elements->append($res);
         }
     }
     if (is_null($idx)) {
         return $elements;
     } else {
         if ($idx < 0) {
             $idx = count($elements) + $idx;
         }
     }
     return isset($elements[$idx]) ? $elements[$idx] : null;
 }
Example #6
0
 public function mergeNodeLists($results)
 {
     $node_list = new NodeList([]);
     foreach ($results as $protocol_result) {
         foreach ($protocol_result as $node_result) {
             if (isset($node_result['node_list'])) {
                 $res = $node_result['node_list'];
                 assert(get_class($res) === 'Kademlia\\NodeList');
                 foreach ($res->toArray() as $n) {
                     assert(get_class($n) === 'Kademlia\\Node' or get_class($n) === 'MockNode');
                 }
                 $node_list->addNodeList($res);
             }
         }
     }
     return $node_list;
 }
Example #7
0
 public static function equals(Node $a, Node $b)
 {
     $diff = true;
     $diff = $diff && $a->id === $a->id;
     $diff = $diff && $a->data === $b->data;
     $diff = $diff && NodeList::equals($a->children, $b->children);
     return $diff;
 }
Example #8
0
 /**
  * append() をテストします.
  * 以下を確認します.
  * 
  * - 任意のスカラー値および Node が追加できること
  * - null および None を指定した場合, 変化がない (追加されない) こと
  * - コンテナを追加した場合, 自身ではなくその子ノードが追加されること
  * 
  * @covers Peach\Markup\Container::append
  */
 public function testAppend()
 {
     $nList = new NodeList();
     $nList->append("foo");
     $nList->append("bar");
     $nList->append("baz");
     $test = $this->test;
     $obj = $this->object;
     $obj->append(null);
     $obj->append("TEXT");
     // (count: 1)
     $obj->append(new EmptyElement("test"));
     // (count: 2)
     $obj->append(None::getInstance());
     // added nothing (count: 2)
     $obj->append($nList);
     // added 3 nodes (count: 5)
     $obj->append(array("A", "B", array("C", "D")));
     // added 4 nodes (count: 9)
     $test->assertSame(9, count($obj->getChildNodes()));
 }
Example #9
0
 function &parse()
 {
     $until = func_get_args();
     $nodelist = new NodeList($this);
     while ($token = $this->tokenstream->next()) {
         //$token = $this->tokenstream->current();
         switch ($token->type) {
             case 'text':
                 $node = new TextNode($token->content, $token->position);
                 break;
             case 'variable':
                 $args = H2o_Parser::parseArguments($token->content, $token->position);
                 $variable = array_shift($args);
                 $filters = $args;
                 $node = new VariableNode($variable, $filters, $token->position);
                 break;
             case 'comment':
                 $node = new CommentNode($token->content);
                 break;
             case 'block':
                 if (in_array($token->content, $until)) {
                     $this->token = $token;
                     return $nodelist;
                 }
                 $temp = preg_split('/\\s+/', $token->content, 2);
                 $name = $temp[0];
                 $args = count($temp) > 1 ? $temp[1] : null;
                 $node = H2o::createTag($name, $args, $this, $token->position);
                 $this->token = $token;
         }
         $this->searching = join(',', $until);
         $this->first = false;
         $nodelist->append($node);
     }
     if ($until) {
         throw new TemplateSyntaxError('Unclose tag, expecting ' . $until[0]);
     }
     return $nodelist;
 }
Example #10
0
 public function emit($definitions)
 {
     return '<h' . $this->level . '>' . parent::emit($definitions) . '</h' . $this->level . '>';
 }
Example #11
0
<?php

include 'Node.php';
include 'Messages/Message.php';
$x = array();
for ($a = 0; $a < $argv[1]; $a++) {
    $x[] = new NodeProperties($a, "127.0.0.1", (int) $argv[2] + $a);
}
for ($i = 0; $i < $argv[1]; ++$i) {
    $pid = pcntl_fork();
    if (!$pid) {
        $b = new NodeList();
        foreach ($x as $key => $value) {
            if ($key != $i) {
                $b->AddNode($value);
            }
        }
        $c = new Node($x[$i], $b);
        $c->NodeStart();
        exit($i);
    }
}
while (pcntl_waitpid(0, $status) != -1) {
    $status = pcntl_wexitstatus($status);
    echo "Child {$status} completed\n";
}
Example #12
0
 /**
  * @param NodeList $nodelist
  * @param string|null $parent_name
  * @param FilterExpression|null $parent_name_expr
  * @param array|null $template_dirs
  */
 public function __construct($nodelist, $parent_name, $parent_name_expr, $template_dirs = null)
 {
     $this->nodelist = $nodelist;
     $this->parent_name = $parent_name;
     $this->parent_name_expr = $parent_name_expr;
     $this->template_dirs = $template_dirs;
     $blocks_ = array();
     foreach ($nodelist->getNodesByType('BlockNode') as $n) {
         $blocks_[$n->name] = $n;
     }
     $this->blocks = $blocks_;
 }
Example #13
0
 /**
  * @param Context $context
  *
  * @return SafeString
  */
 public function render_($context)
 {
     return $this->nodelist->render($context);
 }
Example #14
0
    $network_id = $_REQUEST["network_id"];
    if ($network_id == "") {
        $network = null;
    } else {
        try {
            $network = Network::getObject($network_id);
        } catch (Exception $e) {
            $network = Network::getDefaultNetwork();
        }
    }
} else {
    $network = Network::getDefaultNetwork();
}
if ($network or NodeList::getAllowsNullNetwork($format)) {
    // Init node list type
    $nodeList = NodeList::getObject($format, $network);
    /**
     * XSLT support for Hotspot status page
     * ====================================
     *
     * If you want to enable XSLT support for the Hotspot status page enable this
     * value.
     *
     * Enabling it will let you you display hostpot status in any format.
     * http://server_ip/hotspot_status.php?format=XML&xslt=http://xslt_server/xslt/wifidog_status.xsl
     */
    // If a XSL transform stylesheet has been specified, try to use it.
    if ($format == "XML" && !empty($_REQUEST['xslt'])) {
        if (Dependency::check("xsl")) {
            // Load the XSLT
            if (($xslt_dom = @DomDocument::load(trim($_REQUEST['xslt']))) === false) {
Example #15
0
 public function testGetOriginalShouldReturnIdenticalObject()
 {
     $a = new ArrayObject(array('foo'));
     $nl = new NodeList($a);
     $r = $nl->getOriginal();
     $this->assertSame($a, $r);
 }
Example #16
0
 /**
  * Context の handleNodeList() が呼び出されることを確認します.
  * 
  * @covers Peach\Markup\NodeList::accept
  */
 public function testAccept()
 {
     $context = new TestContext();
     $this->object->accept($context);
     $this->assertSame("handleNodeList", $context->getResult());
 }
Example #17
0
 /**
  * @param Context $context
  * @return SafeString|string
  * @throws Exception|TypeError|VariableDoesNotExist
  * @throws RuntimeError
  */
 public function render($context)
 {
     if (isset($context['forloop'])) {
         $parentloop = $context['forloop'];
     } else {
         $parentloop = array();
     }
     $context->push();
     try {
         $values = $this->sequence->resolve($context, True);
     } catch (VariableDoesNotExist $e) {
         $values = array();
     }
     if ($values === null) {
         $values = array();
     } elseif (is_string($values)) {
         // We convert a string to array to make it iterable.
         $values = str_split($values);
         // TODO Check unicode handling.
     }
     if (!is_array($values) && !$values instanceof Iterator) {
         throw new RuntimeError('Noniterable "' . $values . '" is passed to for loop.');
     }
     $len_values = count($values);
     if ($len_values < 1) {
         $context->pop();
         return $this->nodelist_empty->render($context);
     }
     $nodelist = new NodeList();
     if ($this->is_reversed) {
         $values_ = $values;
         krsort($values_);
         $values = $values_;
         unset($values_);
     }
     $unpack = count($this->loopvars) > 1;
     // Create a forloop value in the context.  We'll update counters on each iteration just below.
     $loop_dict = $context['forloop'] = array('parentloop' => $parentloop);
     foreach ($values as $i => $item) {
         // Shortcuts for current loop iteration number.
         $loop_dict['counter0'] = $i;
         $loop_dict['counter'] = $i + 1;
         // Reverse counter iteration numbers.
         $loop_dict['revcounter'] = $len_values - $i;
         $loop_dict['revcounter0'] = $len_values - $i - 1;
         // Boolean values designating first and last times through loop.
         $loop_dict['first'] = $i == 0;
         $loop_dict['last'] = $i == $len_values - 1;
         $context['forloop'] = array_merge($context['forloop'], $loop_dict);
         $pop_context = False;
         if ($unpack) {
             // If there are multiple loop variables, unpack the item into them.
             $success_ = True;
             try {
                 $unpacked_vars = py_zip($this->loopvars, $item);
             } catch (TypeError $e) {
                 $success_ = False;
             }
             if ($success_) {
                 $pop_context = True;
                 $context->update($unpacked_vars);
             }
         } else {
             $context[$this->loopvars[0]] = $item;
         }
         // In TEMPLATE_DEBUG mode provide source of the node which actually raised the exception
         if (Dja::getSetting('TEMPLATE_DEBUG')) {
             foreach ($this->nodelist_loop as $node) {
                 /** @var $node Node */
                 try {
                     $nodelist[] = $node->render($context);
                 } catch (Exception $e) {
                     if (!py_hasattr($e, 'django_template_source')) {
                         $e->django_template_source = $node->source;
                     }
                     throw $e;
                 }
             }
         } else {
             foreach ($this->nodelist_loop as $node) {
                 $nodelist[] = $node->render($context);
             }
         }
         if ($pop_context) {
             /*
              * The loop variables were pushed on to the context so pop them
              * off again. This is necessary because the tag lets the length
              * of loopvars differ to the length of each set of items and we
              * don't want to leave any vars from the previous loop on the
              * context.
              */
             $context->pop();
         }
     }
     $context->pop();
     return $nodelist->render($context);
 }
Example #18
0
 /**
  * handleNodeList() のテストです.
  * 
  * @covers Peach\Markup\DebugContext::handleNodeList
  * @covers Peach\Markup\DebugContext::startNode
  * @covers Peach\Markup\DebugContext::handleContainer
  * @covers Peach\Markup\DebugContext::endNode
  */
 public function testHandleNodeList()
 {
     $expected = implode("\r\n", array("NodeList {", "    Comment {", "        Text", "    }", "    EmptyElement(img)", "    Text", "}")) . "\r\n";
     $nodeList = new NodeList();
     $comment = new Comment();
     $comment->append("This is test comment");
     $img = new EmptyElement("img");
     $img->setAttributes(array("src" => "test.jpg", "alt" => ""));
     $nodeList->append($comment);
     $nodeList->append($img);
     $nodeList->append("Test image");
     $context = $this->object;
     $this->expectOutputString($expected);
     $context->handleNodeList($nodeList);
     $this->assertSame($expected, $context->getResult());
 }
Example #19
0
 /**
  * この要素が持つ子要素の個数を返します.
  * @return int 子要素の個数
  */
 public function size()
 {
     return $this->childNodes->size();
 }
Example #20
0
 /**
  * NodeList に含まれる各子ノードを handle することを確認します.
  * 
  * @covers Peach\Markup\DefaultContext::handleNodeList
  * @covers Peach\Markup\DefaultContext::formatChildNodes
  * @covers Peach\Markup\DefaultContext::breakCode
  */
 public function testHandleNodeList()
 {
     $node1 = new EmptyElement("empty");
     $node2 = new Text("Sample Text");
     $node3 = new ContainerElement("container");
     $node3->append("TEST");
     $nodeList = new NodeList();
     $nodeList->append($node1);
     $nodeList->append($node2);
     $nodeList->append($node3);
     $expected = implode("\r\n", array('<empty />', 'Sample Text', '<container>TEST</container>'));
     $this->object->handleNodeList($nodeList);
     $this->assertSame($expected, $this->object->getResult());
 }
Example #21
0
 /**
  * Emit this blockquote element
  *
  * @param	 [:net.daringfireball.markdown.Link] definitions
  * @return string
  */
 public function emit($definitions)
 {
     return '<blockquote>' . parent::emit($definitions) . '</blockquote>';
 }