1
0
mirror of https://github.com/suk-ws/ph-Bookshelf.git synced 2024-12-05 09:26:52 +08:00

现在XML对象解析出现的缺少内容或多出子标签异常会被检验且抛出

This commit is contained in:
A.C.Sukazyo Eyre 2021-03-20 23:52:33 +08:00
parent 74784f83a3
commit 5d25c10933
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
9 changed files with 167 additions and 71 deletions

View File

@ -10,15 +10,25 @@ class Book {
$this->name = $name;
}
/**
* @param DOMNode $xmlData
* @return Book
* @throws Exception
*/
public static function parse (DOMNode $xmlData): Book {
$id = "";
$name = "";
if ($xmlData->hasAttributes()) {
$id = $xmlData->attributes->getNamedItem("id")->nodeValue;
$name = $xmlData->attributes->getNamedItem("name")->nodeValue;
} else {
echo "ERROR PARSE XML BOOK NO ATTRIBUTE\n";
}
$attrName = $xmlData->attributes->getNamedItem("name");
$attrId = $xmlData->attributes->getNamedItem("id");
if ($attrName == null)
if ($attrId == null) throw new Exception("Book xml data missing attribute \"name\"");
else throw new Exception("Book xml data with id \"$attrId->nodeValue\" missing attribute \"name\"");
else $name = $attrName->nodeValue;
if ($attrId == null) throw new Exception("Book xml data named \"$name\" missing attribute \"id\"");
else $id = $attrId->nodeValue;
} else
throw new Exception("Book xml data missing attributes");
if ($xmlData->hasChildNodes())
throw new Exception("Book xml with id \"$id\" have some children which are not supported");
return new Book($id, $name);
}

View File

@ -16,9 +16,21 @@ class BookCollection {
$this->array = $a;
}
/**
* @param DOMNode $root
* @param bool $isRoot
* @return BookCollection
* @throws Exception
*/
public static function parse (DOMNode $root, bool $isRoot = false): BookCollection {
$name = LinkCollection::ROOT;
if (!$isRoot) $name = $root->attributes->getNamedItem("name")->nodeValue;
$name = BookCollection::ROOT;
if (!$isRoot) {
if ($root->hasAttributes()) {
$attrName = $root->attributes->getNamedItem("name");
if ($attrName == null) throw new Exception("BookCollection (not root) xml data missing attribute \"name\"");
else $name = $attrName->nodeValue;
} else throw new Exception("BookCollection (not root) xml data missing attributes");
}
$node = new BookCollection($name, array());
for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) {
switch ($child->nodeName) {
@ -31,12 +43,16 @@ class BookCollection {
case "#text":
break;
default:
echo "ERROR UNSUPPORTED NODE TYPE ON BOOK COLLECTION\n";
throw new Exception("Unsupported element type \"$child->nodeName\" in BookCollection named \"$name\"");
}
}
return $node;
}
public function getName (): string {
return $this->name;
}
/**
* @return Book[]|BookCollection[]
*/

View File

@ -8,32 +8,38 @@ class BookContented extends Book {
private Chapter $childs;
public static function parse (DOMNode $xmlData): BookContented {
$id = "";
$name = "";
if ($xmlData->hasAttributes()) {
$id = $xmlData->attributes->getNamedItem("id")->nodeValue;
$name = $xmlData->attributes->getNamedItem("name")->nodeValue;
} else {
echo "ERROR PARSE XML BOOK WITH CONTENT NO ATTRIBUTE\n";
}
$node = new BookContented($id, $name);
if ($xmlData->hasChildNodes()) {
if ($xmlData->hasAttributes() && $xmlData->hasChildNodes()) {
$attrName = $xmlData->attributes->getNamedItem("name");
$attrId = $xmlData->attributes->getNamedItem("id");
if ($attrName == null)
if ($attrId == null) throw new Exception("BookWithContent xml data missing attribute \"name\"");
else throw new Exception("BookWithContent xml data with id \"$attrId->nodeValue\" missing attribute \"name\"");
else $name = $attrName->nodeValue;
if ($attrId == null) throw new Exception("BookWithContent xml data named \"$name\" missing attribute \"id\"");
else $id = $attrId->nodeValue;
$node = new BookContented($id, $name);
$node->childs = Chapter::parse($xmlData);
} else {
echo "ERROR PARSE XML BOOK WITH CONTENT NO CHILD\n";
}
} else
throw new Exception("No child or attribute found on BookWithContent");
return $node;
}
/**
* @param string $xmlContent
* @return BookContented
* @throws Exception
*/
public static function parseString (string $xmlContent): BookContented {
$node = null;
$dom = new DOMDocument();
if ($dom->loadXML($xmlContent)) {
$node = self::parse($dom->firstChild);
} else echo "ERROR PARSE BOOK CONTENTED DOM FAILED\n";
return $node;
return self::parse($dom->firstChild);
} else throw new Exception("Load BookWithContent xml file failed");
}
public function getChilds (): Chapter {
return $this->childs;
}
}

