settings = Settings::getInstance(); } public function write($str) { if($this->settings->get("debug")) { echo $str; } else { $this->outputBuffer .= $str; } } public function save($filename) { if(!$this->settings->get("debug")) { if(!file_put_contents($filename, $this->outputBuffer)) { throw new Exception("Cannot open file to write."); } } } public function nextLine() { $this->write(Literals::get("LINEFEED")); } public function resetLine() { $this->beginningOfLine = ""; } public function appendToLine($add) { $this->beginningOfLine .= $add; } public function removeLastVariablePart() { list($varName) = explode("=", $this->beginningOfLine); if(preg_match("/_[0-9]+$/", $varName, $matches) || $this->emptyVarNamePart) { // Special case 1 $this->beginningOfLine = preg_replace("/_[0-9]+$/", "", $varName); $this->emptyVarNamePart = !$this->emptyVarNamePart; return; } $matches = preg_split("/(_[0-9]+[_]?)/", $varName, -1, PREG_SPLIT_DELIM_CAPTURE); if(empty($matches[count($matches)-1])) unset($matches[count($matches)-1]); unset($matches[count($matches)-1]); $this->beginningOfLine = implode("", $matches); } /** * * @param VariableValue $ variableValue * @todo escape output? */ public function visitVariableValue(VariableValue $variableValue, $level) { // Print value $str = "='" . $variableValue->getValue() . "'"; $this->write($str); $this->appendToLine($str); // print comment if($variableValue->getComment() == "") { $this->nextLine(); return; } $toFill = Literals::get("START_COMMENT_AT_COLUMN") - strlen($this->beginningOfLine); if($toFill <= 0) { $toFill = Literals::get("START_COMMENT_AT_COLUMN"); $this->nextLine(); } $this->write(str_repeat(" ", $toFill)); $lines = explode(Literals::get("LINEFEED"), $variableValue->getComment()); foreach($lines as $key => $line) { if($key != 0) $this->write(str_repeat(" ", Literals::get("START_COMMENT_AT_COLUMN"))); $this->write($line); $this->write(Literals::get("LINEFEED")); } } /** * * @param VariableInformation $ variableInformation */ public function visitVariableInformation(VariableInformation $variableInformation, $level) { // echo "='" . $variableValue->defaultValue() . "' " . $variableValue->defaultComment() . "\n"; } /** * * @param VariableValueTree $ variableValueTree */ public function visitVariableValueTree(VariableValueTree $variableValueTree, $level) { $str = $variableValueTree->getName(); if(!$variableValueTree->allChildrenOfType("VariableIndex")) { $this->write($this->beginningOfLine); $this->write($str); } $this->appendToLine($str); } /** * * @param VariableInformationTree $ variableInformationTree */ public function visitVariableInformationTree(VariableInformationTree $variableInformationTree, $level) { if(!$variableValueTree->isRoot()) $this->write("_%_"); $this->write($this->variableInformationTree->getName()); } public function visitVariableIndex(VariableIndex $variableIndex, $level) { $str = "_" . $variableIndex->getPos(); if(!$variableIndex->testNameInChildren(null)) // Special case 1 $str .= "_"; $this->appendToLine($str); } /** * * @param Comment $ comment */ public function visitComment(Comment $comment, $level) { $this->write($comment->getComment()); $this->nextLine(Literals::get("LINEFEED")); } public function visitConfigTitle(ConfigTitle $configTitle, $level) { $this->printConfigTitleSeperator(); $this->write($configTitle->getTitle()); $this->nextLine(); $this->printConfigTitleSeperator(); $this->nextLine(); } private function printConfigTitleSeperator() { $this->write("#" . str_repeat(Literals::get("COMMENT_SEPARATOR"), Literals::get("COMMENT_SEPARATOR_LENGTH"))); $this->nextLine(); } public function visitEmptyLine(EmptyLine $emptyLine, $level) { $this->nextLine(); } } // end of configVisitor ?>