wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.DotOutputStrategy.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.DotOutputStrategy.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/output/class.OutputStrategy.php");
20 require_once(BASE."wcmf/lib/util/class.Log.php");
21 
22 /**
23  * @class DotOutputStrategy
24  * @ingroup Output
25  * @brief This OutputStrategy outputs an object's content in a dot file.
26  * @note file locking works not on NFS!
27  *
28  * @author ingo herwig <ingo@wemove.com>
29  */
31 {
32  var $DEFAULT_NODE_STYLE = 'height=0.1,width=1,shape=box,style=filled,color="#49B4CF",fillcolor="#49B4CF",fontcolor=white,fontsize=14,fontname="Helvetica-Bold"';
33  var $DEFAULT_EDGE_STYLE = 'arrowhead=none,arrowtail=none,color="#49B4CF"';
34 
35  var $_file = '';
36  var $_fp = 0;
37  var $_fileOk = false; // indicates if we can write to the file
38  var $_nodeIndex = 0;
39 
40  var $_nodeStyle = '';
41  var $_edgeStyle = '';
42 
43  /**
44  * Constructor.
45  * @param file The output file name.
46  * @param nodeStyle Style definition to use for nodes (see dot documentation).
47  * @param edgeStyle Style definition to use for edges (see dot documentation).
48  */
49  function DotOutputStrategy($file, $nodeStyle='', $edgeStyle='')
50  {
51  $this->_file = $file;
52  $this->_fileOk = false;
53 
54  if ($nodeStyle != '')
55  $this->_nodeStyle = $nodeStyle;
56  else
57  $this->_nodeStyle = $this->DEFAULT_NODE_STYLE;
58  if ($edgeStyle != '')
59  $this->_edgeStyle = $edgeStyle;
60  else
61  $this->_edgeStyle = $this->DEFAULT_EDGE_STYLE;
62  }
63  /**
64  * Write the dot header.
65  */
66  function writeHeader()
67  {
68  // check if file exists and is locked
69  if (file_exists($this->_file))
70  {
71  $this->_fp = fopen($this->_file, "r");
72  if (!$this->_fp)
73  {
74  Log::warn("Can't write to file ".$this->_file.". Another user holds the lock. Try again later.", __CLASS__);
75  return;
76  }
77  else
78  fclose($this->_fp);
79  }
80  // check if file exists and is locked
81  $this->_fp = fopen($this->_file, "w");
82  if ($this->_fp)
83  if (flock ($this->_fp, LOCK_EX))
84  {
85  $this->_fileOk = true;
86  fputs($this->_fp, "digraph G {\n\n");
87  fputs($this->_fp, " node [".$this->_nodeStyle."]\n");
88  fputs($this->_fp, " edge [".$this->_edgeStyle."]\n\n");
89  return true;
90  }
91  }
92  /**
93  * Write the dot footer.
94  */
95  function writeFooter()
96  {
97  if ($this->_fileOk)
98  {
99  fputs($this->_fp, "\n}\n");
100  flock ($this->_fp, LOCK_UN);
101  fclose($this->_fp);
102  }
103  }
104  /**
105  * Write the object's content.
106  * @param obj The object to write.
107  */
108  function writeObject(&$obj)
109  {
110  if (!$obj->getProperty('nodeIndex'))
111  $obj->setProperty('nodeIndex', $this->getNextIndex());
112 
113  if ($this->_fileOk)
114  {
115  fputs($this->_fp, ' n'.$obj->getProperty('nodeIndex').' [label="'.$obj->getOID().'"]'."\n");
116  $children = $obj->getChildren();
117  for($i=0; $i<sizeOf($children); $i++)
118  {
119  if (!$children[$i]->getProperty('nodeIndex'))
120  $children[$i]->setProperty('nodeIndex', $this->getNextIndex());
121  fputs($this->_fp, ' n'.$obj->getProperty('nodeIndex').' -> n'.$children[$i]->getProperty('nodeIndex')."\n");
122  }
123  fputs($this->_fp, "\n");
124  }
125  }
126  /**
127  * Get the next Node index.
128  * @return The next Node index.
129  */
130  function getNextIndex()
131  {
132  return $this->_nodeIndex++;
133  }
134 }
135 ?>
warn($message, $category)
Definition: class.Log.php:59
OutputStrategy is used to write an object's content to a destination (called 'document') using a spec...
DotOutputStrategy($file, $nodeStyle='', $edgeStyle='')
This OutputStrategy outputs an object's content in a dot file.