View File

@ -14,8 +14,17 @@ class Chapter {
$this->childs = $array;
}
/**
* @param DOMNode $xmlData
* @return Chapter
* @throws Exception
*/
public static function parse (DOMNode $xmlData): Chapter {
$node = new Chapter($xmlData->attributes->getNamedItem("name")->nodeValue, array());
if ($xmlData->hasAttributes()) {
$attrName = $xmlData->attributes->getNamedItem("name");
if ($attrName == null) throw new Exception("Chapter xml data missing attribute \"name\"");
else $node = new Chapter($xmlData->attributes->getNamedItem("name")->nodeValue, array());
} else throw new Exception("Chapter xml data missing attributes");
for ($child = $xmlData->firstChild;$child != null ; $child = $child->nextSibling) {
switch ($child->nodeName) {
case "Page":
@ -27,10 +36,21 @@ class Chapter {
case "#text":
break;
default:
echo "ERROR UNSUPPORTED NODE TYPE ON BOOK CHAPTER\n";
throw new Exception("Unsupported element type \"$child->nodeName\" in Chapter \"$node->name\"");
}
}
return $node;
}
public function getName (): string {
return $this->name;
}
/**
* @return Chapter[]|Page[]
*/
public function getChilds (): array {
return $this->childs;
}
}

View File

@ -16,16 +16,24 @@ class Page {
$this->segues = $childs;
}
/**
* @param DOMNode $xmlData
* @return Page
* @throws Exception
*/
public static function parse (DOMNode $xmlData): Page {
$node = null;
if (!$xmlData->hasAttributes()) {
echo "ERROR PARSE XML PAGE NO ATTRIBUTE\n";
} else {
$node = new Page(
$xmlData->attributes->getNamedItem("id")->nodeValue,
$xmlData->attributes->getNamedItem("name")->nodeValue
);
}
if ($xmlData->hasAttributes()) {
$attrName = $xmlData->attributes->getNamedItem("name");
$attrId = $xmlData->attributes->getNamedItem("id");
if ($attrName == null)
if ($attrId == null) throw new Exception("Page xml data missing attribute \"name\"");
else throw new Exception("Page xml data with id \"$attrId->nodeValue\" missing attribute \"name\"");
else $name = $attrName->nodeValue;
if ($attrId == null) throw new Exception("Page xml data named \"$name\" missing attribute \"id\"");
else $id = $attrId->nodeValue;
$node = new Page($id, $name);
} else
throw new Exception("Book xml data missing attributes");
for ($child = $xmlData->firstChild;$child != null ; $child = $child->nextSibling) {
switch ($child->nodeName) {
case "Segment":
@ -34,7 +42,7 @@ class Page {
case "#text":
break;
default:
echo "ERROR UNSUPPORTED NODE TYPE ON PAGE CHAPTER\n";
throw new Exception("Unsupported element type \"$child->nodeName\" in Page with id $id");
}
}
return $node;

View File

@ -10,15 +10,26 @@ class Segment {
$this->name = $name;
}
/**
* @param DOMNode $xmlData
* @return Segment
* @throws Exception
*/
public static function parse (DOMNode $xmlData): Segment {
$node = null;
if ($xmlData->hasAttributes()) {
$node = new Segment(
$xmlData->attributes->getNamedItem("id")->nodeValue,
$xmlData->attributes->getNamedItem("name")->nodeValue
);
}
return $node;
$attrName = $xmlData->attributes->getNamedItem("name");
$attrId = $xmlData->attributes->getNamedItem("id");
if ($attrName == null)
if ($attrId == null) throw new Exception("Segment xml data missing attribute \"name\"");
else throw new Exception("Segment xml data with id \"$attrId->nodeValue\" missing attribute \"name\"");
else $name = $attrName->nodeValue;
if ($attrId == null) throw new Exception("Segment xml data named \"$name\" missing attribute \"id\"");
else $id = $attrId->nodeValue;
} else
throw new Exception("Segment xml data missing attributes");
if ($xmlData->hasChildNodes())
throw new Exception("Segment xml named \"$name\" have some children which are not supported");
return new Segment($id, $name);
}
}

View File

