Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing XiK #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions src/Xik.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);

namespace Kdl\Kdl;

final class Xik
{
private function __construct()
{
}

/**
* Parse a KDL string that uses the XML-in-KDL mapping, and produce a DOM document.
*
* Note: This conversion does not preserve comments or whitespace.
*/
public static function parseString(string $kdl): \DOMDocument
{
return self::parse(Kdl::parse($kdl));
}

/**
* Parse a KDL document that uses the XML-in-KDL mapping, and produce a DOM document.
*
* Note: This conversion does not preserve comments or whitespace.
*/
public static function parse(Document $in): \DOMDocument
{
// Remove doctype
$nodes = $in->getNodes();
if (count($nodes) > 0 && $nodes[0]->getName() === '!doctype') {
array_shift($nodes);
}

// Parse XML declaration
$xmlDecl = [];
if (count($nodes) > 0 && $nodes[0]->getName() === '?xml') {
$xmlDecl = array_shift($nodes)->getProperties();
}

// Create the XML DOM document
$doc = new \DOMDocument(
$xmlDecl['version'] ?? '1.0',
$xmlDecl['encoding'] ?? ''
);

// Parse remaining document nodes
$namespaces = ['' => null];
foreach ($nodes as $node) {
self::parseNode($node, $doc, $doc, $namespaces);
}

return $doc;
}

/**
* Parse a KDL node and create a DOM node.
*
* @param array<string, string> $namespaces
*/
private static function parseNode(
NodeInterface $in,
\DOMParentNode $parent,
\DOMDocument $doc,
array $namespaces,
): void {
$name = $in->getName();

// Skip any processing instructions
if (str_starts_with($name, '?') || str_starts_with($name, '!')) {
return;
}

// Parse any new namespace declarations at this level.
self::parseNamespaceDeclarations($in, $namespaces);

// Create the node.
[$ns, $name] = self::parseName($name, $namespaces);
$out = $doc->createElementNS($ns, $name);

// Add values as a single text node.
$out->appendChild($doc->createTextNode(implode(' ', $in->getValues())));

// Add attributes.
foreach ($in->getProperties() as $name => $value) {
// Namespace declarations were already handled by `parseNamespaceDeclarations`
if ($name === 'xmlns' || str_starts_with($name, 'xmlns:')) {
continue;
}

[$ns, $name] = self::parseName($name, $namespaces);
$out->setAttributeNS($ns, $name, (string) $value);
}

// Add children.
foreach ($in->getChildren() as $node) {
self::parseNode($node, $out, $doc, $namespaces);
}

// Attach to parent.
$parent->append($out);
}

/**
* Parse XML namespace declarations from a KDL node.
*
* @param array<string, ?string> $namespaces
*/
private static function parseNamespaceDeclarations(NodeInterface $node, array &$namespaces): void
{
foreach ($node->getProperties() as $prop => $value) {
if ($prop === 'xmlns') {
$namespaces[''] = $value;
} elseif (str_starts_with($prop, 'xmlns:')) {
$namespaces[substr($prop, 6)] = $value;
}
}
}

/**
* Parse an XML element or attribute name into namespace and name.
*
* @param array<string, ?string> $ns
* @return array{?string, string}
*/
private static function parseName(string $name, array &$namespaces): array
{
$ns = '';
$sepIdx = strpos($name, ':');
if ($sepIdx !== false) {
$ns = substr($name, 0, $sepIdx);
$name = substr($name, $sepIdx + 1);
}
if (!array_key_exists($ns, $namespaces)) {
throw new \Exception('Invalid namespace in element name: ' . $ns);
}
$ns = $namespaces[$ns];

return [$ns, $name];
}
}
23 changes: 23 additions & 0 deletions tests/XikTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Kdl\Kdl\Tests;

class XikTest extends \PHPUnit\Framework\TestCase
{
public function testXik(): void
{
$kdl = file_get_contents(__DIR__ . '/kdl/xik.kdl');
$xml = file_get_contents(__DIR__ . '/kdl/xik-output.xml');

$doc = \Kdl\Kdl\Xik::parseString($kdl);

$formatted = new \DOMDocument();
$formatted->preserveWhiteSpace = false;
$formatted->formatOutput = true;
$formatted->loadXML($doc->saveXML());

self::assertSame($xml, $formatted->saveXML());
}
}
144 changes: 144 additions & 0 deletions tests/kdl/xik-output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>
A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>
After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.
</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>
In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.
</description>
</book>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>
The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.
</description>
</book>
<book id="bk106">
<author>Randall, Cynthia</author>
<title>Lover Birds</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-09-02</publish_date>
<description>
When Carla meets Paul at an ornithology
conference, tempers fly as feathers get ruffled.
</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>
A deep sea diver finds true love twenty
thousand leagues beneath the sea.
</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>
An anthology of horror stories about roaches,
centipedes, scorpions and other insects.
</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>
After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.
</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>
Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.
</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>
The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.
</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>
Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.
</description>
</book>
</catalog>
Loading