/**
  * Serializes the tree structure (without root node)
  */
 public function serializeAsXML($type)
 {
     $id = uniqid(rand());
     $count = 0;
     $result = "";
     $gi_store = SGAGardeningIssuesAccess::getGardeningIssuesAccess();
     foreach ($this->children as $title => $treeObject) {
         $isExpanded = count($treeObject->children) == 0 ? "false" : "true";
         $title_esc = htmlspecialchars($treeObject->getTitle()->getDBkey());
         $titleURLEscaped = htmlspecialchars(SMWOntologyBrowserXMLGenerator::urlescape($treeObject->getTitle()->getDBkey()));
         $issues = $gi_store->getGardeningIssues('smw_consistencybot', NULL, NULL, $treeObject->getTitle());
         $gi_issues = SMWOntologyBrowserErrorHighlighting::getGardeningIssuesAsXML($issues);
         $result .= "<{$type} title_url=\"{$titleURLEscaped}\" title=\"" . $title_esc . "\" img=\"{$type}.gif\" id=\"ID_{$id}{$count}\" expanded=\"{$isExpanded}\">";
         $result .= $gi_issues;
         $result .= $treeObject->serializeAsXML($type);
         $result .= "</{$type}>";
         $count++;
     }
     return $result;
 }
 /**
  * Encapsulates an annotation as XML.
  *
  * @param $instance
  * @param $annotation
  * @param $smwValues
  *
  * @return XML string (fragment)
  */
 private static function encapsulateAsAnnotation(Title $instance, Title $annotationTitle, $smwValues)
 {
     $id = uniqid(rand());
     $count = 0;
     $singleProperties = "";
     $multiProperties = "";
     $isFormula = false;
     $chemistryParser = new ChemEqParser();
     $gi_store = SGAGardeningIssuesAccess::getGardeningIssuesAccess();
     foreach ($smwValues as $smwValue) {
         //list($smwValue, $uri) = $tuple;
         if ($smwValue instanceof SMWRecordValue) {
             // n-ary property
             $needRepaste = false;
             $parameters = "";
             foreach ($smwValue->getDVs() as $params) {
                 if ($params == NULL) {
                     $parameters .= "<param></param>";
                     continue;
                 }
                 if ($params->getTypeID() == '_che') {
                     $isFormula = true;
                     $chemistryParser->checkEquation(array_shift($params->getDBkeys()));
                     $formulaAsHTML = html_entity_decode($chemistryParser->getHtmlFormat());
                     $value = "<![CDATA[" . $formulaAsHTML . "]]>";
                 } else {
                     if ($params->getTypeID() == '_chf') {
                         $isFormula = true;
                         $chemistryParser->checkFormula(array_shift($params->getDBkeys()));
                         $formulaAsHTML = html_entity_decode($chemistryParser->getHtmlFormat());
                         $value = "<![CDATA[" . $formulaAsHTML . "]]>";
                     } else {
                         // escape potential HTML in a CDATA section
                         $value = "<![CDATA[" . html_entity_decode(array_shift($params->getDBkeys())) . " " . html_entity_decode($params->getUnit()) . "]]>";
                     }
                 }
                 // check if re-paste is needed
                 $needRepaste |= html_entity_decode(array_shift($params->getDBkeys())) != array_shift($params->getDBkeys()) || $params->getUnit() != '';
                 // check if target is a wikipage and built param
                 $isLink = $params instanceof SMWWikiPageValue ? "isLink=\"true\"" : "";
                 $parameters .= "<param {$isLink}>{$value}</param>";
             }
             $repasteMarker = $isFormula || $needRepaste ? "needRepaste=\"true\"" : "";
             $title = htmlspecialchars($annotationTitle->getDBkey());
             $titleURLEscaped = htmlspecialchars(self::urlescape($annotationTitle->getDBkey()));
             $issues = $gi_store->getGardeningIssuesForPairs('smw_consistencybot', array(SMW_GARDISSUE_WRONG_DOMAIN_VALUE, SMW_GARDISSUE_TOO_LOW_CARD, SMW_GARDISSUE_TOO_HIGH_CARD, SMW_GARD_ISSUE_MISSING_PARAM, SMW_GARDISSUE_WRONG_TARGET_VALUE), NULL, array($instance, $annotationTitle));
             $gi_issues = SMWOntologyBrowserErrorHighlighting::getAnnotationIssuesAsXML($issues, $smwValue);
             // no metadata available on n-ary properties
             $multiProperties .= "<annotation  title_url=\"{$titleURLEscaped}\" title=\"" . $title . "\" id=\"ID_{$id}{$count}\" {$repasteMarker}>" . $parameters . "{$gi_issues}</annotation>";
         } else {
             if ($smwValue instanceof SMWWikiPageValue || $smwValue->getTypeID() == '_uri') {
                 // relation
                 $title = htmlspecialchars($annotationTitle->getDBkey());
                 $titleURLEscaped = htmlspecialchars(self::urlescape($annotationTitle->getDBkey()));
                 $issues = $gi_store->getGardeningIssuesForPairs('smw_consistencybot', array(SMW_GARDISSUE_WRONG_DOMAIN_VALUE, SMW_GARDISSUE_TOO_LOW_CARD, SMW_GARDISSUE_TOO_HIGH_CARD, SMW_GARDISSUE_WRONG_TARGET_VALUE), NULL, array($instance, $annotationTitle));
                 $gi_issues = SMWOntologyBrowserErrorHighlighting::getAnnotationIssuesAsXML($issues, $smwValue);
                 // metadata
                 // check if metadata patch is applied
                 $metadataTags = "<metadata id=\"" . $id . "_meta_" . $count . "\">";
                 if (method_exists($smwValue, "getMetadataMap")) {
                     // read metadata
                     foreach ($smwValue->getMetadataMap() as $mdProperty => $mdValues) {
                         foreach ($mdValues as $mdValue) {
                             $metadataTags .= "<property name=\"" . htmlspecialchars($mdProperty) . "\">" . htmlspecialchars($mdValue) . "</property>";
                         }
                     }
                 }
                 $metadataTags .= "</metadata>";
                 if ($smwValue instanceof SMWWikiPageValue && !is_null($smwValue->getTitle())) {
                     $targetNotExists = $smwValue->getTitle()->exists() ? "" : "notexists=\"true\"";
                     $singleProperties .= "<annotation  title_url=\"{$titleURLEscaped}\" title=\"" . $title . "\" id=\"ID_{$id}{$count}\">" . "<param isLink=\"true\" {$targetNotExists}><![CDATA[" . $smwValue->getTitle()->getPrefixedDBkey() . "]]></param>" . $gi_issues . $metadataTags . "</annotation>";
                 } else {
                     if ($smwValue->getTypeID() == '_uri') {
                         // any URI
                         $uri = $smwValue->getWikiValue();
                         if (strpos($uri, "#") !== false) {
                             $local = substr($uri, strpos($uri, "#") + 1);
                         } else {
                             if (strrpos($uri, "/") !== false) {
                                 $local = substr($uri, strrpos($uri, "/") + 1);
                             } else {
                                 $local = $uri;
                             }
                         }
                         $uri_att = 'uri="' . htmlspecialchars($uri) . '"';
                         $singleProperties .= "<annotation  title_url=\"{$titleURLEscaped}\" title=\"" . $title . "\" id=\"ID_{$id}{$count}\">" . "<param isLink=\"true\" {$uri_att}><![CDATA[" . $local . "]]></param>" . $metadataTags . "</annotation>";
                     }
                 }
             } else {
                 if ($smwValue != NULL) {
                     // normal attribute
                     if ($smwValue->getTypeID() == '_che') {
                         $isFormula = true;
                         $chemistryParser->checkEquation(array_shift($smwValue->getDBkeys()));
                         $formulaAsHTML = html_entity_decode($chemistryParser->getHtmlFormat());
                         $value = "<![CDATA[" . $formulaAsHTML . "]]>";
                     }
                     if ($smwValue->getTypeID() == '_che') {
                         $isFormula = true;
                         $chemistryParser->checkEquation(array_shift($smwValue->getDBkeys()));
                         $formulaAsHTML = html_entity_decode($chemistryParser->getHtmlFormat());
                         $value = "<![CDATA[" . $formulaAsHTML . "]]>";
                     } else {
                         if ($smwValue->getTypeID() == '_chf') {
                             $isFormula = true;
                             $chemistryParser->checkFormula(array_shift($smwValue->getDBkey()));
                             $formulaAsHTML = html_entity_decode($chemistryParser->getHtmlFormat());
                             $value = "<![CDATA[" . $formulaAsHTML . "]]>";
                         } else {
                             // escape potential HTML in a CDATA section
                             if ($smwValue->getTypeID() == '__typ') {
                                 //SMW_DV_Types
                                 $value = implode(",", $smwValue->getTypeLabels());
                                 $value = strip_tags($value, "<sub><sup><b><i>");
                                 $value = "<![CDATA[" . html_entity_decode($value) . " " . $smwValue->getUnit() . "]]>";
                             }
                             if ($smwValue->getTypeID() == '__tls') {
                                 // SMW_DV_TypeList
                                 $value = "<![CDATA[" . html_entity_decode($smwValue->getWikiValue()) . "]]>";
                             } else {
                                 // small hack for datetime type. It may occur that there is a T at the end.
                                 if ($smwValue->getTypeID() == '_dat') {
                                     $val = array_shift($smwValue->getDBkeys());
                                     $xsdValue = substr($val, -1) == 'T' ? str_replace('T', '', $val) : $val;
                                 } else {
                                     $xsdValue = array_shift($smwValue->getDBkeys());
                                 }
                                 $value = strip_tags($xsdValue, "<sub><sup><b><i>");
                                 $value = "<![CDATA[" . html_entity_decode($value) . " " . $smwValue->getUnit() . "]]>";
                             }
                         }
                     }
                     //special attribute mark for all things needed to get re-pasted in FF.
                     $repasteMarker = $isFormula || strip_tags(array_shift($smwValue->getDBkeys())) != array_shift($smwValue->getDBkeys()) || $smwValue->getUnit() != '' ? "needRepaste=\"true\"" : "";
                     $title = htmlspecialchars($annotationTitle->getDBkey());
                     $titleURLEscaped = htmlspecialchars(self::urlescape($annotationTitle->getDBkey()));
                     $issues = $gi_store->getGardeningIssuesForPairs('smw_consistencybot', array(SMW_GARDISSUE_WRONG_DOMAIN_VALUE, SMW_GARDISSUE_TOO_LOW_CARD, SMW_GARDISSUE_TOO_HIGH_CARD, SMW_GARDISSUE_WRONG_UNIT), NULL, array($instance, $annotationTitle));
                     // gardening issues
                     $gi_issues = SMWOntologyBrowserErrorHighlighting::getAnnotationIssuesAsXML($issues, $smwValue);
                     // metadata
                     // check if metadata patch is applied
                     $metadataTags = "<metadata id=\"" . $id . "_meta_" . $count . "\">";
                     if (method_exists($smwValue, "getMetadataMap")) {
                         // read metadata
                         foreach ($smwValue->getMetadataMap() as $mdProperty => $mdValues) {
                             foreach ($mdValues as $mdValue) {
                                 $metadataTags .= "<property name=\"" . htmlspecialchars($mdProperty) . "\">" . htmlspecialchars($mdValue) . "</property>";
                             }
                         }
                     }
                     $metadataTags .= "</metadata>";
                     $singleProperties .= "<annotation  title_url=\"{$titleURLEscaped}\" title=\"" . $title . "\" id=\"ID_" . $id . $count . "\" {$repasteMarker}>" . "<param>" . $value . "</param>" . $gi_issues . $metadataTags . "</annotation>";
                 }
             }
         }
         $count++;
     }
     return $singleProperties . $multiProperties;
 }