@ -1,8 +1,6 @@
<?php
require_once "./src/Element/Book.php";
require_once "./src/Element/BookCollection.php";
require_once "./src/Element/Link.php";
require_once "./src/Element/LinkCollection.php";
require_once "./src/Element/BookContent/BookContented.php";
@ -15,6 +13,11 @@ class Bookshelf {
private BookContented $rootBook;
/**
* @param string $xmlData
* @return Bookshelf
* @throws Exception
*/
public static function parseString (string $xmlData): Bookshelf {
$return = new Bookshelf();
$dom = new DOMDocument();
@ -23,7 +26,9 @@ class Bookshelf {
if ($dom->hasAttributes() && $dom->hasChildNodes()) {
// Bookshelf 属性
$return->siteName = $dom->attributes->getNamedItem("siteName")->nodeValue;
$attrSiteName = $dom->attributes->getNamedItem("siteName");
if ($attrSiteName == null) throw new Exception("Bookshelf xml data missing attribute \"siteName\"");
$return->siteName = $attrSiteName->nodeValue;
// 对根节点的子节点进行遍历
for ($rc = $dom->firstChild; $rc != null; $rc = $rc->nextSibling) {
@ -40,12 +45,12 @@ class Bookshelf {
case "#text":
break;
default:
echo "ERROR UNSUPPORTED TYPE ON BOOKSHELF\n";
throw new Exception("Unsupported element type \"$rc->nodeName\" in root child of Bookshelf");
}
}
} else echo "ERROR ROOT NO CONTENT\n";
} else echo "ERROR PARSE BOOKSHELF DOM FAILED\n";
} else throw new Exception("No child or attribute found on Bookshelf");
} else throw new Exception("Load Bookshelf xml file failed");
return $return;
}
@ -53,16 +58,10 @@ class Bookshelf {
return $this->siteName;
}
/**
* @return LinkCollection
*/
public function getLinks (): LinkCollection {
return $this->links;
}
/**
* @return BookCollection
*/
public function getBooks (): BookCollection {
return $this->books;
}

View File

@ -11,24 +11,34 @@ class Link {
$this->href = $href;
}
/**
* @param DOMNode $xmlData
* @return Link
* @throws Exception
*/
public static function parse (DOMNode $xmlData): Link {
$name = "";
$href = "";
if ($xmlData->hasAttributes()) {
$name = $xmlData->attributes->getNamedItem("name")->nodeValue;
$href = $xmlData->attributes->getNamedItem("href")->nodeValue;
} else {
echo "ERROR PARSE XML LINK NO ATTRIBUTE\n";
}
$attrName = $xmlData->attributes->getNamedItem("name");
$attrHref = $xmlData->attributes->getNamedItem("href");
if ($attrName == null)
if ($attrHref == null) throw new Exception("Link xml data missing attribute \"name\"");
else throw new Exception("Link xml data which href is \"$attrHref->nodeValue\" missing attribute \"name\"");
else $name = $attrName->nodeValue;
if ($attrHref == null) throw new Exception("Link xml data named \"$name\" missing attribute \"href\"");
else $href = $attrHref->nodeValue;
} else
throw new Exception("Link xml data missing attributes");
if ($xmlData->hasChildNodes())
throw new Exception("Link xml named \"$name\" have some children which are not supported");
return new Link($name, $href);
}
public function getHref (): string {
return $this->href;
}
public function getName (): string {
return $this->name;
}
public function getHref (): string {
return $this->href;
}
}

View File

@ -16,9 +16,21 @@ class LinkCollection {
$this->array = $a;
}
/**
* @param DOMNode $root
* @param bool $isRoot
* @return LinkCollection
* @throws Exception
*/
public static function parse (DOMNode $root, bool $isRoot = false): LinkCollection {
$name = LinkCollection::ROOT;
if (!$isRoot) $name = $root->attributes->getNamedItem("name")->nodeValue;
if (!$isRoot) {
if ($root->hasAttributes()) {
$attrName = $root->attributes->getNamedItem("name");
if ($attrName == null) throw new Exception("LinkCollection (not root) xml data missing attribute \"name\"");
else $name = $attrName->nodeValue;
} else throw new Exception("LinkCollection (not root) xml data missing attributes");
}
$node = new LinkCollection($name, array());
for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) {
switch ($child->nodeName) {
@ -31,12 +43,16 @@ class LinkCollection {
case "#text":
break;
default:
echo "ERROR UNSUPPORTED NODE TYPE ON LINK COLLECTION\n";
throw new Exception("Unsupported element type \"$child->nodeName\" in LinkCollection named \"$name\"");
}
}
return $node;
}
public function getName (): string {
return $this->name;
}
/**
* @return Link[]|LinkCollection[]
*/