function &createChildWithText(&$doc, &$node, $name, $value, $appendIfEmpty = true) { $childNode = null; if ($appendIfEmpty || $value != '') { $childNode =& XMLCustomWriter::createElement($doc, $name); $textNode =& XMLCustomWriter::createTextNode($doc, $value); XMLCustomWriter::appendChild($childNode, $textNode); XMLCustomWriter::appendChild($node, $childNode); } return $childNode; }
/** * Create a new XML node. * @param $doc XMLNode|DOMImplementation * @param $nodePath string an XPath-like string that describes the * node to be created. * @param $value string the value to be added as a text node (if any) * @return XMLNode|DOMDocument */ function &_createNode($doc, $nodePath, $value = null) { // Separate the element name from the attributes. $elementPlusAttributes = explode('[', $nodePath); $element = $elementPlusAttributes[0]; assert(!empty($element)); // Create the element. $newNode =& XMLCustomWriter::createElement($doc, $element); // Add attributes. if (count($elementPlusAttributes) == 2) { // Separate the attribute key/value pairs. $unparsedAttributes = explode('@', rtrim(ltrim($elementPlusAttributes[1], '@'), ']')); foreach ($unparsedAttributes as $unparsedAttribute) { // Split attribute expressions into key and value. list($attributeName, $attributeValue) = explode('=', rtrim($unparsedAttribute, ' ')); $attributeValue = trim($attributeValue, '"'); XMLCustomWriter::setAttribute($newNode, $attributeName, $attributeValue); } } // Insert a text node if we got a value for it. if (!is_null($value)) { $textNode =& XMLCustomWriter::createTextNode($doc, $value); XMLCustomWriter::appendChild($newNode, $textNode); } return $newNode; }
/** * Generate doi_data element - this is what assigns the DOI * @param $doc XMLNode * @param $DOI string * @param $url string * @param $galleys array */ function &_generateDOIdataDom(&$doc, $DOI, $url, $galleys = null) { $request = Application::getRequest(); $journal = $request->getJournal(); $DOIdataNode =& XMLCustomWriter::createElement($doc, 'doi_data'); XMLCustomWriter::createChildWithText($doc, $DOIdataNode, 'doi', $DOI); XMLCustomWriter::createChildWithText($doc, $DOIdataNode, 'resource', $url); /* article galleys */ if ($galleys) { $collectionNode = XMLCustomWriter::createElement($doc, 'collection'); XMLCustomWriter::setAttribute($collectionNode, 'property', 'text-mining'); XMLCustomWriter::appendChild($DOIdataNode, $collectionNode); foreach ($galleys as $galley) { $itemNode = XMLCustomWriter::createElement($doc, 'item'); XMLCustomWriter::appendChild($collectionNode, $itemNode); $resourceNode = XMLCustomWriter::createElement($doc, 'resource'); XMLCustomWriter::appendChild($itemNode, $resourceNode); XMLCustomWriter::setAttribute($resourceNode, 'mime_type', $galley->getFileType()); $urlNode = XMLCustomWriter::createTextNode($doc, $request->url(null, 'article', 'viewFile', array($galley->getArticleId(), $galley->getBestGalleyId($journal)))); XMLCustomWriter::appendChild($resourceNode, $urlNode); } } return $DOIdataNode; }
/** * Create an XML element with a text node. * * FIXME: Move this to XMLCustomWriter? I leave the decision up to PKP... * * @param $name string * @param $value string * @param $attributes array An array with the attribute names as array * keys and attribute values as array values. * * @return XMLNode|DOMImplementation */ function &createElementWithText($name, $value, $attributes = array()) { $element =& XMLCustomWriter::createElement($this->getDoc(), $name); $elementContent =& XMLCustomWriter::createTextNode($this->getDoc(), String::html2text($value)); XMLCustomWriter::appendChild($element, $elementContent); foreach ($attributes as $attributeName => $attributeValue) { XMLCustomWriter::setAttribute($element, $attributeName, $attributeValue); } return $element; }
/** * Create a new XML node. * @param $doc XMLNode|DOMImplementation * @param $nodePath string an XPath-like string that describes the * node to be created. * @param $value string the value to be added as a text node (if any) * @return XMLNode|DOMDocument */ function &_createNode($doc, $nodePath, $value = null) { // Separate the element name from the attributes. $elementPlusAttributes = explode('[', $nodePath); $element = $elementPlusAttributes[0]; assert(!empty($element)); // Create the element. $newNode =& XMLCustomWriter::createElement($doc, $element); // Check for configurable attributes in element value, remove them from value // and add them to regular attributes $attributeOffset = strpos($value, '[@'); if ($attributeOffset !== false) { // no configurable attributes if (count($elementPlusAttributes) < 2) { $elementPlusAttributes[] = ''; } if ($attributeOffset !== 0) { $elementPlusAttributes[1] = rtrim($elementPlusAttributes[1], ']') . ltrim(substr($value, $attributeOffset), '['); $value = substr($value, 0, $attributeOffset); } else { $elementPlusAttributes[1] = rtrim($elementPlusAttributes[1], ']') . ltrim(substr($value, $attributeOffset), '['); $value = ""; } } // Add attributes. if (count($elementPlusAttributes) == 2) { // Separate the attribute key/value pairs. $unparsedAttributes = explode('@', rtrim(ltrim($elementPlusAttributes[1], '@'), ']')); foreach ($unparsedAttributes as $unparsedAttribute) { // Split attribute expressions into key and value. list($attributeName, $attributeValue) = explode('=', rtrim($unparsedAttribute, ' ')); $attributeValue = trim($attributeValue, '"'); XMLCustomWriter::setAttribute($newNode, $attributeName, $attributeValue); } } // Insert a text node if we got a value for it. if (!is_null($value)) { $textNode =& XMLCustomWriter::createTextNode($doc, $value); XMLCustomWriter::appendChild($newNode, $textNode); } return $newNode; }